Skip to content
Draft
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
10 changes: 5 additions & 5 deletions modules/sor/lib/path.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { TokenAmount, SwapKind, Token } from '@balancer/sdk';
import { TokenAmount, SwapKind, BaseToken, Token } from '@balancer/sdk';
import { BasePool } from './poolsV2/basePool';
import { BufferPool } from './poolsV3/buffer/bufferPool';

export class PathLocal {
public readonly pools: BasePool[];
public readonly tokens: Token[];
public readonly tokens: BaseToken[];
public readonly isBuffer: boolean[];

public constructor(tokens: Token[], pools: BasePool[], isBuffer: boolean[]) {
public constructor(tokens: BaseToken[], pools: BasePool[], isBuffer: boolean[]) {
if (pools.length === 0 || tokens.length < 2) {
throw new Error('Invalid path: must contain at least 1 pool and 2 tokens.');
}
Expand Down Expand Up @@ -35,7 +35,7 @@ export class PathWithAmount extends PathLocal {
public readonly swapStepsGreaterThanBufferLimit: number = 0;

public constructor(
tokens: Token[],
tokens: BaseToken[],
pools: BasePool[],
isBuffer: boolean[],
swapAmount: TokenAmount,
Expand All @@ -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.

if (tokens[0].isSameAddress(swapAmount.token.address)) {
this.swapKind = SwapKind.GivenIn;
} else {
this.swapKind = SwapKind.GivenOut;
Expand Down
28 changes: 14 additions & 14 deletions modules/sor/lib/pathGraph/pathGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ export class PathGraph {
const minLimitThreshold = (swapAmount.amount * BigInt(Math.floor(config.minSwapAmountRatio * 100))) / 100n;

const tokenPaths = this.findAllValidTokenPaths({
token: tokenIn.wrapped,
tokenIn: tokenIn.wrapped,
tokenOut: tokenOut.wrapped,
token: tokenIn.address,
tokenIn: tokenIn.address,
tokenOut: tokenOut.address,
config,
tokenPath: [tokenIn.wrapped],
tokenPath: [tokenIn.address],
}).sort((a, b) => (a.length < b.length ? -1 : 1));

const paths: PathGraphEdgeData[][] = [];
Expand Down Expand Up @@ -186,7 +186,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 @@ -226,12 +226,12 @@ export class PathGraph {
}

private addNode(token: Token): void {
this.nodes.set(token.wrapped, {
isPhantomBpt: !!this.poolAddressMap.get(token.wrapped),
this.nodes.set(token.address, {
isPhantomBpt: !!this.poolAddressMap.get(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 Down Expand Up @@ -259,16 +259,16 @@ export class PathGraph {
edgeProps: PathGraphEdgeData;
maxPathsPerTokenPair: number;
}): void {
const tokenInVertex = this.nodes.get(edgeProps.tokenIn.wrapped);
const tokenOutVertex = this.nodes.get(edgeProps.tokenOut.wrapped);
const tokenInNode = this.edges.get(edgeProps.tokenIn.wrapped);
const tokenInVertex = this.nodes.get(edgeProps.tokenIn.address);
const tokenOutVertex = this.nodes.get(edgeProps.tokenOut.address);
const tokenInNode = this.edges.get(edgeProps.tokenIn.address);

if (!tokenInVertex || !tokenOutVertex || !tokenInNode) {
throw new Error('Attempting to add invalid edge');
}

const hasPhantomBpt = tokenInVertex.isPhantomBpt || tokenOutVertex.isPhantomBpt;
const existingEdges = tokenInNode.get(edgeProps.tokenOut.wrapped) || [];
const existingEdges = tokenInNode.get(edgeProps.tokenOut.address) || [];

//TODO: ideally we don't call sort every time, this isn't performant
const sorted = [...existingEdges, edgeProps].sort((a, b) =>
Expand All @@ -278,7 +278,7 @@ export class PathGraph {
// TODO: double check if the hasPhantomBpt issue is not affecting v3 liquidity more frequently (considering all
// pools have their BPT artificially added so we consider them for add/remove liquidity steps)
tokenInNode.set(
edgeProps.tokenOut.wrapped,
edgeProps.tokenOut.address,
sorted.length > maxPathsPerTokenPair && !hasPhantomBpt ? sorted.slice(0, maxPathsPerTokenPair) : sorted,
);
}
Expand Down
12 changes: 6 additions & 6 deletions modules/sor/lib/poolsV2/basePool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PoolType, SwapKind, Token, TokenAmount } from '@balancer/sdk';
import { PoolType, SwapKind, BaseToken, TokenAmount } from '@balancer/sdk';
import { Hex } from 'viem';

import { BasePoolToken } from '../utils/basePoolToken';
Expand All @@ -9,13 +9,13 @@ export interface BasePool {
readonly address: string;
swapFee: bigint;
tokens: BasePoolToken[];
getNormalizedLiquidity(tokenIn: Token, tokenOut: Token): bigint;
swapGivenIn(tokenIn: Token, tokenOut: Token, swapAmount: TokenAmount, mutateBalances?: boolean): TokenAmount;
swapGivenOut(tokenIn: Token, tokenOut: Token, swapAmount: TokenAmount, mutateBalances?: boolean): TokenAmount;
getLimitAmountSwap(tokenIn: Token, tokenOut: Token, swapKind: SwapKind): bigint;
getNormalizedLiquidity(tokenIn: BaseToken, tokenOut: BaseToken): bigint;
swapGivenIn(tokenIn: BaseToken, tokenOut: BaseToken, swapAmount: TokenAmount, mutateBalances?: boolean): TokenAmount;
swapGivenOut(tokenIn: BaseToken, tokenOut: BaseToken, swapAmount: TokenAmount, mutateBalances?: boolean): TokenAmount;
getLimitAmountSwap(tokenIn: BaseToken, tokenOut: BaseToken, swapKind: SwapKind): bigint;
/**
* Validate that pool contains tokenIn and tokenOut provided and returns pool specific token data (e.g. balance, index, weight, rate, etc.)
*/
getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: BasePoolToken; tOut: BasePoolToken };
getPoolTokens(tokenIn: BaseToken, tokenOut: BaseToken): { tIn: BasePoolToken; tOut: BasePoolToken };
copy(): BasePool;
}
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
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
16 changes: 8 additions & 8 deletions modules/sor/lib/poolsV3/basePoolV3.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hex } from 'viem';

import { MAX_UINT256, PoolType, SwapKind, Token, TokenAmount } from '@balancer/sdk';
import { MAX_UINT256, PoolType, SwapKind, BaseToken, TokenAmount } from '@balancer/sdk';
import { AddKind, RemoveKind, Vault, HookState, PoolState } from '@balancer-labs/balancer-maths';
import { Chain } from '@prisma/client';

Expand Down Expand Up @@ -55,7 +55,7 @@ export class BasePoolV3 {
this.vault = new Vault();
}

public getLimitAmountSwap(tokenIn: Token, tokenOut: Token, swapKind: SwapKind): bigint {
public getLimitAmountSwap(tokenIn: BaseToken, tokenOut: BaseToken, swapKind: SwapKind): bigint {
const { tIn, tOut } = this.getPoolTokens(tokenIn, tokenOut);

// remove liquidity
Expand Down Expand Up @@ -90,8 +90,8 @@ export class BasePoolV3 {
}

public swapGivenIn(
tokenIn: Token,
tokenOut: Token,
tokenIn: BaseToken,
tokenOut: BaseToken,
swapAmount: TokenAmount,
mutateBalances?: boolean,
): TokenAmount {
Expand Down Expand Up @@ -163,8 +163,8 @@ export class BasePoolV3 {
}

public swapGivenOut(
tokenIn: Token,
tokenOut: Token,
tokenIn: BaseToken,
tokenOut: BaseToken,
swapAmount: TokenAmount,
mutateBalances?: boolean,
): TokenAmount {
Expand Down Expand Up @@ -235,7 +235,7 @@ export class BasePoolV3 {
return TokenAmount.fromRawAmount(tIn.token, calculatedAmount);
}

public getNormalizedLiquidity(tokenIn: Token, tokenOut: Token): bigint {
public getNormalizedLiquidity(tokenIn: BaseToken, tokenOut: BaseToken): bigint {
const { tIn, tOut } = this.getPoolTokens(tokenIn, tokenOut);

const tokenPair = this.tokenPairs.find(
Expand All @@ -254,7 +254,7 @@ export class BasePoolV3 {
throw new Error('Must be implemented by the subclass');
}

public getPoolTokens(tokenIn: Token, tokenOut: Token): { tIn: BasePoolToken; tOut: BasePoolToken } {
public getPoolTokens(tokenIn: BaseToken, tokenOut: BaseToken): { tIn: BasePoolToken; tOut: BasePoolToken } {
throw new Error('Must be implemented by the subclass');
}

Expand Down
12 changes: 6 additions & 6 deletions modules/sor/lib/poolsV3/buffer/bufferPool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, Hex } from 'viem';
import { MAX_UINT256, SwapKind, Token, TokenAmount } from '@balancer/sdk';
import { MAX_UINT256, SwapKind, Token, TokenAmount, BaseToken } from '@balancer/sdk';
import { BufferState, Vault } from '@balancer-labs/balancer-maths';

import { BasePoolMethodsV3 } from '../basePoolMethodsV3';
Expand Down Expand Up @@ -189,9 +189,9 @@ 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);
public getPoolTokens(tokenIn: BaseToken, tokenOut: BaseToken): { tIn: BasePoolToken; tOut: BasePoolToken } {
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 @@ -213,7 +213,7 @@ export class BufferPool implements BasePoolMethodsV3 {
);
}

public swapGivenInGreaterThanBufferLimit(tokenIn: Token, tokenOut: Token, swapAmount: TokenAmount): boolean {
public swapGivenInGreaterThanBufferLimit(tokenIn: BaseToken, tokenOut: BaseToken, swapAmount: TokenAmount): boolean {
const { tIn, tOut } = this.getPoolTokens(tokenIn, tokenOut);
const mainTokenAmount = this.tokens[0];
const underlyingTokenAmount = this.tokens[1];
Expand All @@ -227,7 +227,7 @@ export class BufferPool implements BasePoolMethodsV3 {
}
}

public swapGivenOutGreaterThanBufferLimit(tokenIn: Token, tokenOut: Token, swapAmount: TokenAmount): boolean {
public swapGivenOutGreaterThanBufferLimit(tokenIn: BaseToken, tokenOut: BaseToken, swapAmount: TokenAmount): boolean {
const { tIn, tOut } = this.getPoolTokens(tokenIn, tokenOut);
const mainTokenAmount = this.tokens[0];
const underlyingTokenAmount = this.tokens[1];
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
Loading
Loading