diff --git a/.changeset/hot-chefs-press.md b/.changeset/hot-chefs-press.md new file mode 100644 index 000000000..d633c0dc7 --- /dev/null +++ b/.changeset/hot-chefs-press.md @@ -0,0 +1,5 @@ +--- +'backend': minor +--- + +replace wrapped functionality with accessing address & refactor to use scoped down Token functionality (removal of isUnderlyingEqual) diff --git a/modules/sor/lib/path.ts b/modules/sor/lib/path.ts index 4c992a30b..94260f261 100644 --- a/modules/sor/lib/path.ts +++ b/modules/sor/lib/path.ts @@ -46,7 +46,7 @@ export class PathWithAmount extends PathLocal { this.mutateBalances = Boolean(mutateBalances); //call to super ensures this array access is safe - if (tokens[0].isUnderlyingEqual(swapAmount.token)) { + if (tokens[0].isSameAddress(swapAmount.token.address)) { this.swapKind = SwapKind.GivenIn; } else { this.swapKind = SwapKind.GivenOut; diff --git a/modules/sor/lib/pathGraph/pathGraph.ts b/modules/sor/lib/pathGraph/pathGraph.ts index 1b5312243..04ff7024a 100644 --- a/modules/sor/lib/pathGraph/pathGraph.ts +++ b/modules/sor/lib/pathGraph/pathGraph.ts @@ -1,4 +1,4 @@ -import { Address, SwapKind, Token, TokenAmount } from '@balancer/sdk'; +import { Address, SwapKind, TokenAmount, Token } from '@balancer/sdk'; import { PathGraphEdgeData, PathGraphTraversalConfig } from './pathGraphTypes'; import { BasePool } from '../poolsV2/basePool'; import { PathLocal } from '../path'; @@ -97,8 +97,8 @@ export class PathGraph { const minLimitThreshold = (swapAmount.amount * BigInt(Math.floor(config.minSwapAmountRatio * 100))) / 100n; const tokenPaths = this.findAllValidTokenPaths({ - tokenIn: tokenIn.wrapped, - tokenOut: tokenOut.wrapped, + tokenIn: tokenIn.address, + tokenOut: tokenOut.address, config, }); @@ -174,7 +174,7 @@ export class PathGraph { tokens.push(new Token(pool.tokens[0].token.chainId, pool.address.toLowerCase() as Address, 18)); // Add BPT as token nodes } for (const token of tokens) { - if (!this.nodes.has(token.wrapped)) { + if (!this.nodes.has(token.address)) { this.addNode(token); } } @@ -241,7 +241,7 @@ export class PathGraph { if (swapKind !== undefined && tokenPrices) { const limit = pool.getLimitAmountSwap(tokenIn, tokenOut, swapKind); const priceToken = (swapKind as SwapKind) === SwapKind.GivenIn ? tokenIn : tokenOut; - const price = tokenPrices.get(priceToken.wrapped.toLowerCase()); + const price = tokenPrices.get(priceToken.address.toLowerCase()); if (price !== undefined) { const amount = Number(formatUnits(limit, priceToken.decimals)); limitUSD = amount * price; @@ -259,10 +259,10 @@ export class PathGraph { } private addNode(token: Token): void { - this.nodes.add(token.wrapped); + this.nodes.add(token.address); - if (!this.edges.has(token.wrapped)) { - this.edges.set(token.wrapped, new Map()); + if (!this.edges.has(token.address)) { + this.edges.set(token.address, new Map()); } } @@ -276,22 +276,22 @@ export class PathGraph { edgeProps: PathGraphEdgeData; maxPathsPerTokenPair: number; }): void { - const tokenInVertex = this.nodes.has(edgeProps.tokenIn.wrapped); - const tokenOutVertex = this.nodes.has(edgeProps.tokenOut.wrapped); - const tokenInNode = this.edges.get(edgeProps.tokenIn.wrapped); + const tokenInVertex = this.nodes.has(edgeProps.tokenIn.address); + const tokenOutVertex = this.nodes.has(edgeProps.tokenOut.address); + const tokenInNode = this.edges.get(edgeProps.tokenIn.address); if (!tokenInVertex || !tokenOutVertex || !tokenInNode) { throw new Error('Attempting to add invalid edge'); } - const existingEdges = tokenInNode.get(edgeProps.tokenOut.wrapped) || []; + const existingEdges = tokenInNode.get(edgeProps.tokenOut.address) || []; const sorted = [...existingEdges, edgeProps].sort((a, b) => a.normalizedLiquidity > b.normalizedLiquidity ? -1 : 1, ); tokenInNode.set( - edgeProps.tokenOut.wrapped, + edgeProps.tokenOut.address, sorted.length > maxPathsPerTokenPair ? sorted.slice(0, maxPathsPerTokenPair) : sorted, ); } diff --git a/modules/sor/lib/poolsV2/composableStable/composableStablePool.ts b/modules/sor/lib/poolsV2/composableStable/composableStablePool.ts index 14118e1d5..369c2967e 100644 --- a/modules/sor/lib/poolsV2/composableStable/composableStablePool.ts +++ b/modules/sor/lib/poolsV2/composableStable/composableStablePool.ts @@ -299,8 +299,8 @@ export class ComposableStablePool implements BasePool { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: PoolTokenWithRate; tOut: PoolTokenWithRate } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV2/fx/fxPool.ts b/modules/sor/lib/poolsV2/fx/fxPool.ts index d60d5d1d6..44b4c97b9 100644 --- a/modules/sor/lib/poolsV2/fx/fxPool.ts +++ b/modules/sor/lib/poolsV2/fx/fxPool.ts @@ -195,8 +195,8 @@ export class FxPool implements BasePool { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: FxPoolToken; tOut: FxPoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Token not found'); diff --git a/modules/sor/lib/poolsV2/gyro2/gyro2Pool.ts b/modules/sor/lib/poolsV2/gyro2/gyro2Pool.ts index 9eea74cc7..54cb86f5c 100644 --- a/modules/sor/lib/poolsV2/gyro2/gyro2Pool.ts +++ b/modules/sor/lib/poolsV2/gyro2/gyro2Pool.ts @@ -222,8 +222,8 @@ export class Gyro2Pool implements BasePool { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: PoolTokenWithRate; tOut: PoolTokenWithRate } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV2/gyro2/test/gyro2Math.test.ts b/modules/sor/lib/poolsV2/gyro2/test/gyro2Math.test.ts index 62fe6a38a..89c976801 100644 --- a/modules/sor/lib/poolsV2/gyro2/test/gyro2Math.test.ts +++ b/modules/sor/lib/poolsV2/gyro2/test/gyro2Math.test.ts @@ -1,7 +1,7 @@ import { _calculateInvariant, _calcOutGivenIn, _findVirtualParams } from "../gyro2Math"; import { Gyro2Pool, Gyro2PoolToken } from "../gyro2Pool"; -import { Token, TokenAmount } from '@balancer/sdk' +import { Token, TokenAmount, NativeToken } from '@balancer/sdk' import { parseEther } from 'viem'; @@ -45,22 +45,22 @@ describe('gyro2Math', () => { [{ tokenA: '0xTokenInAddress', tokenB: '0xTokenOutAddress', normalizedLiquidity: '1000000', spotPrice: '1.5' }] ); - const tIn = new Token( + const tIn = new NativeToken( 42161, "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9", 6, + "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9", "USDT", "Tether USD", - "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9", ) - const tOut = new Token( + const tOut = new NativeToken( 42161, "0xb165a74407fe1e519d6bcbdec1ed3202b35a4140", 6, + "0xb165a74407fe1e519d6bcbdec1ed3202b35a4140", "stataArbUSDT", "Static Aave Arbitrum USDT", - "0xb165a74407fe1e519d6bcbdec1ed3202b35a4140", ) const swapAmount = TokenAmount.fromHumanAmount(tIn, '1'); @@ -104,22 +104,22 @@ describe('gyro2Math', () => { [{ tokenA: '0xTokenInAddress', tokenB: '0xTokenOutAddress', normalizedLiquidity: '1000000', spotPrice: '1.5' }] ); - const tIn = new Token( + const tIn = new NativeToken( 42161, "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9", 6, + "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9", "USDT", "Tether USD", - "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9", ) - const tOut = new Token( + const tOut = new NativeToken( 42161, "0xb165a74407fe1e519d6bcbdec1ed3202b35a4140", 6, + "0xb165a74407fe1e519d6bcbdec1ed3202b35a4140", "stataArbUSDT", "Static Aave Arbitrum USDT", - "0xb165a74407fe1e519d6bcbdec1ed3202b35a4140", ) const swapAmount = TokenAmount.fromHumanAmount(tIn, '0.879697'); diff --git a/modules/sor/lib/poolsV2/gyro3/gyro3Pool.ts b/modules/sor/lib/poolsV2/gyro3/gyro3Pool.ts index fd84a4c88..58dffb464 100644 --- a/modules/sor/lib/poolsV2/gyro3/gyro3Pool.ts +++ b/modules/sor/lib/poolsV2/gyro3/gyro3Pool.ts @@ -172,8 +172,8 @@ export class Gyro3Pool implements BasePool { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: BasePoolToken; tOut: BasePoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); @@ -193,8 +193,8 @@ export class Gyro3Pool implements BasePool { const { tIn, tOut } = this.getPoolTokens(tokenIn, tokenOut); const tertiaryAddress = this.tokens - .map((t) => t.token.wrapped) - .find((a) => a !== tokenIn.wrapped && a !== tokenOut.wrapped); + .map((t) => t.token.address) + .find((a) => a !== tokenIn.address && a !== tokenOut.address); const tertiary = this.tokenMap.get(tertiaryAddress as string); if (!tertiary) { diff --git a/modules/sor/lib/poolsV2/gyroE/gyroEPool.ts b/modules/sor/lib/poolsV2/gyroE/gyroEPool.ts index 9b61b6a87..208ef0be7 100644 --- a/modules/sor/lib/poolsV2/gyroE/gyroEPool.ts +++ b/modules/sor/lib/poolsV2/gyroE/gyroEPool.ts @@ -255,8 +255,8 @@ export class GyroEPool implements BasePool { tIn: PoolTokenWithRate; tOut: PoolTokenWithRate; } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV2/metastable/metastablePool.ts b/modules/sor/lib/poolsV2/metastable/metastablePool.ts index 029249cc4..ac48966b1 100644 --- a/modules/sor/lib/poolsV2/metastable/metastablePool.ts +++ b/modules/sor/lib/poolsV2/metastable/metastablePool.ts @@ -195,8 +195,8 @@ export class MetaStablePool implements BasePool { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: PoolTokenWithRate; tOut: PoolTokenWithRate } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV2/stable/stablePool.ts b/modules/sor/lib/poolsV2/stable/stablePool.ts index 101bc7c06..fd11e9faf 100644 --- a/modules/sor/lib/poolsV2/stable/stablePool.ts +++ b/modules/sor/lib/poolsV2/stable/stablePool.ts @@ -185,8 +185,8 @@ export class StablePool implements BasePool { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: BasePoolToken; tOut: BasePoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV2/weighted/weightedPool.ts b/modules/sor/lib/poolsV2/weighted/weightedPool.ts index 233d68af3..9424a59f0 100644 --- a/modules/sor/lib/poolsV2/weighted/weightedPool.ts +++ b/modules/sor/lib/poolsV2/weighted/weightedPool.ts @@ -155,8 +155,8 @@ export class WeightedPool implements BasePool { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: WeightedPoolToken; tOut: WeightedPoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV3/buffer/bufferPool.ts b/modules/sor/lib/poolsV3/buffer/bufferPool.ts index 3e89b477e..aad7e0234 100644 --- a/modules/sor/lib/poolsV3/buffer/bufferPool.ts +++ b/modules/sor/lib/poolsV3/buffer/bufferPool.ts @@ -190,8 +190,8 @@ export class BufferPool implements BasePoolMethodsV3 { // Helper methods public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: BasePoolToken; tOut: BasePoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV3/gyro2CLP/gyro2CLPPool.ts b/modules/sor/lib/poolsV3/gyro2CLP/gyro2CLPPool.ts index b03bea693..e1731c768 100644 --- a/modules/sor/lib/poolsV3/gyro2CLP/gyro2CLPPool.ts +++ b/modules/sor/lib/poolsV3/gyro2CLP/gyro2CLPPool.ts @@ -132,8 +132,8 @@ export class Gyro2CLPPool extends BasePoolV3 implements BasePoolMethodsV3 { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: GyroPoolToken; tOut: GyroPoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV3/gyroECLP/gyroECLPPool.ts b/modules/sor/lib/poolsV3/gyroECLP/gyroECLPPool.ts index 47ad8f83e..399f59b29 100644 --- a/modules/sor/lib/poolsV3/gyroECLP/gyroECLPPool.ts +++ b/modules/sor/lib/poolsV3/gyroECLP/gyroECLPPool.ts @@ -169,8 +169,8 @@ export class GyroECLPPool extends BasePoolV3 implements BasePoolMethodsV3 { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: GyroPoolToken; tOut: GyroPoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV3/liquidityBootstrapping/liquidityBootstrapping.ts b/modules/sor/lib/poolsV3/liquidityBootstrapping/liquidityBootstrapping.ts index 6f7c45e2f..a9ce2840c 100644 --- a/modules/sor/lib/poolsV3/liquidityBootstrapping/liquidityBootstrapping.ts +++ b/modules/sor/lib/poolsV3/liquidityBootstrapping/liquidityBootstrapping.ts @@ -219,8 +219,8 @@ export class LiquidityBootstrappingPoolV3 extends BasePoolV3 implements BasePool } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: WeightedPoolToken; tOut: WeightedPoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV3/quantAmm/quantAmmPool.ts b/modules/sor/lib/poolsV3/quantAmm/quantAmmPool.ts index 57b0bb022..5edbb999c 100644 --- a/modules/sor/lib/poolsV3/quantAmm/quantAmmPool.ts +++ b/modules/sor/lib/poolsV3/quantAmm/quantAmmPool.ts @@ -158,8 +158,8 @@ export class QuantAmmPool extends BasePoolV3 implements BasePoolMethodsV3 { // Helper methods public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: QuantAmmPoolToken; tOut: QuantAmmPoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV3/reClamm/reClammPool.ts b/modules/sor/lib/poolsV3/reClamm/reClammPool.ts index 2a178ea6a..7f23e9cac 100644 --- a/modules/sor/lib/poolsV3/reClamm/reClammPool.ts +++ b/modules/sor/lib/poolsV3/reClamm/reClammPool.ts @@ -149,8 +149,8 @@ export class ReClammPool extends BasePoolV3 implements BasePoolMethodsV3 { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: ReClammPoolToken; tOut: ReClammPoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV3/stable/stablePool.ts b/modules/sor/lib/poolsV3/stable/stablePool.ts index 858244cd8..a407ec600 100644 --- a/modules/sor/lib/poolsV3/stable/stablePool.ts +++ b/modules/sor/lib/poolsV3/stable/stablePool.ts @@ -124,8 +124,8 @@ export class StablePoolV3 extends BasePoolV3 implements BasePoolMethodsV3 { } public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: StablePoolToken; tOut: StablePoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/poolsV3/weighted/weightedPool.ts b/modules/sor/lib/poolsV3/weighted/weightedPool.ts index eef3346fd..ceb7bc957 100644 --- a/modules/sor/lib/poolsV3/weighted/weightedPool.ts +++ b/modules/sor/lib/poolsV3/weighted/weightedPool.ts @@ -130,8 +130,8 @@ export class WeightedPoolV3 extends BasePoolV3 implements BasePoolMethodsV3 { // Helper methods public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: WeightedPoolToken; tOut: WeightedPoolToken } { - const tIn = this.tokenMap.get(tokenIn.wrapped); - const tOut = this.tokenMap.get(tokenOut.wrapped); + const tIn = this.tokenMap.get(tokenIn.address); + const tOut = this.tokenMap.get(tokenOut.address); if (!tIn || !tOut) { throw new Error('Pool does not contain the tokens provided'); diff --git a/modules/sor/lib/router.ts b/modules/sor/lib/router.ts index 5af0cdd93..87febf314 100644 --- a/modules/sor/lib/router.ts +++ b/modules/sor/lib/router.ts @@ -29,7 +29,7 @@ export class Router { const minSwapAmountRatio = graphTraversalConfig?.minSwapAmountRatio ?? 0.5; // Compute USD threshold if prices provided const priceToken = swapKind === SwapKind.GivenIn ? tokenIn : tokenOut; - const price = tokenPrices.get(priceToken.wrapped.toLowerCase()); + const price = tokenPrices.get(priceToken.address.toLowerCase()); const amount = Number(formatUnits(swapAmount.amount, priceToken.decimals)); const minLimitThresholdUSD = amount * minSwapAmountRatio * (price ?? 0);