Last active
November 28, 2016 15:30
-
-
Save gabmontes/c0c6c0df85ad34bf19de1dddfb49bfc4 to your computer and use it in GitHub Desktop.
Sum up the balances of a group of bitcoin addresses
This file contains hidden or 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
| const { Insight } = require('bitcore-explorers') | |
| const promisify = require('tiny-promisify') | |
| const addresses = [ | |
| /* bitcoin addresses here! */ | |
| ] | |
| const insight = new Insight() | |
| const getUnspentUtxosAsync = promisify(insight.getUnspentUtxos.bind(insight)) | |
| const BTC_SATOSHI = 100000000 | |
| function pick(array, prop) { | |
| return array.map(item => item[prop]) | |
| } | |
| function sum(array, prop) { | |
| return pick(array, prop).reduce((acc, val) => acc + val, 0) | |
| } | |
| function getBalance(address) { | |
| return getUnspentUtxosAsync(address).then(utxos => ({ | |
| address, | |
| satoshis: sum(utxos, 'satoshis') | |
| })) | |
| } | |
| Promise.all(addresses.map(getBalance)).then(function (balances) { | |
| balances.forEach(function ({ address, satoshis }) { | |
| console.log(`${address}: ${satoshis / BTC_SATOSHI}`) | |
| }) | |
| const total = sum(balances, 'satoshis') | |
| console.log(`Total: ${total / BTC_SATOSHI}`) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment