[NEM] node.jsでmosaicを送信

nem-sdkをインストールします
$ npm install nem-sdk

mainnetで送信します。

var nem = require("nem-sdk").default;

const endpoint = nem.model.objects.create('endpoint')(nem.model.nodes.defaultMainnet, nem.model.nodes.defaultPort);

async function main() {

    const toAddress = '';
    const sendAmount = 1;
    const sendMsg = 'Hello World!';
    const password = '';
    const privateKey = '';
    const common = nem.model.objects.create('common')(password, privateKey);
    const yourMosaicNamespace = 'capitalcoin';
    const yourMosaicName = 'hpscript';

    let transferTransaction = nem.model.objects.create('transferTransaction')(toAddress, sendAmount, sendMsg);

    const xemMozaic = nem.model.objects.create('mosaicAttachment')('nem', 'xem', 0);
    transferTransaction.mosaics.push(xemMozaic);

    const yourMosaic = nem.model.objects.create('mosaicAttachment')(yourMosaicNamespace, yourMosaicName, 10);
    transferTransaction.mosaics.push(yourMosaic);

    let mosaicDefinitionMetaDataPair = nem.model.objects.get('mosaicDefinitionMetaDataPair');
    nem.com.requests.namespace.mosaicDefinitions(endpoint, yourMosaic.mosaicId.namespaceId).then(res => {

        const neededDefinition = nem.utils.helpers.searchMosaicDefinitionArray(res.data, [yourMosaicName]);

        const fullMosaicName  = nem.utils.format.mosaicIdToName(yourMosaic.mosaicId);

        if (undefined === neededDefinition[fullMosaicName]) {
            return console.log('Mosaic not found !');
        }

        mosaicDefinitionMetaDataPair[fullMosaicName] = {};
        mosaicDefinitionMetaDataPair[fullMosaicName].mosaicDefinition = neededDefinition[fullMosaicName];

        nem.com.requests.mosaic.supply(endpoint, fullMosaicName).then(supplyRes => {

            mosaicDefinitionMetaDataPair['nem:xem'].supply = 8999999999;
            mosaicDefinitionMetaDataPair[fullMosaicName].supply = supplyRes.supply;

            const transactionEntity = nem.model.transactions.prepare('mosaicTransferTransaction')(common, transferTransaction, mosaicDefinitionMetaDataPair, nem.model.network.data.mainnet.id);

            nem.model.transactions.send(common, transactionEntity, endpoint).then(sendRes => {
                console.log('sendRes:', sendRes);
            }).catch(sendErr => {
                console.log('sendError:', sendErr);
            });
        }).catch(supplyErr => {
            console.log('supplyErr:', supplyErr);
        });
    }).catch(err => {
        console.log('mosaicDefinitionsError:', err);
    });
}

main();

$ node test.js
sendRes: { innerTransactionHash: {},
code: 1,
type: 1,
message: ‘SUCCESS’,
transactionHash:
{ data:
‘f08066a73d37f523482d6dc61a60e53db4cbd5097262f1ee419cae7ae4ad9a7a’ } }

送信結果

おおお、これをUIをつけてやりたい

<body>
	<div class="container">
		<h1 class="title">NEM Project</h1>
		<p>送金ボタンを押してください</p>
		<input type="button" value="送金" onclick="OnButtonClick();">
	</div>
	<script src="nem-sdk.js"></script>
	<script>
		var nem = require("nem-sdk").default;
		
		function OnButtonClick() {
			main();
			console.log("送金完了しました")
		}
		const endpoint = nem.model.objects.create('endpoint')(nem.model.nodes.defaultMainnet, nem.model.nodes.defaultPort);
		async function main() {
		    // 省略
		}
	</script>

あ、出来た^^

受け取るwalletのアドレスとメッセージを入力フォームにして、そこに送信できるようにしたい。