Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/confirmed param balance #392

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.3.8",
"version": "3.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
Expand Down
27 changes: 23 additions & 4 deletions sdk/src/esplora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ export class EsploraClient {
*
* @dev Should return up to 500 UTXOs - depending on the configured limit.
* @param {string} address - The Bitcoin address to check.
* @param {string} [confirmed] - Whether to return only confirmed UTXOs. If omitted, defaults to false.
* @returns {Promise<Array<UTXO>>} A promise that resolves to an array of UTXOs.
*/
async getAddressUtxos(address: string, confirmed?: boolean): Promise<Array<UTXO>> {
Expand Down Expand Up @@ -388,12 +389,19 @@ export class EsploraClient {
}

/**
* Get the balance for an account / address.
* Get the Bitcoin balance for an address, returning both confirmed (on-chain) and unconfirmed (mempool) balances.
*
* @dev This method returns an object containing the confirmed balance, unconfirmed balance, and the total balance.
* The confirmed balance represents the amount that is on-chain, while the mempool balance includes transactions
* 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<number>} A promise that resolves to the balance.
* @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.
* - `total`: The total balance, which is the sum of the confirmed and unconfirmed balances.
danielsimao marked this conversation as resolved.
Show resolved Hide resolved
*/
async getBalance(address: string): Promise<number> {
async getBalance(address: string): Promise<{ chain: number; mempool: number; total: number }> {
const response = await this.getJson<{
address: string;
chain_stats: {
Expand All @@ -414,7 +422,18 @@ export class EsploraClient {

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;

console.log({
chain: chainBalance,
mempool: mempoolBalance,
total: chainBalance + mempoolBalance,
});

danielsimao marked this conversation as resolved.
Show resolved Hide resolved
return {
chain: chainBalance,
mempool: mempoolBalance,
total: chainBalance + mempoolBalance,
};
}

/**
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 @@ -138,6 +138,10 @@ describe('Esplora Tests', () => {
it('should get balance', async () => {
const client = new EsploraClient('testnet');
const balance = await client.getBalance('tb1qjhekcm565spvr0epqu5nvd9mhgwaafg6d0n2yw');
assert.equal(balance, 727499862);
assert.deepEqual(balance, {
chain: 727499862,
mempool: 0,
total: 727499862,
});
});
});
Loading