Skip to content

Commit

Permalink
fix: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsimao committed Oct 16, 2024
1 parent ec54300 commit 00f100b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
24 changes: 9 additions & 15 deletions sdk/src/esplora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
};
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/test/esplora.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
Expand Down

0 comments on commit 00f100b

Please sign in to comment.