Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created June 5, 2018 22:00
Show Gist options
  • Save miguelmota/0b812d709c390d174fcb623b218aeb69 to your computer and use it in GitHub Desktop.
Save miguelmota/0b812d709c390d174fcb623b218aeb69 to your computer and use it in GitHub Desktop.
Electrum client get balance in Node.js
const ElectrumClient = require('electrum-client')
const peers = require('electrum-host-parse').getDefaultPeers('BitcoinSegwit').filter(v => v.ssl)
const getRandomPeer = () => peers[peers.length * Math.random() | 0]
let mainClient = false
const main = async () => {
const peer = getRandomPeer()
console.log('begin connection:', JSON.stringify(peer))
mainClient = new ElectrumClient(peer.ssl, peer.host, 'ssl')
try {
await mainClient.connect()
const ver = await mainClient.server_version('2.7.11', '1.0')
console.log('connected to ', ver)
} catch (e) {
console.log('bad connection:', JSON.stringify(peer))
console.log('trying again')
return main()
}
}
async function getBalance(address) {
await main()
let balance = await mainClient.blockchainAddress_getBalance(address)
return balance.confirmed
}
module.exports = {
getBalance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment