Programming Assignment - Testing out the SDK


#1

Welcome to the discussion about the programming assignment in NEM.

Post your solutions and discuss it in this thread. If you have any questions or you want to discuss something connected to the assignment feel free to do it in this thread as well, but please keep everything to the topic.


#2

The solution is… just kidding, I am still at the beginning of JavaScript :slight_smile:
Thanks for the training Ivan

Beppe


#3

and Filip :wink: (post must be at least 20 chars…)


#4
// prices.js
let nem = require("nem-sdk").default

let endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort)

nem.com.requests.market.xem(endpoint)
    .then(prices => {
        console.log(JSON.stringify(prices, null, 2))
    }, err => console.log(err))
// time.js
let nem = require("nem-sdk").default

let endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort)

nem.com.requests.chain.time(endpoint)
    .then(console.log, console.log)

#5

Here are my solutions to the NEM programming assignment:

Price

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

let endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);

nem.com.requests.market.xem(endpoint).then(function(price){
    console.log(price);
}, function(err){
    console.log(err);
});

Time

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

let endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);

nem.com.requests.chain.time(endpoint).then(function(time){
    console.log(time);
}, function(err){
    console.log(err);
});

#6

Price

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

let endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);

nem.com.requests.market.xem(endpoint).then(function(cena){
  console.log(cena);
}, function(err){
  console.log(err);
});

Time

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

let endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);

nem.com.requests.chain.time(endpoint).then(function(cas){
  console.log(cas);
}, function(err){
  console.log(err);
});

#7

I was going to post mine, though these are literally exactly the same… :D.


#8

CrazyCanadia is spot on! ^^^


#9

I am having problem with price.js
I copy-pasted it from https://github.com/filipmartinsson/nem/blob/master/Assignment1/price.js
but it goes into the function(error) part. It returns some html file.


#10

price.js

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

let endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);

nem.com.requests.market.btc(endpoint).then(function(block){
  console.log(block);
}, function(error){
  console.log(error);
})

time.js

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

let endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);

nem.com.requests.chain.time(endpoint).then(function(block){
  console.log(block);
}, function(error){
  console.log(error);
})

#11

What does the error say?


#12

It gives a response which i have posted here in a txt file, its rather large: https://github.com/filipmartinsson/nem/issues/1


#13

I’m not sure what’s going on there. Did the nem.com.requests.chain.lastBlock function work? As I did in the video.


#14

So I noticed my nem folders keep syncing non stop to the google cloud. Am I running the testnet on their servers?


#15
//priceTime


//Initiate the nem SDK onto the file
var nem = require("nem-sdk").default;
console.log(nem);

//Create endpoint object that contains the node host and port  
let endpoint = nem.model.objects.create("endpoint")(
   nem.model.nodes.defaultTestnet,
   nem.model.nodes.defaultPort
);

//Request time from the nem.com namespace
nem.com.requests.chain.time(endpoint).then(
   function(block){
       console.log(block)
   },
   function(err){
       console.log(err);
 }
);

//Request xem price (in btc) from the nem.com namespace
nem.com.requests.market.xem(endpoint).then(
   function(block){
       console.log(block);
   },function(err){
       console.log(err);
   }
);

//Request BTC price (in USD) from the nem.com namespace
nem.com.requests.market.btc(endpoint).then(
   function(block){
       console.log(block);
   },function(err){
       console.log(err);
   }
);

#16

It’s generally not recommended to sync code to google drive. In my experience it causes a lot of pain and usually doesn’t work between devices anyway. I’m not sure why it keeps syncing. The google servers are not running the testnet, no. But there may be files changed in your directory when you run the scripts that causes the sync to kick in. I’m not sure.


#17

Yes, that worked fine.


#18

price.js
let nem = require(“nem-sdk”).default;

let endpoint = nem.model.objects.create(“endpoint”)(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);

nem.com.requests.market.xem(endpoint).then(function(block){
console.log(block);
}, function(err){
console.log(err);
});

time.js
let nem = require(“nem-sdk”).default;

let endpoint = nem.model.objects.create(“endpoint”)(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);

nem.com.requests.chain.time(endpoint).then(function(block){
console.log(block);
}, function(err){
console.log(err);
});


#19

This does not seem right? I did the transfer exercise and instead of 10 XEM, I transferred 100000000000000000000000000 and: that transaction was accepted and validated, The transaction value ended to be 0, the fee was 1.3 XEM. Should there not be an error message instead? The hash is 104df0a564e5605ef33573c49256ba47637d5d958898c731d3ae7e02735f0463


#20

I’m not sure what happened in that case. When I check the block explorer it looks like you have been able to send xem in the past. But the last transaction indeed was 0.

http://bob.nem.ninja:8765/#/account/TBR5TCXAPLZFCX5NGJ6TQSOYCASRBCKVRTUADCAJ/0

Did you find out what happened?