diff --git a/package.json b/package.json index a1ff4fba..7d81f1c6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@across-protocol/sdk-v2", "author": "UMA Team", - "version": "0.22.1", + "version": "0.22.2", "license": "AGPL-3.0", "homepage": "https://docs.across.to/v/developer-docs/developers/across-sdk", "files": [ diff --git a/src/clients/HubPoolClient.ts b/src/clients/HubPoolClient.ts index 68636853..7629204d 100644 --- a/src/clients/HubPoolClient.ts +++ b/src/clients/HubPoolClient.ts @@ -362,9 +362,6 @@ export class HubPoolClient extends BaseAbstractClient { return { ...partialDeposit, inputToken, inputAmount }; }); - // Map SpokePool token addresses to HubPool token addresses. - const hubPoolTokens: { [originToken: string]: string } = {}; - // Map each HubPool token to an array of unqiue quoteTimestamps. const utilizationTimestamps: { [hubPoolToken: string]: number[] } = {}; @@ -373,22 +370,22 @@ export class HubPoolClient extends BaseAbstractClient { let quoteBlocks: { [quoteTimestamp: number]: number } = {}; + // Map SpokePool token addresses to HubPool token addresses. + // Note: Should only be accessed via `getHubPoolToken()` or `getHubPoolTokens()`. + const hubPoolTokens: { [k: string]: string } = {}; + const getHubPoolToken = (deposit: V3PartialDepositWithBlock, quoteBlockNumber: number): string => { + const tokenKey = `${deposit.originChainId}-${deposit.inputToken}`; + return (hubPoolTokens[tokenKey] ??= this.getL1TokenForDeposit({ ...deposit, quoteBlockNumber })); + }; + const getHubPoolTokens = (): string[] => dedupArray(Object.values(hubPoolTokens)); + // Helper to resolve the unqiue hubPoolToken & quoteTimestamp mappings. - const resolveUniqueQuoteTimestamps = (deposit: (typeof deposits)[0]): void => { - const { originChainId } = deposits[0]; - const { originChainId: chainId, inputToken, quoteTimestamp } = deposit; - assert( - chainId === originChainId, - `Cannot compute bulk realizedLpFeePct for different origin chains (${chainId} != ${originChainId})` - ); + const resolveUniqueQuoteTimestamps = (deposit: V3PartialDepositWithBlock): void => { + const { quoteTimestamp } = deposit; - // Resolve the HubPool token address, if it isn't already known. - const quoteBlockNumber = quoteBlocks[deposit.quoteTimestamp]; - const hubPoolToken = (hubPoolTokens[inputToken] ??= this.getL1TokenForDeposit({ - ...deposit, - inputToken, - quoteBlockNumber, - })); + // Resolve the HubPool token address for this origin chainId/token pair, if it isn't already known. + const quoteBlockNumber = quoteBlocks[quoteTimestamp]; + const hubPoolToken = getHubPoolToken(deposit, quoteBlockNumber); // Append the quoteTimestamp for this HubPool token, if it isn't already enqueued. utilizationTimestamps[hubPoolToken] ??= []; @@ -425,15 +422,15 @@ export class HubPoolClient extends BaseAbstractClient { }; // Helper compute the realizedLpFeePct of an individual deposit based on pre-retrieved batch data. - const computeRealizedLpFeePct = async (deposit: (typeof deposits)[0]) => { - const { originChainId, paymentChainId, inputToken, inputAmount, quoteTimestamp } = deposit; + const computeRealizedLpFeePct = async (deposit: V3PartialDepositWithBlock & { paymentChainId?: number }) => { + const { originChainId, paymentChainId, inputAmount, quoteTimestamp } = deposit; const quoteBlock = quoteBlocks[quoteTimestamp]; if (paymentChainId === undefined) { return { quoteBlock, realizedLpFeePct: bnZero }; } - const hubPoolToken = hubPoolTokens[inputToken]; + const hubPoolToken = getHubPoolToken(deposit, quoteBlock); const rateModel = this.configStoreClient.getRateModelForBlockNumber( hubPoolToken, originChainId, @@ -471,10 +468,7 @@ export class HubPoolClient extends BaseAbstractClient { // For each token / quoteBlock pair, resolve the utilisation for each quoted block. // This can be reused for each deposit with the same HubPool token and quoteTimestamp pair. utilization = Object.fromEntries( - await mapAsync(Object.values(hubPoolTokens), async (hubPoolToken) => [ - hubPoolToken, - await resolveUtilization(hubPoolToken), - ]) + await mapAsync(getHubPoolTokens(), async (hubPoolToken) => [hubPoolToken, await resolveUtilization(hubPoolToken)]) ); // For each deposit, compute the post-relay HubPool utilisation independently.