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:
@@ -1,9 +1,10 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.13;
|
||||
|
||||
import "@src/TychoRouter.sol";
|
||||
import "../src/executors/UniswapV2Executor.sol";
|
||||
import "./Constants.sol";
|
||||
import "./mock/MockERC20.sol";
|
||||
import "@src/TychoRouter.sol";
|
||||
import {WETH} from "../lib/permit2/lib/solmate/src/tokens/WETH.sol";
|
||||
|
||||
contract TychoRouterExposed is TychoRouter {
|
||||
@@ -16,12 +17,20 @@ contract TychoRouterExposed is TychoRouter {
|
||||
function unwrapETH(uint256 amount) external {
|
||||
return _unwrapETH(amount);
|
||||
}
|
||||
|
||||
function splitSwap(uint256 amountIn, uint256 nTokens, bytes calldata swaps)
|
||||
external
|
||||
returns (uint256)
|
||||
{
|
||||
return _splitSwap(amountIn, nTokens, swaps);
|
||||
}
|
||||
}
|
||||
|
||||
contract TychoRouterTestSetup is Test, Constants {
|
||||
TychoRouterExposed tychoRouter;
|
||||
address executorSetter;
|
||||
address permit2Address = address(0x000000000022D473030F116dDEE9F6B43aC78BA3);
|
||||
UniswapV2Executor public usv2Executor;
|
||||
MockERC20[] tokens;
|
||||
|
||||
function setUp() public {
|
||||
@@ -35,10 +44,18 @@ contract TychoRouterTestSetup is Test, Constants {
|
||||
tychoRouter.grantRole(keccak256("FEE_SETTER_ROLE"), FEE_SETTER);
|
||||
tychoRouter.grantRole(keccak256("PAUSER_ROLE"), PAUSER);
|
||||
tychoRouter.grantRole(keccak256("UNPAUSER_ROLE"), UNPAUSER);
|
||||
tychoRouter.grantRole(
|
||||
keccak256("EXECUTOR_SETTER_ROLE"), EXECUTOR_SETTER
|
||||
);
|
||||
executorSetter = BOB;
|
||||
deployDummyContract();
|
||||
vm.stopPrank();
|
||||
|
||||
usv2Executor = new UniswapV2Executor();
|
||||
vm.startPrank(EXECUTOR_SETTER);
|
||||
tychoRouter.setExecutor(address(usv2Executor));
|
||||
vm.stopPrank();
|
||||
|
||||
vm.startPrank(BOB);
|
||||
tokens.push(new MockERC20("Token A", "A"));
|
||||
tokens.push(new MockERC20("Token B", "B"));
|
||||
@@ -57,4 +74,116 @@ contract TychoRouterTestSetup is Test, Constants {
|
||||
tokens[i].mint(to, amount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Handles the Permit2 approval process for Alice, allowing the TychoRouter contract
|
||||
* to spend `amount_in` of `tokenIn` on her behalf.
|
||||
*
|
||||
* This function approves the Permit2 contract to transfer the specified token amount
|
||||
* and constructs a `PermitSingle` struct for the approval. It also generates a valid
|
||||
* EIP-712 signature for the approval using Alice's private key.
|
||||
*
|
||||
* @param tokenIn The address of the token being approved.
|
||||
* @param amount_in The amount of tokens to approve for transfer.
|
||||
* @return permitSingle The `PermitSingle` struct containing the approval details.
|
||||
* @return signature The EIP-712 signature for the approval.
|
||||
*/
|
||||
function handlePermit2Approval(address tokenIn, uint256 amount_in)
|
||||
internal
|
||||
returns (IAllowanceTransfer.PermitSingle memory, bytes memory)
|
||||
{
|
||||
IERC20(tokenIn).approve(permit2Address, amount_in);
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle = IAllowanceTransfer
|
||||
.PermitSingle({
|
||||
details: IAllowanceTransfer.PermitDetails({
|
||||
token: tokenIn,
|
||||
amount: uint160(amount_in),
|
||||
expiration: uint48(block.timestamp + 1 days),
|
||||
nonce: 0
|
||||
}),
|
||||
spender: address(tychoRouter),
|
||||
sigDeadline: block.timestamp + 1 days
|
||||
});
|
||||
|
||||
bytes memory signature = signPermit2(permitSingle, ALICE_PK);
|
||||
return (permitSingle, signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Signs a Permit2 `PermitSingle` struct with the given private key.
|
||||
* @param permit The `PermitSingle` struct to sign.
|
||||
* @param privateKey The private key of the signer.
|
||||
* @return The signature as a `bytes` array.
|
||||
*/
|
||||
function signPermit2(
|
||||
IAllowanceTransfer.PermitSingle memory permit,
|
||||
uint256 privateKey
|
||||
) internal returns (bytes memory) {
|
||||
bytes32 _PERMIT_DETAILS_TYPEHASH = keccak256(
|
||||
"PermitDetails(address token,uint160 amount,uint48 expiration,uint48 nonce)"
|
||||
);
|
||||
bytes32 _PERMIT_SINGLE_TYPEHASH = keccak256(
|
||||
"PermitSingle(PermitDetails details,address spender,uint256 sigDeadline)PermitDetails(address token,uint160 amount,uint48 expiration,uint48 nonce)"
|
||||
);
|
||||
bytes32 domainSeparator = keccak256(
|
||||
abi.encode(
|
||||
keccak256(
|
||||
"EIP712Domain(string name,uint256 chainId,address verifyingContract)"
|
||||
),
|
||||
keccak256("Permit2"),
|
||||
block.chainid,
|
||||
permit2Address
|
||||
)
|
||||
);
|
||||
bytes32 detailsHash =
|
||||
keccak256(abi.encode(_PERMIT_DETAILS_TYPEHASH, permit.details));
|
||||
bytes32 permitHash = keccak256(
|
||||
abi.encode(
|
||||
_PERMIT_SINGLE_TYPEHASH,
|
||||
detailsHash,
|
||||
permit.spender,
|
||||
permit.sigDeadline
|
||||
)
|
||||
);
|
||||
|
||||
bytes32 digest =
|
||||
keccak256(abi.encodePacked("\x19\x01", domainSeparator, permitHash));
|
||||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, digest);
|
||||
|
||||
return abi.encodePacked(r, s, v);
|
||||
}
|
||||
|
||||
function pleEncode(bytes[] memory data)
|
||||
public
|
||||
pure
|
||||
returns (bytes memory encoded)
|
||||
{
|
||||
for (uint256 i = 0; i < data.length; i++) {
|
||||
encoded = bytes.concat(
|
||||
encoded,
|
||||
abi.encodePacked(bytes2(uint16(data[i].length)), data[i])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function encodeSwap(
|
||||
uint8 tokenInIndex,
|
||||
uint8 tokenOutIndex,
|
||||
uint24 split,
|
||||
bytes memory protocolData
|
||||
) internal pure returns (bytes memory) {
|
||||
return
|
||||
abi.encodePacked(tokenInIndex, tokenOutIndex, split, protocolData);
|
||||
}
|
||||
|
||||
function encodeUniswapV2Swap(
|
||||
address tokenIn,
|
||||
address target,
|
||||
address receiver,
|
||||
bool zero2one
|
||||
) internal view returns (bytes memory) {
|
||||
return abi.encodePacked(
|
||||
usv2Executor, bytes4(0), tokenIn, target, receiver, zero2one
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user