diff --git a/src/wallet-manager.ts b/src/wallet-manager.ts index 27e6845..6b2ff52 100644 --- a/src/wallet-manager.ts +++ b/src/wallet-manager.ts @@ -429,29 +429,17 @@ export class WalletManager { }, Object.keys(this.managers).length); } - // pullBalancesAtBlockHeight doesn't need balances to be refreshed in the background - public async pullBalancesAtBlockHeight( - blockHeightByChain?: Record, - ): Promise> { - const balances: Record = {}; - if (blockHeightByChain) { - this.validateBlockHeightByChain(blockHeightByChain); - } - - const blockHeightPerChain = blockHeightByChain ?? await this.getBlockHeightForAllSupportedChains(); - - await mapConcurrent( - Object.entries(this.managers), - async ([chainName, manager]) => { - const blockHeight = blockHeightPerChain[chainName as ChainName]; - const balancesByChain = await manager.pullBalancesAtBlockHeight( - blockHeight, - ); - balances[chainName] = balancesByChain; - }, - ); + // This method is similar to pullBalances, but it gets the block-height of each chain concurrently + // and uses the resulting block-height to pull balances at that specific block-height + // it tends to return a balance that better represents a snapshot in time (as opposed to pullBalances + // which tries to return the latest possible balance) + public async pullBalancesAtCurrentBlockHeight(): Promise> { + const blockHeightByChain = await this.getBlockHeightForAllSupportedChains(); - return balances; + return this.mapToChains(async (chainName: ChainName, manager: ChainWalletManager) => { + const blockHeight = blockHeightByChain[chainName]; + return manager.pullBalancesAtBlockHeight(blockHeight); + }); } public getChainBalances(chainName: ChainName): WalletBalancesByAddress {