Skip to content

Commit

Permalink
fix(InventoryClient): Don't try to query l2 token for l1 token on cha…
Browse files Browse the repository at this point in the history
…in that it isn't enabled for (#1143)

* fix(InventoryClient): Don't try to query l2 token for l1 token on chain that it isn't enabled for

If `constants-v2` and `sdk-v2` aren't aware of a token's address on an L2 (example, WBTC on Base/8453), then attempting to call `HubPoolClient.getDestinationTokenForL1Token()` will throw an error. We should handle this early and exit early like we do in other functions.

The offending function fixed in this PR is `getChainDistribution()`, which is called by `getTokenDistributionPerL1Token()`

* Update InventoryClient.ts

* Update InventoryClient.ts
  • Loading branch information
nicholaspai authored Jan 9, 2024
1 parent fd25b90 commit 7dc2c90
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/clients/InventoryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ export class InventoryClient {
getBalanceOnChainForL1Token(chainId: number | string, l1Token: string): BigNumber {
// We want to skip any l2 token that is not present in the inventory config.
chainId = Number(chainId);
if (
chainId !== this.hubPoolClient.chainId &&
this.inventoryConfig.tokenConfig?.[l1Token]?.[String(chainId)] === undefined
) {
if (chainId !== this.hubPoolClient.chainId && !this._l1TokenEnabledForChain(l1Token, chainId)) {
return bnZero;
}

Expand All @@ -88,10 +85,14 @@ export class InventoryClient {
const cumulativeBalance = this.getCumulativeBalance(l1Token);
const distribution: { [chainId: number]: BigNumber } = {};
this.getEnabledChains().forEach((chainId) => {
if (cumulativeBalance.gt(0)) {
distribution[chainId] = this.getBalanceOnChainForL1Token(chainId, l1Token)
.mul(this.scalar)
.div(cumulativeBalance);
// If token doesn't have entry on chain, skip creating an entry for it since we'll likely run into an error
// later trying to grab the chain equivalent of the L1 token via the HubPoolClient.
if (chainId === this.hubPoolClient.chainId || this._l1TokenEnabledForChain(l1Token, chainId)) {
if (cumulativeBalance.gt(0)) {
distribution[chainId] = this.getBalanceOnChainForL1Token(chainId, l1Token)
.mul(this.scalar)
.div(cumulativeBalance);
}
}
});
return distribution;
Expand Down Expand Up @@ -273,7 +274,7 @@ export class InventoryClient {
for (const chainId of this.getEnabledL2Chains()) {
// Skip if there's no configuration for l1Token on chainId. This is the case for BOBA and BADGER
// as they're not present on all L2s.
if (this.inventoryConfig.tokenConfig?.[l1Token]?.[String(chainId)] === undefined) {
if (!this._l1TokenEnabledForChain(l1Token, chainId)) {
continue;
}

Expand Down Expand Up @@ -674,6 +675,10 @@ export class InventoryClient {
return false;
}

_l1TokenEnabledForChain(l1Token: string, chainId: number): boolean {
return this.inventoryConfig.tokenConfig?.[l1Token]?.[String(chainId)] !== undefined;
}

log(message: string, data?: AnyObject, level: DefaultLogLevels = "debug"): void {
if (this.logger) {
this.logger[level]({ at: "InventoryClient", message, ...data });
Expand Down

0 comments on commit 7dc2c90

Please sign in to comment.