Guessnumbercoin scoreboard for printing balances of a list of addresses.
const Promise = require('bluebird');
const Web3 = require('web3');
const abi = require('./abi.json');
const config = require(process.env['GNC_MINER_CONFIG_PATH'] || './config.json');
const providers = [
'https://mewapi.musicoin.tw'
];
const web3 = new Web3(new Web3.providers.HttpProvider(providers[1]));
const addrContra = config.ADDR_CONTRACT;
const addresses = {
// example:
// 'name': '0x1234...',
};
const contract = new web3.eth.Contract(abi, addrContra);
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
if (!String.prototype.padStart) {
String.prototype.padStart=function(b,a){b>>=0;a=String(a||" ");if(this.length>b)return String(this);
b-=this.length;b>a.length&&(a+=a.repeat(b/a.length));return a.slice(0,b)+String(this)};String
.prototype.padEnd=function(b,a){b>>=0;a=String(a||" ");if(this.length>b)return String(this);b-=this
.length;b>a.length&&(a+=a.repeat(b/a.length));return String(this)+a.slice(0,b)};
}
let totalSupply = null;
function print(addresses) {
return Promise.resolve()
.then(() => {
return Promise.map(Object.keys(addresses),
(name) => Promise.all([
contract.methods.balances(addresses[name]).call(),
web3.eth.getBalance(addresses[name])
])
.spread((bal, money) => [name, bal, money]),
{concurrency: 4});
})
.then(result => {
let sum = result.map((arr) => {
let [name, bal, money] = arr;
let nameFmted = name.padEnd(5, ' ');
let balFmted = bal.padStart(5, ' ');
let moneyFmted = (Math.round(money / 1e13) / 1e5).toFixed(5).padStart(8, ' ');
console.log(`${nameFmted}: ${balFmted}, $${moneyFmted}`);
return bal - 0;
}).reduce((a, b) => (a + b), 0);
console.log(`Sum: ${sum} (${Math.round(sum / totalSupply * 10000) / 100}%)`);
});
}
Promise.resolve()
.then(() => {
return contract.methods.totalSupply().call()
.then((sup) => {
console.log(`Total supply = ${sup}`);
totalSupply = sup;
});
})
.then(() => print(addresses))
.then(() => print(addrRivals));