Last active
August 27, 2018 22:21
-
-
Save ocrybit/81544e79bb8d9c45e016e57dd9699dd2 to your computer and use it in GitHub Desktop.
NEO Blockchain Challenge - Tokyo - Elementary theme
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
/* | |
NEOブロックチェーンチャレンジ - 東京 | |
初級テーマ | |
https://github.com/neo-ngd/Hackathon/blob/master/8.24%20NEO%20Blockchain%20Challenge%20-%20Tokyo.md | |
*/ | |
let https = require("https") | |
let addresses = [ | |
`AXDt2hzT35knLnV3MB3dR9rAvYmadUfVdb`, | |
`AKnbvRwL1MSPFWoS6bdD5v2SNHq2uta5tm` | |
] | |
class NEO { | |
constructor(addresses) { | |
this.addresses = addresses | |
this.nth = 0 | |
this.getNext() | |
} | |
req(url, cb) { | |
const req = https.request(url, res => { | |
let body = "" | |
res.on("data", d => { | |
body += d.toString() | |
}) | |
res.on("end", () => { | |
let json | |
try { | |
json = JSON.parse(body) | |
} catch (e) { | |
console.log(e) | |
process.exit() | |
} | |
cb(json) | |
}) | |
}) | |
req.on("error", e => { | |
console.error(e) | |
process.exit() | |
}) | |
req.end() | |
} | |
getNext() { | |
if (this.addresses.length == 0) { | |
process.exit() | |
} else { | |
let address = this.addresses.shift() | |
this.nth += 1 | |
this.getBalance(address, () => { | |
this.getNext() | |
}) | |
} | |
} | |
getBalance(address) { | |
this.req( | |
`https://api.neoscan.io/api/main_net/v1/get_balance/${address}/`, | |
json => { | |
let balance | |
for (let v of json.balance) { | |
if (v.asset === `NEO`) { | |
balance = v.amount | |
break | |
} | |
} | |
this.getTransactions(address, balance, 1, balance_at => { | |
this.show(this.nth, address, balance_at) | |
this.getNext() | |
}) | |
} | |
) | |
} | |
getTransactions(address, balance, page, cb) { | |
let url = `https://api.neoscan.io/api/main_net/v1/get_address_abstracts/${address}/${page}/` | |
this.req(url, json => { | |
let hasNext = false | |
for (let v of json.entries) { | |
if (v.block_height >= 2640000) { | |
hasNext = true | |
if ( | |
v.asset == | |
`c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b` | |
) { | |
let x = 1 | |
if (v.address_to === address) { | |
x = -1 | |
} | |
balance += v.amount * x | |
} | |
} else { | |
hasNext = false | |
break | |
} | |
} | |
if (hasNext === true) { | |
page += 1 | |
this.getTransactions(address, balance, page) | |
} else { | |
cb(balance) | |
} | |
}) | |
} | |
show(nth, address, balance) { | |
console.log(`残高${nth}:${address}:${balance}`) | |
} | |
} | |
new NEO(addresses) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment