Skip to content

Commit 5b39c28

Browse files
feat: Update Balancer SOR package to handle new Linear Pool paths (0xProject#1269)
* Update SOR, SG query, SOR Config. * Update SOR package - now handles paths where Weighted have Linear tokens. * Fix lint error. * Merged master. * Add config for LBP paths. * Revert "Merged master." This reverts commit cb47a2f. * fix gas estimate to be more accurate for BalancerV2 pools --------- Co-authored-by: Ido Kleinman <idokleinman@users.noreply.github.com> Co-authored-by: Ido Kleinman <idokleinman@gmail.com>
1 parent 12392eb commit 5b39c28

File tree

5 files changed

+108
-9
lines changed

5 files changed

+108
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"@0x/types": "^3.3.6",
116116
"@0x/utils": "^7.0.0",
117117
"@0x/web3-wrapper": "^8.0.0",
118-
"@balancer-labs/sor": "^4.0.1-beta.5",
118+
"@balancer-labs/sor": "^4.1.1-beta.2",
119119
"@bancor/sdk": "0.2.10",
120120
"@cryptoalgebra/core": "^2.0.0",
121121
"@cryptoalgebra/periphery": "^1.0.0",

src/asset-swapper/utils/market_operation_utils/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,7 @@ export const DEFAULT_GAS_SCHEDULE: GasSchedule = {
19711971
[ERC20BridgeSource.KnightSwap]: uniswapV2CloneGasSchedule,
19721972
[ERC20BridgeSource.Balancer]: () => 120e3,
19731973
[ERC20BridgeSource.BalancerV2]: (fillData?: FillData) => {
1974-
return 100e3 + ((fillData as BalancerV2BatchSwapFillData).swapSteps.length - 1) * 50e3;
1974+
return 140e3 + ((fillData as BalancerV2BatchSwapFillData).swapSteps.length - 1) * 80e3;
19751975
},
19761976
[ERC20BridgeSource.MStable]: () => 200e3,
19771977
[ERC20BridgeSource.MakerPsm]: (fillData?: FillData) => {

src/asset-swapper/utils/market_operation_utils/pools_cache/balancer_v2_swap_info_cache.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type BalancerChains =
3232
| ChainId.Optimism
3333
| ChainId.Arbitrum;
3434

35-
const SOR_CONFIG: Record<BalancerChains, SorConfig> = {
35+
const SOR_CONFIG: Record<BalancerChains, SorConfig & { poolsToIgnore?: string[] }> = {
3636
[ChainId.Mainnet]: {
3737
chainId: ChainId.Mainnet,
3838
vault: '0xba12222222228d8ba445958a75a0704d566bf2c8',
@@ -41,31 +41,105 @@ const SOR_CONFIG: Record<BalancerChains, SorConfig> = {
4141
id: '0x32296969ef14eb0c6d29669c550d4a0449130230000200000000000000000080',
4242
address: '0x32296969ef14eb0c6d29669c550d4a0449130230',
4343
},
44+
connectingTokens: [
45+
{
46+
symbol: 'wEth',
47+
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
48+
},
49+
{
50+
symbol: 'wstEth',
51+
address: '0x7f39c581f595b53c5cb19bd0b3f8da6c935e2ca0',
52+
},
53+
{
54+
symbol: 'DOLA',
55+
address: '0x865377367054516e17014ccded1e7d814edc9ce4',
56+
},
57+
],
58+
poolsToIgnore: [
59+
'0xbd482ffb3e6e50dc1c437557c3bea2b68f3683ee', // a pool made by an external dev who was playing with a novel rate provider mechanism in production.
60+
],
61+
lbpRaisingTokens: [
62+
'0x6b175474e89094c44da98b954eedeac495271d0f', // DAI
63+
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', // USDC
64+
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // WETH
65+
],
4466
},
4567
[ChainId.Polygon]: {
4668
chainId: ChainId.Polygon,
4769
vault: '0xba12222222228d8ba445958a75a0704d566bf2c8',
4870
weth: '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270',
71+
connectingTokens: [
72+
{
73+
symbol: 'weth',
74+
address: '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270',
75+
},
76+
{
77+
symbol: 'bbrz2',
78+
address: '0xe22483774bd8611be2ad2f4194078dac9159f4ba',
79+
}, // Joins Stables<>BRZ via https://app.balancer.fi/#/polygon/pool/0x4a0b73f0d13ff6d43e304a174697e3d5cfd310a400020000000000000000091c
80+
],
81+
poolsToIgnore: [
82+
'0x600bd01b6526611079e12e1ff93aba7a3e34226f', // This pool has rateProviders with incorrect scaling
83+
],
84+
lbpRaisingTokens: [
85+
'0x8f3cf7ad23cd3cadbd9735aff958023239c6a063', // DAI
86+
'0x2791bca1f2de4661ed88a30c99a7a9449aa84174', // USDC
87+
'0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270', // WMATIC
88+
],
4989
},
5090
[ChainId.Arbitrum]: {
5191
chainId: ChainId.Arbitrum,
5292
vault: '0xba12222222228d8ba445958a75a0704d566bf2c8',
5393
weth: '0x82af49447d8a07e3bd95bd0d56f35241523fbab1',
94+
connectingTokens: [
95+
{
96+
symbol: 'weth',
97+
address: '0x82af49447d8a07e3bd95bd0d56f35241523fbab1',
98+
},
99+
],
100+
lbpRaisingTokens: [
101+
'0xda10009cbd5d07dd0cecc66161fc93d7c9000da1', // DAI
102+
'0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // USDC
103+
'0x82af49447d8a07e3bd95bd0d56f35241523fbab1', // WETH
104+
],
54105
},
55106
[ChainId.Goerli]: {
56107
chainId: ChainId.Goerli,
57108
vault: '0x65748e8287ce4b9e6d83ee853431958851550311',
58109
weth: '0x9a1000d492d40bfccbc03f413a48f5b6516ec0fd',
110+
connectingTokens: [
111+
{
112+
symbol: 'weth',
113+
address: '0xdfcea9088c8a88a76ff74892c1457c17dfeef9c1',
114+
},
115+
],
59116
},
60117
[ChainId.Optimism]: {
61118
chainId: ChainId.Optimism,
62119
vault: '0xba12222222228d8ba445958a75a0704d566bf2c8',
63120
weth: '0x4200000000000000000000000000000000000006',
121+
connectingTokens: [
122+
{
123+
symbol: 'weth',
124+
address: '0x4200000000000000000000000000000000000006',
125+
},
126+
],
127+
lbpRaisingTokens: [
128+
'0xda10009cbd5d07dd0cecc66161fc93d7c9000da1', // DAI
129+
'0x7f5c764cbc14f9669b88837ca1490cca17c31607', // USDC
130+
'0x4200000000000000000000000000000000000006', // WETH
131+
],
64132
},
65133
[ChainId.Fantom]: {
66134
chainId: ChainId.Fantom,
67135
vault: '0x20dd72ed959b6147912c2e529f0a0c651c33c9ce',
68136
weth: '0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83',
137+
connectingTokens: [
138+
{
139+
symbol: 'weth',
140+
address: '0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83',
141+
},
142+
],
69143
},
70144
};
71145

@@ -95,6 +169,7 @@ export class BalancerV2SwapInfoCache extends SwapInfoCache {
95169
this._poolDataService = new SubgraphPoolDataService({
96170
chainId,
97171
subgraphUrl,
172+
poolsToIgnore: SOR_CONFIG[chainId as BalancerChains].poolsToIgnore,
98173
});
99174
const sor = new SOR(
100175
provider,

src/asset-swapper/utils/market_operation_utils/pools_cache/sgPoolDataService.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { logUtils } from '@0x/utils';
33
import { PoolDataService, SubgraphPoolBase } from '@balancer-labs/sor';
44
import { gql, request } from 'graphql-request';
55

6+
const isSameAddress = (address1: string, address2: string): boolean =>
7+
address1.toLowerCase() === address2.toLowerCase();
8+
69
const queryWithLinear = gql`
710
query fetchTopPoolsWithLinear($maxPoolsFetched: Int!) {
811
pools: pools(
@@ -16,7 +19,7 @@ const queryWithLinear = gql`
1619
poolType
1720
swapFee
1821
totalShares
19-
tokens {
22+
tokens(orderBy: index) {
2023
address
2124
balance
2225
decimals
@@ -38,6 +41,20 @@ const queryWithLinear = gql`
3841
sqrtAlpha
3942
sqrtBeta
4043
root3Alpha
44+
alpha
45+
beta
46+
c
47+
s
48+
lambda
49+
tauAlphaX
50+
tauAlphaY
51+
tauBetaX
52+
tauBetaY
53+
u
54+
v
55+
w
56+
z
57+
dSq
4158
}
4259
}
4360
`;
@@ -64,6 +81,7 @@ export class SubgraphPoolDataService implements PoolDataService {
6481
chainId: number;
6582
subgraphUrl: string | null;
6683
maxPoolsFetched?: number;
84+
poolsToIgnore?: string[];
6785
},
6886
) {
6987
this._config.maxPoolsFetched = this._config.maxPoolsFetched || DEFAULT_MAX_POOLS_FETCHED;
@@ -78,7 +96,13 @@ export class SubgraphPoolDataService implements PoolDataService {
7896
const { pools } = await request<{ pools: SubgraphPoolBase[] }>(this._config.subgraphUrl, this._gqlQuery, {
7997
maxPoolsFetched: this._config.maxPoolsFetched,
8098
});
81-
return pools;
99+
// Filter out any pools that were set to ignore in config
100+
const filteredPools = pools.filter((p) => {
101+
if (!this._config.poolsToIgnore) return true;
102+
const index = this._config.poolsToIgnore.findIndex((addr) => isSameAddress(addr, p.address));
103+
return index === -1;
104+
});
105+
return filteredPools;
82106
} catch (err) {
83107
logUtils.warn(`Failed to fetch BalancerV2 subgraph pools: ${err.message}`);
84108
return [];

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,10 @@
757757
"@babel/helper-validator-identifier" "^7.18.6"
758758
to-fast-properties "^2.0.0"
759759

760-
"@balancer-labs/sor@^4.0.1-beta.5":
761-
version "4.0.1-beta.5"
762-
resolved "https://registry.yarnpkg.com/@balancer-labs/sor/-/sor-4.0.1-beta.5.tgz#a65efd0cbfbf9ce41694387f9c89e617f5c3d07a"
763-
integrity sha512-HRww8+5MtV+cTNiAVgpeEBAHCFrsXApibkkKZxB9Lazv5imhM/f0/RYDie+I9S+p57m82njQOr1L0k5MSUSrPw==
760+
"@balancer-labs/sor@^4.1.1-beta.2":
761+
version "4.1.1-beta.2"
762+
resolved "https://registry.yarnpkg.com/@balancer-labs/sor/-/sor-4.1.1-beta.2.tgz#4ecc381c7823d7d80c891b64bab6fe4d4a56a5ef"
763+
integrity sha512-NNy6/Vm6Rk1waKu9EYolJdTRuoUwUddXPAmeByb+ELQlJ8ZMoCxMb0ZFHjoBeIuHXC7xFky5XWiOfgRHBMcRVg==
764764
dependencies:
765765
isomorphic-fetch "^2.2.1"
766766

0 commit comments

Comments
 (0)