electrum-cash: working with transaction history
Introduction
Electrum-cash is a javascript/typescript library for communicating with electrum servers.
This example demonstrates how to get a list of all transactions for a given address and notifications when the transactions are updated, 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.
Fetching transaction history
To get the transaction history, send a blockchain.address.get_history
request with the address as the first parameter.
// The address we want to get history for.
const address = 'qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst';
// Request the transaction history from the server.
const transactionHistory = await electrum.request('blockchain.address.get_history', address);
// Count the number of confirmed and unconfirmed transactions.
const confirmedCount = transactionHistory.filter((entry) => entry.height > 0).length;
const unconfirmedCount = transactionHistory.filter((entry) => entry.height <= 0).length;
// Log the history information
console.log(`There are ${confirmedCount} confirmed and ${unconfirmedCount} unconfirmed transactions on '${address}'.`);
console.log(`The first transaction was in block #${transactionHistory[0].height} with transaction '${transactionHistory[0].tx_hash}'.`);
Example output with 1 unconfirmed transaction:
There are 605 confirmed and 1 unconfirmed transactions on 'qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst'.
The first transaction was in block #560682 with transaction 'd7820227977fc05170f1c1957453709511fd702f7d608a2f48e450588ad8c5a9'.
Tracking transaction history
To get notifications when the transaction history is updated, subscribe to blockchain.address.subscribe
to get status updates for the address, then request the transaction history.
// Create a function that will handle transaction history updates.
const handleTransactionHistoryUpdates = async function(address)
{
// Request the transaction history from the server.
const transactionHistory = await electrum.request('blockchain.address.get_history', address);
// Log the oldest and newest transactions.
console.log(transactionHistory[0]);
console.log([...transactionHistory].pop());
}
// The address we want to get transaction history updates for.
const address = 'qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst';
// Subscribe to notifications for updates to transaction history for the given address.
// NOTE: this sends notifications for all changes to the transaction history, not only for new transactions.
await electrum.subscribe(handleTransactionHistoryUpdates.bind(this, address), `blockchain.address.subscribe`, address);
Example output with one unconfirmed transaction:
{
height: 560682,
tx_hash: 'd7820227977fc05170f1c1957453709511fd702f7d608a2f48e450588ad8c5a9'
}{
fee: 230,
height: 0,
tx_hash: 'e2183edcfc4e82be0c09651cf815c223bface0fdf5fc670737ee4d883b2d3f21'
}
Conclusion
Using the electrum-cash
library makes it easy to get transaction history and to get notifications when that history changes. Tracking what actually changed can be done by comparing previously acquired transaction history with the updated transaction history, but that is out of the scope of this article.
Thank you for this informative article. I heard of electrum cash and this time i somehow understand how it works. The easy access on the transaction and notifications about it is very helpful and convenient. Thannks sir 😊