Skip to content

Instantly share code, notes, and snippets.

@gabmontes
Last active November 28, 2016 15:30
Show Gist options
  • Select an option

  • Save gabmontes/c0c6c0df85ad34bf19de1dddfb49bfc4 to your computer and use it in GitHub Desktop.

Select an option

Save gabmontes/c0c6c0df85ad34bf19de1dddfb49bfc4 to your computer and use it in GitHub Desktop.
Sum up the balances of a group of bitcoin addresses
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