From 00f100b1963487f52b772da55376e0ae70810e29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Sim=C3=A3o?= Date: Wed, 16 Oct 2024 17:55:49 +0100 Subject: [PATCH] fix: code review --- sdk/src/esplora.ts | 24 +++++++++--------------- sdk/test/esplora.test.ts | 4 ++-- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/sdk/src/esplora.ts b/sdk/src/esplora.ts index 0c80030e..ea4ab8fe 100644 --- a/sdk/src/esplora.ts +++ b/sdk/src/esplora.ts @@ -396,12 +396,12 @@ export class EsploraClient { * that are pending (unconfirmed). The total is the sum of both confirmed and unconfirmed balances. * * @param {string} address - The Bitcoin address to check. - * @returns {Promise<{ chain: number, mempool: number, total: number }>} A promise that resolves to an object containing: - * - `chain`: The confirmed on-chain balance in satoshis. - * - `mempool`: The unconfirmed balance (in mempool) in satoshis. + * @returns {Promise<{ confirmed: number, unconfirmed: number, total: number }>} A promise that resolves to an object containing: + * - `confirmed`: The confirmed on-chain balance in satoshis. + * - `unconfirmed`: The unconfirmed balance (in mempool) in satoshis. * - `total`: The total balance, which is the sum of the confirmed and unconfirmed balances. */ - async getBalance(address: string): Promise<{ chain: number; mempool: number; total: number }> { + async getBalance(address: string): Promise<{ confirmed: number; unconfirmed: number; total: number }> { const response = await this.getJson<{ address: string; chain_stats: { @@ -420,19 +420,13 @@ export class EsploraClient { }; }>(`${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; - - console.log({ - chain: chainBalance, - mempool: mempoolBalance, - total: chainBalance + mempoolBalance, - }); + const confirmedBalance = response.chain_stats.funded_txo_sum - response.chain_stats.spent_txo_sum; + const unconfirmedBalance = response.mempool_stats.funded_txo_sum - response.mempool_stats.spent_txo_sum; return { - chain: chainBalance, - mempool: mempoolBalance, - total: chainBalance + mempoolBalance, + confirmed: confirmedBalance, + unconfirmed: unconfirmedBalance, + total: confirmedBalance + unconfirmedBalance, }; } diff --git a/sdk/test/esplora.test.ts b/sdk/test/esplora.test.ts index d5eecf32..a163a371 100644 --- a/sdk/test/esplora.test.ts +++ b/sdk/test/esplora.test.ts @@ -139,8 +139,8 @@ describe('Esplora Tests', () => { const client = new EsploraClient('testnet'); const balance = await client.getBalance('tb1qjhekcm565spvr0epqu5nvd9mhgwaafg6d0n2yw'); assert.deepEqual(balance, { - chain: 727499862, - mempool: 0, + confirmed: 727499862, + unconfirmed: 0, total: 727499862, }); });