diff --git a/balancer-js/examples/data/alfajores.ts b/balancer-js/examples/data/alfajores.ts index f1ef5015..5e774d83 100644 --- a/balancer-js/examples/data/alfajores.ts +++ b/balancer-js/examples/data/alfajores.ts @@ -10,7 +10,7 @@ const sdk = new BalancerSDK({ }, }); -const { pools, tokenPrices } = sdk.data; +const { tokenPrices } = sdk.data; async function main() { console.log( await tokenPrices.find('0x1d4c35c3f4a91103ba323fe2f4c3f6ebef531c11') diff --git a/balancer-js/package.json b/balancer-js/package.json index 736bbe6a..c3a70e4c 100644 --- a/balancer-js/package.json +++ b/balancer-js/package.json @@ -1,6 +1,6 @@ { "name": "@kolektivo-labs/sdk", - "version": "0.0.1-beta", + "version": "0.0.2-beta", "description": "JavaScript SDK for interacting with the Balancer Protocol V2", "license": "GPL-3.0-only", "homepage": "https://github.com/balancer-labs/balancer-sdk#readme", diff --git a/balancer-js/src/lib/constants/config.ts b/balancer-js/src/lib/constants/config.ts index 0a30e4be..7e5bcf57 100644 --- a/balancer-js/src/lib/constants/config.ts +++ b/balancer-js/src/lib/constants/config.ts @@ -4,6 +4,21 @@ import addressesByNetwork from './addresses.json'; export const balancerVault = '0xBA12222222228d8Ba445958a75a0704d566BF2C8'; +export const vaultAddresses: Record = { + [Network.MAINNET]: balancerVault, + [Network.POLYGON]: balancerVault, + [Network.ARBITRUM]: balancerVault, + [Network.GOERLI]: balancerVault, + [Network.OPTIMISM]: balancerVault, + [Network.GNOSIS]: balancerVault, + [Network.FANTOM]: balancerVault, + [Network.BASE]: balancerVault, + [Network.ZKEVM]: balancerVault, + [Network.AVALANCHE]: balancerVault, + [Network.SEPOLIA]: balancerVault, + [Network.ALFAJORES]: '0xf2D39dd1b3e991f23d8a61bABb1c13873640873F', +}; + // Info fetched using npm package slot20 export const BPT_SLOT = 0; export const BPT_DECIMALS = 18; diff --git a/balancer-js/src/modules/graph/graph.module.spec.ts b/balancer-js/src/modules/graph/graph.module.spec.ts index c8cdf065..5e7bf7b6 100644 --- a/balancer-js/src/modules/graph/graph.module.spec.ts +++ b/balancer-js/src/modules/graph/graph.module.spec.ts @@ -17,6 +17,7 @@ import { } from '@/test/factories/pools'; import { Pool as SdkPool } from '@/types'; import { formatAddress } from '@/test/lib/utils'; +import { BALANCER_NETWORK_CONFIG } from '@/lib/constants/config'; function checkNode( node: Node, @@ -251,7 +252,7 @@ describe('Graph', () => { const poolProvider = new PoolsStaticRepository( linearInfo.linearPools as unknown as SdkPool[] ); - poolsGraph = new PoolGraph(poolProvider); + poolsGraph = new PoolGraph(poolProvider, BALANCER_NETWORK_CONFIG[1]); }); context('using non-wrapped tokens', () => { before(async () => { @@ -327,7 +328,7 @@ describe('Graph', () => { const poolProvider = new PoolsStaticRepository( pools as unknown as SdkPool[] ); - poolsGraph = new PoolGraph(poolProvider); + poolsGraph = new PoolGraph(poolProvider, BALANCER_NETWORK_CONFIG[1]); }); it('should throw when pool doesnt exist', async () => { @@ -487,7 +488,7 @@ describe('Graph', () => { const poolProvider = new PoolsStaticRepository( pools as unknown as SdkPool[] ); - poolsGraph = new PoolGraph(poolProvider); + poolsGraph = new PoolGraph(poolProvider, BALANCER_NETWORK_CONFIG[1]); }); context('using wrapped tokens', () => { @@ -647,7 +648,7 @@ describe('Graph', () => { const poolProvider = new PoolsStaticRepository( pools as unknown as SdkPool[] ); - poolsGraph = new PoolGraph(poolProvider); + poolsGraph = new PoolGraph(poolProvider, BALANCER_NETWORK_CONFIG[1]); }); context('using wrapped tokens', () => { diff --git a/balancer-js/src/modules/graph/graph.ts b/balancer-js/src/modules/graph/graph.ts index 69a08707..04a74b3c 100644 --- a/balancer-js/src/modules/graph/graph.ts +++ b/balancer-js/src/modules/graph/graph.ts @@ -4,7 +4,7 @@ import { Zero, WeiPerEther } from '@ethersproject/constants'; import { BalancerError, BalancerErrorCode } from '@/balancerErrors'; import { isSameAddress, parsePoolInfo } from '@/lib/utils'; import { _downscaleDown } from '@/lib/utils/solidityMaths'; -import { Pool, PoolAttribute, PoolType } from '@/types'; +import { BalancerNetworkConfig, Pool, PoolAttribute, PoolType } from '@/types'; import { Findable } from '../data/types'; import { PoolTypeConcerns } from '../pools/pool-type-concerns'; @@ -62,7 +62,16 @@ exitActions.set(PoolType.Weighted, 'exitPool'); exitActions.set(PoolType.ComposableStable, 'exitPool'); export class PoolGraph { - constructor(private pools: Findable) {} + private pools: Findable; + private chainId: number; + + constructor( + pools: Findable, + networkConfig: BalancerNetworkConfig + ) { + this.pools = pools; + this.chainId = networkConfig.chainId; + } async buildGraphFromRootPool( poolId: string, @@ -136,7 +145,10 @@ export class PoolGraph { const tokenTotal = this.getTokenTotal(pool); // Spot price service - const { spotPriceCalculator } = PoolTypeConcerns.from(pool.poolType); + const { spotPriceCalculator } = PoolTypeConcerns.from( + pool.poolType, + this.chainId + ); const spotPrices: SpotPrices = {}; let decimals = 18; // Spot price of a path is product of the sp of each pool in path. We calculate the sp for each pool token here to use as required later. diff --git a/balancer-js/src/modules/liquidity/liquidity.module.spec.ts b/balancer-js/src/modules/liquidity/liquidity.module.spec.ts index 4a477217..da96b534 100644 --- a/balancer-js/src/modules/liquidity/liquidity.module.spec.ts +++ b/balancer-js/src/modules/liquidity/liquidity.module.spec.ts @@ -9,6 +9,7 @@ import tokens from '@/test/fixtures/liquidityTokens.json'; import { StaticTokenPriceProvider } from '../data'; import { formatFixed, parseFixed } from '@ethersproject/bignumber'; import { tokensToTokenPrices } from '@/lib/utils'; +import { BALANCER_NETWORK_CONFIG } from '@/lib/constants/config'; const tokenPrices = tokensToTokenPrices(tokens); @@ -18,7 +19,11 @@ const poolProvider = new PoolsStaticRepository(pools as Pool[]); let liquidityProvider: Liquidity; beforeEach(() => { - liquidityProvider = new Liquidity(poolProvider, tokenPriceProvider); + liquidityProvider = new Liquidity( + poolProvider, + tokenPriceProvider, + BALANCER_NETWORK_CONFIG[1] + ); }); function findPool(address: string): Pool { diff --git a/balancer-js/src/modules/liquidity/liquidity.module.ts b/balancer-js/src/modules/liquidity/liquidity.module.ts index 2ef16ed4..2421ee35 100644 --- a/balancer-js/src/modules/liquidity/liquidity.module.ts +++ b/balancer-js/src/modules/liquidity/liquidity.module.ts @@ -1,4 +1,10 @@ -import { Findable, Pool, PoolToken, Price } from '@/types'; +import { + BalancerNetworkConfig, + Findable, + Pool, + PoolToken, + Price, +} from '@/types'; import { PoolAttribute } from '../data'; import { PoolTypeConcerns } from '../pools/pool-type-concerns'; import { BigNumber } from '@ethersproject/bignumber'; @@ -12,10 +18,19 @@ export interface PoolBPTValue { } export class Liquidity { + private pools: Findable; + private tokenPrices: Findable; + private chainId: number; + constructor( - private pools: Findable, - private tokenPrices: Findable - ) {} + pools: Findable, + tokenPrices: Findable, + networkConfig: BalancerNetworkConfig + ) { + this.pools = pools; + this.tokenPrices = tokenPrices; + this.chainId = networkConfig.chainId; + } async getLiquidity(pool: Pool): Promise { // Remove any tokens with same address as pool as they are pre-printed BPT @@ -82,7 +97,8 @@ export class Liquidity { // } const tokenLiquidity = PoolTypeConcerns.from( - pool.poolType + pool.poolType, + this.chainId ).liquidity.calcTotal(nonPoolTokensWithUpdatedPrice); const parsedTokenLiquidity = parseFixed(tokenLiquidity, SCALE); diff --git a/balancer-js/src/modules/pools/apr/apr.ts b/balancer-js/src/modules/pools/apr/apr.ts index 679dac80..b2b197b9 100644 --- a/balancer-js/src/modules/pools/apr/apr.ts +++ b/balancer-js/src/modules/pools/apr/apr.ts @@ -481,7 +481,11 @@ export class PoolApr { */ private async totalLiquidity(pool: Pool): Promise { try { - const liquidityService = new Liquidity(this.pools, this.tokenPrices); + const liquidityService = new Liquidity( + this.pools, + this.tokenPrices, + BALANCER_NETWORK_CONFIG[pool.chainId as Network] + ); const liquidity = await liquidityService.getLiquidity(pool); return liquidity; } catch (err) { diff --git a/balancer-js/src/modules/pools/factory/composable-stable/composable-stable.factory.ts b/balancer-js/src/modules/pools/factory/composable-stable/composable-stable.factory.ts index 1fe47322..26c315a8 100644 --- a/balancer-js/src/modules/pools/factory/composable-stable/composable-stable.factory.ts +++ b/balancer-js/src/modules/pools/factory/composable-stable/composable-stable.factory.ts @@ -7,7 +7,7 @@ import { JoinPoolDecodedAttributes, JoinPoolRequestDecodedAttributes, } from '@/modules/pools/factory/types'; -import { balancerVault, networkAddresses } from '@/lib/constants/config'; +import { networkAddresses } from '@/lib/constants/config'; import { AssetHelpers, getRandomBytes32 } from '@/lib/utils'; import { PoolFactory } from '@/modules/pools/factory/pool-factory'; import { ComposableStablePoolEncoder } from '@/pool-composable-stable'; @@ -23,10 +23,12 @@ import { Contract } from '@ethersproject/contracts'; import { ContractInstances } from '@/modules/contracts/contracts.module'; import { BytesLike } from '@ethersproject/bytes'; import { ComposableStablePoolInterface } from '@/contracts/ComposableStablePool'; +import { getVault } from '@/modules/sdk.helpers'; export class ComposableStableFactory implements PoolFactory { private wrappedNativeAsset: string; private contracts: ContractInstances; + private balancerVault: string; constructor( networkConfig: BalancerNetworkConfig, @@ -35,6 +37,7 @@ export class ComposableStableFactory implements PoolFactory { const { tokens } = networkAddresses(networkConfig.chainId); this.wrappedNativeAsset = tokens.wrappedNativeAsset; this.contracts = contracts; + this.balancerVault = getVault(networkConfig.chainId); } /** @@ -226,7 +229,7 @@ export class ComposableStableFactory implements PoolFactory { const { functionName, data } = this.encodeInitJoinFunctionData(params); return { - to: balancerVault, + to: this.balancerVault, functionName, data, attributes, diff --git a/balancer-js/src/modules/pools/factory/weighted/weighted.factory.ts b/balancer-js/src/modules/pools/factory/weighted/weighted.factory.ts index c6e52c03..d4870730 100644 --- a/balancer-js/src/modules/pools/factory/weighted/weighted.factory.ts +++ b/balancer-js/src/modules/pools/factory/weighted/weighted.factory.ts @@ -6,7 +6,7 @@ import { JsonRpcProvider, TransactionReceipt } from '@ethersproject/providers'; import { Vault__factory } from '@/contracts/factories/Vault__factory'; import { WeightedPoolFactory__factory } from '@/contracts/factories/WeightedPoolFactory__factory'; -import { balancerVault, networkAddresses } from '@/lib/constants/config'; +import { networkAddresses } from '@/lib/constants/config'; import { AssetHelpers, findEventInReceiptLogs, @@ -27,10 +27,12 @@ import { WeightedPool__factory } from '@/contracts'; import { SolidityMaths } from '@/lib/utils/solidityMaths'; import { BalancerError, BalancerErrorCode } from '@/balancerErrors'; import { WeightedPoolInterface } from '@/contracts/WeightedPool'; +import { getVault } from '@/modules/sdk.helpers'; export class WeightedFactory implements PoolFactory { private wrappedNativeAsset: string; private contracts: ContractInstances; + private balancerVault: string; constructor( networkConfig: BalancerNetworkConfig, @@ -39,6 +41,7 @@ export class WeightedFactory implements PoolFactory { const { tokens } = networkAddresses(networkConfig.chainId); this.wrappedNativeAsset = tokens.wrappedNativeAsset; this.contracts = contracts; + this.balancerVault = getVault(networkConfig.chainId); } /** @@ -207,7 +210,7 @@ export class WeightedFactory implements PoolFactory { const { functionName, data } = this.encodeInitJoinFunctionData(params); return { - to: balancerVault, + to: this.balancerVault, functionName, data, attributes, diff --git a/balancer-js/src/modules/pools/index.ts b/balancer-js/src/modules/pools/index.ts index d81a6b59..f1a73025 100644 --- a/balancer-js/src/modules/pools/index.ts +++ b/balancer-js/src/modules/pools/index.ts @@ -73,13 +73,17 @@ export class Pools implements Findable { ); this.liquidityService = new Liquidity( repositories.pools, - repositories.tokenPrices + repositories.tokenPrices, + networkConfig ); this.simulationService = new Simulation( networkConfig, this.repositories.poolsForSimulations ); - this.graphService = new PoolGraph(this.repositories.poolsOnChain); + this.graphService = new PoolGraph( + this.repositories.poolsOnChain, + networkConfig + ); this.joinService = new Join( this.graphService, networkConfig, @@ -116,7 +120,7 @@ export class Pools implements Findable { let queries: Queries.ParamsBuilder; let methods; try { - concerns = PoolTypeConcerns.from(pool.poolType); + concerns = PoolTypeConcerns.from(pool.poolType, networkConfig.chainId); methods = { buildJoin: ( joiner: string, @@ -350,7 +354,10 @@ export class Pools implements Findable { userAddress: string; slippage: string; }): JoinPoolAttributes { - const concerns = PoolTypeConcerns.from(pool.poolType); + const concerns = PoolTypeConcerns.from( + pool.poolType, + this.networkConfig.chainId + ); if (!concerns) throw `buildJoin for poolType ${pool.poolType} not implemented`; @@ -381,7 +388,10 @@ export class Pools implements Findable { shouldUnwrapNativeAsset?: boolean; singleTokenOut?: string; }): ExitExactBPTInAttributes { - const concerns = PoolTypeConcerns.from(pool.poolType); + const concerns = PoolTypeConcerns.from( + pool.poolType, + this.networkConfig.chainId + ); if (!concerns || !concerns.exit.buildExitExactBPTIn) throw `buildExit for poolType ${pool.poolType} not implemented`; @@ -411,7 +421,10 @@ export class Pools implements Findable { slippage: string; toInternalBalance?: boolean; }): ExitExactBPTInAttributes { - const concerns = PoolTypeConcerns.from(pool.poolType); + const concerns = PoolTypeConcerns.from( + pool.poolType, + this.networkConfig.chainId + ); if (!concerns || !concerns.exit.buildRecoveryExit) throw `buildRecoveryExit for poolType ${pool.poolType} not implemented`; @@ -518,7 +531,10 @@ export class Pools implements Findable { bptAmount: string; isJoin: boolean; }): string { - const concerns = PoolTypeConcerns.from(pool.poolType); + const concerns = PoolTypeConcerns.from( + pool.poolType, + this.networkConfig.chainId + ); return concerns.priceImpactCalculator.calcPriceImpact( pool, tokenAmounts.map(BigInt), diff --git a/balancer-js/src/modules/pools/pool-type-concerns.ts b/balancer-js/src/modules/pools/pool-type-concerns.ts index 5e6d739b..735e80cd 100644 --- a/balancer-js/src/modules/pools/pool-type-concerns.ts +++ b/balancer-js/src/modules/pools/pool-type-concerns.ts @@ -9,6 +9,7 @@ import { BalancerError, BalancerErrorCode } from '@/balancerErrors'; import { isLinearish } from '@/lib/utils'; import { FX } from '@/modules/pools/pool-types/fx.module'; import { Gyro } from '@/modules/pools/pool-types/gyro.module'; +import { getNetworkConfig } from '../sdk.helpers'; /** * Wrapper around pool type specific methods. @@ -16,18 +17,26 @@ import { Gyro } from '@/modules/pools/pool-types/gyro.module'; * Returns a class instance of a type specific method handlers. */ export class PoolTypeConcerns { - constructor( - config: BalancerSdkConfig, - public weighted = new Weighted(), - public stable = new Stable(), - public composableStable = new ComposableStable(), - public metaStable = new MetaStable(), - public stablePhantom = new StablePhantom(), - public linear = new Linear() - ) {} + public weighted: Weighted; + public stable: Stable; + public composableStable: ComposableStable; + public metaStable: MetaStable; + public stablePhantom: StablePhantom; + public linear: Linear; + + constructor(config: BalancerSdkConfig) { + const networkConfig = getNetworkConfig(config); + this.weighted = new Weighted(networkConfig.chainId); + this.stable = new Stable(networkConfig.chainId); + this.composableStable = new ComposableStable(networkConfig.chainId); + this.metaStable = new MetaStable(networkConfig.chainId); + this.stablePhantom = new StablePhantom(); + this.linear = new Linear(networkConfig.chainId); + } static from( - poolType: PoolType + poolType: PoolType, + chainId: number ): | Weighted | Stable @@ -38,7 +47,7 @@ export class PoolTypeConcerns { // Calculate spot price using pool type switch (poolType) { case 'ComposableStable': { - return new ComposableStable(); + return new ComposableStable(chainId); } case 'FX': { return new FX(); @@ -49,10 +58,10 @@ export class PoolTypeConcerns { return new Gyro(); } case 'MetaStable': { - return new MetaStable(); + return new MetaStable(chainId); } case 'Stable': { - return new Stable(); + return new Stable(chainId); } case 'StablePhantom': { return new StablePhantom(); @@ -60,11 +69,11 @@ export class PoolTypeConcerns { case 'Investment': case 'LiquidityBootstrapping': case 'Weighted': { - return new Weighted(); + return new Weighted(chainId); } default: { // Handles all Linear pool types - if (isLinearish(poolType)) return new Linear(); + if (isLinearish(poolType)) return new Linear(chainId); throw new BalancerError(BalancerErrorCode.UNSUPPORTED_POOL_TYPE); } } diff --git a/balancer-js/src/modules/pools/pool-types/composableStable.module.ts b/balancer-js/src/modules/pools/pool-types/composableStable.module.ts index 1a894d3a..3b768b5f 100644 --- a/balancer-js/src/modules/pools/pool-types/composableStable.module.ts +++ b/balancer-js/src/modules/pools/pool-types/composableStable.module.ts @@ -13,11 +13,17 @@ import { } from './concerns/types'; export class ComposableStable implements PoolType { - constructor( - public exit: ExitConcern = new ComposableStablePoolExit(), - public liquidity: LiquidityConcern = new StablePoolLiquidity(), - public spotPriceCalculator: SpotPriceConcern = new PhantomStablePoolSpotPrice(), - public priceImpactCalculator: PriceImpactConcern = new StablePoolPriceImpact(), - public join: JoinConcern = new ComposableStablePoolJoin() - ) {} + public exit: ExitConcern; + public liquidity: LiquidityConcern; + public spotPriceCalculator: SpotPriceConcern; + public priceImpactCalculator: PriceImpactConcern; + public join: JoinConcern; + + constructor(chainId: number) { + this.exit = new ComposableStablePoolExit(chainId); + this.liquidity = new StablePoolLiquidity(); + this.spotPriceCalculator = new PhantomStablePoolSpotPrice(); + this.priceImpactCalculator = new StablePoolPriceImpact(); + this.join = new ComposableStablePoolJoin(chainId); + } } diff --git a/balancer-js/src/modules/pools/pool-types/concerns/composableStable/exit.concern.ts b/balancer-js/src/modules/pools/pool-types/concerns/composableStable/exit.concern.ts index 2d3e9e93..3bea17e5 100644 --- a/balancer-js/src/modules/pools/pool-types/concerns/composableStable/exit.concern.ts +++ b/balancer-js/src/modules/pools/pool-types/concerns/composableStable/exit.concern.ts @@ -16,7 +16,6 @@ import { _downscaleDownArray, _upscaleArray, } from '@/lib/utils/solidityMaths'; -import { balancerVault } from '@/lib/constants/config'; import { ComposableStablePoolEncoder } from '@/pool-composable-stable'; import { Pool } from '@/types'; import { @@ -30,6 +29,7 @@ import { } from '../types'; import { BasePoolEncoder } from '@/pool-base'; import { StablePoolPriceImpact } from '../stable/priceImpact.concern'; +import { getVault } from '@/modules/sdk.helpers'; interface SortedValues { poolTokens: string[]; @@ -80,6 +80,12 @@ type EncodeExitParams = Pick< }; export class ComposableStablePoolExit implements ExitConcern { + private balancerVault: string; + + constructor(chainId: number) { + this.balancerVault = getVault(chainId); + } + buildExitExactBPTIn = ({ exiter, pool, @@ -586,7 +592,7 @@ export class ComposableStablePoolExit implements ExitConcern { toInternalBalance, } = params; - const to = balancerVault; + const to = this.balancerVault; const functionName = 'exitPool'; const attributes: ExitPool = { poolId: poolId, diff --git a/balancer-js/src/modules/pools/pool-types/concerns/composableStable/join.concern.ts b/balancer-js/src/modules/pools/pool-types/concerns/composableStable/join.concern.ts index 7651fd8e..f242db36 100644 --- a/balancer-js/src/modules/pools/pool-types/concerns/composableStable/join.concern.ts +++ b/balancer-js/src/modules/pools/pool-types/concerns/composableStable/join.concern.ts @@ -3,7 +3,6 @@ import { BigNumber } from '@ethersproject/bignumber'; import { BalancerError, BalancerErrorCode } from '@/balancerErrors'; import { Vault__factory } from '@/contracts/factories/Vault__factory'; -import { balancerVault } from '@/lib/constants/config'; import { AssetHelpers, parsePoolInfo, @@ -24,6 +23,7 @@ import { JoinPool, } from '../types'; import { AddressZero } from '@ethersproject/constants'; +import { getVault } from '@/modules/sdk.helpers'; interface SortedValues { sortedAmountsIn: string[]; @@ -42,6 +42,12 @@ type SortedInputs = SortedValues & }; export class ComposableStablePoolJoin implements JoinConcern { + private balancerVault: string; + + constructor(chainId: number) { + this.balancerVault = getVault(chainId); + } + buildJoin = ({ joiner, pool, @@ -79,7 +85,7 @@ export class ComposableStablePoolJoin implements JoinConcern { return { ...encodedData, - to: balancerVault, + to: this.balancerVault, value, priceImpact, }; diff --git a/balancer-js/src/modules/pools/pool-types/concerns/linear/exit.concern.ts b/balancer-js/src/modules/pools/pool-types/concerns/linear/exit.concern.ts index da79d4e0..cfd6a519 100644 --- a/balancer-js/src/modules/pools/pool-types/concerns/linear/exit.concern.ts +++ b/balancer-js/src/modules/pools/pool-types/concerns/linear/exit.concern.ts @@ -4,7 +4,6 @@ import { BigNumber } from '@ethersproject/bignumber'; import { BalancerError, BalancerErrorCode } from '@/balancerErrors'; import { Vault__factory } from '@/contracts'; -import { balancerVault } from '@/lib/constants/config'; import { insert, parsePoolInfo, removeItem } from '@/lib/utils'; import { _downscaleDownArray } from '@/lib/utils/solidityMaths'; import { subSlippage } from '@/lib/utils/slippageHelper'; @@ -20,6 +19,7 @@ import { ExitPool, } from '../types'; import { LinearPriceImpact } from '../linear/priceImpact.concern'; +import { getVault } from '@/modules/sdk.helpers'; interface SortedValues { bptIndex: number; @@ -44,6 +44,12 @@ type EncodeExitParams = Pick< }; export class LinearPoolExit implements ExitConcern { + private balancerVault: string; + + constructor(chainId: number) { + this.balancerVault = getVault(chainId); + } + buildExitExactBPTIn = ({ exiter, pool, @@ -205,7 +211,7 @@ export class LinearPoolExit implements ExitConcern { toInternalBalance, } = params; - const to = balancerVault; + const to = this.balancerVault; const functionName = 'exitPool'; const attributes: ExitPool = { poolId: poolId, diff --git a/balancer-js/src/modules/pools/pool-types/concerns/stable/exit.concern.ts b/balancer-js/src/modules/pools/pool-types/concerns/stable/exit.concern.ts index 7681b0ed..643fd584 100644 --- a/balancer-js/src/modules/pools/pool-types/concerns/stable/exit.concern.ts +++ b/balancer-js/src/modules/pools/pool-types/concerns/stable/exit.concern.ts @@ -13,7 +13,6 @@ import { import { AssetHelpers, isSameAddress, parsePoolInfo } from '@/lib/utils'; import { Vault__factory } from '@/contracts/factories/Vault__factory'; import { addSlippage, subSlippage } from '@/lib/utils/slippageHelper'; -import { balancerVault } from '@/lib/constants/config'; import { BalancerError, BalancerErrorCode } from '@/balancerErrors'; import { StablePoolEncoder } from '@/pool-stable'; import { @@ -24,6 +23,7 @@ import { import { Pool } from '@/types'; import { BasePoolEncoder } from '@/pool-base'; import { StablePoolPriceImpact } from '../stable/priceImpact.concern'; +import { getVault } from '@/modules/sdk.helpers'; interface SortedValues { poolTokens: string[]; @@ -71,6 +71,12 @@ type EncodeExitParams = Pick< }; export class StablePoolExit implements ExitConcern { + private balancerVault: string; + + constructor(chainId: number) { + this.balancerVault = getVault(chainId); + } + buildExitExactBPTIn = ({ exiter, pool, @@ -489,7 +495,7 @@ export class StablePoolExit implements ExitConcern { userData, toInternalBalance, }: EncodeExitParams): ExitPoolAttributes => { - const to = balancerVault; + const to = this.balancerVault; const functionName = 'exitPool'; const attributes: ExitPool = { poolId, diff --git a/balancer-js/src/modules/pools/pool-types/concerns/stable/join.concern.ts b/balancer-js/src/modules/pools/pool-types/concerns/stable/join.concern.ts index 1d7848ce..190c40c2 100644 --- a/balancer-js/src/modules/pools/pool-types/concerns/stable/join.concern.ts +++ b/balancer-js/src/modules/pools/pool-types/concerns/stable/join.concern.ts @@ -3,7 +3,6 @@ import { BigNumber } from '@ethersproject/bignumber'; import { BalancerError, BalancerErrorCode } from '@/balancerErrors'; import { Vault__factory } from '@/contracts/factories/Vault__factory'; -import { balancerVault } from '@/lib/constants/config'; import { AssetHelpers, getEthValue, parsePoolInfo } from '@/lib/utils'; import { subSlippage } from '@/lib/utils/slippageHelper'; import { _upscaleArray } from '@/lib/utils/solidityMaths'; @@ -18,6 +17,7 @@ import { JoinPoolParameters, } from '../types'; import { AddressZero } from '@ethersproject/constants'; +import { getVault } from '@/modules/sdk.helpers'; type SortedValues = { poolTokens: string[]; @@ -37,6 +37,12 @@ type EncodeJoinPoolParams = { Pick; export class StablePoolJoin implements JoinConcern { + private balancerVault: string; + + constructor(chainId: number) { + this.balancerVault = getVault(chainId); + } + buildJoin = ({ joiner, pool, @@ -197,7 +203,7 @@ export class StablePoolJoin implements JoinConcern { minBPTOut ); - const to = balancerVault; + const to = this.balancerVault; const functionName = 'joinPool'; const attributes: JoinPool = { poolId, diff --git a/balancer-js/src/modules/pools/pool-types/concerns/weighted/exit.concern.ts b/balancer-js/src/modules/pools/pool-types/concerns/weighted/exit.concern.ts index d25f7e7a..3aca69f5 100644 --- a/balancer-js/src/modules/pools/pool-types/concerns/weighted/exit.concern.ts +++ b/balancer-js/src/modules/pools/pool-types/concerns/weighted/exit.concern.ts @@ -13,7 +13,6 @@ import { import { AssetHelpers, isSameAddress, parsePoolInfo } from '@/lib/utils'; import { Vault__factory } from '@/contracts/factories/Vault__factory'; import { addSlippage, subSlippage } from '@/lib/utils/slippageHelper'; -import { balancerVault } from '@/lib/constants/config'; import { BalancerError, BalancerErrorCode } from '@/balancerErrors'; import { WeightedPoolEncoder } from '@/pool-weighted'; import { @@ -24,6 +23,7 @@ import { import { Pool } from '@/types'; import { BasePoolEncoder } from '@/pool-base'; import { WeightedPoolPriceImpact } from '../weighted/priceImpact.concern'; +import { getVault } from '@/modules/sdk.helpers'; interface SortedValues { poolTokens: string[]; @@ -71,6 +71,12 @@ type EncodeExitParams = Pick< }; export class WeightedPoolExit implements ExitConcern { + private balancerVault: string; + + constructor(chainId: number) { + this.balancerVault = getVault(chainId); + } + buildExitExactBPTIn = ({ exiter, pool, @@ -489,7 +495,7 @@ export class WeightedPoolExit implements ExitConcern { userData, toInternalBalance, }: EncodeExitParams): ExitPoolAttributes => { - const to = balancerVault; + const to = this.balancerVault; const functionName = 'exitPool'; const attributes: ExitPool = { poolId, diff --git a/balancer-js/src/modules/pools/pool-types/concerns/weighted/join.concern.ts b/balancer-js/src/modules/pools/pool-types/concerns/weighted/join.concern.ts index 0c934678..36c68350 100644 --- a/balancer-js/src/modules/pools/pool-types/concerns/weighted/join.concern.ts +++ b/balancer-js/src/modules/pools/pool-types/concerns/weighted/join.concern.ts @@ -4,7 +4,6 @@ import { AddressZero } from '@ethersproject/constants'; import { BalancerError, BalancerErrorCode } from '@/balancerErrors'; import { Vault__factory } from '@/contracts/factories/Vault__factory'; -import { balancerVault } from '@/lib/constants/config'; import { AssetHelpers, getEthValue, parsePoolInfo } from '@/lib/utils'; import { subSlippage } from '@/lib/utils/slippageHelper'; import { _upscaleArray } from '@/lib/utils/solidityMaths'; @@ -17,6 +16,7 @@ import { JoinPoolParameters, } from '../types'; import { WeightedPoolPriceImpact } from '../weighted/priceImpact.concern'; +import { getVault } from '@/modules/sdk.helpers'; type SortedValues = { poolTokens: string[]; @@ -29,6 +29,12 @@ type SortedValues = { }; export class WeightedPoolJoin implements JoinConcern { + private balancerVault: string; + + constructor(chainId: number) { + this.balancerVault = getVault(chainId); + } + buildJoin = ({ joiner, pool, @@ -187,7 +193,7 @@ export class WeightedPoolJoin implements JoinConcern { sortedAmountsIn, minBPTOut ); - const to = balancerVault; + const to = this.balancerVault; const functionName = 'joinPool'; const attributes: JoinPool = { poolId, diff --git a/balancer-js/src/modules/pools/pool-types/linear.module.ts b/balancer-js/src/modules/pools/pool-types/linear.module.ts index 687ed119..c23c878a 100644 --- a/balancer-js/src/modules/pools/pool-types/linear.module.ts +++ b/balancer-js/src/modules/pools/pool-types/linear.module.ts @@ -13,11 +13,17 @@ import { } from './concerns/types'; export class Linear implements PoolType { - constructor( - public exit: ExitConcern = new LinearPoolExit(), - public join: JoinConcern = new LinearPoolJoin(), - public liquidity: LiquidityConcern = new LinearPoolLiquidity(), - public spotPriceCalculator: SpotPriceConcern = new LinearPoolSpotPrice(), - public priceImpactCalculator: PriceImpactConcern = new LinearPriceImpact() - ) {} + public exit: ExitConcern; + public join: JoinConcern; + public liquidity: LiquidityConcern; + public spotPriceCalculator: SpotPriceConcern; + public priceImpactCalculator: PriceImpactConcern; + + constructor(chainId: number) { + this.exit = new LinearPoolExit(chainId); + this.join = new LinearPoolJoin(); + this.liquidity = new LinearPoolLiquidity(); + this.spotPriceCalculator = new LinearPoolSpotPrice(); + this.priceImpactCalculator = new LinearPriceImpact(); + } } diff --git a/balancer-js/src/modules/pools/pool-types/metaStable.module.ts b/balancer-js/src/modules/pools/pool-types/metaStable.module.ts index 3e942252..97c083ca 100644 --- a/balancer-js/src/modules/pools/pool-types/metaStable.module.ts +++ b/balancer-js/src/modules/pools/pool-types/metaStable.module.ts @@ -13,11 +13,17 @@ import { StablePoolExit } from '@/modules/pools/pool-types/concerns/stable/exit. import { StablePoolJoin } from '@/modules/pools/pool-types/concerns/stable/join.concern'; export class MetaStable implements PoolType { - constructor( - public exit: ExitConcern = new StablePoolExit(), - public join: JoinConcern = new StablePoolJoin(), - public liquidity: LiquidityConcern = new MetaStablePoolLiquidity(), - public spotPriceCalculator: SpotPriceConcern = new MetaStablePoolSpotPrice(), - public priceImpactCalculator: PriceImpactConcern = new StablePoolPriceImpact() - ) {} + public exit: ExitConcern; + public join: JoinConcern; + public liquidity: LiquidityConcern; + public spotPriceCalculator: SpotPriceConcern; + public priceImpactCalculator: PriceImpactConcern; + + constructor(chainId: number) { + this.exit = new StablePoolExit(chainId); + this.join = new StablePoolJoin(chainId); + this.liquidity = new MetaStablePoolLiquidity(); + this.spotPriceCalculator = new MetaStablePoolSpotPrice(); + this.priceImpactCalculator = new StablePoolPriceImpact(); + } } diff --git a/balancer-js/src/modules/pools/pool-types/stable.module.ts b/balancer-js/src/modules/pools/pool-types/stable.module.ts index 69bae600..a68e05a8 100644 --- a/balancer-js/src/modules/pools/pool-types/stable.module.ts +++ b/balancer-js/src/modules/pools/pool-types/stable.module.ts @@ -13,11 +13,17 @@ import { } from './concerns/types'; export class Stable implements PoolType { - constructor( - public exit: ExitConcern = new StablePoolExit(), - public join: JoinConcern = new StablePoolJoin(), - public liquidity: LiquidityConcern = new StablePoolLiquidity(), - public spotPriceCalculator: SpotPriceConcern = new StablePoolSpotPrice(), - public priceImpactCalculator: PriceImpactConcern = new StablePoolPriceImpact() - ) {} + public exit: ExitConcern; + public join: JoinConcern; + public liquidity: LiquidityConcern; + public spotPriceCalculator: SpotPriceConcern; + public priceImpactCalculator: PriceImpactConcern; + + constructor(chainId: number) { + this.exit = new StablePoolExit(chainId); + this.join = new StablePoolJoin(chainId); + this.liquidity = new StablePoolLiquidity(); + this.spotPriceCalculator = new StablePoolSpotPrice(); + this.priceImpactCalculator = new StablePoolPriceImpact(); + } } diff --git a/balancer-js/src/modules/pools/pool-types/weighted.module.ts b/balancer-js/src/modules/pools/pool-types/weighted.module.ts index 416eac5f..0fea45ce 100644 --- a/balancer-js/src/modules/pools/pool-types/weighted.module.ts +++ b/balancer-js/src/modules/pools/pool-types/weighted.module.ts @@ -13,11 +13,17 @@ import { } from './concerns/types'; export class Weighted implements PoolType { - constructor( - public exit: ExitConcern = new WeightedPoolExit(), - public join: JoinConcern = new WeightedPoolJoin(), - public liquidity: LiquidityConcern = new WeightedPoolLiquidity(), - public spotPriceCalculator: SpotPriceConcern = new WeightedPoolSpotPrice(), - public priceImpactCalculator: PriceImpactConcern = new WeightedPoolPriceImpact() - ) {} + public exit: ExitConcern; + public join: JoinConcern; + public liquidity: LiquidityConcern; + public spotPriceCalculator: SpotPriceConcern; + public priceImpactCalculator: PriceImpactConcern; + + constructor(chainId: number) { + this.exit = new WeightedPoolExit(chainId); + this.join = new WeightedPoolJoin(chainId); + this.liquidity = new WeightedPoolLiquidity(); + this.spotPriceCalculator = new WeightedPoolSpotPrice(); + this.priceImpactCalculator = new WeightedPoolPriceImpact(); + } } diff --git a/balancer-js/src/modules/sdk.helpers.ts b/balancer-js/src/modules/sdk.helpers.ts index 81e55ee7..0f4fd5f3 100644 --- a/balancer-js/src/modules/sdk.helpers.ts +++ b/balancer-js/src/modules/sdk.helpers.ts @@ -1,4 +1,7 @@ -import { BALANCER_NETWORK_CONFIG } from '@/lib/constants/config'; +import { + BALANCER_NETWORK_CONFIG, + vaultAddresses, +} from '@/lib/constants/config'; import { BalancerNetworkConfig, BalancerSdkConfig } from '@/types'; export function getNetworkConfig( config: BalancerSdkConfig @@ -25,3 +28,7 @@ export function getNetworkConfig( tenderly: config.network.tenderly, }; } + +export function getVault(chainId: number): string { + return vaultAddresses[chainId]; +} diff --git a/balancer-js/src/modules/swaps/swaps.module.ts b/balancer-js/src/modules/swaps/swaps.module.ts index 82c13181..7da98d6d 100644 --- a/balancer-js/src/modules/swaps/swaps.module.ts +++ b/balancer-js/src/modules/swaps/swaps.module.ts @@ -14,7 +14,6 @@ import { SwapsOptions, } from './types'; import { queryBatchSwap, getSorSwapInfo } from './queryBatchSwap'; -import { balancerVault } from '@/lib/constants/config'; import { getLimitsForSlippage } from './helpers'; import { BalancerSdkConfig } from '@/types'; import { SwapInput } from './types'; @@ -30,6 +29,7 @@ import { import { BigNumber } from '@ethersproject/bignumber'; import { AddressZero } from '@ethersproject/constants'; import { GraphQLArgs } from '@/lib/graphql'; +import { getVault } from '../sdk.helpers'; const buildRouteDefaultOptions = { maxPools: 4, @@ -56,6 +56,8 @@ export class Swaps { else this.chainId = sorOrConfig.network.chainId; } + const balancerVault = getVault(this.chainId); + this.vaultContract = Vault__factory.connect( balancerVault, this.sor.provider diff --git a/balancer-js/src/test/lib/utils.ts b/balancer-js/src/test/lib/utils.ts index 327e1ec6..cd466304 100644 --- a/balancer-js/src/test/lib/utils.ts +++ b/balancer-js/src/test/lib/utils.ts @@ -28,7 +28,6 @@ import { BALANCER_NETWORK_CONFIG, ERC20__factory, } from '@/.'; -import { balancerVault } from '@/lib/constants/config'; import { parseEther } from '@ethersproject/units'; import { setBalance } from '@nomicfoundation/hardhat-network-helpers'; @@ -42,6 +41,7 @@ import polygonPools from '../fixtures/pools-polygon.json'; import { PoolsJsonRepository } from './pools-json-repository'; import { Contracts } from '@/modules/contracts/contracts.module'; import { SubgraphPool } from '@/modules/subgraph/subgraph'; +import { getVault } from '@/modules/sdk.helpers'; dotenv.config(); @@ -199,6 +199,8 @@ export const approveToken = async ( signer: JsonRpcSigner ): Promise => { const tokenContract = ERC20__factory.connect(token, signer); + const chainId = await signer.getChainId(); + const balancerVault = getVault(chainId); const txReceipt = await ( await tokenContract.approve(balancerVault, amount) ).wait();