Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ume committed Dec 10, 2024
1 parent 92f3abf commit 5d83767
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 18 deletions.
15 changes: 0 additions & 15 deletions contracts/8/DexRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,6 @@ contract DexRouter is
address(uint160(receiver)),
balanceBefore
);
_handleRefundEth(payer);

return swappedAmount - commissionAmount;
}

Expand Down Expand Up @@ -735,8 +733,6 @@ contract DexRouter is
receiver,
balanceBefore
);
_handleRefundEth(payer);

return swappedAmount - commissionAmount;
}
/// @notice Executes a token swap using the Unxswap protocol, sending the output directly to a specified receiver.
Expand Down Expand Up @@ -799,7 +795,6 @@ contract DexRouter is
receiver,
balanceBefore
);
_handleRefundEth(payer);
return swappedAmount - commissionAmount;
}

Expand All @@ -824,14 +819,4 @@ contract DexRouter is
SafeERC20.safeTransfer(IERC20(token), to, amount);
}
}
// behave the same as uniswap universal router.
function _handleRefundEth(address payer) internal {
if (msg.value == 0) return;
uint amount = IERC20(_WETH).balanceOf(address(this));
if (amount > 0) {
IWETH(_WETH).transfer(_WNATIVE_RELAY, amount);
IWNativeRelayer(_WNATIVE_RELAY).withdraw(amount);
payable(payer).call{value: amount}("");
}
}
}
20 changes: 20 additions & 0 deletions contracts/8/UnxswapV3Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ contract UnxswapV3Router is IUniswapV3SwapCallback, CommonUtils {
// concatenation of withdraw(uint),transfer()
bytes32 private constant _SELECTORS2 =
0x2e1a7d4da9059cbb000000000000000000000000000000000000000000000000;
bytes32 private constant _SELECTORS3 =
0xa9059cbb70a08231000000000000000000000000000000000000000000000000;
uint160 private constant _MIN_SQRT_RATIO = 4_295_128_739 + 1;
uint160 private constant _MAX_SQRT_RATIO =
1_461_446_703_485_210_103_287_273_052_203_988_822_378_723_970_342 - 1;
Expand Down Expand Up @@ -331,6 +333,23 @@ contract UnxswapV3Router is IUniswapV3SwapCallback, CommonUtils {
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
}
function getBalanceAndTransfer(emptyPtr, token) {
mstore(emptyPtr, _SELECTORS3)
mstore(add(8, emptyPtr), address())
if iszero(
staticcall(gas(), token, add(4, emptyPtr), 36, 0, 32)
) {
reRevert()
}
let amount := mload(0)
if gt(amount, 0) {
mstore(add(4, emptyPtr), origin())
mstore(add(36, emptyPtr), amount)
validateERC20Transfer(
call(gas(), token, 0, emptyPtr, 0x44, 0, 0x20)
)
}
}

function validateERC20Transfer(status) {
if iszero(status) {
Expand Down Expand Up @@ -405,6 +424,7 @@ contract UnxswapV3Router is IUniswapV3SwapCallback, CommonUtils {
validateERC20Transfer(
call(gas(), token, 0, add(emptyPtr, 0x0c), 0x44, 0, 0x20)
)
getBalanceAndTransfer(emptyPtr, token)
}
default {
// approveProxy.claimTokens(token, payer, msg.sender, amount);
Expand Down
4 changes: 4 additions & 0 deletions contracts/8/adapter/UniV3Adapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ contract UniV3Adapter is IAdapter, IUniswapV3SwapCallback {
: sqrtX96,
data
);
uint amount = IERC20(fromToken).balanceOf(address(this));
if (amount > 0) {
SafeERC20.safeTransfer(IERC20(fromToken), tx.origin, amount);
}
}

function sellBase(
Expand Down
138 changes: 135 additions & 3 deletions src/tests/UniV3Refund.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import "forge-std/test.sol";
import "forge-std/console2.sol";

import "@dex/DexRouter.sol";

import "@dex/adapter/UniV3Adapter.sol";
contract UniswapV3Test is Test {
address user = 0x07d3915Efd92a536c406F5063918d2Df0d9708e7;
address payable okx_dexrouter =
payable(0x7D0CcAa3Fac1e5A943c5168b6CEd828691b46B36);
address uni_router = 0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD;
address pool = 0x4416056ccF79fFD3abd99e61ccF80eA13EA4311c;
address USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address FILM = 0xe344Fb85b4FAb79e0ef32cE77c00732CE8566244;
address fee_collector = 0x000000fee13a103A10D593b9AE06b3e05F2E7E1c;
address inch_router = 0x111111125421cA6dc452d289314280a0f8842A65;
address constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
address adapter = 0x03F911AeDc25c770e701B8F563E8102CfACd62c0;
address pool_usdc = 0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640;
address token_approve = 0x40aA958dd87FC8305b97f2BA922CDdCa374bcD7f;

function setUp() public {
vm.createSelectFork(
Expand All @@ -25,12 +30,20 @@ contract UniswapV3Test is Test {
)
);
vm.etch(address(okx_dexrouter), address(new DexRouter()).code);
vm.etch(
address(adapter),
address(new UniV3Adapter(payable(WETH))).code
);
vm.prank(user);
IERC20(USDC).approve(token_approve, type(uint).max);

deal(USDC, user, 120000 * 10 ** 6);
}

function test_okx() public {
function _test_okx() public {
uint256[] memory pools = new uint256[](1);
pools[0] = uint256(bytes32(abi.encodePacked(bytes12(0), pool)));
vm.prank(user);
vm.prank(user, user);
DexRouter(okx_dexrouter).uniswapV3SwapTo{value: 3 ether}(
uint256(
bytes32(abi.encodePacked(bytes9(0), bytes3(0x019b8d), user))
Expand All @@ -40,6 +53,125 @@ contract UniswapV3Test is Test {
pools
);
}
function test_okx_unxv3() public {
uint256[] memory pools = new uint256[](2);
pools[0] = uint256(
bytes32(abi.encodePacked(bytes1(0x00), bytes11(0), pool_usdc))
);
pools[1] = uint256(bytes32(abi.encodePacked(bytes12(0), pool)));
vm.prank(user, user);
DexRouter(okx_dexrouter).uniswapV3SwapTo(
uint256(
bytes32(abi.encodePacked(bytes9(0), bytes3(0x019b8d), user))
),
120000 * 10 ** 6,
390789165003,
pools
);
}
struct SwapInfo {
uint256 orderId;
DexRouter.BaseRequest baseRequest;
uint256[] batchesAmount;
DexRouter.RouterPath[][] batches;
PMMLib.PMMSwapRequest[] extraData;
}
function _test_okx_smartswap() public {
uint amount = 3 ether;
SwapInfo memory swapInfo;
swapInfo.baseRequest.fromToken = uint256(uint160(address(ETH_ADDRESS)));
swapInfo.baseRequest.toToken = FILM;
swapInfo.baseRequest.fromTokenAmount = amount;
swapInfo.baseRequest.minReturnAmount = 0;
swapInfo.baseRequest.deadLine = block.timestamp;

swapInfo.batchesAmount = new uint[](1);
swapInfo.batchesAmount[0] = amount;

swapInfo.batches = new DexRouter.RouterPath[][](1);
swapInfo.batches[0] = new DexRouter.RouterPath[](1);
swapInfo.batches[0][0].mixAdapters = new address[](1);
swapInfo.batches[0][0].mixAdapters[0] = address(adapter);
swapInfo.batches[0][0].assetTo = new address[](1);
swapInfo.batches[0][0].assetTo[0] = address(adapter);
swapInfo.batches[0][0].rawData = new uint[](1);
swapInfo.batches[0][0].rawData[0] = uint256(
bytes32(abi.encodePacked(false, uint88(10000), address(pool)))
);
swapInfo.batches[0][0].extraData = new bytes[](1);
swapInfo.batches[0][0].extraData[0] = abi.encode(
uint160(0),
abi.encode(address(WETH), address(FILM), uint24(3000))
);
swapInfo.batches[0][0].fromToken = uint256(uint160(address(WETH)));

swapInfo.extraData = new PMMLib.PMMSwapRequest[](0);
vm.prank(user, user);
DexRouter(okx_dexrouter).smartSwapByOrderId{value: 3 ether}(
swapInfo.orderId,
swapInfo.baseRequest,
swapInfo.batchesAmount,
swapInfo.batches,
swapInfo.extraData
);
}
function _test_okx_smartswap_usdc() public {
uint amount = 120000 * 10 ** 6;
SwapInfo memory swapInfo;
swapInfo.baseRequest.fromToken = uint256(uint160(address(USDC)));
swapInfo.baseRequest.toToken = FILM;
swapInfo.baseRequest.fromTokenAmount = amount;
swapInfo.baseRequest.minReturnAmount = 0;
swapInfo.baseRequest.deadLine = block.timestamp;

swapInfo.batchesAmount = new uint[](1);
swapInfo.batchesAmount[0] = amount;

swapInfo.batches = new DexRouter.RouterPath[][](1);
swapInfo.batches[0] = new DexRouter.RouterPath[](2);
swapInfo.batches[0][0].mixAdapters = new address[](1);
swapInfo.batches[0][0].mixAdapters[0] = address(adapter);
swapInfo.batches[0][0].assetTo = new address[](1);
swapInfo.batches[0][0].assetTo[0] = address(adapter);
swapInfo.batches[0][0].rawData = new uint[](1);
swapInfo.batches[0][0].rawData[0] = uint256(
bytes32(
abi.encodePacked(uint8(0x80), uint88(10000), address(pool_usdc))
)
);
swapInfo.batches[0][0].extraData = new bytes[](1);
swapInfo.batches[0][0].extraData[0] = abi.encode(
uint160(0),
abi.encode(address(USDC), address(WETH), uint24(3000))
);
swapInfo.batches[0][0].fromToken = uint256(uint160(address(USDC)));

swapInfo.batches[0][1].mixAdapters = new address[](1);
swapInfo.batches[0][1].mixAdapters[0] = address(adapter);
swapInfo.batches[0][1].assetTo = new address[](1);
swapInfo.batches[0][1].assetTo[0] = address(adapter);
swapInfo.batches[0][1].rawData = new uint[](1);
swapInfo.batches[0][1].rawData[0] = uint256(
bytes32(abi.encodePacked(false, uint88(10000), address(pool)))
);
swapInfo.batches[0][1].extraData = new bytes[](1);
swapInfo.batches[0][1].extraData[0] = abi.encode(
uint160(0),
abi.encode(address(WETH), address(FILM), uint24(3000))
);
swapInfo.batches[0][1].fromToken = uint256(uint160(address(WETH)));

swapInfo.extraData = new PMMLib.PMMSwapRequest[](0);

vm.prank(user, user);
DexRouter(okx_dexrouter).smartSwapByOrderId(
swapInfo.orderId,
swapInfo.baseRequest,
swapInfo.batchesAmount,
swapInfo.batches,
swapInfo.extraData
);
}
// {
// "Func": "execute",
// "Params": [
Expand Down

0 comments on commit 5d83767

Please sign in to comment.