chore: merge main
This commit is contained in:
@@ -140,27 +140,51 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
uint256 nTokens,
|
||||
address receiver,
|
||||
bytes calldata swaps
|
||||
) external payable whenNotPaused nonReentrant returns (uint256 amountOut) {
|
||||
) public payable whenNotPaused nonReentrant returns (uint256 amountOut) {
|
||||
if (receiver == address(0)) {
|
||||
revert TychoRouter__AddressZero();
|
||||
}
|
||||
|
||||
// Assume funds already in our router.
|
||||
// Assume funds are already in the router.
|
||||
if (wrapEth) {
|
||||
_wrapETH(amountIn);
|
||||
tokenIn = address(_weth);
|
||||
}
|
||||
|
||||
return _executeSwap(
|
||||
amountIn,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
minAmountOut,
|
||||
unwrapEth,
|
||||
nTokens,
|
||||
receiver,
|
||||
swaps
|
||||
);
|
||||
uint256 initialBalance = tokenIn == address(0)
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
|
||||
amountOut = _swap(amountIn, nTokens, swaps);
|
||||
|
||||
uint256 currentBalance = tokenIn == address(0)
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
|
||||
uint256 amountConsumed = initialBalance - currentBalance;
|
||||
|
||||
if (amountConsumed < amountIn) {
|
||||
uint256 leftoverAmount = amountIn - amountConsumed;
|
||||
revert TychoRouter__AmountInNotFullySpent(leftoverAmount);
|
||||
}
|
||||
|
||||
if (fee > 0) {
|
||||
uint256 feeAmount = (amountOut * fee) / 10000;
|
||||
amountOut -= feeAmount;
|
||||
IERC20(tokenOut).safeTransfer(feeReceiver, feeAmount);
|
||||
}
|
||||
|
||||
if (minAmountOut > 0 && amountOut < minAmountOut) {
|
||||
revert TychoRouter__NegativeSlippage(amountOut, minAmountOut);
|
||||
}
|
||||
|
||||
if (unwrapEth) {
|
||||
_unwrapETH(amountOut);
|
||||
}
|
||||
if (tokenOut == address(0)) {
|
||||
Address.sendValue(payable(receiver), amountOut);
|
||||
} else {
|
||||
IERC20(tokenOut).safeTransfer(receiver, amountOut);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,16 +226,9 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
IAllowanceTransfer.PermitSingle calldata permitSingle,
|
||||
bytes calldata signature,
|
||||
bytes calldata swaps
|
||||
) external payable whenNotPaused nonReentrant returns (uint256 amountOut) {
|
||||
if (receiver == address(0)) {
|
||||
revert TychoRouter__AddressZero();
|
||||
}
|
||||
|
||||
) external payable whenNotPaused returns (uint256 amountOut) {
|
||||
// For native ETH, assume funds already in our router. Else, transfer and handle approval.
|
||||
if (wrapEth) {
|
||||
_wrapETH(amountIn);
|
||||
tokenIn = address(_weth);
|
||||
} else if (tokenIn != address(0)) {
|
||||
if (tokenIn != address(0)) {
|
||||
permit2.permit(msg.sender, permitSingle, signature);
|
||||
permit2.transferFrom(
|
||||
msg.sender,
|
||||
@@ -221,11 +238,12 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
);
|
||||
}
|
||||
|
||||
return _executeSwap(
|
||||
return swap(
|
||||
amountIn,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
minAmountOut,
|
||||
wrapEth,
|
||||
unwrapEth,
|
||||
nTokens,
|
||||
receiver,
|
||||
@@ -233,61 +251,6 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Internal implementation of the core swap logic shared between swap() and swapPermit2().
|
||||
*
|
||||
* @notice This function centralizes the swap execution logic.
|
||||
* @notice For detailed documentation on parameters and behavior, see the documentation for
|
||||
* swap() and swapPermit2() functions.
|
||||
*
|
||||
*/
|
||||
function _executeSwap(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint256 minAmountOut,
|
||||
bool unwrapEth,
|
||||
uint256 nTokens,
|
||||
address receiver,
|
||||
bytes calldata swaps
|
||||
) internal returns (uint256 amountOut) {
|
||||
uint256 initialBalance = tokenIn == address(0)
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
|
||||
amountOut = _swap(amountIn, nTokens, swaps);
|
||||
|
||||
uint256 currentBalance = tokenIn == address(0)
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
|
||||
uint256 amountConsumed = initialBalance - currentBalance;
|
||||
|
||||
if (amountConsumed < amountIn) {
|
||||
uint256 leftoverAmount = amountIn - amountConsumed;
|
||||
revert TychoRouter__AmountInNotFullySpent(leftoverAmount);
|
||||
}
|
||||
|
||||
if (fee > 0) {
|
||||
uint256 feeAmount = (amountOut * fee) / 10000;
|
||||
amountOut -= feeAmount;
|
||||
IERC20(tokenOut).safeTransfer(feeReceiver, feeAmount);
|
||||
}
|
||||
|
||||
if (minAmountOut > 0 && amountOut < minAmountOut) {
|
||||
revert TychoRouter__NegativeSlippage(amountOut, minAmountOut);
|
||||
}
|
||||
|
||||
if (unwrapEth) {
|
||||
_unwrapETH(amountOut);
|
||||
}
|
||||
if (tokenOut == address(0)) {
|
||||
Address.sendValue(payable(receiver), amountOut);
|
||||
} else {
|
||||
IERC20(tokenOut).safeTransfer(receiver, amountOut);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Executes sequential swaps as defined by the provided swap graph.
|
||||
*
|
||||
|
||||
@@ -3,7 +3,15 @@ pragma solidity ^0.8.26;
|
||||
|
||||
import "forge-std/Test.sol";
|
||||
|
||||
contract Constants is Test {
|
||||
contract BaseConstants {
|
||||
address BASE_USDC = 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913;
|
||||
address BASE_MAG7 = 0x9E6A46f294bB67c20F1D1E7AfB0bBEf614403B55;
|
||||
|
||||
// uniswap v2
|
||||
address USDC_MAG7_POOL = 0x739c2431670A12E2cF8e11E3603eB96e6728a789;
|
||||
}
|
||||
|
||||
contract Constants is Test, BaseConstants {
|
||||
address ADMIN = makeAddr("admin"); //admin=us
|
||||
address BOB = makeAddr("bob"); //bob=someone!=us
|
||||
address FUND_RESCUER = makeAddr("fundRescuer");
|
||||
|
||||
@@ -978,4 +978,26 @@ contract TychoRouterTest is TychoRouterTestSetup {
|
||||
|
||||
assertEq(IERC20(WBTC_ADDR).balanceOf(tychoRouterAddr), 102718);
|
||||
}
|
||||
|
||||
// Base Network Tests
|
||||
// Make sure to set the RPC_URL to base network
|
||||
function testSwapSingleBase() public {
|
||||
vm.skip(true);
|
||||
vm.rollFork(26857267);
|
||||
uint256 amountIn = 10 * 10 ** 6;
|
||||
deal(BASE_USDC, tychoRouterAddr, amountIn);
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
BASE_USDC, USDC_MAG7_POOL, tychoRouterAddr, true
|
||||
);
|
||||
|
||||
bytes memory swap = encodeSwap(
|
||||
uint8(0), uint8(1), uint24(0), address(usv2Executor), protocolData
|
||||
);
|
||||
bytes[] memory swaps = new bytes[](1);
|
||||
swaps[0] = swap;
|
||||
|
||||
tychoRouter.exposedSwap(amountIn, 2, pleEncode(swaps));
|
||||
assertGt(IERC20(BASE_MAG7).balanceOf(tychoRouterAddr), 1379830606);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,4 +156,21 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants {
|
||||
vm.expectRevert(UniswapV2Executor__InvalidTarget.selector);
|
||||
uniswapV2Exposed.swap(amountIn, protocolData);
|
||||
}
|
||||
|
||||
// Base Network Tests
|
||||
// Make sure to set the RPC_URL to base network
|
||||
function testSwapBaseNetwork() public {
|
||||
vm.skip(true);
|
||||
vm.rollFork(26857267);
|
||||
uint256 amountIn = 10 * 10 ** 6;
|
||||
bool zeroForOne = true;
|
||||
bytes memory protocolData =
|
||||
abi.encodePacked(BASE_USDC, USDC_MAG7_POOL, BOB, zeroForOne);
|
||||
|
||||
deal(BASE_USDC, address(uniswapV2Exposed), amountIn);
|
||||
|
||||
uniswapV2Exposed.swap(amountIn, protocolData);
|
||||
|
||||
assertEq(IERC20(BASE_MAG7).balanceOf(BOB), 1379830606);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user