Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Add helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongeric committed Mar 28, 2024
1 parent ee0376d commit 8ebb18f
Showing 1 changed file with 17 additions and 89 deletions.
106 changes: 17 additions & 89 deletions test/uniswapTrades.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,12 @@ describe('Uniswap', () => {
amountOut,
}
}

function compareUniswapTrades(left: UniswapTrade, right: UniswapTrade): void {
expect(SwapRouter.swapCallParameters(left).calldata).to.eq(SwapRouter.swapCallParameters(right).calldata)
expect(SwapRouter.swapCallParameters(left).value).to.eq(SwapRouter.swapCallParameters(right).value)
}

it('v2 - erc20 <> erc20', async () => {
const [tokenIn, tokenOut] = [DAI, USDC]
const inputAmount = ethers.utils
Expand All @@ -811,7 +817,6 @@ describe('Uniswap', () => {
const opts = swapOptions({})
// amount should always be interms of output token
const trade = new V2Trade(new RouteV2([USDC_DAI_V2], DAI, USDC), rawInputAmount, tradeType)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: DAI.address,
Expand All @@ -830,14 +835,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v3 - erc20 <> erc20', async () => {
Expand All @@ -849,7 +848,6 @@ describe('Uniswap', () => {

const opts = swapOptions({})
const trade = await V3Trade.fromRoute(new RouteV3([USDC_DAI_V3], tokenIn, tokenOut), rawInputAmount, tradeType)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: DAI.address,
Expand All @@ -868,14 +866,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v2 - handles weth input properly', async () => {
Expand All @@ -887,7 +879,6 @@ describe('Uniswap', () => {

const opts = swapOptions({})
const trade = new V2Trade(new RouteV2([WETH_USDC_V2], tokenIn, tokenOut), rawInputAmount, tradeType)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: WETH.address,
Expand All @@ -906,14 +897,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v3 - handles weth input properly', async () => {
Expand All @@ -925,7 +910,6 @@ describe('Uniswap', () => {

const opts = swapOptions({})
const trade = await V3Trade.fromRoute(new RouteV3([WETH_USDC_V3], WETH, USDC), rawInputAmount, tradeType)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: WETH.address,
Expand All @@ -944,14 +928,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v2 - handles eth input properly', async () => {
Expand All @@ -963,7 +941,6 @@ describe('Uniswap', () => {

const opts = swapOptions({})
const trade = new V2Trade(new RouteV2([WETH_USDC_V2], Ether.onChain(1), USDC), rawInputAmount, tradeType)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: ETH_ADDRESS,
Expand All @@ -983,14 +960,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v2 - handles eth input properly - 0xeeee...eeee address', async () => {
Expand All @@ -1002,7 +973,6 @@ describe('Uniswap', () => {

const opts = swapOptions({})
const trade = new V2Trade(new RouteV2([WETH_USDC_V2], Ether.onChain(1), USDC), rawInputAmount, tradeType)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: E_ETH_ADDRESS,
Expand All @@ -1022,14 +992,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v3 - handles eth input properly', async () => {
Expand All @@ -1045,7 +1009,6 @@ describe('Uniswap', () => {
rawInputAmount,
tradeType
)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: ETH_ADDRESS,
Expand All @@ -1065,14 +1028,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v2 - handles eth output properly', async () => {
Expand All @@ -1084,7 +1041,6 @@ describe('Uniswap', () => {

const opts = swapOptions({})
const trade = new V2Trade(new RouteV2([WETH_USDC_V2], tokenIn, tokenOut), rawInputAmount, tradeType)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: USDC.address,
Expand All @@ -1104,14 +1060,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v3 - handles eth output properly', async () => {
Expand All @@ -1123,7 +1073,6 @@ describe('Uniswap', () => {

const opts = swapOptions({})
const trade = await V3Trade.fromRoute(new RouteV3([WETH_USDC_V3], tokenIn, tokenOut), rawInputAmount, tradeType)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: USDC.address,
Expand All @@ -1143,14 +1092,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v3 - multi pool erc20 <> erc20', async () => {
Expand All @@ -1166,7 +1109,6 @@ describe('Uniswap', () => {
rawInputAmount,
tradeType
)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
tokenIn: DAI.address,
Expand All @@ -1192,14 +1134,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade]), opts), new UniswapTrade(routerTrade, opts))
})

it('v3 - handles split routes properly', async () => {
Expand All @@ -1220,10 +1156,8 @@ describe('Uniswap', () => {
rawInputAmount.divide(2),
tradeType
)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade1, trade2]), opts))

const splitRouteInputAmounts = [trade1.inputAmount.quotient.toString(), trade2.inputAmount.quotient.toString()]

const splitRouteOutputAmounts = [
trade1.outputAmount.quotient.toString(),
trade2.outputAmount.quotient.toString(),
Expand All @@ -1247,14 +1181,8 @@ describe('Uniswap', () => {
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)

// ensure that we can generate the encoded call parameters
const methodParameters = SwapRouter.swapCallParameters(uniswapTrade)
expect(hexToDecimalString(methodParameters.value)).to.eq(hexToDecimalString(SDKMethodParameters.value))

// ensure that the encoded call parameters are the same
expect(methodParameters.calldata).to.eq(SDKMethodParameters.calldata)
compareUniswapTrades(new UniswapTrade(buildTrade([trade1, trade2]), opts), new UniswapTrade(routerTrade, opts))
})
})
}
Expand Down

0 comments on commit 8ebb18f

Please sign in to comment.