Skip to content

Commit

Permalink
refactor pullBalancesAtBlockHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
solanoepalacio committed Dec 6, 2023
1 parent 97aea59 commit 03f8af8
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions src/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,29 +430,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<ChainName, number>,
): Promise<Record<string, WalletBalancesByAddress>> {
const balances: Record<string, WalletBalancesByAddress> = {};
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<Record<string, WalletBalancesByAddress>> {
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 {
Expand Down

0 comments on commit 03f8af8

Please sign in to comment.