Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-chefs-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'backend': minor
---

replace wrapped functionality with accessing address & refactor to use scoped down Token functionality (removal of isUnderlyingEqual)
2 changes: 1 addition & 1 deletion modules/sor/lib/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brunoguerios I am slowly progressing through replacing references to Token with BaseToken (working name). Commenting here as I had to refactor to use functionality of the BaseToken here now instead of Token.

There are other opportunities for me to replace Token with BaseToken (think TokenAmount). However, if I do these changes in the sdk. (TokenAmount can either accept Token or BaseToken in the constructor I am getting alot of downstream build errors in the sdk.

So, the question here is:

  • Would a "perfect" refactor also need to change how TokenAmount is used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please elaborate on which changes would be required to TokenAmount?
Things such as replacing isUnderlyingEqual with isSameAddress make sense 👍

Ps: regarding variable naming, my suggestion would be to keep Token as the base token (without wrapped) and have a NativeToken (with wrapped). Which should result in less downstream changes compared to renaming Token with BaseToken.

Ps2: I'd expect TokenAmount could accept only Token (without wrapped) - if that's not the case, then it's important to evaluate if this refactor won't increase complexity in any way. The overall idea is to aim for more simplicity and type safety.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have almost fully replaced the Token with BaseToken (which does not include wrapped). This led to quite a big dif.

if (tokens[0].isSameAddress(swapAmount.token.address)) {
this.swapKind = SwapKind.GivenIn;
} else {
this.swapKind = SwapKind.GivenOut;
Expand Down
26 changes: 13 additions & 13 deletions modules/sor/lib/pathGraph/pathGraph.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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());
}
}

Expand All @@ -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,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV2/fx/fxPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV2/gyro2/gyro2Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
18 changes: 9 additions & 9 deletions modules/sor/lib/poolsV2/gyro2/test/gyro2Math.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');

Expand Down
8 changes: 4 additions & 4 deletions modules/sor/lib/poolsV2/gyro3/gyro3Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV2/gyroE/gyroEPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV2/metastable/metastablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV2/stable/stablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV2/weighted/weightedPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV3/buffer/bufferPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV3/gyro2CLP/gyro2CLPPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV3/gyroECLP/gyroECLPPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV3/quantAmm/quantAmmPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV3/reClamm/reClammPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV3/stable/stablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/lib/poolsV3/weighted/weightedPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion modules/sor/lib/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down