fix: reciever issue
This commit is contained in:
@@ -2,14 +2,20 @@
|
|||||||
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";
|
||||||
import {Actions} from "@uniswap/v4-periphery/src/libraries/Actions.sol";
|
import {Actions} from "@uniswap/v4-periphery/src/libraries/Actions.sol";
|
||||||
import {IV4Router} from "@uniswap/v4-periphery/src/interfaces/IV4Router.sol";
|
import {IV4Router} from "@uniswap/v4-periphery/src/interfaces/IV4Router.sol";
|
||||||
|
import "forge-std/console.sol";
|
||||||
|
|
||||||
error UniswapV4Executor__InvalidDataLength();
|
error UniswapV4Executor__InvalidDataLength();
|
||||||
error UniswapV4Executor__SwapFailed();
|
error UniswapV4Executor__SwapFailed();
|
||||||
@@ -20,21 +26,23 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
|||||||
|
|
||||||
constructor(IPoolManager _poolManager) V4Router(_poolManager) {}
|
constructor(IPoolManager _poolManager) V4Router(_poolManager) {}
|
||||||
|
|
||||||
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,
|
||||||
uint24 fee,
|
uint24 fee,
|
||||||
address receiver, // TODO: This is not used right now
|
address receiver,
|
||||||
bool zeroForOne,
|
bool zeroForOne,
|
||||||
uint24 tickSpacing
|
uint24 tickSpacing
|
||||||
) = _decodeData(data);
|
) = _decodeData(data);
|
||||||
|
|
||||||
uint128 amountIn128 = uint128(amountIn);
|
uint128 amountIn128 = uint128(amountIn);
|
||||||
uint128 amountOut128 = uint128(amountOut);
|
uint256 balanceBefore = IERC20(tokenOut).balanceOf(receiver);
|
||||||
|
|
||||||
PoolKey memory key = PoolKey({
|
PoolKey memory key = PoolKey({
|
||||||
currency0: Currency.wrap(zeroForOne ? tokenIn : tokenOut),
|
currency0: Currency.wrap(zeroForOne ? tokenIn : tokenOut),
|
||||||
currency1: Currency.wrap(zeroForOne ? tokenOut : tokenIn),
|
currency1: Currency.wrap(zeroForOne ? tokenOut : tokenIn),
|
||||||
@@ -46,7 +54,7 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
|||||||
bytes memory actions = abi.encodePacked(
|
bytes memory actions = abi.encodePacked(
|
||||||
uint8(Actions.SWAP_EXACT_IN_SINGLE),
|
uint8(Actions.SWAP_EXACT_IN_SINGLE),
|
||||||
uint8(Actions.SETTLE_ALL),
|
uint8(Actions.SETTLE_ALL),
|
||||||
uint8(Actions.TAKE_ALL)
|
uint8(Actions.TAKE)
|
||||||
);
|
);
|
||||||
|
|
||||||
bytes[] memory params = new bytes[](3);
|
bytes[] memory params = new bytes[](3);
|
||||||
@@ -56,17 +64,20 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
|||||||
poolKey: key,
|
poolKey: key,
|
||||||
zeroForOne: zeroForOne,
|
zeroForOne: zeroForOne,
|
||||||
amountIn: amountIn128,
|
amountIn: amountIn128,
|
||||||
amountOutMinimum: amountOut128,
|
amountOutMinimum: 0,
|
||||||
hookData: bytes("")
|
hookData: bytes("")
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
params[1] = abi.encode(key.currency0, amountIn128);
|
params[1] = abi.encode(key.currency0, amountIn128);
|
||||||
params[2] = abi.encode(key.currency1, amountOut128);
|
params[2] = abi.encode(key.currency1, receiver, 0);
|
||||||
|
|
||||||
this.executeActions(abi.encode(actions, params));
|
this.executeActions(abi.encode(actions, params));
|
||||||
|
|
||||||
// TODO: This is still hardcode to zero, find a way to return the actual amount out
|
amountOut = IERC20(tokenOut).balanceOf(receiver) - balanceBefore;
|
||||||
|
|
||||||
|
if (amountOut == 0) revert UniswapV4Executor__SwapFailed();
|
||||||
|
|
||||||
return amountOut;
|
return amountOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,9 +85,7 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
|||||||
_executeActions(actions);
|
_executeActions(actions);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _decodeData(
|
function _decodeData(bytes calldata data)
|
||||||
bytes calldata data
|
|
||||||
)
|
|
||||||
internal
|
internal
|
||||||
pure
|
pure
|
||||||
returns (
|
returns (
|
||||||
@@ -100,11 +109,10 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
|||||||
tickSpacing = uint24(bytes3(data[64:67]));
|
tickSpacing = uint24(bytes3(data[64:67]));
|
||||||
}
|
}
|
||||||
|
|
||||||
function _pay(
|
function _pay(Currency token, address payer, uint256 amount)
|
||||||
Currency token,
|
internal
|
||||||
address payer,
|
override
|
||||||
uint256 amount
|
{
|
||||||
) internal override {
|
|
||||||
token.transfer(payer, amount);
|
token.transfer(payer, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,12 @@ pragma solidity ^0.8.26;
|
|||||||
import "@src/executors/UniswapV4Executor.sol";
|
import "@src/executors/UniswapV4Executor.sol";
|
||||||
import {Test} from "../../lib/forge-std/src/Test.sol";
|
import {Test} from "../../lib/forge-std/src/Test.sol";
|
||||||
import {Constants} from "../Constants.sol";
|
import {Constants} from "../Constants.sol";
|
||||||
|
import {console} from "forge-std/console.sol";
|
||||||
|
|
||||||
contract UniswapV4ExecutorExposed is UniswapV4Executor {
|
contract UniswapV4ExecutorExposed is UniswapV4Executor {
|
||||||
constructor(IPoolManager _poolManager) UniswapV4Executor(_poolManager) {}
|
constructor(IPoolManager _poolManager) UniswapV4Executor(_poolManager) {}
|
||||||
|
|
||||||
function decodeData(
|
function decodeData(bytes calldata data)
|
||||||
bytes calldata data
|
|
||||||
)
|
|
||||||
external
|
external
|
||||||
pure
|
pure
|
||||||
returns (
|
returns (
|
||||||
@@ -32,24 +31,19 @@ contract UniswapV4ExecutorTest is Test, Constants {
|
|||||||
UniswapV4ExecutorExposed uniswapV4Exposed;
|
UniswapV4ExecutorExposed uniswapV4Exposed;
|
||||||
IERC20 USDE = IERC20(USDE_ADDR);
|
IERC20 USDE = IERC20(USDE_ADDR);
|
||||||
IERC20 USDT = IERC20(USDT_ADDR);
|
IERC20 USDT = IERC20(USDT_ADDR);
|
||||||
|
address poolManager = 0x000000000004444c5dc75cB358380D2e3dE08A90;
|
||||||
|
|
||||||
function setUp() public {
|
function setUp() public {
|
||||||
uint256 forkBlock = 21817316;
|
uint256 forkBlock = 21817316;
|
||||||
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
||||||
uniswapV4Exposed = new UniswapV4ExecutorExposed(
|
uniswapV4Exposed =
|
||||||
IPoolManager(0x000000000004444c5dc75cB358380D2e3dE08A90)
|
new UniswapV4ExecutorExposed(IPoolManager(poolManager));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testDecodeParamsUniswapV4() public view {
|
function testDecodeParamsUniswapV4() public view {
|
||||||
uint24 expectedPoolFee = 500;
|
uint24 expectedPoolFee = 500;
|
||||||
bytes memory data = abi.encodePacked(
|
bytes memory data = abi.encodePacked(
|
||||||
USDE_ADDR,
|
USDE_ADDR, USDT_ADDR, expectedPoolFee, address(2), false, int24(1)
|
||||||
USDT_ADDR,
|
|
||||||
expectedPoolFee,
|
|
||||||
address(2),
|
|
||||||
false,
|
|
||||||
int24(1)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
(
|
(
|
||||||
@@ -78,43 +72,30 @@ contract UniswapV4ExecutorTest is Test, Constants {
|
|||||||
|
|
||||||
function testSwapUniswapV4() public {
|
function testSwapUniswapV4() public {
|
||||||
vm.startPrank(BOB);
|
vm.startPrank(BOB);
|
||||||
uint256 amountIn = 1 ether;
|
uint256 amountIn = 100 ether;
|
||||||
deal(USDE_ADDR, address(uniswapV4Exposed), amountIn);
|
deal(USDE_ADDR, address(uniswapV4Exposed), amountIn);
|
||||||
assertEq(USDE.balanceOf(address(uniswapV4Exposed)), amountIn);
|
uint256 usdeBalanceBeforePool = USDE.balanceOf(poolManager);
|
||||||
|
uint256 usdeBalanceBeforeSwapExecutor =
|
||||||
|
USDE.balanceOf(address(uniswapV4Exposed));
|
||||||
|
assertEq(usdeBalanceBeforeSwapExecutor, amountIn);
|
||||||
|
uint256 usdtBalanceBeforeSwapBob = USDT.balanceOf(address(BOB));
|
||||||
|
assertEq(usdtBalanceBeforeSwapBob, 0);
|
||||||
|
|
||||||
bytes memory data = abi.encodePacked(
|
bytes memory data = abi.encodePacked(
|
||||||
USDE_ADDR,
|
USDE_ADDR,
|
||||||
USDT_ADDR,
|
USDT_ADDR,
|
||||||
uint24(100), // 0.01% fee tier
|
uint24(100), // 0.01% fee tier
|
||||||
address(this),
|
BOB,
|
||||||
true,
|
true,
|
||||||
int24(1)
|
int24(1)
|
||||||
);
|
);
|
||||||
|
|
||||||
uint256 amountOut = uniswapV4Exposed.swap(amountIn, data);
|
uint256 amountOut = uniswapV4Exposed.swap(amountIn, data);
|
||||||
assertEq(USDE.balanceOf(address(uniswapV4Exposed)), 0);
|
assertEq(USDE.balanceOf(poolManager), usdeBalanceBeforePool + amountIn);
|
||||||
vm.stopPrank();
|
|
||||||
}
|
|
||||||
|
|
||||||
function testSwapUniswapV4With1Inch() public {
|
|
||||||
vm.startPrank(BOB);
|
|
||||||
uint256 amountIn = 1 ether;
|
|
||||||
deal(INCH_ADDR, address(uniswapV4Exposed), amountIn);
|
|
||||||
assertEq(
|
assertEq(
|
||||||
IERC20(INCH_ADDR).balanceOf(address(uniswapV4Exposed)),
|
USDE.balanceOf(address(uniswapV4Exposed)),
|
||||||
amountIn
|
usdeBalanceBeforeSwapExecutor - amountIn
|
||||||
);
|
);
|
||||||
|
assertTrue(USDT.balanceOf(BOB) == amountOut && amountOut > 0);
|
||||||
bytes memory data = abi.encodePacked(
|
|
||||||
INCH_ADDR,
|
|
||||||
USDC_ADDR,
|
|
||||||
uint24(10000), // 0.01% fee tier
|
|
||||||
address(this),
|
|
||||||
true,
|
|
||||||
int24(200)
|
|
||||||
);
|
|
||||||
|
|
||||||
uint256 amountOut = uniswapV4Exposed.swap(amountIn, data);
|
|
||||||
assertEq(IERC20(INCH_ADDR).balanceOf(address(uniswapV4Exposed)), 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user