They were in TychoRouterSplitSwap.t.sol Add docs to _balancerOf Took 2 hours 8 minutes Took 18 seconds
275 lines
17 KiB
Solidity
275 lines
17 KiB
Solidity
// SPDX-License-Identifier: BUSL-1.1
|
|
pragma solidity ^0.8.26;
|
|
|
|
import "./TychoRouterTestSetup.sol";
|
|
import "./executors/UniswapV4Utils.sol";
|
|
|
|
contract TychoRouterTestProtocolIntegration is TychoRouterTestSetup {
|
|
function testSingleSwapUSV4CallbackPermit2() public {
|
|
vm.startPrank(ALICE);
|
|
uint256 amountIn = 100 ether;
|
|
deal(USDE_ADDR, ALICE, amountIn);
|
|
(
|
|
IAllowanceTransfer.PermitSingle memory permitSingle,
|
|
bytes memory signature
|
|
) = handlePermit2Approval(USDE_ADDR, tychoRouterAddr, amountIn);
|
|
|
|
UniswapV4Executor.UniswapV4Pool[] memory pools =
|
|
new UniswapV4Executor.UniswapV4Pool[](1);
|
|
pools[0] = UniswapV4Executor.UniswapV4Pool({
|
|
intermediaryToken: USDT_ADDR,
|
|
fee: uint24(100),
|
|
tickSpacing: int24(1)
|
|
});
|
|
|
|
bytes memory protocolData = UniswapV4Utils.encodeExactInput(
|
|
USDE_ADDR,
|
|
USDT_ADDR,
|
|
true,
|
|
TokenTransfer.TransferType.TRANSFER_PERMIT2_TO_PROTOCOL,
|
|
ALICE,
|
|
pools
|
|
);
|
|
|
|
bytes memory swap =
|
|
encodeSingleSwap(address(usv4Executor), protocolData);
|
|
|
|
tychoRouter.singleSwapPermit2(
|
|
amountIn,
|
|
USDE_ADDR,
|
|
USDT_ADDR,
|
|
99943850,
|
|
false,
|
|
false,
|
|
ALICE,
|
|
permitSingle,
|
|
signature,
|
|
swap
|
|
);
|
|
|
|
assertEq(IERC20(USDT_ADDR).balanceOf(ALICE), 99963618);
|
|
vm.stopPrank();
|
|
}
|
|
|
|
function testSplitSwapMultipleUSV4Callback() public {
|
|
// This test has two uniswap v4 hops that will be executed inside of the V4 pool manager
|
|
// USDE -> USDT -> WBTC
|
|
uint256 amountIn = 100 ether;
|
|
deal(USDE_ADDR, tychoRouterAddr, amountIn);
|
|
|
|
UniswapV4Executor.UniswapV4Pool[] memory pools =
|
|
new UniswapV4Executor.UniswapV4Pool[](2);
|
|
pools[0] = UniswapV4Executor.UniswapV4Pool({
|
|
intermediaryToken: USDT_ADDR,
|
|
fee: uint24(100),
|
|
tickSpacing: int24(1)
|
|
});
|
|
pools[1] = UniswapV4Executor.UniswapV4Pool({
|
|
intermediaryToken: WBTC_ADDR,
|
|
fee: uint24(3000),
|
|
tickSpacing: int24(60)
|
|
});
|
|
|
|
bytes memory protocolData = UniswapV4Utils.encodeExactInput(
|
|
USDE_ADDR,
|
|
WBTC_ADDR,
|
|
true,
|
|
TokenTransfer.TransferType.TRANSFER_TO_PROTOCOL,
|
|
ALICE,
|
|
pools
|
|
);
|
|
|
|
bytes memory swap =
|
|
encodeSingleSwap(address(usv4Executor), protocolData);
|
|
|
|
tychoRouter.singleSwap(
|
|
amountIn, USDE_ADDR, WBTC_ADDR, 118280, false, false, ALICE, swap
|
|
);
|
|
|
|
assertEq(IERC20(WBTC_ADDR).balanceOf(ALICE), 118281);
|
|
}
|
|
|
|
function testSequentialUSV4Integration() public {
|
|
// Test created with calldata from our router encoder.
|
|
|
|
// Performs a sequential swap from USDC to PEPE though ETH using two
|
|
// consecutive USV4 pools
|
|
//
|
|
// USDC ──(USV4)──> ETH ───(USV4)──> PEPE
|
|
//
|
|
deal(USDC_ADDR, ALICE, 1 ether);
|
|
uint256 balanceBefore = IERC20(PEPE_ADDR).balanceOf(ALICE);
|
|
|
|
// Approve permit2
|
|
vm.startPrank(ALICE);
|
|
IERC20(USDC_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
|
// Encoded solution generated using `test_sequential_encoding_strategy_usv4`
|
|
(bool success,) = tychoRouterAddr.call(
|
|
hex"7c553846000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000006982508145454ce325ddbe47a25d4ec3d23119330000000000000000000000000000000000000000005064ff624d54346285543f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000006828a8d900000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d0139500000000000000000000000000000000000000000000000000000000680122e10000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000412fb2c4e85c1b2236aef343641c10f81e4abfd675f520d86778cb9db16c9f500d11fe28b99285dd1bef082b9ccde3360a8077c57ece0775677fddfd5ff11b6e081c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008d008b0001000000f62849f9a0b5bf2913b396098f7c7019b51a820aa0b86991c6218b36c1d19d4a2e9eb0ce3606eb486982508145454ce325ddbe47a25d4ec3d23119330002cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000000bb800003c6982508145454ce325ddbe47a25d4ec3d23119330061a80001f400000000000000000000000000000000000000"
|
|
);
|
|
|
|
vm.stopPrank();
|
|
|
|
uint256 balanceAfter = IERC20(PEPE_ADDR).balanceOf(ALICE);
|
|
|
|
assertTrue(success, "Call Failed");
|
|
assertEq(balanceAfter - balanceBefore, 123172000092711286554274694);
|
|
}
|
|
|
|
function testMultiProtocolIntegration() public {
|
|
// Test created with calldata from our router encoder.
|
|
//
|
|
// DAI ─(USV2)─> WETH ─(bal)─> WBTC ─(curve)─> USDT ─(ekubo)─> ETH ─(USV4)─> USDC
|
|
|
|
deal(DAI_ADDR, ALICE, 1500 ether);
|
|
uint256 balanceBefore = address(ALICE).balance;
|
|
|
|
// Approve permit2
|
|
vm.startPrank(ALICE);
|
|
IERC20(DAI_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
|
// Encoded solution generated using `test_multi_protocol`
|
|
(bool success,) = tychoRouterAddr.call(
|
|
hex"51bcc7b600000000000000000000000000000000000000000000005150ae84a8cdf000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a2958f36da71a9200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000005150ae84a8cdf0000000000000000000000000000000000000000000000000000000000000682f990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d01395000000000000000000000000000000000000000000000000000000006808130800000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000026000000000000000000000000000000000000000000000000000000000000000414e46e936cfd0f718a485f27c233cb85a64ab162edca753cbc7c9d1bc393a688275dc15bd930e210af2e5dd8e8d8f90ec8438b821297c469d80712aadcff73b071c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021400525615deb798bb3e4dfa0139dfa1b3d433cc23b72f6b175474e89094c44da98b954eedeac495271d0fa478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d0139501020072c7183455a4c133ae270771860664b6b7ec320bb1c02aaa39b223fe8d0a0e5c4f27ead9083c756cc22260fac5e5542a773aa44fbcfedf7c193bc2c599a6f548df93de924d73be7d25dc02554c6bd66db500020000000000000000000e3ede3eca2a72b3aecc820e955b36f38437d01395010500691d1499e622d69689cdf9004d05ec547d650ff2112260fac5e5542a773aa44fbcfedf7c193bc2c599dac17f958d2ee523a2206206994597c13d831ec7d51a44d3fae010294c616388b506acda1bfaae4603010001053ede3eca2a72b3aecc820e955b36f38437d013950071a0cb889707d426a7a386870a03bc70d1b0697598003ede3eca2a72b3aecc820e955b36f38437d01395dac17f958d2ee523a2206206994597c13d831ec7a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000001a36e2eb1c43200000032006cf62849f9a0b5bf2913b396098f7c7019b51a820aa0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000000bb800003c000000000000000000000000"
|
|
);
|
|
|
|
vm.stopPrank();
|
|
|
|
uint256 balanceAfter = address(ALICE).balance;
|
|
|
|
assertTrue(success, "Call Failed");
|
|
assertEq(balanceAfter - balanceBefore, 732214216964381330);
|
|
}
|
|
|
|
function testSingleUSV4IntegrationInputETH() public {
|
|
// Test created with calldata from our router encoder.
|
|
|
|
// Performs a single swap from ETH to PEPE without wrapping or unwrapping
|
|
//
|
|
// ETH ───(USV4)──> PEPE
|
|
//
|
|
deal(ALICE, 1 ether);
|
|
uint256 balanceBefore = IERC20(PEPE_ADDR).balanceOf(ALICE);
|
|
|
|
// Encoded solution generated using `test_single_encoding_strategy_usv4_eth_in`
|
|
(bool success,) = tychoRouterAddr.call{value: 1 ether}(
|
|
hex"30ace1b10000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006982508145454ce325ddbe47a25d4ec3d23119330000000000000000000000000000000000000000007e0a55d4322a6e93c2379c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000682f92ce00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000068080cd600000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000041006930677d9715fb8c55f73546d3aaff4176ee1342b9b7ae34431a4356fc98a915f3103639d8e34cbaa591a3493e887dad6e816228200dee0a693408b4fa6fdc1b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006cf62849f9a0b5bf2913b396098f7c7019b51a820a00000000000000000000000000000000000000006982508145454ce325ddbe47a25d4ec3d23119330105cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc26982508145454ce325ddbe47a25d4ec3d23119330061a80001f40000000000000000000000000000000000000000"
|
|
);
|
|
|
|
vm.stopPrank();
|
|
|
|
uint256 balanceAfter = IERC20(PEPE_ADDR).balanceOf(ALICE);
|
|
|
|
assertTrue(success, "Call Failed");
|
|
assertEq(balanceAfter - balanceBefore, 235610487387677804636755778);
|
|
}
|
|
|
|
function testSingleUSV4IntegrationOutputETH() public {
|
|
// Test created with calldata from our router encoder.
|
|
|
|
// Performs a single swap from USDC to ETH without wrapping or unwrapping
|
|
//
|
|
// USDC ───(USV4)──> ETH
|
|
//
|
|
deal(USDC_ADDR, ALICE, 3000_000000);
|
|
uint256 balanceBefore = ALICE.balance;
|
|
|
|
// Approve permit2
|
|
vm.startPrank(ALICE);
|
|
IERC20(USDC_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
|
|
|
// Encoded solution generated using `test_single_encoding_strategy_usv4_eth_out`
|
|
(bool success,) = tychoRouterAddr.call(
|
|
hex"7c55384600000000000000000000000000000000000000000000000000000000b2d05e00000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f81490b4f29aade000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000b2d05e00000000000000000000000000000000000000000000000000000000006828a8d900000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d0139500000000000000000000000000000000000000000000000000000000680122e1000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041bf1373f3d3943e0865f8081b1569b4deb66b56b8690500c4c9f1c1f7e1299510720e3d4c92abf6ec75f0b14a87b92957fd43408562f26b8616857469f94012e21b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007300710001000000f62849f9a0b5bf2913b396098f7c7019b51a820aa0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000002cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000000bb800003c00000000000000000000000000"
|
|
);
|
|
|
|
vm.stopPrank();
|
|
|
|
uint256 balanceAfter = ALICE.balance;
|
|
|
|
assertTrue(success, "Call Failed");
|
|
console.logUint(balanceAfter - balanceBefore);
|
|
assertEq(balanceAfter - balanceBefore, 1474406268748155809);
|
|
}
|
|
|
|
function testSingleEkuboIntegration() public {
|
|
vm.stopPrank();
|
|
|
|
deal(ALICE, 1 ether);
|
|
uint256 balanceBefore = IERC20(USDC_ADDR).balanceOf(ALICE);
|
|
|
|
// Approve permit2
|
|
vm.startPrank(ALICE);
|
|
// Encoded solution generated using `test_single_encoding_strategy_ekubo`
|
|
(bool success,) = address(tychoRouter).call{value: 1 ether}(
|
|
hex"20144a070000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000003e800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000071a0cb889707d426a7a386870a03bc70d1b069759805cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4851d02a5948496a67827242eabc5725531342527c000000000000000000000000000000000000000000000000000000"
|
|
);
|
|
|
|
uint256 balanceAfter = IERC20(USDC_ADDR).balanceOf(ALICE);
|
|
|
|
assertTrue(success, "Call Failed");
|
|
assertGe(balanceAfter - balanceBefore, 26173932);
|
|
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
|
}
|
|
|
|
function testSingleCurveIntegration() public {
|
|
deal(UWU_ADDR, ALICE, 1 ether);
|
|
|
|
vm.startPrank(ALICE);
|
|
IERC20(UWU_ADDR).approve(tychoRouterAddr, type(uint256).max);
|
|
// Encoded solution generated using `test_single_encoding_strategy_curve`
|
|
(bool success,) = tychoRouterAddr.call(
|
|
hex"20144a070000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000055c08ca52497e2f1534b59e2917bf524d4765257000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000691d1499e622d69689cdf9004d05ec547d650ff21155c08ca52497e2f1534b59e2917bf524d4765257c02aaa39b223fe8d0a0e5c4f27ead9083c756cc277146b0a1d08b6844376df6d9da99ba7f1b19e710201000103cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000000000"
|
|
);
|
|
|
|
assertTrue(success, "Call Failed");
|
|
assertEq(IERC20(WETH_ADDR).balanceOf(ALICE), 2877855391767);
|
|
|
|
vm.stopPrank();
|
|
}
|
|
|
|
function testSingleSwapUSV3Permit2() public {
|
|
// Trade 1 WETH for DAI with 1 swap on Uniswap V3 using Permit2
|
|
// Tests entire USV3 flow including callback
|
|
// 1 WETH -> DAI
|
|
// (USV3)
|
|
vm.startPrank(ALICE);
|
|
uint256 amountIn = 10 ** 18;
|
|
deal(WETH_ADDR, ALICE, amountIn);
|
|
(
|
|
IAllowanceTransfer.PermitSingle memory permitSingle,
|
|
bytes memory signature
|
|
) = handlePermit2Approval(WETH_ADDR, tychoRouterAddr, amountIn);
|
|
|
|
uint256 expAmountOut = 1205_128428842122129186; //Swap 1 WETH for 1205.12 DAI
|
|
bool zeroForOne = false;
|
|
bytes memory protocolData = encodeUniswapV3Swap(
|
|
WETH_ADDR,
|
|
DAI_ADDR,
|
|
ALICE,
|
|
DAI_WETH_USV3,
|
|
zeroForOne,
|
|
TokenTransfer.TransferType.TRANSFER_PERMIT2_TO_PROTOCOL
|
|
);
|
|
bytes memory swap =
|
|
encodeSingleSwap(address(usv3Executor), protocolData);
|
|
|
|
tychoRouter.singleSwapPermit2(
|
|
amountIn,
|
|
WETH_ADDR,
|
|
DAI_ADDR,
|
|
expAmountOut - 1,
|
|
false,
|
|
false,
|
|
ALICE,
|
|
permitSingle,
|
|
signature,
|
|
swap
|
|
);
|
|
|
|
uint256 finalBalance = IERC20(DAI_ADDR).balanceOf(ALICE);
|
|
assertGe(finalBalance, expAmountOut);
|
|
|
|
vm.stopPrank();
|
|
}
|
|
}
|