feat: add uniswapV3SwapCallback in USV3 executor
This commit is contained in:
@@ -96,13 +96,11 @@ contract ExecutionDispatcher {
|
||||
revert ExecutionDispatcher__UnapprovedExecutor();
|
||||
}
|
||||
|
||||
selector = selector == bytes4(0)
|
||||
? ICallback.handleCallback.selector
|
||||
: selector;
|
||||
selector =
|
||||
selector == bytes4(0) ? ICallback.handleCallback.selector : selector;
|
||||
// slither-disable-next-line controlled-delegatecall,low-level-calls
|
||||
(bool success, bytes memory result) = executor.delegatecall(
|
||||
abi.encodeWithSelector(selector, data)
|
||||
);
|
||||
(bool success, bytes memory result) =
|
||||
executor.delegatecall(abi.encodeWithSelector(selector, data));
|
||||
|
||||
if (!success) {
|
||||
revert(
|
||||
|
||||
@@ -57,21 +57,16 @@ contract TychoRouter is
|
||||
uint256 public fee;
|
||||
|
||||
event Withdrawal(
|
||||
address indexed token,
|
||||
uint256 amount,
|
||||
address indexed receiver
|
||||
address indexed token, uint256 amount, address indexed receiver
|
||||
);
|
||||
event FeeReceiverSet(
|
||||
address indexed oldFeeReceiver,
|
||||
address indexed newFeeReceiver
|
||||
address indexed oldFeeReceiver, address indexed newFeeReceiver
|
||||
);
|
||||
event FeeSet(uint256 indexed oldFee, uint256 indexed newFee);
|
||||
|
||||
constructor(
|
||||
IPoolManager _poolManager,
|
||||
address _permit2,
|
||||
address weth
|
||||
) SafeCallback(_poolManager) {
|
||||
constructor(IPoolManager _poolManager, address _permit2, address weth)
|
||||
SafeCallback(_poolManager)
|
||||
{
|
||||
if (_permit2 == address(0) || weth == address(0)) {
|
||||
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.
|
||||
*/
|
||||
function _swap(
|
||||
uint256 amountIn,
|
||||
uint256 nTokens,
|
||||
bytes calldata swaps_
|
||||
) internal returns (uint256) {
|
||||
function _swap(uint256 amountIn, uint256 nTokens, bytes calldata swaps_)
|
||||
internal
|
||||
returns (uint256)
|
||||
{
|
||||
if (swaps_.length == 0) {
|
||||
revert TychoRouter__EmptySwaps();
|
||||
}
|
||||
@@ -256,10 +250,10 @@ contract TychoRouter is
|
||||
/**
|
||||
* @dev Allows granting roles to multiple accounts in a single call.
|
||||
*/
|
||||
function batchGrantRole(
|
||||
bytes32 role,
|
||||
address[] memory accounts
|
||||
) external onlyRole(DEFAULT_ADMIN_ROLE) {
|
||||
function batchGrantRole(bytes32 role, address[] memory accounts)
|
||||
external
|
||||
onlyRole(DEFAULT_ADMIN_ROLE)
|
||||
{
|
||||
for (uint256 i = 0; i < accounts.length; i++) {
|
||||
_grantRole(role, accounts[i]);
|
||||
}
|
||||
@@ -269,9 +263,10 @@ contract TychoRouter is
|
||||
* @dev Entrypoint to add or replace an approved executor contract address
|
||||
* @param targets address of the executor contract
|
||||
*/
|
||||
function setExecutors(
|
||||
address[] memory targets
|
||||
) external onlyRole(EXECUTOR_SETTER_ROLE) {
|
||||
function setExecutors(address[] memory targets)
|
||||
external
|
||||
onlyRole(EXECUTOR_SETTER_ROLE)
|
||||
{
|
||||
for (uint256 i = 0; i < targets.length; i++) {
|
||||
_setExecutor(targets[i]);
|
||||
}
|
||||
@@ -281,18 +276,20 @@ contract TychoRouter is
|
||||
* @dev Entrypoint to remove an approved executor contract address
|
||||
* @param target address of the executor contract
|
||||
*/
|
||||
function removeExecutor(
|
||||
address target
|
||||
) external onlyRole(EXECUTOR_SETTER_ROLE) {
|
||||
function removeExecutor(address target)
|
||||
external
|
||||
onlyRole(EXECUTOR_SETTER_ROLE)
|
||||
{
|
||||
_removeExecutor(target);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Allows setting the fee receiver.
|
||||
*/
|
||||
function setFeeReceiver(
|
||||
address newfeeReceiver
|
||||
) external onlyRole(FEE_SETTER_ROLE) {
|
||||
function setFeeReceiver(address newfeeReceiver)
|
||||
external
|
||||
onlyRole(FEE_SETTER_ROLE)
|
||||
{
|
||||
if (newfeeReceiver == address(0)) revert TychoRouter__AddressZero();
|
||||
emit FeeReceiverSet(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.
|
||||
*/
|
||||
function withdraw(
|
||||
IERC20[] memory tokens,
|
||||
address receiver
|
||||
) external onlyRole(FUND_RESCUER_ROLE) {
|
||||
function withdraw(IERC20[] memory tokens, address receiver)
|
||||
external
|
||||
onlyRole(FUND_RESCUER_ROLE)
|
||||
{
|
||||
if (receiver == address(0)) revert TychoRouter__AddressZero();
|
||||
|
||||
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.
|
||||
* The contract should never hold any NATIVE tokens for security reasons.
|
||||
*/
|
||||
function withdrawNative(
|
||||
address receiver
|
||||
) external onlyRole(FUND_RESCUER_ROLE) {
|
||||
function withdrawNative(address receiver)
|
||||
external
|
||||
onlyRole(FUND_RESCUER_ROLE)
|
||||
{
|
||||
if (receiver == address(0)) revert TychoRouter__AddressZero();
|
||||
|
||||
uint256 amount = address(this).balance;
|
||||
@@ -359,9 +357,8 @@ contract TychoRouter is
|
||||
* @param amount of WETH to unwrap.
|
||||
*/
|
||||
function _unwrapETH(uint256 amount) internal {
|
||||
uint256 unwrapAmount = amount == 0
|
||||
? _weth.balanceOf(address(this))
|
||||
: amount;
|
||||
uint256 unwrapAmount =
|
||||
amount == 0 ? _weth.balanceOf(address(this)) : amount;
|
||||
_weth.withdraw(unwrapAmount);
|
||||
}
|
||||
|
||||
@@ -380,14 +377,15 @@ contract TychoRouter is
|
||||
bytes calldata msgData
|
||||
) external {
|
||||
_handleCallback(
|
||||
bytes4(0),
|
||||
abi.encodePacked(amount0Delta, amount1Delta, msgData)
|
||||
bytes4(0), abi.encodePacked(amount0Delta, amount1Delta, msgData)
|
||||
);
|
||||
}
|
||||
|
||||
function _unlockCallback(
|
||||
bytes calldata data
|
||||
) internal override returns (bytes memory) {
|
||||
function _unlockCallback(bytes calldata data)
|
||||
internal
|
||||
override
|
||||
returns (bytes memory)
|
||||
{
|
||||
require(data.length >= 20, "Invalid data length");
|
||||
bytes4 selector = bytes4(data[data.length - 24: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
|
||||
(bool success, ) = executor.delegatecall(
|
||||
(bool success,) = executor.delegatecall(
|
||||
abi.encodeWithSelector(selector, protocolData)
|
||||
);
|
||||
require(success, "delegatecall to uniswap v4 callback failed");
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
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
|
||||
import {IAsset} from "@balancer-labs/v2-interfaces/contracts/vault/IAsset.sol";
|
||||
// slither-disable-next-line solc-version
|
||||
@@ -16,10 +19,11 @@ contract BalancerV2Executor is IExecutor {
|
||||
address private constant VAULT = 0xBA12222222228d8Ba445958a75a0704d566BF2C8;
|
||||
|
||||
// slither-disable-next-line locked-ether
|
||||
function swap(
|
||||
uint256 givenAmount,
|
||||
bytes calldata data
|
||||
) external payable returns (uint256 calculatedAmount) {
|
||||
function swap(uint256 givenAmount, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 calculatedAmount)
|
||||
{
|
||||
(
|
||||
IERC20 tokenIn,
|
||||
IERC20 tokenOut,
|
||||
@@ -51,17 +55,11 @@ contract BalancerV2Executor is IExecutor {
|
||||
|
||||
uint256 limit = 0;
|
||||
|
||||
calculatedAmount = IVault(VAULT).swap(
|
||||
singleSwap,
|
||||
funds,
|
||||
limit,
|
||||
block.timestamp
|
||||
);
|
||||
calculatedAmount =
|
||||
IVault(VAULT).swap(singleSwap, funds, limit, block.timestamp);
|
||||
}
|
||||
|
||||
function _decodeData(
|
||||
bytes calldata data
|
||||
)
|
||||
function _decodeData(bytes calldata data)
|
||||
internal
|
||||
pure
|
||||
returns (
|
||||
|
||||
@@ -11,10 +11,11 @@ contract UniswapV2Executor is IExecutor {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
// slither-disable-next-line locked-ether
|
||||
function swap(
|
||||
uint256 givenAmount,
|
||||
bytes calldata data
|
||||
) external payable returns (uint256 calculatedAmount) {
|
||||
function swap(uint256 givenAmount, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 calculatedAmount)
|
||||
{
|
||||
address target;
|
||||
address receiver;
|
||||
bool zeroForOne;
|
||||
@@ -32,9 +33,7 @@ contract UniswapV2Executor is IExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
function _decodeData(
|
||||
bytes calldata data
|
||||
)
|
||||
function _decodeData(bytes calldata data)
|
||||
internal
|
||||
pure
|
||||
returns (
|
||||
@@ -53,20 +52,20 @@ contract UniswapV2Executor is IExecutor {
|
||||
zeroForOne = uint8(data[60]) > 0;
|
||||
}
|
||||
|
||||
function _getAmountOut(
|
||||
address target,
|
||||
uint256 amountIn,
|
||||
bool zeroForOne
|
||||
) internal view returns (uint256 amount) {
|
||||
function _getAmountOut(address target, uint256 amountIn, bool zeroForOne)
|
||||
internal
|
||||
view
|
||||
returns (uint256 amount)
|
||||
{
|
||||
IUniswapV2Pair pair = IUniswapV2Pair(target);
|
||||
uint112 reserveIn;
|
||||
uint112 reserveOut;
|
||||
if (zeroForOne) {
|
||||
// slither-disable-next-line unused-return
|
||||
(reserveIn, reserveOut, ) = pair.getReserves();
|
||||
(reserveIn, reserveOut,) = pair.getReserves();
|
||||
} else {
|
||||
// slither-disable-next-line unused-return
|
||||
(reserveOut, reserveIn, ) = pair.getReserves();
|
||||
(reserveOut, reserveIn,) = pair.getReserves();
|
||||
}
|
||||
|
||||
require(reserveIn > 0 && reserveOut > 0, "L");
|
||||
|
||||
@@ -5,11 +5,13 @@ import "@interfaces/IExecutor.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
|
||||
import "@uniswap/v3-updated/CallbackValidationV2.sol";
|
||||
import "@interfaces/ICallback.sol";
|
||||
import "forge-std/console.sol";
|
||||
|
||||
error UniswapV3Executor__InvalidDataLength();
|
||||
error UniswapV3Executor__InvalidFactory();
|
||||
|
||||
contract UniswapV3Executor is IExecutor {
|
||||
contract UniswapV3Executor is IExecutor, ICallback {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
uint160 private constant MIN_SQRT_RATIO = 4295128739;
|
||||
@@ -28,10 +30,11 @@ contract UniswapV3Executor is IExecutor {
|
||||
}
|
||||
|
||||
// slither-disable-next-line locked-ether
|
||||
function swap(
|
||||
uint256 amountIn,
|
||||
bytes calldata data
|
||||
) external payable returns (uint256 amountOut) {
|
||||
function swap(uint256 amountIn, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 amountOut)
|
||||
{
|
||||
(
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
@@ -64,36 +67,53 @@ contract UniswapV3Executor is IExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
function handleCallback(
|
||||
bytes calldata msgData
|
||||
) external returns (bytes memory result) {
|
||||
(int256 amount0Delta, int256 amount1Delta) = abi.decode(
|
||||
msgData[:64],
|
||||
(int256, int256)
|
||||
function uniswapV3SwapCallback(
|
||||
int256 amount0Delta,
|
||||
int256 amount1Delta,
|
||||
bytes calldata data
|
||||
) external {
|
||||
// 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 tokenOut = address(bytes20(msgData[84:104]));
|
||||
uint24 poolFee = uint24(bytes3(msgData[104:107]));
|
||||
|
||||
CallbackValidationV2.verifyCallback(
|
||||
factory,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
poolFee
|
||||
);
|
||||
// slither-disable-next-line unused-return
|
||||
CallbackValidationV2.verifyCallback(factory, tokenIn, tokenOut, poolFee);
|
||||
|
||||
uint256 amountOwed = amount0Delta > 0
|
||||
? uint256(amount0Delta)
|
||||
: uint256(amount1Delta);
|
||||
uint256 amountOwed =
|
||||
amount0Delta > 0 ? uint256(amount0Delta) : uint256(amount1Delta);
|
||||
|
||||
IERC20(tokenIn).safeTransfer(msg.sender, amountOwed);
|
||||
return abi.encode(amountOwed, tokenIn);
|
||||
}
|
||||
|
||||
function _decodeData(
|
||||
bytes calldata data
|
||||
)
|
||||
function _decodeData(bytes calldata data)
|
||||
internal
|
||||
pure
|
||||
returns (
|
||||
@@ -116,11 +136,11 @@ contract UniswapV3Executor is IExecutor {
|
||||
zeroForOne = uint8(data[83]) > 0;
|
||||
}
|
||||
|
||||
function _makeV3CallbackData(
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint24 fee
|
||||
) internal view returns (bytes memory) {
|
||||
function _makeV3CallbackData(address tokenIn, address tokenOut, uint24 fee)
|
||||
internal
|
||||
view
|
||||
returns (bytes memory)
|
||||
{
|
||||
return abi.encodePacked(tokenIn, tokenOut, fee, self);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,14 @@
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
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 {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 {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
|
||||
import {V4Router} from "@uniswap/v4-periphery/src/V4Router.sol";
|
||||
@@ -18,16 +23,13 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
|
||||
constructor(IPoolManager _poolManager) V4Router(_poolManager) {}
|
||||
|
||||
function swap(
|
||||
uint256,
|
||||
bytes calldata data
|
||||
) external payable returns (uint256 calculatedAmount) {
|
||||
(
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
bool isExactInput,
|
||||
uint256 amount
|
||||
) = _decodeData(data);
|
||||
function swap(uint256, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 calculatedAmount)
|
||||
{
|
||||
(address tokenIn, address tokenOut, bool isExactInput, uint256 amount) =
|
||||
_decodeData(data);
|
||||
|
||||
uint256 tokenOutBalanceBefore;
|
||||
uint256 tokenInBalanceBefore;
|
||||
@@ -62,9 +64,7 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
return calculatedAmount;
|
||||
}
|
||||
|
||||
function _decodeData(
|
||||
bytes calldata data
|
||||
)
|
||||
function _decodeData(bytes calldata data)
|
||||
internal
|
||||
pure
|
||||
returns (
|
||||
@@ -74,19 +74,15 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
uint256 amount
|
||||
)
|
||||
{
|
||||
(bytes memory actions, bytes[] memory params) = abi.decode(
|
||||
data,
|
||||
(bytes, bytes[])
|
||||
);
|
||||
(bytes memory actions, bytes[] memory params) =
|
||||
abi.decode(data, (bytes, bytes[]));
|
||||
|
||||
// First byte of actions determines the swap type
|
||||
uint8 action = uint8(bytes1(actions[0]));
|
||||
|
||||
if (action == uint8(Actions.SWAP_EXACT_IN_SINGLE)) {
|
||||
IV4Router.ExactInputSingleParams memory swapParams = abi.decode(
|
||||
params[0],
|
||||
(IV4Router.ExactInputSingleParams)
|
||||
);
|
||||
IV4Router.ExactInputSingleParams memory swapParams =
|
||||
abi.decode(params[0], (IV4Router.ExactInputSingleParams));
|
||||
|
||||
tokenIn = swapParams.zeroForOne
|
||||
? address(uint160(swapParams.poolKey.currency0.toId()))
|
||||
@@ -97,10 +93,8 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
isExactInput = true;
|
||||
amount = swapParams.amountIn;
|
||||
} else if (action == uint8(Actions.SWAP_EXACT_OUT_SINGLE)) {
|
||||
IV4Router.ExactOutputSingleParams memory swapParams = abi.decode(
|
||||
params[0],
|
||||
(IV4Router.ExactOutputSingleParams)
|
||||
);
|
||||
IV4Router.ExactOutputSingleParams memory swapParams =
|
||||
abi.decode(params[0], (IV4Router.ExactOutputSingleParams));
|
||||
|
||||
tokenIn = swapParams.zeroForOne
|
||||
? address(uint160(swapParams.poolKey.currency0.toId()))
|
||||
@@ -111,23 +105,18 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
isExactInput = false;
|
||||
amount = swapParams.amountOut;
|
||||
} else if (action == uint8(Actions.SWAP_EXACT_IN)) {
|
||||
IV4Router.ExactInputParams memory swapParams = abi.decode(
|
||||
params[0],
|
||||
(IV4Router.ExactInputParams)
|
||||
);
|
||||
IV4Router.ExactInputParams memory swapParams =
|
||||
abi.decode(params[0], (IV4Router.ExactInputParams));
|
||||
|
||||
tokenIn = address(uint160(swapParams.currencyIn.toId()));
|
||||
PathKey memory lastPath = swapParams.path[
|
||||
swapParams.path.length - 1
|
||||
];
|
||||
PathKey memory lastPath =
|
||||
swapParams.path[swapParams.path.length - 1];
|
||||
tokenOut = address(uint160(lastPath.intermediateCurrency.toId()));
|
||||
isExactInput = true;
|
||||
amount = swapParams.amountIn;
|
||||
} else if (action == uint8(Actions.SWAP_EXACT_OUT)) {
|
||||
IV4Router.ExactOutputParams memory swapParams = abi.decode(
|
||||
params[0],
|
||||
(IV4Router.ExactOutputParams)
|
||||
);
|
||||
IV4Router.ExactOutputParams memory swapParams =
|
||||
abi.decode(params[0], (IV4Router.ExactOutputParams));
|
||||
|
||||
PathKey memory firstPath = swapParams.path[0];
|
||||
tokenIn = address(uint160(firstPath.intermediateCurrency.toId()));
|
||||
@@ -137,14 +126,12 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
}
|
||||
}
|
||||
|
||||
function _pay(
|
||||
Currency token,
|
||||
address payer,
|
||||
uint256 amount
|
||||
) internal override {
|
||||
function _pay(Currency token, address payer, uint256 amount)
|
||||
internal
|
||||
override
|
||||
{
|
||||
IERC20(Currency.unwrap(token)).safeTransfer(
|
||||
address(poolManager),
|
||||
amount
|
||||
address(poolManager), amount
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user