31
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

仮想通貨NEMアプリをスピード開発する小技・Tips

Last updated at Posted at 2017-12-12

仮想通貨NEMがIT技術者の周りでにわかに注目を集めています。
NEMは柔軟なAPIが提供されているため、送金や情報管理などのアプリケーションを手軽に構築できる可能性を秘めています。

今回は仮想通貨NEMのAPIを利用してWebアプリをスピード開発するためのノウハウを紹介します。

後日整理するかもしれませんが、突貫エントリーのため、解説なしです。

###金額を3ケタカンマ、小数点表記に変換する

sample.js
var dispAmount2 = function(amount,divisibility){
	if(divisibility > 0){

		if(amount < Math.pow(10, divisibility)){

			return "0." + paddingright(amount.toString(),0,divisibility);
		}else{
			var str_amount = amount.toString();
			var r = str_amount.slice(-divisibility);
			var l = str_amount.substring(0,str_amount.length - divisibility);
			l = l.replace( /(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
		}
		return l + "." + r;

	}else{
		l = amount.toString().replace( /(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
		return l
	}

}
function paddingright(val,char,n){
	for(; val.length < n; val= char + val);
	return val;
}

###チェーンに刻まれたHEX文字を日本語メッセージに変換する

sample.js
var hex2a = function hex2a(hexx) {
	var hex = hexx.toString();
	var str = '';
	for (var i = 0; i < hex.length; i += 2) {
		str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
	}
	return str;
};

###ハイフン無し、小文字アドレスを大文字に統一する

sample.js
address = address.replace( /-/g , "" ).toUpperCase();

###Google先生にウォレット対応QRコードを出力してもらう

sample.js
var bill = '{"v":2,"type":2,"data":{"addr":"' + ADDRESS + '","amount":1000000,"msg":"' + msg + '"}}';
$("#qr").attr("src","http://chart.apis.google.com/chart?chs=180x180&cht=qr&chl=" + bill);

###poloniexとblockchain.infoからxemの現在価格を日本円で計算する

sample.js
$.when(
	sendAjax("https://poloniex.com/public?command=returnTicker"),
	sendAjax("https://blockchain.info/ticker?cors=true")
)
.done(function(res1, res2) {

	POLO_JPY_XEM = res1[0].BTC_XEM.last * res2[0].JPY.last;
	POLO_JPY_XEM = Math.round(POLO_JPY_XEM * 1000) / 1000;
})
.fail(function(xhr, textStatus, errorThrown) {
	alert("時価の取得に失敗しました。");
});

###URL部分に指定されたアドレスをスクリプト内で利用する。

sample.js
var address ="";
if (1 < document.location.search.length) {

	var query = document.location.search.substring(1);
	var prms = query.split('&');
	var item = new Object();
	for (var i = 0; i < prms.length; i++) {
		var elm   = prms[i].split('=');
		var idx   = decodeURIComponent(elm[0]);
		var val   = decodeURIComponent(elm[1]);
		item[idx] = decodeURIComponent(val);
	}
	address = item["address"];
}

if( address == ""){
	var address = prompt('NEMアドレスを入力してください',address);
}

###モザイク、マルチシグなどで構成された複雑なトランザクションからXEMだけを抽出する。

sample.js

var tran = val.transaction;

var tran_amount = 0;
var tran_fee = tran.fee;

if(tran.type == 4100){

	tran_fee    = tran.otherTrans.fee + tran.fee ;
	tran = tran.otherTrans;
}

if (tran.type == 257 || tran.type == 8193 ){

	//モザイクが存在した場合
	var has_mosaic = false;
	if(tran.mosaics){

		for(key  in tran.mosaics){
			has_mosaic = true;
			var mosaic = tran.mosaics[key];
			if(mosaic.mosaicId.name == "xem" && mosaic.mosaicId.namespaceId == "nem"){
				tran_amount = mosaic.quantity;
			}
		}
	}
	//通常送金
	if(!has_mosaic){
		if (tran.type == 8193 ){
			tran_amount = tran.rentalFee;

		}else{
			tran_amount = tran.amount;
		}
	}
}

31
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?