Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(universal-router-sdk): Wrap and Unwrap v4 trades #122

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,6 @@ contract SwapERC20CallParametersTest is Test, Interop, DeployRouter {
assertGt(USDC.balanceOf(RECIPIENT), 2000 * ONE_USDC);
}

// v4-sdk 1.6.3 allows this
// function testV4ExactInputEthWithWrap() public {
// MethodParameters memory params = readFixture(json, "._UNISWAP_V4_1_ETH_FOR_USDC_WITH_WRAP");
// }

function testV4ExactInWithFee() public {
MethodParameters memory params = readFixture(json, "._UNISWAP_V4_1_ETH_FOR_USDC_WITH_FEE");

Expand Down Expand Up @@ -606,11 +601,14 @@ contract SwapERC20CallParametersTest is Test, Interop, DeployRouter {
assertGe(from.balance, startingRecipientBalance);
}

// v4-sdk 1.6.3 allows this
// function testV4ExactInNativeOutputWithUnwrap() public {
// MethodParameters memory params = readFixture(json, "._UNISWAP_V4_1000_USDC_FOR_ETH_WITH_UNWRAP");
// }

// function testV4ExactInputEthWithWrap() public {
// MethodParameters memory params = readFixture(json, "._UNISWAP_V4_1_ETH_FOR_USDC_WITH_WRAP");
// }

function testV4ExactInMultiHop() public {
MethodParameters memory params = readFixture(json, "._UNISWAP_V4_ETH_FOR_DAI");

Expand Down
12 changes: 12 additions & 0 deletions sdks/universal-router-sdk/test/forge/interop.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,17 @@
"_UNISWAP_V4_DAI_FOR_1_ETH_2_HOP": {
"calldata": "0x24856bc300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004a00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000030709120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000ea8d710c6670fe50000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000000",
"value": "0"
},
"_UNISWAP_V4_1_ETH_FOR_USDC_WITH_WRAP": {
"calldata": "0x24856bc300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000003c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000003050912000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000d2d62035241defd00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000000",
"value": "1000000000000000000"
},
"_UNISWAP_V4_1000_USDC_FOR_ETH_WITH_UNWRAP": {
"calldata": "0x24856bc300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000003c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000003050912000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000003898956000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000000",
"value": "0"
},
"_UNISWAP_V4_DAI_FOR_1_ETH_2_HOP_WITH_UNWRAP": {
"calldata": "0x24856bc300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004a00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000030709120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000ea8d710c6670fe50000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000000",
"value": "0"
}
}
86 changes: 68 additions & 18 deletions sdks/universal-router-sdk/test/uniswapTrades.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ describe('Uniswap', () => {
CurrencyAmount.fromRawAmount(USDC, outputUSDC),
TradeType.EXACT_OUTPUT
)
const routerTrade = buildTrade([trade])
const opts = swapOptions({})
const methodParameters = SwapRouter.swapCallParameters(buildTrade([trade]), opts)
registerFixture('_UNISWAP_V2_ETH_FOR_1000_USDC', methodParameters)
Expand Down Expand Up @@ -531,19 +530,19 @@ describe('Uniswap', () => {
expect(hexToDecimalString(methodParameters.value)).to.eq(inputEther)
})

// this test needs v4-sdk 1.6.3 to merge
// it('encodes a single exactInput ETH->USDC swap, via WETH', async () => {
// const inputEther = utils.parseEther('1').toString()
// const trade = await V4Trade.fromRoute(
// new V4Route([WETH_USDC_V4], ETHER, USDC),
// CurrencyAmount.fromRawAmount(ETHER, inputEther),
// TradeType.EXACT_INPUT
// )
// const opts = swapOptions({})
// const methodParameters = SwapRouter.swapCallParameters(buildTrade([trade]), opts)
// registerFixture('_UNISWAP_V4_1_ETH_FOR_USDC_WITH_WRAP', methodParameters)
// expect(hexToDecimalString(methodParameters.value)).to.eq(inputEther)
// })
it('encodes a single exactInput ETH->USDC swap, via WETH', async () => {
const inputEther = utils.parseEther('1').toString()
const trade = await V4Trade.fromRoute(
// ETH to USDC but using a WETH pool
new V4Route([WETH_USDC_V4], ETHER, USDC),
CurrencyAmount.fromRawAmount(ETHER, inputEther),
TradeType.EXACT_INPUT
)
const opts = swapOptions({})
const methodParameters = SwapRouter.swapCallParameters(buildTrade([trade]), opts)
registerFixture('_UNISWAP_V4_1_ETH_FOR_USDC_WITH_WRAP', methodParameters)
expect(hexToDecimalString(methodParameters.value)).to.eq(inputEther)
})

it('encodes a single exactInput ETH->USDC swap, with a fee', async () => {
const inputEther = utils.parseEther('1').toString()
Expand Down Expand Up @@ -586,6 +585,19 @@ describe('Uniswap', () => {
expect(hexToDecimalString(methodParameters.value)).to.eq('0')
})

it('encodes a single exactInput USDC->ETH swap, via WETH', async () => {
const inputUSDC = utils.parseUnits('1000', 6).toString()
const trade = await V4Trade.fromRoute(
new V4Route([WETH_USDC_V4], USDC, ETHER),
CurrencyAmount.fromRawAmount(USDC, inputUSDC),
TradeType.EXACT_INPUT
)
const opts = swapOptions({})
const methodParameters = SwapRouter.swapCallParameters(buildTrade([trade]), opts)
registerFixture('_UNISWAP_V4_1000_USDC_FOR_ETH_WITH_UNWRAP', methodParameters)
expect(hexToDecimalString(methodParameters.value)).to.eq('0')
})

it('encodes a single exactInput ETH->USDC->DAI swap', async () => {
const inputEther = utils.parseEther('1').toString()
const trade = await V4Trade.fromRoute(
Expand All @@ -612,9 +624,47 @@ describe('Uniswap', () => {
expect(hexToDecimalString(methodParameters.value)).to.equal('0')
})

it.skip('encodes an exactOutput DAI->USDC->ETH swap, with WETH fee', async () => {
it('encodes an exactOutput ETH->USDC swap', async () => {
const outputUSDC = utils.parseUnits('1000', 6).toString()
const trade = await V4Trade.fromRoute(
new V4Route([ETH_USDC_V4], ETHER, USDC),
CurrencyAmount.fromRawAmount(USDC, outputUSDC),
TradeType.EXACT_OUTPUT
)
const opts = swapOptions({})
const methodParameters = SwapRouter.swapCallParameters(buildTrade([trade]), opts)
registerFixture('_UNISWAP_V4_ETH_FOR_USDC_EXACTOUT', methodParameters)
expect(hexToDecimalString(methodParameters.value)).to.equal('0')
})

it('encodes an exactOutput ETH->USDC swap, via WETH', async () => {
const outputUSDC = utils.parseUnits('1000', 6).toString()
const trade = await V4Trade.fromRoute(
new V4Route([WETH_USDC_V4], ETHER, USDC),
CurrencyAmount.fromRawAmount(USDC, outputUSDC),
TradeType.EXACT_OUTPUT
)
const opts = swapOptions({})
const methodParameters = SwapRouter.swapCallParameters(buildTrade([trade]), opts)
registerFixture('_UNISWAP_V4_ETH_FOR_USDC_EXACTOUT', methodParameters)
expect(hexToDecimalString(methodParameters.value)).to.equal('0')
})

it('encodes an exactOutput DAI->USDC->ETH swap, via WETH', async () => {
const outputEther = utils.parseEther('1').toString()
const trade = await V4Trade.fromRoute(
new V4Route([USDC_DAI_V4, WETH_USDC_V4], DAI, ETHER),
CurrencyAmount.fromRawAmount(ETHER, outputEther),
TradeType.EXACT_OUTPUT
)
const opts = swapOptions({})
const methodParameters = SwapRouter.swapCallParameters(buildTrade([trade]), opts)
registerFixture('_UNISWAP_V4_DAI_FOR_1_ETH_2_HOP_WITH_UNWRAP', methodParameters)
expect(hexToDecimalString(methodParameters.value)).to.equal('0')
})

it('encodes an exactOutput DAI->USDC->ETH swap, with WETH fee', async () => {
// "exact output" of 1ETH. We must adjust for a 5% fee
// v4-sdk 1.6.3 needed
const outputEther = utils.parseEther('1')
const adjustedOutputEther = outputEther
.mul(10000)
Expand All @@ -629,8 +679,8 @@ describe('Uniswap', () => {
const opts = swapOptions({ fee: feeOptions })
buildTrade([trade])
const methodParameters = SwapRouter.swapCallParameters(buildTrade([trade]), opts)
// registerFixture('_UNISWAP_V4_DAI_FOR_1_ETH_2_HOP_WITH_WETH_FEE', methodParameters)
// expect(hexToDecimalString(methodParameters.value)).to.equal('0')
registerFixture('_UNISWAP_V4_DAI_FOR_1_ETH_2_HOP_WITH_WETH_FEE', methodParameters)
expect(hexToDecimalString(methodParameters.value)).to.equal('0')
})
})

Expand Down
Loading