Last active
May 29, 2017 22:27
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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