Skip to content

Commit aa1016e

Browse files
authored
Do not initialize BalancerV2SwapInfoCache on unsupported chains [TKR-365] (0xProject#472)
* Do not initialize BalancerV2SwapInfoCache on unsupported chains * Update CHANGELOG.json
1 parent 423ef57 commit aa1016e

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

packages/asset-swapper/CHANGELOG.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
[
2+
{
3+
"version": "16.57.3",
4+
"changes": [
5+
{
6+
"note": "Fix a runtime error related to BalancerV2SwapInfoCache",
7+
"pr": 472
8+
}
9+
]
10+
},
211
{
312
"version": "16.57.2",
413
"changes": [

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.
@@ -1499,15 +1502,19 @@ export class SamplerOperations {
14991502
),
15001503
);
15011504
case ERC20BridgeSource.BalancerV2: {
1502-
const swaps = this.poolsCaches[source].getCachedSwapInfoForPair(takerToken, makerToken);
1505+
const cache = this.poolsCaches[source];
1506+
if (!cache) {
1507+
return [];
1508+
}
15031509

1510+
const swaps = cache.getCachedSwapInfoForPair(takerToken, makerToken);
15041511
const vault = BALANCER_V2_VAULT_ADDRESS_BY_CHAIN[this.chainId];
15051512
if (!swaps || vault === NULL_ADDRESS) {
15061513
return [];
15071514
}
15081515
// Changed to retrieve queryBatchSwap for swap steps > 1 of length
15091516
return swaps.swapInfoExactIn.map(swapInfo =>
1510-
this.getBalancerV2MulthopSellQuotes(vault, swapInfo, swapInfo, takerFillAmounts, source),
1517+
this.getBalancerV2MultihopSellQuotes(vault, swapInfo, swapInfo, takerFillAmounts, source),
15111518
);
15121519
}
15131520
case ERC20BridgeSource.Beethovenx: {
@@ -1822,15 +1829,19 @@ export class SamplerOperations {
18221829
),
18231830
);
18241831
case ERC20BridgeSource.BalancerV2: {
1825-
const swaps = this.poolsCaches[source].getCachedSwapInfoForPair(takerToken, makerToken);
1832+
const cache = this.poolsCaches[source];
1833+
if (!cache) {
1834+
return [];
1835+
}
18261836

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

0 commit comments

Comments
 (0)