Skip to content

Commit 01c584e

Browse files
committed
[Issue-196] optimize speed get bitcoin balance
1 parent 7dde54d commit 01c584e

File tree

2 files changed

+22
-34
lines changed
  • packages/extension-base/src/services
    • balance-service/helpers/balance
    • chain-service/handler/bitcoin/strategy/BlockStream

2 files changed

+22
-34
lines changed

packages/extension-base/src/services/balance-service/helpers/balance/bitcoin.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { _BitcoinApi } from '@subwallet/extension-base/services/chain-service/ty
99
import { _getChainNativeTokenSlug, _getRuneId } from '@subwallet/extension-base/services/chain-service/utils';
1010
import { BalanceItem, UtxoResponseItem } from '@subwallet/extension-base/types';
1111
import { filterAssetsByChainAndType, filteredOutTxsUtxos, getInscriptionUtxos, getRuneUtxos } from '@subwallet/extension-base/utils';
12-
import BigN from 'bignumber.js';
1312

1413
// todo: update bitcoin params
1514
function subscribeRuneBalance (bitcoinApi: _BitcoinApi, addresses: string[], assetMap: Record<string, _ChainAsset>, chainInfo: _ChainInfo, callback: (rs: BalanceItem[]) => void) {
@@ -161,17 +160,11 @@ export const getTransferableBitcoinUtxos = async (bitcoinApi: _BitcoinApi, addre
161160
async function getBitcoinBalance (bitcoinApi: _BitcoinApi, addresses: string[]) {
162161
return await Promise.all(addresses.map(async (address) => {
163162
try {
164-
const filteredUtxos = await getTransferableBitcoinUtxos(bitcoinApi, address);
163+
const addressInfo = await bitcoinApi.api.getAddressSummaryInfo(address);
165164

166-
let balanceValue = new BigN(0);
167-
168-
filteredUtxos.forEach((utxo) => {
169-
balanceValue = balanceValue.plus(utxo.value);
170-
});
171-
172-
return balanceValue.toString();
165+
return addressInfo.balance.toString();
173166
} catch (error) {
174-
console.log('Error while fetching Bitcoin balances', error);
167+
console.log(`Error while fetching Bitcoin balances for address ${address}`, error);
175168

176169
return '0';
177170
}

packages/extension-base/src/services/chain-service/handler/bitcoin/strategy/BlockStream/types.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ export interface BitcoinAddressSummaryInfo {
3232
spent_txo_count: number,
3333
spent_txo_sum: number,
3434
tx_count: number
35-
}
35+
},
36+
balance: number,
37+
balance_inscription: number,
38+
balance_rune: number,
39+
total_inscription: number
3640
}
3741

3842
// todo: combine RunesByAddressResponse & RunesCollectionInfoResponse
@@ -274,27 +278,18 @@ export interface RuneMetadata {
274278
id: string,
275279
mintable: boolean,
276280
parent: string,
277-
entry: RuneInfo
278-
}
279-
280-
interface RuneInfo {
281-
block: number,
282-
burned: string,
283-
divisibility: number,
284-
etching: string,
285-
mints: string,
286-
number: number,
287-
premine: string,
288-
spaced_rune: string,
289-
symbol: string,
290-
terms: RuneTerms
291-
timestamp: string,
292-
turbo: boolean
293-
}
294-
295-
interface RuneTerms {
296-
amount: string,
297-
cap: string,
298-
height: string[],
299-
offset: string[]
281+
entry: {
282+
block: number,
283+
burned: number,
284+
divisibility: number,
285+
etching: string,
286+
mints: number,
287+
number: number,
288+
premine: number,
289+
spaced_rune: string,
290+
symbol: string,
291+
// terms:
292+
timestamp: number,
293+
turbo: boolean
294+
}
300295
}

0 commit comments

Comments
 (0)