Join 74,293 users and earn money for participation

electrum-cash: working with balances

15 423 exc boost
Avatar for JonathanSilverblood
Written by   236
1 year ago (Last updated: 11 months ago)

Introduction

Electrum-cash is a javascript/typescript library for communicating with electrum servers.

This example demonstrates how to get the balance of an address and how to get notified of changes to the balance of an address, using v1.4.3 of the electrum protocol.

To keep the example simple and clear, the code to set up and close a connection is omitted.

Fetch current balance

To get the current balance for an address, send a blockchain.address.get_balance request to the server with the address as the first parameter.

// The address we want to get balance for.
const address = 'qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst';

// Request the balance from the server.
const balance = await electrum.request('blockchain.address.get_balance', address);

// Print out the current balance, in satoshis.
console.log(`Balance for '${address}' is ${balance.confirmed} satoshis confirmed, and ${balance.unconfirmed} satoshis unconfirmed.`);

Example output for an existing wallet with a balance:

Balance for 'qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst' is 1165925 satoshis confirmed, and 0 satoshis unconfirmed.

Subscribe to balance changes

To subscribe to balance update notifications, send a blockchain.address.subscribe subscription to the server with the address as the first parameter, then request the updated balance any time the status of the address changes.

// The address we want to get balance updates for.
const address = 'qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst';

// Create a callback handler for balance updates.
const balanceUpdateHandler = async function(adress)
{
	// Request the balance from the server.
	const balance = await electrum.request('blockchain.address.get_balance', address);

	// Print out the updated balance, in satoshis.
	console.log(`Balance for '${address}' is ${balance.confirmed} satoshis confirmed, and ${balance.unconfirmed} satoshis unconfirmed.`);
}

// Subscribe to update notifications to the address status from the server.
const balance = await electrum.subscribe(balanceUpdateHandler.bind(this, address), 'blockchain.address.subscribe', address);

Example output when sending out all funds in the wallet:

Balance for 'qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst' is 1165925 satoshis confirmed, and 0 satoshis unconfirmed.

Balance for 'qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst' is 1165925 satoshis confirmed, and -1165925 satoshis unconfirmed.

Balance for 'qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst' is 0 satoshis confirmed, and 0 satoshis unconfirmed.

70
$ 4.04
$ 1.00 from @emergent_reasons
$ 1.00 from @rosco
$ 0.76 from @TheRandomRewarder
+ 8
Avatar for JonathanSilverblood
Written by   236
1 year ago (Last updated: 11 months ago)
Enjoyed this article?  Earn Bitcoin Cash by sharing it! Explain
...and you will also help the author collect more tips.

Comments

Thank you for giving us an idea what electrum cash is and how it works. In that case, we can try to research about that and learn something. It's interesting 😊

$ 0.00
1 year ago

Very cool. Will probably build something on top of this to track whales balance changes lol. Just need a list of whale BCH addresses!

$ 0.00
1 year ago

This might get you started, it's a list of outputs and their addresses, sorted by size and filtered to show only spendable funds:

https://blockchair.com/bitcoin-cash/outputs?s=value(desc)&q=is_spendable(true)#

$ 0.00
1 year ago

This is great! Thank you! So are you able to just run some kind of a group by query order by balance descending?

$ 0.00
1 year ago

more or less. you have the controls on the site to fiddle around with, I just thought I'd point you in a possible direction. It's not the only way to get the data, but it is one of the easier ways.

$ 0.00
1 year ago

Oh ok. That definitely helps. Thanks for pointing me in the right direction!

$ 0.00
1 year ago

Wait. This is news to me. I mean, I wish there is a much simpler article that would help me understand this things better. Everything is like a jargon to me. :(

$ 0.00
1 year ago

This is a programming-focused article with a specific library and programming language, so if you are not a programmer it might be difficult to understand.

If you are a programmer working with javascript and you need help understanding this, let me know.

$ 0.00
1 year ago

Wow. I am amazed that even here in read cash, you all manage to educate a LOT of people. Keep it up!

I have some friends that currently studying programming, maybe I'll just ask them about it.

Thanks a lot for the offer tho.

$ 0.00
1 year ago

The explanations are simple and brief enough. Please input what @read.cash asked to do so as to make the information more clearer. Thanks.

$ 0.00
1 year ago

I think this misses the intro about where to put this code. Do you import something? Do you go to Electron Cash and put it somewhere?

$ 0.00
1 year ago

It intentionally omits it. I will tie up this loose end soon enough, likely by prefacing and linking to relevant resources.

For now, it should be small, short and solve for the very specific case when someone searching for "how do I get balance for an address with electrum?"

I will write articles for setting up and tearing down as well, but until I have done that I will refer people to this introduction article: https://read.cash/@JonathanSilverblood/javascript-electrum-library-1ad95de5

At the end of the serie, I will problably write an overview article glueing them all together.

$ 0.00
1 year ago

Yeah, I think it's still better with introduction, because people might land here by googling "how to get bitcoin cash balance", then they'll have no idea what this talks about (without the introduction)

$ 0.00
1 year ago

Added an introduction to electrum-cash with a link at the top. Will add something to the footer later on.

$ 0.00
1 year ago