Skip to content

Commit

Permalink
Merge pull request #337 from bob-collective/feat/get-balance
Browse files Browse the repository at this point in the history
feat: add esplora get balance
  • Loading branch information
gregdhill committed Sep 5, 2024
2 parents a6876a9 + 8f4e17f commit 4c1c127
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gobob/bob-sdk",
"version": "2.2.4",
"version": "2.2.5",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
Expand Down
34 changes: 34 additions & 0 deletions sdk/src/esplora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,14 @@ export class EsploraClient {
/**
* Get the Unspent Transaction Outputs (UTXOs) for an address.
*
* @dev Should return up to 500 UTXOs - depending on the configured limit.
* @param {string} address - The Bitcoin address to check.
* @returns {Promise<Array<UTXO>>} A promise that resolves to an array of UTXOs.
*/
async getAddressUtxos(address: string, confirmed?: boolean): Promise<Array<UTXO>> {
// https://github.com/Blockstream/electrs/blob/306f66acf2ab10bcd99b8012e95a0de30b2cc012/src/rest.rs#L860
// https://github.com/Blockstream/electrs/blob/306f66acf2ab10bcd99b8012e95a0de30b2cc012/src/new_index/query.rs#L82
// https://github.com/Blockstream/electrs/blob/306f66acf2ab10bcd99b8012e95a0de30b2cc012/src/config.rs#L177
const response = await this.getJson<Array<{
txid: string,
vout: number,
Expand Down Expand Up @@ -372,6 +376,36 @@ export class EsploraClient {
return await res.text();
}

/**
* Get the balance for an account / address.
*
* @param {string} address - The Bitcoin address to check.
* @returns {Promise<number>} A promise that resolves to the balance.
*/
async getBalance(address: string): Promise<number> {
const response = await this.getJson<{
address: string,
chain_stats: {
funded_txo_count: number,
funded_txo_sum: number,
spent_txo_count: number,
spent_txo_sum: number,
tx_count: number
},
mempool_stats: {
funded_txo_count: number,
funded_txo_sum: number,
spent_txo_count: number,
spent_txo_sum: number,
tx_count: number
}
}>(`${this.basePath}/address/${address}`);

const chainBalance = response.chain_stats.funded_txo_sum - response.chain_stats.spent_txo_sum;
const mempoolBalance = response.mempool_stats.funded_txo_sum - response.mempool_stats.spent_txo_sum;
return chainBalance + mempoolBalance;
}

/**
* @ignore
*/
Expand Down
2 changes: 0 additions & 2 deletions sdk/src/wallet/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ export async function createBitcoinPsbt(

const esploraClient = new EsploraClient(addressInfo.network);

// NOTE: esplora only returns the 25 most recent UTXOs
// TODO: change this to use the pagination API and return all UTXOs
const [confirmedUtxos, feeRate] = await Promise.all([
esploraClient.getAddressUtxos(fromAddress),
esploraClient.getFeeEstimate(confirmationTarget),
Expand Down
6 changes: 5 additions & 1 deletion sdk/test/esplora.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,9 @@ describe("Esplora Tests", () => {
assert.isAtLeast(feeRate, 1);
});


it("should get balance", async () => {
const client = new EsploraClient("testnet");
const balance = await client.getBalance("tb1qjhekcm565spvr0epqu5nvd9mhgwaafg6d0n2yw");
assert.equal(balance, 727499862);
});
});

0 comments on commit 4c1c127

Please sign in to comment.