Skip to content

Commit 2f8b487

Browse files
kyu-cdextracker
authored andcommitted
Do not initialize BalancerV2SwapInfoCache on unsupported chains [TKR-365] (#472)
* Do not initialize BalancerV2SwapInfoCache on unsupported chains * Update CHANGELOG.json
1 parent 40bc000 commit 2f8b487

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

packages/asset-swapper/CHANGELOG.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "16.57.3",
44
"changes": [
55
{
6-
"note": "Add BiSwap on BSC",
7-
"pr": 471
6+
"note": "Fix a runtime error related to BalancerV2SwapInfoCache",
7+
"pr": 472
88
}
99
]
1010
},

packages/asset-swapper/src/utils/market_operation_utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ export class MarketOperationUtils {
777777
private async _refreshPoolCacheIfRequiredAsync(takerToken: string, makerToken: string): Promise<void> {
778778
void Promise.all(
779779
Object.values(this._sampler.poolsCaches).map(async cache => {
780-
if (cache.isFresh(takerToken, makerToken)) {
780+
if (!cache || cache.isFresh(takerToken, makerToken)) {
781781
return Promise.resolve([]);
782782
}
783783
return cache.getFreshPoolsForPairAsync(takerToken, makerToken);

packages/asset-swapper/src/utils/market_operation_utils/sampler_operations.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export const TWO_HOP_SOURCE_FILTERS = SourceFilters.all().exclude([
107107
export const BATCH_SOURCE_FILTERS = SourceFilters.all().exclude([ERC20BridgeSource.MultiHop, ERC20BridgeSource.Native]);
108108

109109
export type PoolsCacheMap = { [key in Exclude<SourcesWithPoolsCache, ERC20BridgeSource.BalancerV2>]: PoolsCache } & {
110-
[ERC20BridgeSource.BalancerV2]: BalancerV2SwapInfoCache;
110+
[ERC20BridgeSource.BalancerV2]: BalancerV2SwapInfoCache | undefined;
111111
};
112112

113113
// tslint:disable:no-inferred-empty-object-type no-unbound-method
@@ -151,7 +151,10 @@ export class SamplerOperations {
151151
),
152152
[ERC20BridgeSource.Balancer]: new BalancerPoolsCache(),
153153
[ERC20BridgeSource.Cream]: new CreamPoolsCache(),
154-
[ERC20BridgeSource.BalancerV2]: new BalancerV2SwapInfoCache(chainId),
154+
[ERC20BridgeSource.BalancerV2]:
155+
BALANCER_V2_VAULT_ADDRESS_BY_CHAIN[chainId] === NULL_ADDRESS
156+
? undefined
157+
: new BalancerV2SwapInfoCache(chainId),
155158
};
156159

157160
const aaveSubgraphUrl = AAVE_V2_SUBGRAPH_URL_BY_CHAIN_ID[chainId];
@@ -571,7 +574,7 @@ export class SamplerOperations {
571574
});
572575
}
573576

574-
public getBalancerV2MulthopSellQuotes(
577+
public getBalancerV2MultihopSellQuotes(
575578
vault: string,
576579
quoteSwaps: BalancerSwapInfo, // Should always be sell swap steps.
577580
fillSwaps: BalancerSwapInfo, // Should always be sell swap steps.
@@ -592,7 +595,7 @@ export class SamplerOperations {
592595
});
593596
}
594597

595-
public getBalancerV2MulthopBuyQuotes(
598+
public getBalancerV2MultihopBuyQuotes(
596599
vault: string,
597600
quoteSwaps: BalancerSwapInfo, // Should always be buy swap steps.
598601
fillSwaps: BalancerSwapInfo, // Should always be a sell quote.
@@ -1500,15 +1503,19 @@ export class SamplerOperations {
15001503
),
15011504
);
15021505
case ERC20BridgeSource.BalancerV2: {
1503-
const swaps = this.poolsCaches[source].getCachedSwapInfoForPair(takerToken, makerToken);
1506+
const cache = this.poolsCaches[source];
1507+
if (!cache) {
1508+
return [];
1509+
}
15041510

1511+
const swaps = cache.getCachedSwapInfoForPair(takerToken, makerToken);
15051512
const vault = BALANCER_V2_VAULT_ADDRESS_BY_CHAIN[this.chainId];
15061513
if (!swaps || vault === NULL_ADDRESS) {
15071514
return [];
15081515
}
15091516
// Changed to retrieve queryBatchSwap for swap steps > 1 of length
15101517
return swaps.swapInfoExactIn.map(swapInfo =>
1511-
this.getBalancerV2MulthopSellQuotes(vault, swapInfo, swapInfo, takerFillAmounts, source),
1518+
this.getBalancerV2MultihopSellQuotes(vault, swapInfo, swapInfo, takerFillAmounts, source),
15121519
);
15131520
}
15141521
case ERC20BridgeSource.Beethovenx: {
@@ -1824,15 +1831,19 @@ export class SamplerOperations {
18241831
),
18251832
);
18261833
case ERC20BridgeSource.BalancerV2: {
1827-
const swaps = this.poolsCaches[source].getCachedSwapInfoForPair(takerToken, makerToken);
1834+
const cache = this.poolsCaches[source];
1835+
if (!cache) {
1836+
return [];
1837+
}
18281838

1839+
const swaps = cache.getCachedSwapInfoForPair(takerToken, makerToken);
18291840
const vault = BALANCER_V2_VAULT_ADDRESS_BY_CHAIN[this.chainId];
18301841
if (!swaps || vault === NULL_ADDRESS) {
18311842
return [];
18321843
}
18331844
// Changed to retrieve queryBatchSwap for swap steps > 1 of length
18341845
return swaps.swapInfoExactOut.map((quoteSwapInfo, i) =>
1835-
this.getBalancerV2MulthopBuyQuotes(
1846+
this.getBalancerV2MultihopBuyQuotes(
18361847
vault,
18371848
quoteSwapInfo,
18381849
swaps.swapInfoExactIn[i],

0 commit comments

Comments
 (0)