Skip to content

Instantly share code, notes, and snippets.

@evias

evias/save_score.js

Created May 29, 2017
Embed
What would you like to do?
NEM-sdk: Send a Multi Signature Mosaic Transfer Transaction (Save a High Score as a Mosaic Transfer Transaction on the NEM Blockchain)
console.log("Hello, NEMber");
// Configure a Blockchain endpoint and include SDK
var NEM_HOST = "http://bob.nem.ninja"; // bob is Testnet!
var NEM_PORT = 7890;
var nem = require("nem-sdk").default;
var node = nem.model.objects.create("endpoint")(NEM_HOST, NEM_PORT);
// Define the player data
var playerXEM = "TATKHV5JJTQXCUCXPXH2WPHLAYE73REUMGDOZKUW";
var playerScore = 29081988;
// create the NEM objects that we will need for Mosaic Transfer Transactions
// Objects Description:
// * `privStore` : object for storage of password / private key.
// * `mosaicDefPair` : [MosaicDefinitionMetaDataPair](http://bob.nem.ninja/docs/#mosaicDefinitionMetaDataPair)
// * `mosaicAttachCheese` : [MosaicAttachment](http://bob.nem.ninja/docs/#mosaicAttachment)
// * `transferTransaction` : unprepared Multi Signature Mosaic Transfer Transaction
// you can also provide your private key in the second argument and leave the password empty
// NO CONNECTION NEEDED FOR THE FOLLOWING BLOCK
var privStore = nem.model.objects.create("common")("yourSecurePassword", "");
var mosaicDefPair = nem.model.objects.get("mosaicDefinitionMetaDataPair");
var mosaicAttachCheese = nem.model.objects.create("mosaicAttachment")("evias.pacnem", "cheese", playerScore); // High score here
var cheeseSlug = nem.utils.helpers.mosaicIdToName(mosaicAttachCheese.mosaicId);
var txMessage = "You played great!";
var transferTransaction = nem.model.objects.create("transferTransaction")(playerXEM, 1, txMessage); // 1 is a multiplier of Mosaic Attachments
// we need to modify the transaction to be a multi signature transaction
// Replace the public key with the public key of your MULTISIG account (the account which is sending the Cheese Mosaics)
transferTransaction.isMultisig = true;
transferTransaction.multisigAccount = {publicKey: "7e5606db494a485d64f40c41416e43bb9a1405f52ba8dc7dcbe8abeb2dde1f40"};
// now we can attach our attachments to the transaction
transferTransaction.mosaics.push(mosaicAttachCheese);
// NOW WE WILL NEED AN INTERNET CONNECTION (or a running local node)
// We need the mosaic definition of evias.pacnem:cheese to calculate adequate fees, so we get it from the network.
nem.com.requests.namespace
.mosaicDefinitions(node_, pacNEM_NS_).then(
function(res) {
// interpret response from node and store the mosaic definition data
var cheeseDef = nem.utils.helpers.searchMosaicDefinitionArray(res, ["cheese"]);
mosaicDefPair[cheeseSlug].mosaicDefinition = cheeseDef[cheeseSlug];
// now we can prepare the Mosaic Transfer Transaction
var entity = nem.model.transactions.prepare("mosaicTransferTransaction")(privStore, transferTransaction, mosaicDefPair, nem.model.network.data.testnet.id);
// SEND the transaction (will land in unconfirmed until the *pacnem-sign-bot* co-signatory signs the transaction)
nem.model.transactions.send(privStore, entity, node).then(
function(res) {
// If code >= 2, it's an error
if (res.code >= 2) {
console.log("[NEM] [ERROR] Could not send Transaction to " + playerXEM + ": " + JSON.stringify(res));
return false;
}
var trxHash = res.transactionHash.data;
console.log("[SUCCESS] Successfully sent mosaic transfer transaction, Hash is: " + trxHash);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment