Skip to content

Commit

Permalink
wallet-manager: refactor getting block-heights concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
solanoepalacio committed Dec 6, 2023
1 parent a509b4d commit 97aea59
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions src/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export class WalletManager {
}
}

public async mapToChains<T>(method: (chain: ChainName, manager: ChainWalletManager) => Promise<T>): Promise<MapChainsResult<T>> {
public async mapToChains<T>(method: (chain: ChainName, manager: ChainWalletManager) => Promise<T>, concurrency?: number): Promise<MapChainsResult<T>> {
const result = {} as MapChainsResult<T>;

await mapConcurrent(
Expand All @@ -383,6 +383,7 @@ export class WalletManager {
const chainName = chain as ChainName;
result[chainName] = await method(chainName as ChainName, manager) as T;
},
concurrency,
);

return result;
Expand Down Expand Up @@ -424,26 +425,9 @@ export class WalletManager {
public async getBlockHeightForAllSupportedChains(): Promise<
Record<ChainName, number>
> {
// Required concurrency is the number of chains as we want to fetch the block height for all chains in parallel
// to be precise about the block height at the time of fetching balances
let blockHeightPerChain = {} as Record<ChainName, number>;
const requiredConcurrency = Object.keys(this.managers).length;
await mapConcurrent(
Object.entries(this.managers),
async ([chainName, manager]) => {
try {
const blockHeight = await manager.getBlockHeight();
blockHeightPerChain = {
...blockHeightPerChain,
[chainName]: blockHeight,
} as Record<ChainName, number>;
} catch (err) {
throw new Error(`No block height found for chain: ${chainName}, error: ${err}`);
}
},
requiredConcurrency,
);
return blockHeightPerChain;
return this.mapToChains<number>(async (chainName: ChainName, manager: ChainWalletManager) => {
return manager.getBlockHeight();
}, Object.keys(this.managers).length);
}

// pullBalancesAtBlockHeight doesn't need balances to be refreshed in the background
Expand Down

0 comments on commit 97aea59

Please sign in to comment.