Skip to content

Instantly share code, notes, and snippets.

@evias
Last active May 29, 2017 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evias/098b997f67d53fbb4e28459065647788 to your computer and use it in GitHub Desktop.
Save evias/098b997f67d53fbb4e28459065647788 to your computer and use it in GitHub Desktop.
NEM-sdk: Read Mosaics owned of a given account (Game Credits stored as Mosaic 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);
// Issue the Mosaics request for the given Account (insert Player XEM address here)
nem.com.requests.account.mosaics(node, "TCTIMURL5LPKNJYF3OB3ACQVAXO3GK5IU2BJMPSU")
.then(function(response)
{
var countPlayerCredits = 0;
// Each row in the `response.data` array represents a different
// Mosaic that the Account owns.
for (var i in response.data) {
var mosaic = response.data[i];
// add a human-readable name for our console output
var slug = mosaic.mosaicId.namespaceId + ":" + mosaic.mosaicId.name;
console.log("Player has " + mosaic.quantity + " " + slug + " Mosaics");
if ("evias.pacnem:heart" != slug)
continue; // not our Game Credits Mosaic.
countPlayerCredits = mosaic.quantity;
}
console.log("Player has " + countPlayerCredits + " Game Credits for PacNEM!");
},
function(error) { // General Error OR wrong Network for Address OR No Mosaics available });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment