Join 75,918 users and earn money for participation

electrum-cash: broadcasting transactions

5 192 exc boost
Avatar for JonathanSilverblood
Written by   236
1 year ago

Introduction

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

This example demonstrates how to broadcast a raw transaction and how to get notified of when the transaction gets included in a block, 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.

Broadcasting a transaction

To broadcast a raw transaction, send a blockchain.transaction.broadcast request with the raw transaction hex as the first parameter:

// The raw transaction we want to broadcast.
const transactionHex = '01000000017616e3106ca8e7a92eed190319fea34327edfb0991714444a5cc757000cde21a000000006a473044022007b6e2e864f9807602b9937d25b7df7ffdcd1ff559dad0cc130e9174bfea5cd8022070ae24a132f9420090d878defb17b78db7f15ced5ea4090c2f423d65f0d70d6c412102c94e85aa74899fb9378079e42c0e225c7481689c7fd7b3accd567ddafb53c6acfeffffff026db40600000000001976a914ebdeb6430f3d16a9c6758d6c0d7a400c8e6bbee488acdc120b00000000001976a9142c805fc822ea73b954040d787a8ea69e8d8caf5c88acf5d30900';

// Broadcast the transaction.
const transactionHash = await electrum.request('blockchain.transaction.broadcast', transactionHex);

// Log the transaction hash.
console.log(transactionHash);

Example output:

6df81e9abee5702d9c8b0ad941e0c0d663855d2186162fb5e86136d99904c48c

Checking transaction confirmations

To look up transaction confirmations, send a blockchain.transaction.get request with the transaction hash as the first parameter and set verbose flag as the second parameter to true.

// The transaction we want to look up.
const transactionHash = '6df81e9abee5702d9c8b0ad941e0c0d663855d2186162fb5e86136d99904c48c';

// Request verbose transaction details which includes the number of confirmations.
const { confirmations } = await electrum.request('blockchain.transaction.get', transactionHash, true);

// Log the transaction confirmations.
console.log(confirmations);

Example output when the transaction is still in the mempool:

undefined

Example output when the transaction has 4 confirmations:

4

Listening for transaction block inclusion

To get notification when your transaction is included in a block, subscribe to new block notifications with blockchain.headers.subscribe, then request the transaction status. Remember to unsubscribe when you no longer need the information.

Disclaimer: the unsubscribe feature will be included in the v1.2.0 release of electrum-cash

// Create a function that will handle confirmation updates.
const handleConfirmationUpdates = async function(callbackFunction, transactionHash)
{
	// Request verbose transaction details which includes the number of confirmations.
	const { confirmations } = await electrum.request('blockchain.transaction.get', transactionHash, true);

	// If the transaction now have confirmations..
	if(confirmations)
	{
		// Log that the transaction was confirmed.
		console.log(`Transaction '${transactionHash}' now have ${confirmations} confirmations.`);

		// Unsubscribe from further updates.
		// NOTE: we bind the transaction to ensure that it uses the same name as when we subscribed.
		electrum.unsubscribe(callbackFunction.bind(this), `blockchain.headers.subscribe`);
	}
}

// The transaction we want to look up.
const transactionHash = '6df81e9abee5702d9c8b0ad941e0c0d663855d2186162fb5e86136d99904c48c';

// Subscribe to notifications for new blocks
// NOTE: we bind the transaction to provide the transaction hash to it.
await electrum.subscribe(handleConfirmationUpdates.bind(this, handleConfirmationUpdates, transactionHash), `blockchain.headers.subscribe`);

Example output when the transaction gets included in a block:

Transaction '6df81e9abee5702d9c8b0ad941e0c0d663855d2186162fb5e86136d99904c48c' now have 1 confirmations.

Conclusion

Broadcasting a transaction with electrum-cash is easy, but tracking when the transaction gets confirmed involves tracking different related data which can cause problems if you naively unsubscribe while having multiple active subscriptions.

I have reached out to the backend developers to try and coordinate an update of the protocol to provide blockchain.transaction.subscribe, a subscription call that sends notifications when the status of a transaction changes which would simplify this process.

Issues requesting the new feature:

53
$ 6.93
$ 4.98 from @TheRandomRewarder
$ 1.00 from @rosco
$ 0.50 from @emergent_reasons
+ 3
Avatar for JonathanSilverblood
Written by   236
1 year ago
Enjoyed this article?  Earn Bitcoin Cash by sharing it! Explain
...and you will also help the author collect more tips.

Comments

This stuff seems hard...

$ 0.00
1 year ago

Anything worthwhile is.

$ 0.00
1 year ago

Thanks for showing this. I just subscribed to you Sir, I hope to get more information. Thanks

$ 0.00
1 year ago

At minimum I expect to write 4 more articles:

  • Setting up a connection to a single server
  • Setting up a high-availability connection to a cluster of servers.
  • Transaction history and transaction notifications.
  • Finding unspent transaction outputs needed to make transactions
$ 0.00
1 year ago