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

@@ -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");