Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
6 changes: 3 additions & 3 deletions examples/swaps/auraBalSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
Relayer,
Slippage,
AuraBalSwap,
Token,
TokenAmount,
SwapKind,
isAuraBalSwap,
Expand All @@ -24,15 +23,16 @@ import { ANVIL_NETWORKS, startFork } from '../../test/anvil/anvil-global-setup';
import { makeForkTx } from 'examples/lib/makeForkTx';
import { getSlot } from 'examples/lib/getSlot';
import { exit } from 'node:process';
import { BaseToken } from '@/entities/baseToken';

const auraBalSwap = async ({ rpcUrl, client, userAccount, chainId }) => {
const tokenIn = new Token(
const tokenIn = new BaseToken(
ChainId.MAINNET,
'0xba100000625a3754423978a60c9317c58a424e3D', // BAL
18,
);

const tokenOut = new Token(
const tokenOut = new BaseToken(
ChainId.MAINNET,
'0x616e8BfA43F920657B3497DBf40D6b1A02D4608d', // auraBal
18,
Expand Down
6 changes: 3 additions & 3 deletions examples/swaps/querySmartPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import {
API_ENDPOINT,
ChainId,
SwapKind,
Token,
TokenAmount,
Swap,
ExactInQueryOutput,
ExactOutQueryOutput,
} from '../../src';
import { BaseToken } from '@/entities/baseToken';

interface QuerySmartPath {
rpcUrl: string;
chainId: ChainId;
swapKind: SwapKind;
tokenIn: Token;
tokenOut: Token;
tokenIn: BaseToken;
tokenOut: BaseToken;
swapAmount: TokenAmount;
}

Expand Down
9 changes: 5 additions & 4 deletions examples/swaps/swapV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@ import {
ChainId,
Slippage,
SwapKind,
Token,
TokenAmount,
SwapBuildCallInput,
VAULT_V2,
} from '../../src';
import { BaseToken } from '@/entities/baseToken';
import { querySmartPath } from './querySmartPath';
import { setupExampleFork } from '../lib/setupExampleFork';
import { TOKENS, approveSpenderOnToken } from 'test/lib/utils';
import { TokenAmount } from '@/entities';

const swapV2 = async () => {
// Choose chain id to start fork
const chainId = ChainId.MAINNET;
const { client, rpcUrl, userAccount } = await setupExampleFork({ chainId });

// User defines these params for querying swap with SOR
const swapKind = SwapKind.GivenIn;
const tokenIn = new Token(
const tokenIn = new BaseToken(
chainId,
TOKENS[chainId].WETH.address,
TOKENS[chainId].WETH.decimals,
'WETH',
);
const tokenOut = new Token(
const tokenOut = new BaseToken(
chainId,
TOKENS[chainId].BAL.address,
TOKENS[chainId].BAL.decimals,
Expand Down
10 changes: 7 additions & 3 deletions src/entities/addLiquidity/addLiquidityCowAmm/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { encodeFunctionData } from 'viem';
import { cowAmmPoolAbi } from '@/abi/cowAmmPool';
import { Token } from '@/entities/token';
import { BaseToken } from '@/entities/baseToken';
import { TokenAmount } from '@/entities/tokenAmount';
import { PoolState } from '@/entities/types';
import { poolTypeError, protocolVersionError } from '@/utils';
Expand Down Expand Up @@ -47,12 +47,16 @@ export class AddLiquidityCowAmm implements AddLiquidityBase {
);

const bptOut = TokenAmount.fromRawAmount(
new Token(input.chainId, bptAmount.address, bptAmount.decimals),
new BaseToken(input.chainId, bptAmount.address, bptAmount.decimals),
bptAmount.rawAmount,
);
const amountsIn = tokenAmounts.map((amountIn) =>
TokenAmount.fromRawAmount(
new Token(input.chainId, amountIn.address, amountIn.decimals),
new BaseToken(
input.chainId,
amountIn.address,
amountIn.decimals,
),
amountIn.rawAmount,
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { encodeFunctionData } from 'viem';
import { Token } from '@/entities/token';
import { BaseToken } from '@/entities/baseToken';
import { TokenAmount } from '@/entities/tokenAmount';
import { protocolVersionError, VAULT_V2, ZERO_ADDRESS } from '@/utils';
import { vaultV2Abi } from '@/abi';
Expand Down Expand Up @@ -56,7 +56,7 @@ export class AddLiquidityComposableStable implements AddLiquidityBase {
args,
);

const bpt = new Token(input.chainId, poolState.address, 18);
const bpt = new BaseToken(input.chainId, poolState.address, 18);
const bptOut = TokenAmount.fromRawAmount(bpt, queryOutput.bptOut);

const amountsIn = queryOutput.amountsIn.map((a, i) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { encodeFunctionData } from 'viem';
import { Token } from '@/entities/token';
import { BaseToken } from '@/entities/baseToken';
import { TokenAmount } from '@/entities/tokenAmount';
import { StableEncoder } from '@/entities/encoders/stable';
import { protocolVersionError, VAULT_V2, ZERO_ADDRESS } from '@/utils';
Expand Down Expand Up @@ -49,7 +49,7 @@ export class AddLiquidityStable implements AddLiquidityBase {
args,
);

const bpt = new Token(input.chainId, poolState.address, 18);
const bpt = new BaseToken(input.chainId, poolState.address, 18);
const bptOut = TokenAmount.fromRawAmount(bpt, queryOutput.bptOut);

const amountsIn = queryOutput.amountsIn.map((a, i) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { encodeFunctionData } from 'viem';
import { Token } from '@/entities/token';
import { BaseToken } from '@/entities/baseToken';
import { TokenAmount } from '@/entities/tokenAmount';
import { WeightedEncoder } from '@/entities/encoders/weighted';
import { protocolVersionError, VAULT_V2, ZERO_ADDRESS } from '@/utils';
Expand Down Expand Up @@ -51,7 +51,7 @@ export class AddLiquidityWeighted implements AddLiquidityBase {
args,
);

const bpt = new Token(input.chainId, poolState.address, 18);
const bpt = new BaseToken(input.chainId, poolState.address, 18);
const bptOut = TokenAmount.fromRawAmount(bpt, queryOutput.bptOut);

const amountsIn = queryOutput.amountsIn.map((a, i) =>
Expand Down
4 changes: 2 additions & 2 deletions src/entities/addLiquidity/addLiquidityV3/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { encodeFunctionData, zeroAddress } from 'viem';
import { balancerRouterAbiExtended } from '@/abi';
import { Permit2 } from '@/entities/permit2Helper';
import { Token } from '@/entities/token';
import { BaseToken } from '@/entities/baseToken';
import { TokenAmount } from '@/entities/tokenAmount';
import { PoolState } from '@/entities/types';
import {
Expand Down Expand Up @@ -33,7 +33,7 @@ export class AddLiquidityV3 implements AddLiquidityBase {
block?: bigint,
): Promise<AddLiquidityBaseQueryOutput> {
const sortedTokens = getSortedTokens(poolState.tokens, input.chainId);
const bptToken = new Token(input.chainId, poolState.address, 18);
const bptToken = new BaseToken(input.chainId, poolState.address, 18);

let bptOut: TokenAmount;
let amountsIn: TokenAmount[];
Expand Down
6 changes: 3 additions & 3 deletions src/entities/addLiquidityBoosted/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '../addLiquidity/types';
import { doAddLiquidityUnbalancedQuery } from './doAddLiquidityUnbalancedQuery';
import { doAddLiquidityProportionalQuery } from './doAddLiquidityPropotionalQuery';
import { Token } from '../token';
import { BaseToken } from '@/entities/baseToken';
import { balancerV3Contracts, protocolVersionError } from '@/utils';
import {
balancerCompositeLiquidityRouterBoostedAbiExtended,
Expand Down Expand Up @@ -52,7 +52,7 @@ export class AddLiquidityBoostedV3 {
type: 'Boosted',
});

const bptToken = new Token(input.chainId, poolState.address, 18);
const bptToken = new BaseToken(input.chainId, poolState.address, 18);
const wrapUnderlying: boolean[] = new Array(
poolState.tokens.length,
).fill(false);
Expand Down Expand Up @@ -151,7 +151,7 @@ export class AddLiquidityBoostedV3 {
amountsIn = tokensIn.map((t, i) => {
const tokenInAddress = t.toLowerCase() as Address;
const { decimals } = poolStateTokenMap[tokenInAddress];
const token = new Token(
const token = new BaseToken(
input.chainId,
tokenInAddress,
decimals,
Expand Down
6 changes: 3 additions & 3 deletions src/entities/addLiquidityBuffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Permit2 } from '@/entities/permit2Helper';
import { BufferState } from '@/entities/types';

import { doAddLiquidityQuery } from './doAddLiquidityQuery';
import { Token } from '../token';
import { BaseToken } from '../baseToken';
import {
balancerBufferRouterAbiExtended,
balancerRouterAbiExtended,
Expand Down Expand Up @@ -35,7 +35,7 @@ export class AddLiquidityBufferV3 {
input.exactSharesToIssue,
block,
);
const underlyingToken = new Token(
const underlyingToken = new BaseToken(
input.chainId,
bufferState.underlyingToken.address,
bufferState.underlyingToken.decimals,
Expand All @@ -44,7 +44,7 @@ export class AddLiquidityBufferV3 {
underlyingToken,
amountUnderlyingIn,
);
const wrappedToken = new Token(
const wrappedToken = new BaseToken(
input.chainId,
bufferState.wrappedToken.address,
bufferState.wrappedToken.decimals,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, PoolType } from '@/types';
import { Token } from '@/entities/token';
import { BaseToken } from '@/entities/baseToken';
import {
ZERO_ADDRESS,
BALANCER_RELAYER,
Expand Down Expand Up @@ -31,7 +31,7 @@ export const getQueryCallsAttributes = (
for (const pool of poolsSortedByLevel) {
const sortedTokens = pool.tokens
.sort((a, b) => a.index - b.index)
.map((t) => new Token(chainId, t.address, t.decimals));
.map((t) => new BaseToken(chainId, t.address, t.decimals));
const maxAmountsIn = getMaxAmountsIn(sortedTokens, amountsIn, calls);
if (maxAmountsIn.every((a) => a.amount === 0n && !a.isRef)) {
continue;
Expand Down Expand Up @@ -61,7 +61,7 @@ export const getQueryCallsAttributes = (
};

const getMaxAmountsIn = (
sortedTokens: Token[],
sortedTokens: BaseToken[],
amountsIn: { address: Address; rawAmount: bigint }[],
calls: AddLiquidityNestedCallAttributes[],
): { amount: bigint; isRef: boolean }[] => {
Expand Down
4 changes: 2 additions & 2 deletions src/entities/addLiquidityNested/addLiquidityNestedV2/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { encodeFunctionData } from 'viem';
import { Token } from '../../token';
import { BaseToken } from '../../baseToken';
import { BALANCER_RELAYER, ZERO_ADDRESS } from '../../../utils';
import { Relayer } from '../../relayer';
import { encodeCalls } from './encodeCalls';
Expand Down Expand Up @@ -50,7 +50,7 @@ export class AddLiquidityNestedV2 {
encodedMulticall,
);

const tokenOut = new Token(
const tokenOut = new BaseToken(
input.chainId,
callsAttributes[callsAttributes.length - 1].poolAddress,
18,
Expand Down
4 changes: 2 additions & 2 deletions src/entities/addLiquidityNested/addLiquidityNestedV2/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, Hex } from 'viem';
import { PoolType } from '../../../types';
import { ChainId } from '../../../utils';
import { Token } from '../../token';
import { BaseToken } from '../../baseToken';
import { TokenAmount } from '../../tokenAmount';
import { PoolKind } from '../../types';
import { Slippage } from '@/entities/slippage';
Expand All @@ -14,7 +14,7 @@ export type AddLiquidityNestedInputV2 = AddLiquidityNestedBaseInput & {
export type AddLiquidityNestedCallAttributes = {
chainId: ChainId;
wethIsEth?: boolean;
sortedTokens: Token[];
sortedTokens: BaseToken[];
poolId: Hex;
poolAddress: Address;
poolType: PoolType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inputValidationError, NATIVE_ASSETS } from '@/utils';

import { Token } from '../../token';
import { BaseToken } from '../../baseToken';
import { TokenAmount } from '../../tokenAmount';
import { NestedPoolState } from '../../types';
import { AddLiquidityNestedInput } from '../types';
Expand All @@ -11,7 +11,7 @@ export const validateQueryInput = (
nestedPoolState: NestedPoolState,
): TokenAmount[] => {
const mainTokens = nestedPoolState.mainTokens.map(
(t) => new Token(input.chainId, t.address, t.decimals),
(t) => new BaseToken(input.chainId, t.address, t.decimals),
);
const amountsIn = input.amountsIn.map((amountIn) => {
const tokenIn = mainTokens.find((t) =>
Expand All @@ -35,7 +35,7 @@ export const validateBuildCallInput = (
if (input.wethIsEth) {
if (
!input.amountsIn.some((a) =>
a.token.isUnderlyingEqual(NATIVE_ASSETS[chainId]),
a.token.isSameAddress(NATIVE_ASSETS[chainId].wrapped),
Copy link
Member

Choose a reason for hiding this comment

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

Can use the new isWrapped here?

)
) {
throw inputValidationError(
Expand Down
6 changes: 3 additions & 3 deletions src/entities/addLiquidityNested/addLiquidityNestedV3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
AddLiquidityNestedBuildCallOutput,
AddLiquidityNestedInput,
} from '../types';
import { Token } from '@/entities/token';
import { BaseToken } from '@/entities/baseToken';
import { getAmounts, getValue } from '@/entities/utils';
import { TokenAmount } from '@/entities/tokenAmount';
import {
Expand Down Expand Up @@ -57,7 +57,7 @@ export class AddLiquidityNestedV3 {
// query function input, `tokensIn` array, must have all tokens from child pools
// and all tokens that are not BPTs from the nested pool (parent pool).
const mainTokens = nestedPoolState.mainTokens.map(
(t) => new Token(input.chainId, t.address, t.decimals),
(t) => new BaseToken(input.chainId, t.address, t.decimals),
);
// This will add 0 amount for any tokensIn the user hasn't included
const maxAmountsIn = getAmounts(mainTokens, input.amountsIn, 0n);
Expand All @@ -74,7 +74,7 @@ export class AddLiquidityNestedV3 {
block,
);

const bptToken = new Token(input.chainId, parentPool.address, 18);
const bptToken = new BaseToken(input.chainId, parentPool.address, 18);

return {
to: BALANCER_COMPOSITE_LIQUIDITY_ROUTER_NESTED[input.chainId],
Expand Down
45 changes: 45 additions & 0 deletions src/entities/baseToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Address } from 'viem';
import { InputToken } from '../types';

/**
* BaseToken contains the core functionality for all tokens
* This class handles the essential token properties and methods
* without any wrapped token logic
*/
export class BaseToken {
public readonly chainId: number;
public readonly address: Address;
public readonly decimals: number;
public readonly symbol?: string;
public readonly name?: string;

public constructor(
chainId: number,
address: Address,
decimals: number,
symbol?: string,
name?: string,
) {
this.chainId = chainId;
// Addresses are always lowercased for speed
this.address = address.toLowerCase() as Address;
this.decimals = decimals;
this.symbol = symbol;
this.name = name;
}

public isEqual(token: BaseToken) {
return this.chainId === token.chainId && this.address === token.address;
}

public isSameAddress(address: Address) {
return this.address === address.toLowerCase();
}

public toInputToken(): InputToken {
return {
address: this.address,
decimals: this.decimals,
};
}
}
Loading