feat: add uniswapV3SwapCallback in USV3 executor

This commit is contained in:
royvardhan
2025-02-15 00:34:07 +05:30
parent cccb252bf2
commit 9d3b96f997
8 changed files with 218 additions and 246 deletions

View File

@@ -96,13 +96,11 @@ contract ExecutionDispatcher {
revert ExecutionDispatcher__UnapprovedExecutor(); revert ExecutionDispatcher__UnapprovedExecutor();
} }
selector = selector == bytes4(0) selector =
? ICallback.handleCallback.selector selector == bytes4(0) ? ICallback.handleCallback.selector : selector;
: selector;
// slither-disable-next-line controlled-delegatecall,low-level-calls // slither-disable-next-line controlled-delegatecall,low-level-calls
(bool success, bytes memory result) = executor.delegatecall( (bool success, bytes memory result) =
abi.encodeWithSelector(selector, data) executor.delegatecall(abi.encodeWithSelector(selector, data));
);
if (!success) { if (!success) {
revert( revert(

View File

@@ -57,21 +57,16 @@ contract TychoRouter is
uint256 public fee; uint256 public fee;
event Withdrawal( event Withdrawal(
address indexed token, address indexed token, uint256 amount, address indexed receiver
uint256 amount,
address indexed receiver
); );
event FeeReceiverSet( event FeeReceiverSet(
address indexed oldFeeReceiver, address indexed oldFeeReceiver, address indexed newFeeReceiver
address indexed newFeeReceiver
); );
event FeeSet(uint256 indexed oldFee, uint256 indexed newFee); event FeeSet(uint256 indexed oldFee, uint256 indexed newFee);
constructor( constructor(IPoolManager _poolManager, address _permit2, address weth)
IPoolManager _poolManager, SafeCallback(_poolManager)
address _permit2, {
address weth
) SafeCallback(_poolManager) {
if (_permit2 == address(0) || weth == address(0)) { if (_permit2 == address(0) || weth == address(0)) {
revert TychoRouter__AddressZero(); revert TychoRouter__AddressZero();
} }
@@ -185,11 +180,10 @@ contract TychoRouter is
* *
* @return The total amount of the buy token obtained after all swaps have been executed. * @return The total amount of the buy token obtained after all swaps have been executed.
*/ */
function _swap( function _swap(uint256 amountIn, uint256 nTokens, bytes calldata swaps_)
uint256 amountIn, internal
uint256 nTokens, returns (uint256)
bytes calldata swaps_ {
) internal returns (uint256) {
if (swaps_.length == 0) { if (swaps_.length == 0) {
revert TychoRouter__EmptySwaps(); revert TychoRouter__EmptySwaps();
} }
@@ -256,10 +250,10 @@ contract TychoRouter is
/** /**
* @dev Allows granting roles to multiple accounts in a single call. * @dev Allows granting roles to multiple accounts in a single call.
*/ */
function batchGrantRole( function batchGrantRole(bytes32 role, address[] memory accounts)
bytes32 role, external
address[] memory accounts onlyRole(DEFAULT_ADMIN_ROLE)
) external onlyRole(DEFAULT_ADMIN_ROLE) { {
for (uint256 i = 0; i < accounts.length; i++) { for (uint256 i = 0; i < accounts.length; i++) {
_grantRole(role, accounts[i]); _grantRole(role, accounts[i]);
} }
@@ -269,9 +263,10 @@ contract TychoRouter is
* @dev Entrypoint to add or replace an approved executor contract address * @dev Entrypoint to add or replace an approved executor contract address
* @param targets address of the executor contract * @param targets address of the executor contract
*/ */
function setExecutors( function setExecutors(address[] memory targets)
address[] memory targets external
) external onlyRole(EXECUTOR_SETTER_ROLE) { onlyRole(EXECUTOR_SETTER_ROLE)
{
for (uint256 i = 0; i < targets.length; i++) { for (uint256 i = 0; i < targets.length; i++) {
_setExecutor(targets[i]); _setExecutor(targets[i]);
} }
@@ -281,18 +276,20 @@ contract TychoRouter is
* @dev Entrypoint to remove an approved executor contract address * @dev Entrypoint to remove an approved executor contract address
* @param target address of the executor contract * @param target address of the executor contract
*/ */
function removeExecutor( function removeExecutor(address target)
address target external
) external onlyRole(EXECUTOR_SETTER_ROLE) { onlyRole(EXECUTOR_SETTER_ROLE)
{
_removeExecutor(target); _removeExecutor(target);
} }
/** /**
* @dev Allows setting the fee receiver. * @dev Allows setting the fee receiver.
*/ */
function setFeeReceiver( function setFeeReceiver(address newfeeReceiver)
address newfeeReceiver external
) external onlyRole(FEE_SETTER_ROLE) { onlyRole(FEE_SETTER_ROLE)
{
if (newfeeReceiver == address(0)) revert TychoRouter__AddressZero(); if (newfeeReceiver == address(0)) revert TychoRouter__AddressZero();
emit FeeReceiverSet(feeReceiver, newfeeReceiver); emit FeeReceiverSet(feeReceiver, newfeeReceiver);
feeReceiver = newfeeReceiver; feeReceiver = newfeeReceiver;
@@ -309,10 +306,10 @@ contract TychoRouter is
/** /**
* @dev Allows withdrawing any ERC20 funds if funds get stuck in case of a bug. * @dev Allows withdrawing any ERC20 funds if funds get stuck in case of a bug.
*/ */
function withdraw( function withdraw(IERC20[] memory tokens, address receiver)
IERC20[] memory tokens, external
address receiver onlyRole(FUND_RESCUER_ROLE)
) external onlyRole(FUND_RESCUER_ROLE) { {
if (receiver == address(0)) revert TychoRouter__AddressZero(); if (receiver == address(0)) revert TychoRouter__AddressZero();
for (uint256 i = 0; i < tokens.length; i++) { for (uint256 i = 0; i < tokens.length; i++) {
@@ -329,9 +326,10 @@ contract TychoRouter is
* @dev Allows withdrawing any NATIVE funds if funds get stuck in case of a bug. * @dev Allows withdrawing any NATIVE funds if funds get stuck in case of a bug.
* The contract should never hold any NATIVE tokens for security reasons. * The contract should never hold any NATIVE tokens for security reasons.
*/ */
function withdrawNative( function withdrawNative(address receiver)
address receiver external
) external onlyRole(FUND_RESCUER_ROLE) { onlyRole(FUND_RESCUER_ROLE)
{
if (receiver == address(0)) revert TychoRouter__AddressZero(); if (receiver == address(0)) revert TychoRouter__AddressZero();
uint256 amount = address(this).balance; uint256 amount = address(this).balance;
@@ -359,9 +357,8 @@ contract TychoRouter is
* @param amount of WETH to unwrap. * @param amount of WETH to unwrap.
*/ */
function _unwrapETH(uint256 amount) internal { function _unwrapETH(uint256 amount) internal {
uint256 unwrapAmount = amount == 0 uint256 unwrapAmount =
? _weth.balanceOf(address(this)) amount == 0 ? _weth.balanceOf(address(this)) : amount;
: amount;
_weth.withdraw(unwrapAmount); _weth.withdraw(unwrapAmount);
} }
@@ -380,14 +377,15 @@ contract TychoRouter is
bytes calldata msgData bytes calldata msgData
) external { ) external {
_handleCallback( _handleCallback(
bytes4(0), bytes4(0), abi.encodePacked(amount0Delta, amount1Delta, msgData)
abi.encodePacked(amount0Delta, amount1Delta, msgData)
); );
} }
function _unlockCallback( function _unlockCallback(bytes calldata data)
bytes calldata data internal
) internal override returns (bytes memory) { override
returns (bytes memory)
{
require(data.length >= 20, "Invalid data length"); require(data.length >= 20, "Invalid data length");
bytes4 selector = bytes4(data[data.length - 24:data.length - 20]); bytes4 selector = bytes4(data[data.length - 24:data.length - 20]);
address executor = address(uint160(bytes20(data[data.length - 20:]))); address executor = address(uint160(bytes20(data[data.length - 20:])));
@@ -398,7 +396,7 @@ contract TychoRouter is
} }
// slither-disable-next-line controlled-delegatecall,low-level-calls // slither-disable-next-line controlled-delegatecall,low-level-calls
(bool success, ) = executor.delegatecall( (bool success,) = executor.delegatecall(
abi.encodeWithSelector(selector, protocolData) abi.encodeWithSelector(selector, protocolData)
); );
require(success, "delegatecall to uniswap v4 callback failed"); require(success, "delegatecall to uniswap v4 callback failed");

View File

@@ -2,7 +2,10 @@
pragma solidity ^0.8.26; pragma solidity ^0.8.26;
import "@interfaces/IExecutor.sol"; import "@interfaces/IExecutor.sol";
import {IERC20, SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {
IERC20,
SafeERC20
} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
// slither-disable-next-line solc-version // slither-disable-next-line solc-version
import {IAsset} from "@balancer-labs/v2-interfaces/contracts/vault/IAsset.sol"; import {IAsset} from "@balancer-labs/v2-interfaces/contracts/vault/IAsset.sol";
// slither-disable-next-line solc-version // slither-disable-next-line solc-version
@@ -16,10 +19,11 @@ contract BalancerV2Executor is IExecutor {
address private constant VAULT = 0xBA12222222228d8Ba445958a75a0704d566BF2C8; address private constant VAULT = 0xBA12222222228d8Ba445958a75a0704d566BF2C8;
// slither-disable-next-line locked-ether // slither-disable-next-line locked-ether
function swap( function swap(uint256 givenAmount, bytes calldata data)
uint256 givenAmount, external
bytes calldata data payable
) external payable returns (uint256 calculatedAmount) { returns (uint256 calculatedAmount)
{
( (
IERC20 tokenIn, IERC20 tokenIn,
IERC20 tokenOut, IERC20 tokenOut,
@@ -51,17 +55,11 @@ contract BalancerV2Executor is IExecutor {
uint256 limit = 0; uint256 limit = 0;
calculatedAmount = IVault(VAULT).swap( calculatedAmount =
singleSwap, IVault(VAULT).swap(singleSwap, funds, limit, block.timestamp);
funds,
limit,
block.timestamp
);
} }
function _decodeData( function _decodeData(bytes calldata data)
bytes calldata data
)
internal internal
pure pure
returns ( returns (

View File

@@ -11,10 +11,11 @@ contract UniswapV2Executor is IExecutor {
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
// slither-disable-next-line locked-ether // slither-disable-next-line locked-ether
function swap( function swap(uint256 givenAmount, bytes calldata data)
uint256 givenAmount, external
bytes calldata data payable
) external payable returns (uint256 calculatedAmount) { returns (uint256 calculatedAmount)
{
address target; address target;
address receiver; address receiver;
bool zeroForOne; bool zeroForOne;
@@ -32,9 +33,7 @@ contract UniswapV2Executor is IExecutor {
} }
} }
function _decodeData( function _decodeData(bytes calldata data)
bytes calldata data
)
internal internal
pure pure
returns ( returns (
@@ -53,20 +52,20 @@ contract UniswapV2Executor is IExecutor {
zeroForOne = uint8(data[60]) > 0; zeroForOne = uint8(data[60]) > 0;
} }
function _getAmountOut( function _getAmountOut(address target, uint256 amountIn, bool zeroForOne)
address target, internal
uint256 amountIn, view
bool zeroForOne returns (uint256 amount)
) internal view returns (uint256 amount) { {
IUniswapV2Pair pair = IUniswapV2Pair(target); IUniswapV2Pair pair = IUniswapV2Pair(target);
uint112 reserveIn; uint112 reserveIn;
uint112 reserveOut; uint112 reserveOut;
if (zeroForOne) { if (zeroForOne) {
// slither-disable-next-line unused-return // slither-disable-next-line unused-return
(reserveIn, reserveOut, ) = pair.getReserves(); (reserveIn, reserveOut,) = pair.getReserves();
} else { } else {
// slither-disable-next-line unused-return // slither-disable-next-line unused-return
(reserveOut, reserveIn, ) = pair.getReserves(); (reserveOut, reserveIn,) = pair.getReserves();
} }
require(reserveIn > 0 && reserveOut > 0, "L"); require(reserveIn > 0 && reserveOut > 0, "L");

View File

@@ -5,11 +5,13 @@ import "@interfaces/IExecutor.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol"; import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import "@uniswap/v3-updated/CallbackValidationV2.sol"; import "@uniswap/v3-updated/CallbackValidationV2.sol";
import "@interfaces/ICallback.sol";
import "forge-std/console.sol";
error UniswapV3Executor__InvalidDataLength(); error UniswapV3Executor__InvalidDataLength();
error UniswapV3Executor__InvalidFactory(); error UniswapV3Executor__InvalidFactory();
contract UniswapV3Executor is IExecutor { contract UniswapV3Executor is IExecutor, ICallback {
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
uint160 private constant MIN_SQRT_RATIO = 4295128739; uint160 private constant MIN_SQRT_RATIO = 4295128739;
@@ -28,10 +30,11 @@ contract UniswapV3Executor is IExecutor {
} }
// slither-disable-next-line locked-ether // slither-disable-next-line locked-ether
function swap( function swap(uint256 amountIn, bytes calldata data)
uint256 amountIn, external
bytes calldata data payable
) external payable returns (uint256 amountOut) { returns (uint256 amountOut)
{
( (
address tokenIn, address tokenIn,
address tokenOut, address tokenOut,
@@ -64,36 +67,53 @@ contract UniswapV3Executor is IExecutor {
} }
} }
function handleCallback( function uniswapV3SwapCallback(
bytes calldata msgData int256 amount0Delta,
) external returns (bytes memory result) { int256 amount1Delta,
(int256 amount0Delta, int256 amount1Delta) = abi.decode( bytes calldata data
msgData[:64], ) external {
(int256, int256) // slither-disable-next-line low-level-calls
(bool success, bytes memory result) = self.delegatecall(
abi.encodeWithSelector(
ICallback.handleCallback.selector,
abi.encodePacked(
amount0Delta, amount1Delta, data[:data.length - 20]
)
)
); );
if (!success) {
revert(
string(
result.length > 0
? result
: abi.encodePacked("Callback failed")
)
);
}
}
function handleCallback(bytes calldata msgData)
external
returns (bytes memory result)
{
(int256 amount0Delta, int256 amount1Delta) =
abi.decode(msgData[:64], (int256, int256));
address tokenIn = address(bytes20(msgData[64:84])); address tokenIn = address(bytes20(msgData[64:84]));
address tokenOut = address(bytes20(msgData[84:104])); address tokenOut = address(bytes20(msgData[84:104]));
uint24 poolFee = uint24(bytes3(msgData[104:107])); uint24 poolFee = uint24(bytes3(msgData[104:107]));
CallbackValidationV2.verifyCallback( // slither-disable-next-line unused-return
factory, CallbackValidationV2.verifyCallback(factory, tokenIn, tokenOut, poolFee);
tokenIn,
tokenOut,
poolFee
);
uint256 amountOwed = amount0Delta > 0 uint256 amountOwed =
? uint256(amount0Delta) amount0Delta > 0 ? uint256(amount0Delta) : uint256(amount1Delta);
: uint256(amount1Delta);
IERC20(tokenIn).safeTransfer(msg.sender, amountOwed); IERC20(tokenIn).safeTransfer(msg.sender, amountOwed);
return abi.encode(amountOwed, tokenIn); return abi.encode(amountOwed, tokenIn);
} }
function _decodeData( function _decodeData(bytes calldata data)
bytes calldata data
)
internal internal
pure pure
returns ( returns (
@@ -116,11 +136,11 @@ contract UniswapV3Executor is IExecutor {
zeroForOne = uint8(data[83]) > 0; zeroForOne = uint8(data[83]) > 0;
} }
function _makeV3CallbackData( function _makeV3CallbackData(address tokenIn, address tokenOut, uint24 fee)
address tokenIn, internal
address tokenOut, view
uint24 fee returns (bytes memory)
) internal view returns (bytes memory) { {
return abi.encodePacked(tokenIn, tokenOut, fee, self); return abi.encodePacked(tokenIn, tokenOut, fee, self);
} }
} }

View File

@@ -2,9 +2,14 @@
pragma solidity ^0.8.26; pragma solidity ^0.8.26;
import "@interfaces/IExecutor.sol"; import "@interfaces/IExecutor.sol";
import {IERC20, SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {
IERC20,
SafeERC20
} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol"; import {
Currency, CurrencyLibrary
} from "@uniswap/v4-core/src/types/Currency.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol"; import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {V4Router} from "@uniswap/v4-periphery/src/V4Router.sol"; import {V4Router} from "@uniswap/v4-periphery/src/V4Router.sol";
@@ -18,16 +23,13 @@ contract UniswapV4Executor is IExecutor, V4Router {
constructor(IPoolManager _poolManager) V4Router(_poolManager) {} constructor(IPoolManager _poolManager) V4Router(_poolManager) {}
function swap( function swap(uint256, bytes calldata data)
uint256, external
bytes calldata data payable
) external payable returns (uint256 calculatedAmount) { returns (uint256 calculatedAmount)
( {
address tokenIn, (address tokenIn, address tokenOut, bool isExactInput, uint256 amount) =
address tokenOut, _decodeData(data);
bool isExactInput,
uint256 amount
) = _decodeData(data);
uint256 tokenOutBalanceBefore; uint256 tokenOutBalanceBefore;
uint256 tokenInBalanceBefore; uint256 tokenInBalanceBefore;
@@ -62,9 +64,7 @@ contract UniswapV4Executor is IExecutor, V4Router {
return calculatedAmount; return calculatedAmount;
} }
function _decodeData( function _decodeData(bytes calldata data)
bytes calldata data
)
internal internal
pure pure
returns ( returns (
@@ -74,19 +74,15 @@ contract UniswapV4Executor is IExecutor, V4Router {
uint256 amount uint256 amount
) )
{ {
(bytes memory actions, bytes[] memory params) = abi.decode( (bytes memory actions, bytes[] memory params) =
data, abi.decode(data, (bytes, bytes[]));
(bytes, bytes[])
);
// First byte of actions determines the swap type // First byte of actions determines the swap type
uint8 action = uint8(bytes1(actions[0])); uint8 action = uint8(bytes1(actions[0]));
if (action == uint8(Actions.SWAP_EXACT_IN_SINGLE)) { if (action == uint8(Actions.SWAP_EXACT_IN_SINGLE)) {
IV4Router.ExactInputSingleParams memory swapParams = abi.decode( IV4Router.ExactInputSingleParams memory swapParams =
params[0], abi.decode(params[0], (IV4Router.ExactInputSingleParams));
(IV4Router.ExactInputSingleParams)
);
tokenIn = swapParams.zeroForOne tokenIn = swapParams.zeroForOne
? address(uint160(swapParams.poolKey.currency0.toId())) ? address(uint160(swapParams.poolKey.currency0.toId()))
@@ -97,10 +93,8 @@ contract UniswapV4Executor is IExecutor, V4Router {
isExactInput = true; isExactInput = true;
amount = swapParams.amountIn; amount = swapParams.amountIn;
} else if (action == uint8(Actions.SWAP_EXACT_OUT_SINGLE)) { } else if (action == uint8(Actions.SWAP_EXACT_OUT_SINGLE)) {
IV4Router.ExactOutputSingleParams memory swapParams = abi.decode( IV4Router.ExactOutputSingleParams memory swapParams =
params[0], abi.decode(params[0], (IV4Router.ExactOutputSingleParams));
(IV4Router.ExactOutputSingleParams)
);
tokenIn = swapParams.zeroForOne tokenIn = swapParams.zeroForOne
? address(uint160(swapParams.poolKey.currency0.toId())) ? address(uint160(swapParams.poolKey.currency0.toId()))
@@ -111,23 +105,18 @@ contract UniswapV4Executor is IExecutor, V4Router {
isExactInput = false; isExactInput = false;
amount = swapParams.amountOut; amount = swapParams.amountOut;
} else if (action == uint8(Actions.SWAP_EXACT_IN)) { } else if (action == uint8(Actions.SWAP_EXACT_IN)) {
IV4Router.ExactInputParams memory swapParams = abi.decode( IV4Router.ExactInputParams memory swapParams =
params[0], abi.decode(params[0], (IV4Router.ExactInputParams));
(IV4Router.ExactInputParams)
);
tokenIn = address(uint160(swapParams.currencyIn.toId())); tokenIn = address(uint160(swapParams.currencyIn.toId()));
PathKey memory lastPath = swapParams.path[ PathKey memory lastPath =
swapParams.path.length - 1 swapParams.path[swapParams.path.length - 1];
];
tokenOut = address(uint160(lastPath.intermediateCurrency.toId())); tokenOut = address(uint160(lastPath.intermediateCurrency.toId()));
isExactInput = true; isExactInput = true;
amount = swapParams.amountIn; amount = swapParams.amountIn;
} else if (action == uint8(Actions.SWAP_EXACT_OUT)) { } else if (action == uint8(Actions.SWAP_EXACT_OUT)) {
IV4Router.ExactOutputParams memory swapParams = abi.decode( IV4Router.ExactOutputParams memory swapParams =
params[0], abi.decode(params[0], (IV4Router.ExactOutputParams));
(IV4Router.ExactOutputParams)
);
PathKey memory firstPath = swapParams.path[0]; PathKey memory firstPath = swapParams.path[0];
tokenIn = address(uint160(firstPath.intermediateCurrency.toId())); tokenIn = address(uint160(firstPath.intermediateCurrency.toId()));
@@ -137,14 +126,12 @@ contract UniswapV4Executor is IExecutor, V4Router {
} }
} }
function _pay( function _pay(Currency token, address payer, uint256 amount)
Currency token, internal
address payer, override
uint256 amount {
) internal override {
IERC20(Currency.unwrap(token)).safeTransfer( IERC20(Currency.unwrap(token)).safeTransfer(
address(poolManager), address(poolManager), amount
amount
); );
} }

View File

@@ -18,9 +18,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
event CallbackVerifierSet(address indexed callbackVerifier); event CallbackVerifierSet(address indexed callbackVerifier);
event Withdrawal( event Withdrawal(
address indexed token, address indexed token, uint256 amount, address indexed receiver
uint256 amount,
address indexed receiver
); );
function testSetExecutorsValidRole() public { function testSetExecutorsValidRole() public {
@@ -216,10 +214,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
deal(WETH_ADDR, tychoRouterAddr, amountIn); deal(WETH_ADDR, tychoRouterAddr, amountIn);
bytes memory protocolData = encodeUniswapV2Swap( bytes memory protocolData = encodeUniswapV2Swap(
WETH_ADDR, WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
WETH_DAI_POOL,
tychoRouterAddr,
false
); );
bytes memory swap = encodeSwap( bytes memory swap = encodeSwap(
@@ -256,10 +251,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
address(usv2Executor), address(usv2Executor),
bytes4(0), bytes4(0),
encodeUniswapV2Swap( encodeUniswapV2Swap(
WETH_ADDR, WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
WETH_DAI_POOL,
tychoRouterAddr,
false
) )
); );
@@ -298,10 +290,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
address(usv2Executor), address(usv2Executor),
bytes4(0), bytes4(0),
encodeUniswapV2Swap( encodeUniswapV2Swap(
WETH_ADDR, WETH_ADDR, WETH_WBTC_POOL, tychoRouterAddr, false
WETH_WBTC_POOL,
tychoRouterAddr,
false
) )
); );
// WBTC -> USDC // WBTC -> USDC
@@ -312,10 +301,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
address(usv2Executor), address(usv2Executor),
bytes4(0), bytes4(0),
encodeUniswapV2Swap( encodeUniswapV2Swap(
WBTC_ADDR, WBTC_ADDR, USDC_WBTC_POOL, tychoRouterAddr, true
USDC_WBTC_POOL,
tychoRouterAddr,
true
) )
); );
// WETH -> DAI // WETH -> DAI
@@ -326,10 +312,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
address(usv2Executor), address(usv2Executor),
bytes4(0), bytes4(0),
encodeUniswapV2Swap( encodeUniswapV2Swap(
WETH_ADDR, WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
WETH_DAI_POOL,
tychoRouterAddr,
false
) )
); );
@@ -365,10 +348,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
) = handlePermit2Approval(WETH_ADDR, amountIn); ) = handlePermit2Approval(WETH_ADDR, amountIn);
bytes memory protocolData = encodeUniswapV2Swap( bytes memory protocolData = encodeUniswapV2Swap(
WETH_ADDR, WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
WETH_DAI_POOL,
tychoRouterAddr,
false
); );
bytes memory swap = encodeSwap( bytes memory swap = encodeSwap(
@@ -421,10 +401,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
) = handlePermit2Approval(WETH_ADDR, amountIn); ) = handlePermit2Approval(WETH_ADDR, amountIn);
bytes memory protocolData = encodeUniswapV2Swap( bytes memory protocolData = encodeUniswapV2Swap(
WETH_ADDR, WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
WETH_DAI_POOL,
tychoRouterAddr,
false
); );
bytes memory swap = encodeSwap( bytes memory swap = encodeSwap(
@@ -483,10 +460,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
) = handlePermit2Approval(WETH_ADDR, amountIn); ) = handlePermit2Approval(WETH_ADDR, amountIn);
bytes memory protocolData = encodeUniswapV2Swap( bytes memory protocolData = encodeUniswapV2Swap(
WETH_ADDR, WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
WETH_DAI_POOL,
tychoRouterAddr,
false
); );
bytes memory swap = encodeSwap( bytes memory swap = encodeSwap(
@@ -518,10 +492,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
assertEq(amountOut, expectedAmount); assertEq(amountOut, expectedAmount);
uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(ALICE); uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(ALICE);
assertEq(daiBalance, expectedAmount); assertEq(daiBalance, expectedAmount);
assertEq( assertEq(IERC20(DAI_ADDR).balanceOf(FEE_RECEIVER), 26598819248184436997);
IERC20(DAI_ADDR).balanceOf(FEE_RECEIVER),
26598819248184436997
);
vm.stopPrank(); vm.stopPrank();
} }
@@ -534,8 +505,8 @@ contract TychoRouterTest is TychoRouterTestSetup {
vm.startPrank(ALICE); vm.startPrank(ALICE);
IAllowanceTransfer.PermitSingle IAllowanceTransfer.PermitSingle memory emptyPermitSingle =
memory emptyPermitSingle = IAllowanceTransfer.PermitSingle({ IAllowanceTransfer.PermitSingle({
details: IAllowanceTransfer.PermitDetails({ details: IAllowanceTransfer.PermitDetails({
token: address(0), token: address(0),
amount: 0, amount: 0,
@@ -546,10 +517,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
sigDeadline: 0 sigDeadline: 0
}); });
bytes memory protocolData = encodeUniswapV2Swap( bytes memory protocolData = encodeUniswapV2Swap(
WETH_ADDR, WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
WETH_DAI_POOL,
tychoRouterAddr,
false
); );
bytes memory swap = encodeSwap( bytes memory swap = encodeSwap(
@@ -598,12 +566,8 @@ contract TychoRouterTest is TychoRouterTestSetup {
bytes memory signature bytes memory signature
) = handlePermit2Approval(DAI_ADDR, amountIn); ) = handlePermit2Approval(DAI_ADDR, amountIn);
bytes memory protocolData = encodeUniswapV2Swap( bytes memory protocolData =
DAI_ADDR, encodeUniswapV2Swap(DAI_ADDR, WETH_DAI_POOL, tychoRouterAddr, true);
WETH_DAI_POOL,
tychoRouterAddr,
true
);
bytes memory swap = encodeSwap( bytes memory swap = encodeSwap(
uint8(0), uint8(0),
@@ -647,11 +611,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
uint256 expAmountOut = 1205_128428842122129186; //Swap 1 WETH for 1205.12 DAI uint256 expAmountOut = 1205_128428842122129186; //Swap 1 WETH for 1205.12 DAI
bool zeroForOne = false; bool zeroForOne = false;
bytes memory protocolData = encodeUniswapV3Swap( bytes memory protocolData = encodeUniswapV3Swap(
WETH_ADDR, WETH_ADDR, DAI_ADDR, tychoRouterAddr, DAI_WETH_USV3, zeroForOne
DAI_ADDR,
tychoRouterAddr,
DAI_WETH_USV3,
zeroForOne
); );
bytes memory swap = encodeSwap( bytes memory swap = encodeSwap(
uint8(0), uint8(0),
@@ -693,7 +653,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
// but manually replacing the executor address // but manually replacing the executor address
// `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test // `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test
// `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f` // `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f`
(bool success, ) = tychoRouterAddr.call( (bool success,) = tychoRouterAddr.call(
hex"4860f9ed0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d481bb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfbc3000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041f2740fde9662d8bc1f8fe8e8fc29447c1832d625f06f4a56ee5103ad555c12323af5d50eb840f73d17873383ae3b7573956d5df7b2bf76bddba768c2837894a51b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000" hex"4860f9ed0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d481bb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfbc3000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041f2740fde9662d8bc1f8fe8e8fc29447c1832d625f06f4a56ee5103ad555c12323af5d50eb840f73d17873383ae3b7573956d5df7b2bf76bddba768c2837894a51b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000"
); );
@@ -722,7 +682,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
// but manually replacing the executor address // but manually replacing the executor address
// `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test // `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test
// `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f` // `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f`
(bool success, ) = tychoRouterAddr.call{value: 1 ether}( (bool success,) = tychoRouterAddr.call{value: 1 ether}(
hex"4860f9ed0000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d4806b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfa73000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041c36406a750c499ac7f79f7666650f0d4f20fc27bb49ab68121c0be6554cb5cab6caf90dc3aab2e21083a8fa46976521a1e9df41ce74be59abf03e0d3691541e91c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00020000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000" hex"4860f9ed0000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d4806b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfa73000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041c36406a750c499ac7f79f7666650f0d4f20fc27bb49ab68121c0be6554cb5cab6caf90dc3aab2e21083a8fa46976521a1e9df41ce74be59abf03e0d3691541e91c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00020000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000"
); );
@@ -751,7 +711,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
// but manually replacing the executor address // but manually replacing the executor address
// `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test // `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test
// `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f` // `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f`
(bool success, ) = tychoRouterAddr.call( (bool success,) = tychoRouterAddr.call(
hex"4860f9ed0000000000000000000000000000000000000000000000a2a15d09519be000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000a2a15d09519be000000000000000000000000000000000000000000000000000000000000067d4809800000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfaa000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000004146411c70ec7fee0d5d260803cb220f5365792426c5d94f7a0a4d37abb05205752c5418b1fadd059570a71f0911814e546728e1f21876f2a1c6d38d34bd235fd61c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625ab6b175474e89094c44da98b954eedeac495271d0fa478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950100000000" hex"4860f9ed0000000000000000000000000000000000000000000000a2a15d09519be000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000a2a15d09519be000000000000000000000000000000000000000000000000000000000000067d4809800000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfaa000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000004146411c70ec7fee0d5d260803cb220f5365792426c5d94f7a0a4d37abb05205752c5418b1fadd059570a71f0911814e546728e1f21876f2a1c6d38d34bd235fd61c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625ab6b175474e89094c44da98b954eedeac495271d0fa478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950100000000"
); );
@@ -782,7 +742,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
// but manually replacing the executor address // but manually replacing the executor address
// `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test // `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test
// `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f` // `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f`
(bool success, ) = tychoRouterAddr.call( (bool success,) = tychoRouterAddr.call(
hex"4860f9ed0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d4810d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfb15000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041ecaab75f0791c9683b001ea2f0e01a0a6aaf03e6e49c83e9c8a8e588a38e3be9230d962926628ffbf6a5370cda559ff0e7876a63ed38eebe33dbef5b5e2e46ef1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000170005a00028000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d0139500005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d9403ede3eca2a72b3aecc820e955b36f38437d0139500005a02030000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625ab6b175474e89094c44da98b954eedeac495271d0fae461ca67b15dc8dc81ce7615e0320da1a9ab8d53ede3eca2a72b3aecc820e955b36f38437d0139501005a01030000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625ab2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a4163ede3eca2a72b3aecc820e955b36f38437d013950100000000000000000000000000000000" hex"4860f9ed0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d4810d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfb15000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041ecaab75f0791c9683b001ea2f0e01a0a6aaf03e6e49c83e9c8a8e588a38e3be9230d962926628ffbf6a5370cda559ff0e7876a63ed38eebe33dbef5b5e2e46ef1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000170005a00028000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d0139500005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d9403ede3eca2a72b3aecc820e955b36f38437d0139500005a02030000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625ab6b175474e89094c44da98b954eedeac495271d0fae461ca67b15dc8dc81ce7615e0320da1a9ab8d53ede3eca2a72b3aecc820e955b36f38437d0139501005a01030000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625ab2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a4163ede3eca2a72b3aecc820e955b36f38437d013950100000000000000000000000000000000"
); );
@@ -812,10 +772,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
) = handlePermit2Approval(WETH_ADDR, amountIn); ) = handlePermit2Approval(WETH_ADDR, amountIn);
bytes memory protocolData = encodeUniswapV2Swap( bytes memory protocolData = encodeUniswapV2Swap(
WETH_ADDR, WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
WETH_DAI_POOL,
tychoRouterAddr,
false
); );
bytes memory swap = encodeSwap( bytes memory swap = encodeSwap(
@@ -832,8 +789,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
vm.expectRevert( vm.expectRevert(
abi.encodeWithSelector( abi.encodeWithSelector(
TychoRouter__AmountInNotFullySpent.selector, TychoRouter__AmountInNotFullySpent.selector, 400000000000000000
400000000000000000
) )
); );
@@ -859,12 +815,7 @@ contract TychoRouterTest is TychoRouterTestSetup {
deal(USDE_ADDR, tychoRouterAddr, amountIn); deal(USDE_ADDR, tychoRouterAddr, amountIn);
bytes memory protocolData = UniswapV4Utils.encodeExactInputSingle( bytes memory protocolData = UniswapV4Utils.encodeExactInputSingle(
USDE_ADDR, USDE_ADDR, USDT_ADDR, 100, true, 1, uint128(amountIn)
USDT_ADDR,
100,
true,
1,
uint128(amountIn)
); );
// add executor and selector for callback // add executor and selector for callback

View File

@@ -8,9 +8,7 @@ import {Constants} from "../Constants.sol";
contract UniswapV3ExecutorExposed is UniswapV3Executor { contract UniswapV3ExecutorExposed is UniswapV3Executor {
constructor(address _factory) UniswapV3Executor(_factory) {} constructor(address _factory) UniswapV3Executor(_factory) {}
function decodeData( function decodeData(bytes calldata data)
bytes calldata data
)
external external
pure pure
returns ( returns (
@@ -44,12 +42,7 @@ contract UniswapV3ExecutorTest is Test, Constants {
function testDecodeParams() public view { function testDecodeParams() public view {
uint24 expectedPoolFee = 500; uint24 expectedPoolFee = 500;
bytes memory data = abi.encodePacked( bytes memory data = abi.encodePacked(
WETH_ADDR, WETH_ADDR, DAI_ADDR, expectedPoolFee, address(2), address(3), false
DAI_ADDR,
expectedPoolFee,
address(2),
address(3),
false
); );
( (
@@ -70,11 +63,8 @@ contract UniswapV3ExecutorTest is Test, Constants {
} }
function testDecodeParamsInvalidDataLength() public { function testDecodeParamsInvalidDataLength() public {
bytes memory invalidParams = abi.encodePacked( bytes memory invalidParams =
WETH_ADDR, abi.encodePacked(WETH_ADDR, address(2), address(3));
address(2),
address(3)
);
vm.expectRevert(UniswapV3Executor__InvalidDataLength.selector); vm.expectRevert(UniswapV3Executor__InvalidDataLength.selector);
uniswapV3Exposed.decodeData(invalidParams); uniswapV3Exposed.decodeData(invalidParams);
@@ -100,4 +90,35 @@ contract UniswapV3ExecutorTest is Test, Constants {
uint256 finalPoolReserve = IERC20(WETH_ADDR).balanceOf(DAI_WETH_USV3); uint256 finalPoolReserve = IERC20(WETH_ADDR).balanceOf(DAI_WETH_USV3);
assertEq(finalPoolReserve - initialPoolReserve, amountOwed); assertEq(finalPoolReserve - initialPoolReserve, amountOwed);
} }
function testSwapWETHForDAI() public {
uint256 amountIn = 10 ** 18;
deal(WETH_ADDR, address(uniswapV3Exposed), amountIn);
uint256 expAmountOut = 1205_128428842122129186; //Swap 1 WETH for 1205.12 DAI
bool zeroForOne = false;
bytes memory data = encodeUniswapV3Swap(
WETH_ADDR, DAI_ADDR, address(this), DAI_WETH_USV3, zeroForOne
);
uint256 amountOut = uniswapV3Exposed.swap(amountIn, data);
assertGe(amountOut, expAmountOut);
assertEq(IERC20(WETH_ADDR).balanceOf(address(uniswapV3Exposed)), 0);
assertGe(IERC20(DAI_ADDR).balanceOf(address(this)), expAmountOut);
}
function encodeUniswapV3Swap(
address tokenIn,
address tokenOut,
address receiver,
address target,
bool zero2one
) internal view returns (bytes memory) {
IUniswapV3Pool pool = IUniswapV3Pool(target);
return abi.encodePacked(
tokenIn, tokenOut, pool.fee(), receiver, target, zero2one
);
}
} }