Instantly share code, notes, and snippets.

Embed
What would you like to do?
web3 script for printing Ethereum account balances
//
// Pretty print accounts summary. Constant function, does not consume gas. No unlocks needed.
//
// Usage:
// Save to file, e.g. '/tmp/checkAllBalances.js'
// From the geth console, do: loadScript('/tmp/checkAllBalances.js');
// Execute with checkAllBalances()
//
// For the price feed ABI, see http://forum.ethereum.org/discussion/3417/ask-%CE%9E-community-what-do-you-think-of-our-new-smart-contract-pricefeed
var priceFeedAbi = [
{
"constant":true,
"inputs": [{"name":"symbol","type":"bytes32"}],
"name":"getPrice",
"outputs":[{"name":"currPrice","type":"uint256"}],
"type":"function"
},
{
"constant":true,"inputs":[{"name":"symbol","type":"bytes32"}],
"name":"getTimestamp",
"outputs":[{"name":"timestamp","type":"uint256"}],
"type":"function"
}];
var priceFeed = web3.eth.contract(priceFeedAbi).at("0x1194e966965418c7d73a42cceeb254d875860356");
function priceFeedQuery(fun, symbol) {
var cbValue;
var cb = function(name, type) {
cbValue = type;
return true;
}
fun.call(symbol, cb);
return parseInt(cbValue);
}
var trunc = function(number, precision) {
var shift = Math.pow(10, precision)
return parseInt(number * shift) / shift
}
function padString(str, len) {
while (str.length < len)
str = " " + str;
return str
}
function prettyPrintEther(ether) {
var str;
if (ether >= 1)
str = trunc(ether, 3) + " ether";
else if (ether > 1e-5)
str = trunc(ether*1000, 3) + " finney";
else if (ether > 1e-7)
str = trunc(ether*1000, 6) + " finney";
else if (ether > 1e-12)
str = trunc(ether*1e12, 3) + " gwei";
else
str = parseInt(web3.toWei(ether)) + " wei";
return str;
}
function checkAllBalances() {
var btc_timestamp = priceFeedQuery(priceFeed.getTimestamp, "BTC_ETH");
var btc_eth = priceFeedQuery(priceFeed.getPrice, "BTC_ETH") / 1000000;
var usd_eth = priceFeedQuery(priceFeed.getPrice, "USDT_ETH") / 1000000;
console.log("");
console.log("Current price at " + new Date(btc_timestamp*1000).toString())
console.log("\t" + (btc_eth*1000) + " mBTC/ether")
console.log("\t$" + usd_eth + "/ether");
console.log("\t$" + (priceFeedQuery(priceFeed.getPrice, "USDT_BTC") / 1000000) + "/฿");
console.log("");
var i = 0;
var totalBtc = 0, totalDollar = 0, totalEther = 0;
eth.accounts.forEach( function(e) {
var ether = parseFloat(web3.fromWei(eth.getBalance(e), "ether"));
var btc = ether * btc_eth;
var dollar = ether * usd_eth;
totalBtc = totalBtc + btc;
totalDollar = totalDollar + dollar;
totalEther = totalEther + ether;
console.log("eth.accounts["+i+"]: " + e + ": " +
padString(prettyPrintEther(ether), 15) + ", " +
padString("฿" + trunc(btc, 3) + ", ", 12) +
"$" + trunc(dollar, 2));
i++;
})
console.log("");
console.log("Total: " +
padString(prettyPrintEther(totalEther), 69) + ", " +
padString("฿" + trunc(totalBtc, 6) + ", ", 12) +
"$" + trunc(totalDollar, 2))
console.log("");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment