feat: Add swap method with tests
Changes: - If the tokenIn is ETH, skip permit2 approval - Make executors payable: When using delegatecall the executor inherits the execution context of whoever calls it. Our main swap function can accept ETH, it needs to be payable so by consequence the executors also need to be. - Set uniswap v2 executor in test router - Add tests for all possible cases of swap - Add tests for all cases of splitSwap - Add test functions to handle permit2 and encode swaps --- don't change below this line --- ENG-4041 Took 3 hours 50 minutes Took 49 seconds Took 14 seconds
This commit is contained in:
@@ -87,32 +87,60 @@ contract TychoRouter is
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Executes a swap graph supporting internal token amount
|
||||
* splits, checking that the user gets more than minUserAmount of buyToken.
|
||||
* @notice Executes a swap operation based on a predefined swap graph, supporting internal token amount splits.
|
||||
* This function enables multi-step swaps, optional ETH wrapping/unwrapping, and validates the output amount
|
||||
* against a user-specified minimum.
|
||||
*
|
||||
* @dev
|
||||
* - If `wrapEth` is true, the contract wraps the provided native ETH into WETH and uses it as the sell token.
|
||||
* - If `unwrapEth` is true, the contract converts the resulting WETH back into native ETH before sending it to the receiver.
|
||||
* - For ERC20 tokens, Permit2 is used to approve and transfer tokens from the caller to the router.
|
||||
* - Swaps are executed sequentially using the `_splitSwap` function.
|
||||
* - A fee is deducted from the output token if `fee > 0`, and the remaining amount is sent to the receiver.
|
||||
* - Reverts with `TychoRouter__NegativeSlippage` if the output amount is less than `minAmountOut` and `checkMinAmount` is true.
|
||||
*
|
||||
* @param amountIn The input token amount to be swapped.
|
||||
* @param tokenIn The address of the input token. Use `address(0)` for native ETH when `wrapEth` is true.
|
||||
* @param tokenOut The address of the output token. Use `address(0)` for native ETH when `unwrapEth` is true.
|
||||
* @param checkMinAmount A boolean indicating whether to enforce the `minAmountOut` check.
|
||||
* @param minAmountOut The minimum acceptable amount of the output token. Reverts if this condition is not met.
|
||||
* @param wrapEth If true, treats the input token as native ETH and wraps it into WETH.
|
||||
* @param unwrapEth If true, unwraps the resulting WETH into native ETH and sends it to the receiver.
|
||||
* @param nTokens The total number of tokens involved in the swap graph (used to initialize arrays for internal calculations).
|
||||
* @param receiver The address to receive the output tokens.
|
||||
* @param permitSingle A Permit2 structure containing token approval details for the input token. Ignored if `wrapEth` is true.
|
||||
* @param signature A valid signature authorizing the Permit2 approval. Ignored if `wrapEth` is true.
|
||||
* @param swaps Encoded swap graph data containing details of each swap.
|
||||
*
|
||||
* @return amountOut The total amount of the output token received by the receiver, after deducting fees if applicable.
|
||||
*/
|
||||
function swap(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint256 checkAmountOut,
|
||||
bool wrapEth, // This means ETH is the sell token
|
||||
bool unwrapEth, // This means ETH is the buy token
|
||||
bool checkMinAmount,
|
||||
uint256 minAmountOut,
|
||||
bool wrapEth,
|
||||
bool unwrapEth,
|
||||
uint256 nTokens,
|
||||
address receiver,
|
||||
IAllowanceTransfer.PermitSingle calldata permitSingle,
|
||||
bytes calldata signature,
|
||||
bytes calldata swaps
|
||||
) external whenNotPaused returns (uint256 amountOut) {
|
||||
) external payable whenNotPaused returns (uint256 amountOut) {
|
||||
// For native ETH, assume funds already in our router. Else, transfer and handle approval.
|
||||
if (wrapEth) {
|
||||
_wrapETH(amountIn);
|
||||
} else {
|
||||
permit2.permit(msg.sender, permitSingle, signature);
|
||||
permit2.transferFrom(
|
||||
msg.sender,
|
||||
address(this),
|
||||
uint160(amountIn),
|
||||
permitSingle.details.token
|
||||
);
|
||||
if (tokenIn != address(0)) {
|
||||
permit2.permit(msg.sender, permitSingle, signature);
|
||||
permit2.transferFrom(
|
||||
msg.sender,
|
||||
address(this),
|
||||
uint160(amountIn),
|
||||
permitSingle.details.token
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
amountOut = _splitSwap(amountIn, nTokens, swaps);
|
||||
@@ -121,10 +149,13 @@ contract TychoRouter is
|
||||
uint256 feeAmount = (amountOut * fee) / 10000;
|
||||
amountOut -= feeAmount;
|
||||
IERC20(tokenOut).safeTransfer(feeReceiver, feeAmount);
|
||||
if (unwrapEth == false) {
|
||||
IERC20(tokenOut).safeTransfer(receiver, amountOut);
|
||||
}
|
||||
}
|
||||
|
||||
if (amountOut < checkAmountOut) {
|
||||
revert TychoRouter__NegativeSlippage(amountOut, checkAmountOut);
|
||||
if (checkMinAmount && amountOut < minAmountOut) {
|
||||
revert TychoRouter__NegativeSlippage(amountOut, minAmountOut);
|
||||
}
|
||||
|
||||
if (unwrapEth) {
|
||||
@@ -152,15 +183,14 @@ contract TychoRouter is
|
||||
|
||||
while (swaps_.length > 0) {
|
||||
(swapData, swaps_) = swaps_.next();
|
||||
split = swapData.splitPercentage();
|
||||
tokenInIndex = swapData.tokenInIndex();
|
||||
tokenOutIndex = swapData.tokenOutIndex();
|
||||
split = swapData.splitPercentage();
|
||||
currentAmountIn = split > 0
|
||||
? (amounts[tokenInIndex] * split) / 0xffffff
|
||||
: remainingAmounts[tokenInIndex];
|
||||
currentAmountOut =
|
||||
_callExecutor(currentAmountIn, swapData.protocolData());
|
||||
|
||||
amounts[tokenOutIndex] += currentAmountOut;
|
||||
remainingAmounts[tokenOutIndex] += currentAmountOut;
|
||||
remainingAmounts[tokenInIndex] -= currentAmountIn;
|
||||
|
||||
@@ -12,6 +12,7 @@ contract UniswapV2Executor is IExecutor {
|
||||
|
||||
function swap(uint256 givenAmount, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 calculatedAmount)
|
||||
{
|
||||
address target;
|
||||
|
||||
Reference in New Issue
Block a user