Merge branch 'refs/heads/feature/gas-optimization' into router/dc/ENG-4411-refactor-callback-transient-storage
# Conflicts: # foundry/test/TychoRouter.t.sol # src/encoding/evm/strategy_encoder/strategy_encoders.rs Took 5 minutes Took 35 seconds
This commit is contained in:
@@ -6,9 +6,8 @@ use tycho_common::{
|
||||
Bytes,
|
||||
};
|
||||
use tycho_execution::encoding::{
|
||||
evm::encoder_builder::EVMEncoderBuilder,
|
||||
evm::encoder_builders::TychoRouterEncoderBuilder,
|
||||
models::{Solution, Swap},
|
||||
tycho_encoder::TychoEncoder,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
@@ -19,10 +18,9 @@ fn main() {
|
||||
.expect("Failed to create user address");
|
||||
|
||||
// Initialize the encoder
|
||||
let encoder = EVMEncoderBuilder::new()
|
||||
let encoder = TychoRouterEncoderBuilder::new()
|
||||
.chain(Chain::Ethereum)
|
||||
.initialize_tycho_router_with_permit2(swapper_pk)
|
||||
.expect("Failed to create encoder builder")
|
||||
.swapper_pk(swapper_pk)
|
||||
.build()
|
||||
.expect("Failed to build encoder");
|
||||
|
||||
@@ -64,7 +62,7 @@ fn main() {
|
||||
|
||||
// Encode the solution
|
||||
let tx = encoder
|
||||
.encode_router_calldata(vec![solution.clone()])
|
||||
.encode_calldata(vec![solution.clone()])
|
||||
.expect("Failed to encode router calldata")[0]
|
||||
.clone();
|
||||
println!(" ====== Simple swap WETH -> USDC ======");
|
||||
@@ -137,7 +135,7 @@ fn main() {
|
||||
|
||||
// Encode the solution
|
||||
let complex_tx = encoder
|
||||
.encode_router_calldata(vec![complex_solution])
|
||||
.encode_calldata(vec![complex_solution])
|
||||
.expect("Failed to encode router calldata")[0]
|
||||
.clone();
|
||||
|
||||
|
||||
@@ -2,36 +2,42 @@
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
library LibSwap {
|
||||
/// Returns the InToken index into an array of tokens
|
||||
function tokenInIndex(
|
||||
bytes calldata swap
|
||||
) internal pure returns (uint8 res) {
|
||||
res = uint8(swap[0]);
|
||||
/**
|
||||
* @dev Returns arguments required to perform a single swap
|
||||
*/
|
||||
function decodeSingleSwap(bytes calldata swap)
|
||||
internal
|
||||
pure
|
||||
returns (address executor, bytes calldata protocolData)
|
||||
{
|
||||
executor = address(uint160(bytes20(swap[0:20])));
|
||||
protocolData = swap[20:];
|
||||
}
|
||||
|
||||
/// The OutToken index into an array of tokens
|
||||
function tokenOutIndex(
|
||||
bytes calldata swap
|
||||
) internal pure returns (uint8 res) {
|
||||
res = uint8(swap[1]);
|
||||
/**
|
||||
* @dev Returns arguments required to perform a sequential swap
|
||||
*/
|
||||
function decodeSequentialSwap(bytes calldata swap)
|
||||
internal
|
||||
pure
|
||||
returns (address executor, bytes calldata protocolData)
|
||||
{
|
||||
executor = address(uint160(bytes20(swap[0:20])));
|
||||
protocolData = swap[20:];
|
||||
}
|
||||
|
||||
/// The relative amount of token quantity routed into this swap
|
||||
function splitPercentage(
|
||||
bytes calldata swap
|
||||
) internal pure returns (uint24 res) {
|
||||
res = uint24(bytes3(swap[2:5]));
|
||||
}
|
||||
|
||||
/// The address of the executor contract
|
||||
function executor(bytes calldata swap) internal pure returns (address res) {
|
||||
res = address(uint160(bytes20(swap[5:25])));
|
||||
}
|
||||
|
||||
/// Remaining bytes are interpreted as protocol data
|
||||
function protocolData(
|
||||
bytes calldata swap
|
||||
) internal pure returns (bytes calldata res) {
|
||||
res = swap[25:];
|
||||
/**
|
||||
* @dev Returns arguments required to perform a split swap
|
||||
*/
|
||||
function decodeSplitSwap(bytes calldata swap)
|
||||
internal
|
||||
pure
|
||||
returns (uint8 tokenInIndex, uint8 tokenOutIndex, uint24 split, address executor, bytes calldata protocolData)
|
||||
{
|
||||
tokenInIndex = uint8(swap[0]);
|
||||
tokenOutIndex = uint8(swap[1]);
|
||||
split = uint24(bytes3(swap[2:5]));
|
||||
executor = address(uint160(bytes20(swap[5:25])));
|
||||
protocolData = swap[25:];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ pragma solidity ^0.8.26;
|
||||
import "@interfaces/IExecutor.sol";
|
||||
import "@interfaces/ICallback.sol";
|
||||
|
||||
error Dispatcher__UnapprovedExecutor();
|
||||
error Dispatcher__UnapprovedExecutor(address executor);
|
||||
error Dispatcher__NonContractExecutor();
|
||||
error Dispatcher__InvalidDataLength();
|
||||
|
||||
@@ -59,7 +59,7 @@ contract Dispatcher {
|
||||
bytes calldata data
|
||||
) internal returns (uint256 calculatedAmount) {
|
||||
if (!executors[executor]) {
|
||||
revert Dispatcher__UnapprovedExecutor();
|
||||
revert Dispatcher__UnapprovedExecutor(executor);
|
||||
}
|
||||
|
||||
assembly {
|
||||
@@ -95,7 +95,7 @@ contract Dispatcher {
|
||||
}
|
||||
|
||||
if (!executors[executor]) {
|
||||
revert Dispatcher__UnapprovedExecutor();
|
||||
revert Dispatcher__UnapprovedExecutor(executor);
|
||||
}
|
||||
|
||||
// slither-disable-next-line controlled-delegatecall,low-level-calls
|
||||
|
||||
@@ -132,7 +132,7 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
*
|
||||
* @return amountOut The total amount of the output token received by the receiver, after deducting fees if applicable.
|
||||
*/
|
||||
function swap(
|
||||
function splitSwap(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
@@ -148,7 +148,7 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
msg.sender, address(this), amountIn
|
||||
);
|
||||
}
|
||||
return _swapChecked(
|
||||
return _splitSwapChecked(
|
||||
amountIn,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
@@ -188,7 +188,7 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
*
|
||||
* @return amountOut The total amount of the output token received by the receiver, after deducting fees if applicable.
|
||||
*/
|
||||
function swapPermit2(
|
||||
function splitSwapPermit2(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
@@ -212,7 +212,7 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
);
|
||||
}
|
||||
|
||||
return _swapChecked(
|
||||
return _splitSwapChecked(
|
||||
amountIn,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
@@ -226,14 +226,225 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Internal implementation of the core swap logic shared between swap() and swapPermit2().
|
||||
* @notice Executes a swap operation based on a predefined swap graph with no split routes.
|
||||
* This function enables multi-step swaps, optional ETH wrapping/unwrapping, and validates the output amount
|
||||
* against a user-specified minimum. This function performs a transferFrom to retrieve tokens from the caller.
|
||||
*
|
||||
* @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.
|
||||
* - Swaps are executed sequentially using the `_swap` 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 `minAmountOut` is greater than 0.
|
||||
*
|
||||
* @param amountIn The input token amount to be swapped.
|
||||
* @param tokenIn The address of the input token. Use `address(0)` for native ETH
|
||||
* @param tokenOut The address of the output token. Use `address(0)` for native ETH
|
||||
* @param minAmountOut The minimum acceptable amount of the output token. Reverts if this condition is not met. This should always be set to avoid losing funds due to slippage.
|
||||
* @param wrapEth If true, wraps the input token (native ETH) into WETH.
|
||||
* @param unwrapEth If true, unwraps the resulting WETH into native ETH and sends it to the receiver.
|
||||
* @param receiver The address to receive the output tokens.
|
||||
* @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 sequentialSwap(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint256 minAmountOut,
|
||||
bool wrapEth,
|
||||
bool unwrapEth,
|
||||
address receiver,
|
||||
bytes calldata swaps
|
||||
) public payable whenNotPaused nonReentrant returns (uint256 amountOut) {
|
||||
IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
|
||||
return _sequentialSwapChecked(
|
||||
amountIn,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
minAmountOut,
|
||||
wrapEth,
|
||||
unwrapEth,
|
||||
receiver,
|
||||
swaps
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Executes a swap operation based on a predefined swap graph with no split routes.
|
||||
* 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.
|
||||
* - 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 `minAmountOut` is greater than 0.
|
||||
*
|
||||
* @param amountIn The input token amount to be swapped.
|
||||
* @param tokenIn The address of the input token. Use `address(0)` for native ETH
|
||||
* @param tokenOut The address of the output token. Use `address(0)` for native ETH
|
||||
* @param minAmountOut The minimum acceptable amount of the output token. Reverts if this condition is not met. This should always be set to avoid losing funds due to slippage.
|
||||
* @param wrapEth If true, wraps the input token (native ETH) into WETH.
|
||||
* @param unwrapEth If true, unwraps the resulting WETH into native ETH and sends it to the receiver.
|
||||
* @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 sequentialSwapPermit2(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint256 minAmountOut,
|
||||
bool wrapEth,
|
||||
bool unwrapEth,
|
||||
address receiver,
|
||||
IAllowanceTransfer.PermitSingle calldata permitSingle,
|
||||
bytes calldata signature,
|
||||
bytes calldata swaps
|
||||
) external payable whenNotPaused nonReentrant returns (uint256 amountOut) {
|
||||
// For native ETH, assume funds already in our router. Else, transfer and handle approval.
|
||||
if (tokenIn != address(0)) {
|
||||
permit2.permit(msg.sender, permitSingle, signature);
|
||||
permit2.transferFrom(
|
||||
msg.sender,
|
||||
address(this),
|
||||
uint160(amountIn),
|
||||
permitSingle.details.token
|
||||
);
|
||||
}
|
||||
|
||||
return _sequentialSwapChecked(
|
||||
amountIn,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
minAmountOut,
|
||||
wrapEth,
|
||||
unwrapEth,
|
||||
receiver,
|
||||
swaps
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Executes a single swap operation.
|
||||
* This function enables optional ETH wrapping/unwrapping, and validates the output amount against a user-specified minimum.
|
||||
* This function performs a transferFrom to retrieve tokens from the caller.
|
||||
*
|
||||
* @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.
|
||||
* - 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 `minAmountOut` is greater than 0.
|
||||
*
|
||||
* @param amountIn The input token amount to be swapped.
|
||||
* @param tokenIn The address of the input token. Use `address(0)` for native ETH
|
||||
* @param tokenOut The address of the output token. Use `address(0)` for native ETH
|
||||
* @param minAmountOut The minimum acceptable amount of the output token. Reverts if this condition is not met. This should always be set to avoid losing funds due to slippage.
|
||||
* @param wrapEth If true, wraps the input token (native ETH) into WETH.
|
||||
* @param unwrapEth If true, unwraps the resulting WETH into native ETH and sends it to the receiver.
|
||||
* @param receiver The address to receive the output tokens.
|
||||
* @param swapData Encoded swap details.
|
||||
*
|
||||
* @return amountOut The total amount of the output token received by the receiver, after deducting fees if applicable.
|
||||
*/
|
||||
function singleSwap(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint256 minAmountOut,
|
||||
bool wrapEth,
|
||||
bool unwrapEth,
|
||||
address receiver,
|
||||
bytes calldata swapData
|
||||
) public payable whenNotPaused nonReentrant returns (uint256 amountOut) {
|
||||
IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
|
||||
return _singleSwap(
|
||||
amountIn,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
minAmountOut,
|
||||
wrapEth,
|
||||
unwrapEth,
|
||||
receiver,
|
||||
swapData
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Executes a single swap operation.
|
||||
* This function enables 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.
|
||||
* - 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 `minAmountOut` is greater than 0.
|
||||
*
|
||||
* @param amountIn The input token amount to be swapped.
|
||||
* @param tokenIn The address of the input token. Use `address(0)` for native ETH
|
||||
* @param tokenOut The address of the output token. Use `address(0)` for native ETH
|
||||
* @param minAmountOut The minimum acceptable amount of the output token. Reverts if this condition is not met. This should always be set to avoid losing funds due to slippage.
|
||||
* @param wrapEth If true, wraps the input token (native ETH) into WETH.
|
||||
* @param unwrapEth If true, unwraps the resulting WETH into native ETH and sends it to the receiver.
|
||||
* @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 swapData Encoded swap details.
|
||||
*
|
||||
* @return amountOut The total amount of the output token received by the receiver, after deducting fees if applicable.
|
||||
*/
|
||||
function singleSwapPermit2(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint256 minAmountOut,
|
||||
bool wrapEth,
|
||||
bool unwrapEth,
|
||||
address receiver,
|
||||
IAllowanceTransfer.PermitSingle calldata permitSingle,
|
||||
bytes calldata signature,
|
||||
bytes calldata swapData
|
||||
) external payable whenNotPaused nonReentrant returns (uint256 amountOut) {
|
||||
// For native ETH, assume funds already in our router. Else, transfer and handle approval.
|
||||
if (tokenIn != address(0)) {
|
||||
permit2.permit(msg.sender, permitSingle, signature);
|
||||
permit2.transferFrom(
|
||||
msg.sender,
|
||||
address(this),
|
||||
uint160(amountIn),
|
||||
permitSingle.details.token
|
||||
);
|
||||
}
|
||||
|
||||
return _singleSwap(
|
||||
amountIn,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
minAmountOut,
|
||||
wrapEth,
|
||||
unwrapEth,
|
||||
receiver,
|
||||
swapData
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Internal implementation of the core swap logic shared between splitSwap() and splitSwapPermit2().
|
||||
*
|
||||
* @notice This function centralizes the swap execution logic.
|
||||
* @notice For detailed documentation on parameters and behavior, see the documentation for
|
||||
* swap() and swapPermit2() functions.
|
||||
* splitSwap() and splitSwapPermit2() functions.
|
||||
*
|
||||
*/
|
||||
function _swapChecked(
|
||||
function _splitSwapChecked(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
@@ -261,7 +472,147 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
|
||||
amountOut = _swap(amountIn, nTokens, swaps);
|
||||
amountOut = _splitSwap(amountIn, nTokens, swaps);
|
||||
uint256 currentBalance = tokenIn == address(0)
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
|
||||
uint256 amountConsumed = initialBalance - currentBalance;
|
||||
|
||||
if (tokenIn != tokenOut && amountConsumed != amountIn) {
|
||||
revert TychoRouter__AmountInDiffersFromConsumed(
|
||||
amountIn, amountConsumed
|
||||
);
|
||||
}
|
||||
|
||||
if (fee > 0) {
|
||||
uint256 feeAmount = (amountOut * fee) / 10000;
|
||||
amountOut -= feeAmount;
|
||||
IERC20(tokenOut).safeTransfer(feeReceiver, feeAmount);
|
||||
}
|
||||
|
||||
if (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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Internal implementation of the core swap logic shared between singleSwap() and singleSwapPermit2().
|
||||
*
|
||||
* @notice This function centralizes the swap execution logic.
|
||||
* @notice For detailed documentation on parameters and behavior, see the documentation for
|
||||
* singleSwap() and singleSwapPermit2() functions.
|
||||
*
|
||||
*/
|
||||
function _singleSwap(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint256 minAmountOut,
|
||||
bool wrapEth,
|
||||
bool unwrapEth,
|
||||
address receiver,
|
||||
bytes calldata swap_
|
||||
) internal returns (uint256 amountOut) {
|
||||
if (receiver == address(0)) {
|
||||
revert TychoRouter__AddressZero();
|
||||
}
|
||||
if (minAmountOut == 0) {
|
||||
revert TychoRouter__UndefinedMinAmountOut();
|
||||
}
|
||||
|
||||
// Assume funds are already in the router.
|
||||
if (wrapEth) {
|
||||
_wrapETH(amountIn);
|
||||
tokenIn = address(_weth);
|
||||
}
|
||||
|
||||
uint256 initialBalance = tokenIn == address(0)
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
|
||||
(address executor, bytes calldata protocolData) =
|
||||
swap_.decodeSingleSwap();
|
||||
|
||||
amountOut = _callExecutor(executor, amountIn, protocolData);
|
||||
uint256 currentBalance = tokenIn == address(0)
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
|
||||
uint256 amountConsumed = initialBalance - currentBalance;
|
||||
|
||||
if (amountConsumed != amountIn) {
|
||||
revert TychoRouter__AmountInDiffersFromConsumed(
|
||||
amountIn, amountConsumed
|
||||
);
|
||||
}
|
||||
|
||||
if (fee > 0) {
|
||||
uint256 feeAmount = (amountOut * fee) / 10000;
|
||||
amountOut -= feeAmount;
|
||||
IERC20(tokenOut).safeTransfer(feeReceiver, feeAmount);
|
||||
}
|
||||
|
||||
if (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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Internal implementation of the core swap logic shared between sequentialSwap() and sequentialSwapPermit2().
|
||||
*
|
||||
* @notice This function centralizes the swap execution logic.
|
||||
* @notice For detailed documentation on parameters and behavior, see the documentation for
|
||||
* sequentialSwap() and sequentialSwapPermit2() functions.
|
||||
*
|
||||
*/
|
||||
function _sequentialSwapChecked(
|
||||
uint256 amountIn,
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint256 minAmountOut,
|
||||
bool wrapEth,
|
||||
bool unwrapEth,
|
||||
address receiver,
|
||||
bytes calldata swaps
|
||||
) internal returns (uint256 amountOut) {
|
||||
if (receiver == address(0)) {
|
||||
revert TychoRouter__AddressZero();
|
||||
}
|
||||
if (minAmountOut == 0) {
|
||||
revert TychoRouter__UndefinedMinAmountOut();
|
||||
}
|
||||
|
||||
// Assume funds are already in the router.
|
||||
if (wrapEth) {
|
||||
_wrapETH(amountIn);
|
||||
tokenIn = address(_weth);
|
||||
}
|
||||
|
||||
uint256 initialBalance = tokenIn == address(0)
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
|
||||
amountOut = _sequentialSwap(amountIn, swaps);
|
||||
|
||||
uint256 currentBalance = tokenIn == address(0)
|
||||
? address(this).balance
|
||||
: IERC20(tokenIn).balanceOf(address(this));
|
||||
@@ -317,10 +668,11 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
*
|
||||
* @return The total amount of the buy token obtained after all swaps have been executed.
|
||||
*/
|
||||
function _swap(uint256 amountIn, uint256 nTokens, bytes calldata swaps_)
|
||||
internal
|
||||
returns (uint256)
|
||||
{
|
||||
function _splitSwap(
|
||||
uint256 amountIn,
|
||||
uint256 nTokens,
|
||||
bytes calldata swaps_
|
||||
) internal returns (uint256) {
|
||||
if (swaps_.length == 0) {
|
||||
revert TychoRouter__EmptySwaps();
|
||||
}
|
||||
@@ -330,6 +682,8 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
uint8 tokenInIndex = 0;
|
||||
uint8 tokenOutIndex = 0;
|
||||
uint24 split;
|
||||
address executor;
|
||||
bytes calldata protocolData;
|
||||
bytes calldata swapData;
|
||||
|
||||
uint256[] memory remainingAmounts = new uint256[](nTokens);
|
||||
@@ -340,17 +694,16 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
|
||||
while (swaps_.length > 0) {
|
||||
(swapData, swaps_) = swaps_.next();
|
||||
tokenInIndex = swapData.tokenInIndex();
|
||||
tokenOutIndex = swapData.tokenOutIndex();
|
||||
split = swapData.splitPercentage();
|
||||
|
||||
(tokenInIndex, tokenOutIndex, split, executor, protocolData) =
|
||||
swapData.decodeSplitSwap();
|
||||
|
||||
currentAmountIn = split > 0
|
||||
? (amounts[tokenInIndex] * split) / 0xffffff
|
||||
: remainingAmounts[tokenInIndex];
|
||||
|
||||
currentAmountOut = _callExecutor(
|
||||
swapData.executor(), currentAmountIn, swapData.protocolData()
|
||||
);
|
||||
currentAmountOut =
|
||||
_callExecutor(executor, currentAmountIn, protocolData);
|
||||
// Checks if the output token is the same as the input token
|
||||
if (tokenOutIndex == 0) {
|
||||
cyclicSwapAmountOut += currentAmountOut;
|
||||
@@ -363,6 +716,31 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
return tokenOutIndex == 0 ? cyclicSwapAmountOut : amounts[tokenOutIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Executes sequential swaps as defined by the provided swap graph.
|
||||
*
|
||||
* @param amountIn The initial amount of the sell token to be swapped.
|
||||
* @param swaps_ Encoded swap graph data containing the details of each swap operation.
|
||||
*
|
||||
* @return calculatedAmount The total amount of the buy token obtained after all swaps have been executed.
|
||||
*/
|
||||
function _sequentialSwap(uint256 amountIn, bytes calldata swaps_)
|
||||
internal
|
||||
returns (uint256 calculatedAmount)
|
||||
{
|
||||
bytes calldata swap;
|
||||
calculatedAmount = amountIn;
|
||||
while (swaps_.length > 0) {
|
||||
(swap, swaps_) = swaps_.next();
|
||||
|
||||
(address executor, bytes calldata protocolData) =
|
||||
swap.decodeSingleSwap();
|
||||
|
||||
calculatedAmount =
|
||||
_callExecutor(executor, calculatedAmount, protocolData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev We use the fallback function to allow flexibility on callback.
|
||||
*/
|
||||
|
||||
@@ -7,7 +7,45 @@ import "../lib/LibSwap.sol";
|
||||
contract LibSwapTest is Test {
|
||||
using LibSwap for bytes;
|
||||
|
||||
function testSwap() public view {
|
||||
function testSingleSwap() public view {
|
||||
address executor = 0x1234567890123456789012345678901234567890;
|
||||
bytes memory protocolData = abi.encodePacked(uint256(123));
|
||||
|
||||
bytes memory swap = abi.encodePacked(executor, protocolData);
|
||||
this.assertSingleSwap(swap, executor, protocolData);
|
||||
}
|
||||
|
||||
function assertSingleSwap(
|
||||
bytes calldata swap,
|
||||
address executor,
|
||||
bytes calldata protocolData
|
||||
) public pure {
|
||||
(address decodedExecutor, bytes memory decodedProtocolData) =
|
||||
swap.decodeSingleSwap();
|
||||
assertEq(decodedExecutor, executor);
|
||||
assertEq(decodedProtocolData, protocolData);
|
||||
}
|
||||
|
||||
function testSequentialSwap() public view {
|
||||
address executor = 0x1234567890123456789012345678901234567890;
|
||||
bytes memory protocolData = abi.encodePacked(uint256(234));
|
||||
|
||||
bytes memory swap = abi.encodePacked(executor, protocolData);
|
||||
this.assertSequentialSwap(swap, executor, protocolData);
|
||||
}
|
||||
|
||||
function assertSequentialSwap(
|
||||
bytes calldata swap,
|
||||
address executor,
|
||||
bytes calldata protocolData
|
||||
) public pure {
|
||||
(address decodedExecutor, bytes memory decodedProtocolData) =
|
||||
swap.decodeSequentialSwap();
|
||||
assertEq(decodedExecutor, executor);
|
||||
assertEq(decodedProtocolData, protocolData);
|
||||
}
|
||||
|
||||
function testSplitSwap() public view {
|
||||
uint8 tokenInIndex = 1;
|
||||
uint8 tokenOutIndex = 2;
|
||||
uint24 split = 3;
|
||||
@@ -17,20 +55,32 @@ contract LibSwapTest is Test {
|
||||
bytes memory swap = abi.encodePacked(
|
||||
tokenInIndex, tokenOutIndex, split, executor, protocolData
|
||||
);
|
||||
this.assertSwap(swap, tokenInIndex, tokenOutIndex, split, executor);
|
||||
this.assertSplitSwap(
|
||||
swap, tokenInIndex, tokenOutIndex, split, executor, protocolData
|
||||
);
|
||||
}
|
||||
|
||||
// This is necessary so that the compiler accepts bytes as a LibSwap.sol
|
||||
function assertSwap(
|
||||
// This is necessary so that the compiler accepts bytes as a LibSwap.sol for testing
|
||||
// This is because this function takes calldata as input
|
||||
function assertSplitSwap(
|
||||
bytes calldata swap,
|
||||
uint8 tokenInIndex,
|
||||
uint8 tokenOutIndex,
|
||||
uint24 split,
|
||||
address executor
|
||||
address executor,
|
||||
bytes calldata protocolData
|
||||
) public pure {
|
||||
assert(swap.tokenInIndex() == tokenInIndex);
|
||||
assert(swap.tokenOutIndex() == tokenOutIndex);
|
||||
assert(swap.splitPercentage() == split);
|
||||
assert(swap.executor() == executor);
|
||||
(
|
||||
uint8 decodedTokenInIndex,
|
||||
uint8 decodedTokenOutIndex,
|
||||
uint24 decodedSplit,
|
||||
address decodedExecutor,
|
||||
bytes memory decodedProtocolData
|
||||
) = swap.decodeSplitSwap();
|
||||
assertEq(decodedTokenInIndex, tokenInIndex);
|
||||
assertEq(decodedTokenOutIndex, tokenOutIndex);
|
||||
assertEq(decodedSplit, split);
|
||||
assertEq(decodedExecutor, executor);
|
||||
assertEq(decodedProtocolData, protocolData);
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
353
foundry/test/TychoRouterIntegration.t.sol
Normal file
353
foundry/test/TychoRouterIntegration.t.sol
Normal file
@@ -0,0 +1,353 @@
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
import "./TychoRouterTestSetup.sol";
|
||||
|
||||
contract TychoRouterTestIntegration is TychoRouterTestSetup {
|
||||
function testSplitSwapSingleIntegration() public {
|
||||
// Tests swapping WETH -> DAI on a USV2 pool
|
||||
deal(WETH_ADDR, ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
// Encoded solution generated using `test_split_swap_strategy_encoder_simple`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"7c5538460000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000903146e5f6c59c064b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000681362ea00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ebdcf2000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041a82e9bdde90314de4b1bf918cc2e8b27da98adcab46e8e99d4e77472a572d6381837e9453095f4cc5e9b25691b678288174e547e040a67d12b36ddfdd1e672d21b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000058005600010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d01395000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 2659881924818443699787);
|
||||
}
|
||||
|
||||
function testSplitSwapSingleWithoutPermit2Integration() public {
|
||||
// Tests swapping WETH -> DAI on a USV2 pool without permit2
|
||||
deal(WETH_ADDR, ALICE, 1 ether);
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), 1 ether);
|
||||
uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
// Encoded solution generated using `test_split_swap_strategy_encoder_simple_route_no_permit2`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"79b9b93b0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000008f1d5c1cae37400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000058005600010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d01395000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 2659881924818443699787);
|
||||
}
|
||||
|
||||
function testSplitUSV4Integration() public {
|
||||
// Test created with calldata from our router encoder.
|
||||
|
||||
// Performs a sequential swap from USDC to PEPE though ETH using two
|
||||
// consecutive USV4 pools
|
||||
//
|
||||
// USDC ──(USV4)──> ETH ───(USV4)──> PEPE
|
||||
//
|
||||
deal(USDC_ADDR, ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(PEPE_ADDR).balanceOf(ALICE);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(USDC_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
// Encoded solution generated using `test_split_encoding_strategy_usv4`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"7c553846000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000006982508145454ce325ddbe47a25d4ec3d23119330000000000000000000000000000000000000000005064ff624d54346285543f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000006814875700000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ed015f0000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000413a7c6367c69ac46fc2b633fd53e583b74b20ec9b3ea83b069fe564765560a4cb335af200fd90ddb5f56d11e469c11a97420499f1b3ee0c1db13149a74daa90db1b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008c008a0001000000f62849f9a0b5bf2913b396098f7c7019b51a820aa0b86991c6218b36c1d19d4a2e9eb0ce3606eb486982508145454ce325ddbe47a25d4ec3d231193300f62849f9a0b5bf2913b396098f7c7019b51a820a0000000000000000000000000000000000000000000bb800003c6982508145454ce325ddbe47a25d4ec3d23119330061a80001f40000000000000000000000000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = IERC20(PEPE_ADDR).balanceOf(ALICE);
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 97191013220606467325121599);
|
||||
}
|
||||
|
||||
function testSplitUSV4IntegrationInputETH() public {
|
||||
// Test created with calldata from our router encoder.
|
||||
|
||||
// Performs a single swap from ETH to PEPE without wrapping or unwrapping
|
||||
//
|
||||
// ETH ───(USV4)──> PEPE
|
||||
//
|
||||
deal(ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(PEPE_ADDR).balanceOf(ALICE);
|
||||
|
||||
// Encoded solution generated using `test_split_encoding_strategy_usv4_eth_in`
|
||||
(bool success,) = tychoRouterAddr.call{value: 1 ether}(
|
||||
hex"7c5538460000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006982508145454ce325ddbe47a25d4ec3d2311933000000000000000000000000000000000000000000c87c939ae635f92dc2379c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000006814877000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ed017800000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000004193acc98d79044e8ec1bc3ced832dc679e38ac8c6fe9b5befd1e5e44cb44edb0e365f1c5d6e3ca6590ed1a053f1841aede29e5b573f046387aff794520a0f22581b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007200700001000000f62849f9a0b5bf2913b396098f7c7019b51a820a00000000000000000000000000000000000000006982508145454ce325ddbe47a25d4ec3d231193301f62849f9a0b5bf2913b396098f7c7019b51a820a6982508145454ce325ddbe47a25d4ec3d23119330061a80001f40000000000000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = IERC20(PEPE_ADDR).balanceOf(ALICE);
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 242373460199848577067005852);
|
||||
}
|
||||
|
||||
function testSplitUSV4IntegrationOutputETH() public {
|
||||
// Test created with calldata from our router encoder.
|
||||
|
||||
// Performs a single swap from USDC to ETH without wrapping or unwrapping
|
||||
//
|
||||
// USDC ───(USV4)──> ETH
|
||||
//
|
||||
deal(USDC_ADDR, ALICE, 3000_000000);
|
||||
uint256 balancerBefore = ALICE.balance;
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(USDC_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
|
||||
// Encoded solution generated using `test_split_encoding_strategy_usv4_eth_out`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"7c55384600000000000000000000000000000000000000000000000000000000b2d05e00000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f81490b4f29aade000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000b2d05e00000000000000000000000000000000000000000000000000000000006814878000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ed018800000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000004190134d2d142caff6dbea417292a15685119bd676b2b73bad35fe39f720f7c3163f16d057327499019506b6f690a3916fd3375c579c9cb814113b1516187380531b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007200700001000000f62849f9a0b5bf2913b396098f7c7019b51a820aa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000f62849f9a0b5bf2913b396098f7c7019b51a820a0000000000000000000000000000000000000000000bb800003c0000000000000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = ALICE.balance;
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
console.logUint(balancerAfter - balancerBefore);
|
||||
assertEq(balancerAfter - balancerBefore, 1117254495486192350);
|
||||
}
|
||||
|
||||
function testSplitSwapSingleWithWrapIntegration() public {
|
||||
// Tests swapping WETH -> DAI on a USV2 pool, but ETH is received from the user
|
||||
// and wrapped before the swap
|
||||
deal(ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
// Encoded solution generated using `test_split_swap_strategy_encoder_simple_route_wrap`
|
||||
(bool success,) = tychoRouterAddr.call{value: 1 ether}(
|
||||
hex"7c5538460000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000903146e5f6c59c064b000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000006813638900000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ebdd91000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041f6ff7411a7ec76cb7dcafecf5e7f11121b1aa88af505635dc7faae6057e4f44e2859712f58331a14a1624f1e5edf2af80ddd2d90b5453d74df1b1fea10b9a2f91c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000058005600020000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d01395000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 2659881924818443699787);
|
||||
}
|
||||
|
||||
function testSplitSwapSingleWithUnwrapIntegration() public {
|
||||
// Tests swapping DAI -> WETH on a USV2 pool, and WETH is unwrapped to ETH
|
||||
// before sending back to the user
|
||||
deal(DAI_ADDR, ALICE, 3000 ether);
|
||||
uint256 balancerBefore = ALICE.balance;
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(DAI_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
// Encoded solution generated using `test_split_swap_strategy_encoder_simple_route_unwrap`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"7c5538460000000000000000000000000000000000000000000000a2a15d09519be000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000a2a15d09519be00000000000000000000000000000000000000000000000000000000000006813615200000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ebdb5a000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041a7da748b04674485a5da185055affefc85b6d8fe412accce55b6f67842116f0f7f7130de5d74c68c20e1cedcdf93b8741b9171de2e6a3f2567887382a0712e3f1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000058005600010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72f6b175474e89094c44da98b954eedeac495271d0fa478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d01395010000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = ALICE.balance;
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 1120007305574805922);
|
||||
}
|
||||
|
||||
function testSplitEkuboIntegration() public {
|
||||
// Test needs to be run on block 22082754 or later
|
||||
// notice that the addresses for the tycho router and the executors are different because we are redeploying
|
||||
vm.rollFork(22082754);
|
||||
tychoRouter = deployRouter();
|
||||
address[] memory executors = deployExecutors();
|
||||
vm.startPrank(EXECUTOR_SETTER);
|
||||
tychoRouter.setExecutors(executors);
|
||||
vm.stopPrank();
|
||||
|
||||
// TEMPORARY while the Ekubo executor address is hardcoded in TychoRouter
|
||||
// This allows us to change the code at that address to be the testing executor code
|
||||
vm.etch(
|
||||
0x4f88f6630a33dB05BEa1FeF7Dc7ff7508D1c531D,
|
||||
0x3D7Ebc40AF7092E3F1C81F2e996cbA5Cae2090d7.code
|
||||
);
|
||||
|
||||
deal(ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
// Encoded solution generated using `test_split_encoding_strategy_ekubo`
|
||||
(bool success,) = address(tychoRouter).call{value: 1 ether}(
|
||||
hex"79b9b93b0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000077007500010000003d7ebc40af7092e3f1c81f2e996cba5cae2090d7a4ad4f68d0b91cfd19687c881e50f3a00242828c0000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4851d02a5948496a67827242eabc5725531342527c000000000000000000000000000000000000000000"
|
||||
);
|
||||
|
||||
uint256 balancerAfter = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
assertGe(balancerAfter - balancerBefore, 26173932);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testSplitSwapIntegration() public {
|
||||
// Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools
|
||||
//
|
||||
// ┌──(USV2)──> WBTC ───(USV2)──> USDC
|
||||
// WETH ─┤
|
||||
// └──(USV2)──> DAI ───(USV2)──> USDC
|
||||
deal(WETH_ADDR, ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
// Encoded solution generated using `test_split_swap_strategy_encoder_complex`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"7c5538460000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000681363a300000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ebddab0000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000415b7ff43991de10c4c3a0372653891d27eb305ce04228bfe46a7d84a0978063fc4cb05183f19b83511bcb689b002d4f8e170f1d3cd77cf18c638229ccb67e0cac1c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000160005600028000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d0139500005600010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d9403ede3eca2a72b3aecc820e955b36f38437d0139500005602030000005615deb798bb3e4dfa0139dfa1b3d433cc23b72f6b175474e89094c44da98b954eedeac495271d0fae461ca67b15dc8dc81ce7615e0320da1a9ab8d53ede3eca2a72b3aecc820e955b36f38437d0139501005601030000005615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a4163ede3eca2a72b3aecc820e955b36f38437d0139501"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
assertGe(balancerAfter - balancerBefore, 26173932);
|
||||
|
||||
// All input tokens are transferred to the router at first. Make sure we used
|
||||
// all of it (and thus our splits are correct).
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testSequentialSwapIntegrationPermit2() public {
|
||||
// Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools
|
||||
//
|
||||
// WETH ──(USV2)──> WBTC ───(USV2)──> USDC
|
||||
deal(WETH_ADDR, ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
// Encoded solution generated using `test_sequential_swap_strategy_encoder_complex_route`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"51bcc7b60000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000068168aea00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ef04f200000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000260000000000000000000000000000000000000000000000000000000000000004154956683effd126a9182e2d82ebd3d778e5283b93d571b13cdbc9dfbf3d9f655057a2332ed566f79bed7514a22ef1c52969132bc71a5a2ef125d78e39ec264511c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a600515615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d9403ede3eca2a72b3aecc820e955b36f38437d013950000515615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a4163ede3eca2a72b3aecc820e955b36f38437d01395010000000000000000000000000000000000000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 2552915143);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testSequentialSwapIntegration() public {
|
||||
// Performs a split swap from WETH to USDC though WBTC and DAI using USV2 pools
|
||||
//
|
||||
// WETH ──(USV2)──> WBTC ───(USV2)──> USDC
|
||||
deal(WETH_ADDR, ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(tychoRouterAddr, type(uint256).max);
|
||||
// Encoded solution generated using `test_sequential_swap_strategy_encoder_no_permit2`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"e8a980d70000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000018f61ec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000a600515615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d9403ede3eca2a72b3aecc820e955b36f38437d013950000515615deb798bb3e4dfa0139dfa1b3d433cc23b72f2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a4163ede3eca2a72b3aecc820e955b36f38437d01395010000000000000000000000000000000000000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 2552915143);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testCyclicSequentialSwapIntegration() public {
|
||||
deal(USDC_ADDR, ALICE, 100 * 10 ** 6);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(USDC_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
// Encoded solution generated using `test_cyclic_sequential_swap`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"7c5538460000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005f4308e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005f5e10000000000000000000000000000000000000000000000000000000000681363d200000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067ebddda0000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000418d58a54a3b8afc5d2e228ce6c5a1ab6b342cb5bfd9a00d57b869a4703ca2bb084d10d21f6842be9652a9ff2392673fbdcb961439ccc962de09f6bc64e5e665fe1c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de006d00010000002e234dae75c793f67a35089c9d99245e1c58470ba0b86991c6218b36c1d19d4a2e9eb0ce3606eb48c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f43ede3eca2a72b3aecc820e955b36f38437d0139588e6a0c2ddd26feeb64f039a2c41296fcb3f564001006d01000000002e234dae75c793f67a35089c9d99245e1c58470bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000bb83ede3eca2a72b3aecc820e955b36f38437d013958ad599c3a0ff1de082011efddc58f1908eb6e6d8000000"
|
||||
);
|
||||
|
||||
assertEq(IERC20(USDC_ADDR).balanceOf(ALICE), 99889294);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitInputCyclicSwapIntegration() public {
|
||||
deal(USDC_ADDR, ALICE, 100 * 10 ** 6);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(USDC_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
// Encoded solution generated using `test_split_input_cyclic_swap`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"7c5538460000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005ef619b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000000000000000000000000000000000006816408300000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067eeba8b0000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000416de253b927fdcf110d157372e620e70c7220d3c01f04e01cdffb076edbb8b42052d281dd6c55a2349502742a0a8de58d2d1dbdc452f6c9d695b1c732c023d0561c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000136006d00019999992e234dae75c793f67a35089c9d99245e1c58470ba0b86991c6218b36c1d19d4a2e9eb0ce3606eb48c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f43ede3eca2a72b3aecc820e955b36f38437d0139588e6a0c2ddd26feeb64f039a2c41296fcb3f564001006d00010000002e234dae75c793f67a35089c9d99245e1c58470ba0b86991c6218b36c1d19d4a2e9eb0ce3606eb48c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000bb83ede3eca2a72b3aecc820e955b36f38437d013958ad599c3a0ff1de082011efddc58f1908eb6e6d801005601000000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2b4e16d0168e52d35cacd2c6185b44281ec28c9dc3ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000"
|
||||
);
|
||||
|
||||
assertEq(IERC20(USDC_ADDR).balanceOf(ALICE), 99574171);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitOutputCyclicSwapIntegration() public {
|
||||
deal(USDC_ADDR, ALICE, 100 * 10 ** 6);
|
||||
|
||||
// Approve permit2
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(USDC_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
// Encoded solution generated using `test_split_output_cyclic_swap`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"7c5538460000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005eea514000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000000000000000000000000000000000006816418400000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067eebb8c0000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000000412c44c7de8f7eaaea61e49dbdefdc5606925db6f93db0789e632899ac88d3c7677cc8b69719603ab1b5ecef07d659b7254881d0667a49ebccbf43949b760b041a1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000136005600010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48b4e16d0168e52d35cacd2c6185b44281ec28c9dc3ede3eca2a72b3aecc820e955b36f38437d0139501006d01009999992e234dae75c793f67a35089c9d99245e1c58470bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f43ede3eca2a72b3aecc820e955b36f38437d0139588e6a0c2ddd26feeb64f039a2c41296fcb3f564000006d01000000002e234dae75c793f67a35089c9d99245e1c58470bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000bb83ede3eca2a72b3aecc820e955b36f38437d013958ad599c3a0ff1de082011efddc58f1908eb6e6d80000000000000000000000"
|
||||
);
|
||||
|
||||
assertEq(IERC20(USDC_ADDR).balanceOf(ALICE), 99525908);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitCurveIntegration() public {
|
||||
deal(UWU_ADDR, ALICE, 1 ether);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(UWU_ADDR).approve(tychoRouterAddr, type(uint256).max);
|
||||
// Encoded solution generated using `test_split_encoding_strategy_curve`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"79b9b93b0000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000055c08ca52497e2f1534b59e2917bf524d4765257000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000005b005900010000001d1499e622d69689cdf9004d05ec547d650ff21155c08ca52497e2f1534b59e2917bf524d4765257c02aaa39b223fe8d0a0e5c4f27ead9083c756cc277146b0a1d08b6844376df6d9da99ba7f1b19e71020100010000000000"
|
||||
);
|
||||
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(ALICE), 4691958787921);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitCurveIntegrationStETH() public {
|
||||
deal(ALICE, 1 ether);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
// Encoded solution generated using `test_split_encoding_strategy_curve_st_eth`
|
||||
(bool success,) = tychoRouterAddr.call{value: 1 ether}(
|
||||
hex"79b9b93b0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ae7ab96520de3a18e5e111b5eaab095312d7fe840000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000005b005900010000001d1499e622d69689cdf9004d05ec547d650ff211eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeae7ab96520de3a18e5e111b5eaab095312d7fe84dc24316b9ae028f1497c275eb9192a3ea0f67022010001000000000000"
|
||||
);
|
||||
|
||||
assertEq(IERC20(STETH_ADDR).balanceOf(ALICE), 1000754689941529590);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
}
|
||||
344
foundry/test/TychoRouterSequentialSwap.t.sol
Normal file
344
foundry/test/TychoRouterSequentialSwap.t.sol
Normal file
@@ -0,0 +1,344 @@
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
import "@src/executors/UniswapV4Executor.sol";
|
||||
import {TychoRouter} from "@src/TychoRouter.sol";
|
||||
import "./TychoRouterTestSetup.sol";
|
||||
import "./executors/UniswapV4Utils.sol";
|
||||
import {SafeCallback} from "@uniswap/v4-periphery/src/base/SafeCallback.sol";
|
||||
|
||||
contract TychoRouterSequentialSwapTest is TychoRouterTestSetup {
|
||||
bytes32 public constant FEE_SETTER_ROLE =
|
||||
0xe6ad9a47fbda1dc18de1eb5eeb7d935e5e81b4748f3cfc61e233e64f88182060;
|
||||
|
||||
function _getSequentialSwaps() internal view returns (bytes[] memory) {
|
||||
// Trade 1 WETH for USDC through DAI with 2 swaps on Uniswap V2
|
||||
// 1 WETH -> DAI -> USDC
|
||||
// (univ2) (univ2)
|
||||
bytes[] memory swaps = new bytes[](2);
|
||||
// WETH -> DAI
|
||||
swaps[0] = encodeSequentialSwap(
|
||||
address(usv2Executor),
|
||||
encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
)
|
||||
);
|
||||
|
||||
// DAI -> USDC
|
||||
swaps[1] = encodeSequentialSwap(
|
||||
address(usv2Executor),
|
||||
encodeUniswapV2Swap(DAI_ADDR, DAI_USDC_POOL, tychoRouterAddr, true)
|
||||
);
|
||||
return swaps;
|
||||
}
|
||||
|
||||
function testSequentialSwapInternalMethod() public {
|
||||
// Trade 1 WETH for USDC through DAI - see _getSequentialSwaps for more info
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, tychoRouterAddr, amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSequentialSwaps();
|
||||
tychoRouter.exposedSequentialSwap(amountIn, pleEncode(swaps));
|
||||
|
||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(tychoRouterAddr);
|
||||
assertEq(usdcBalance, 2644659787);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testSequentialSwapPermit2() public {
|
||||
// Trade 1 WETH for USDC through DAI - see _getSequentialSwaps for more info
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(WETH_ADDR, amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSequentialSwaps();
|
||||
tychoRouter.sequentialSwapPermit2(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
1000_000000, // min amount
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
assertEq(usdcBalance, 2644659787);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testSequentialSwapNoPermit2() public {
|
||||
// Trade 1 WETH for USDC through DAI - see _getSequentialSwaps for more info
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(tychoRouterAddr, amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSequentialSwaps();
|
||||
tychoRouter.sequentialSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
1000_000000, // min amount
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
assertEq(usdcBalance, 2644659787);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testSequentialSwapUndefinedMinAmount() public {
|
||||
// Trade 1 WETH for USDC through DAI - see _getSequentialSwaps for more info
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(tychoRouterAddr, amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSequentialSwaps();
|
||||
vm.expectRevert(TychoRouter__UndefinedMinAmountOut.selector);
|
||||
tychoRouter.sequentialSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
0, // min amount
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
}
|
||||
|
||||
function testSequentialSwapInsufficientApproval() public {
|
||||
// Trade 1 WETH for USDC through DAI - see _getSequentialSwaps for more info
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(tychoRouterAddr, amountIn - 1);
|
||||
|
||||
bytes[] memory swaps = _getSequentialSwaps();
|
||||
vm.expectRevert();
|
||||
tychoRouter.sequentialSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
0, // min amount
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
}
|
||||
|
||||
function testSequentialSwapNegativeSlippageFailure() public {
|
||||
// Trade 1 WETH for USDC through DAI - see _getSequentialSwaps for more info
|
||||
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(WETH_ADDR, amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSequentialSwaps();
|
||||
|
||||
uint256 minAmountOut = 3000 * 1e18;
|
||||
|
||||
vm.expectRevert(
|
||||
abi.encodeWithSelector(
|
||||
TychoRouter__NegativeSlippage.selector,
|
||||
2644659787, // actual amountOut
|
||||
minAmountOut
|
||||
)
|
||||
);
|
||||
tychoRouter.sequentialSwapPermit2(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
minAmountOut,
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSequentialSwapFee() public {
|
||||
// Trade 1 WETH for USDC
|
||||
// Takes 1% fee at the end
|
||||
|
||||
vm.startPrank(FEE_SETTER);
|
||||
tychoRouter.setFee(100);
|
||||
tychoRouter.setFeeReceiver(FEE_RECEIVER);
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(WETH_ADDR, amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSequentialSwaps();
|
||||
|
||||
uint256 amountOut = tychoRouter.sequentialSwapPermit2(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
1000_000000,
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
uint256 expectedAmount = 2618213190;
|
||||
assertEq(amountOut, expectedAmount);
|
||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
assertEq(usdcBalance, expectedAmount);
|
||||
assertEq(IERC20(USDC_ADDR).balanceOf(FEE_RECEIVER), 26446597);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSequentialSwapWrapETH() public {
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
|
||||
IAllowanceTransfer.PermitSingle memory emptyPermitSingle =
|
||||
IAllowanceTransfer.PermitSingle({
|
||||
details: IAllowanceTransfer.PermitDetails({
|
||||
token: address(0),
|
||||
amount: 0,
|
||||
expiration: 0,
|
||||
nonce: 0
|
||||
}),
|
||||
spender: address(0),
|
||||
sigDeadline: 0
|
||||
});
|
||||
|
||||
bytes[] memory swaps = _getSequentialSwaps();
|
||||
|
||||
uint256 amountOut = tychoRouter.sequentialSwapPermit2{value: amountIn}(
|
||||
amountIn,
|
||||
address(0),
|
||||
USDC_ADDR,
|
||||
1000_000000,
|
||||
true,
|
||||
false,
|
||||
ALICE,
|
||||
emptyPermitSingle,
|
||||
"",
|
||||
pleEncode(swaps)
|
||||
);
|
||||
uint256 expectedAmount = 2644659787;
|
||||
assertEq(amountOut, expectedAmount);
|
||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
assertEq(usdcBalance, expectedAmount);
|
||||
assertEq(ALICE.balance, 0);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSequentialSwapUnwrapETH() public {
|
||||
// Trade 3k DAI for WETH with 1 swap on Uniswap V2 and unwrap it at the end
|
||||
|
||||
uint256 amountIn = 3_000 * 10 ** 6;
|
||||
deal(USDC_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(USDC_ADDR, amountIn);
|
||||
|
||||
bytes[] memory swaps = new bytes[](2);
|
||||
|
||||
// USDC -> DAI
|
||||
swaps[0] = encodeSequentialSwap(
|
||||
address(usv2Executor),
|
||||
encodeUniswapV2Swap(
|
||||
USDC_ADDR, DAI_USDC_POOL, tychoRouterAddr, false
|
||||
)
|
||||
);
|
||||
|
||||
// DAI -> WETH
|
||||
swaps[1] = encodeSequentialSwap(
|
||||
address(usv2Executor),
|
||||
encodeUniswapV2Swap(DAI_ADDR, WETH_DAI_POOL, tychoRouterAddr, true)
|
||||
);
|
||||
|
||||
uint256 amountOut = tychoRouter.sequentialSwapPermit2(
|
||||
amountIn,
|
||||
USDC_ADDR,
|
||||
address(0),
|
||||
1 * 10 ** 18, // min amount
|
||||
false,
|
||||
true,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
uint256 expectedAmount = 1111174255471849849; // 1.11 ETH
|
||||
assertEq(amountOut, expectedAmount);
|
||||
assertEq(ALICE.balance, expectedAmount);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testCyclicSequentialSwap() public {
|
||||
// This test has start and end tokens that are the same
|
||||
// The flow is:
|
||||
// USDC --(USV3)--> WETH --(USV3)--> USDC
|
||||
uint256 amountIn = 100 * 10 ** 6;
|
||||
deal(USDC_ADDR, tychoRouterAddr, amountIn);
|
||||
|
||||
bytes memory usdcWethV3Pool1ZeroOneData = encodeUniswapV3Swap(
|
||||
USDC_ADDR, WETH_ADDR, tychoRouterAddr, USDC_WETH_USV3, true
|
||||
);
|
||||
|
||||
bytes memory usdcWethV3Pool2OneZeroData = encodeUniswapV3Swap(
|
||||
WETH_ADDR, USDC_ADDR, tychoRouterAddr, USDC_WETH_USV3_2, false
|
||||
);
|
||||
|
||||
bytes[] memory swaps = new bytes[](2);
|
||||
// USDC -> WETH
|
||||
swaps[0] = encodeSequentialSwap(
|
||||
address(usv3Executor), usdcWethV3Pool1ZeroOneData
|
||||
);
|
||||
// WETH -> USDC
|
||||
swaps[1] = encodeSequentialSwap(
|
||||
address(usv3Executor), usdcWethV3Pool2OneZeroData
|
||||
);
|
||||
|
||||
tychoRouter.exposedSequentialSwap(amountIn, pleEncode(swaps));
|
||||
assertEq(IERC20(USDC_ADDR).balanceOf(tychoRouterAddr), 99889294);
|
||||
}
|
||||
}
|
||||
345
foundry/test/TychoRouterSingleSwap.t.sol
Normal file
345
foundry/test/TychoRouterSingleSwap.t.sol
Normal file
@@ -0,0 +1,345 @@
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
import "@src/executors/UniswapV4Executor.sol";
|
||||
import {TychoRouter} from "@src/TychoRouter.sol";
|
||||
import "./TychoRouterTestSetup.sol";
|
||||
import "./executors/UniswapV4Utils.sol";
|
||||
import {SafeCallback} from "@uniswap/v4-periphery/src/base/SafeCallback.sol";
|
||||
|
||||
contract TychoRouterSingleSwapTest is TychoRouterTestSetup {
|
||||
bytes32 public constant FEE_SETTER_ROLE =
|
||||
0xe6ad9a47fbda1dc18de1eb5eeb7d935e5e81b4748f3cfc61e233e64f88182060;
|
||||
|
||||
function testSingleSwapPermit2() public {
|
||||
// Trade 1 WETH for DAI with 1 swap on Uniswap V2 using Permit2
|
||||
// 1 WETH -> DAI
|
||||
// (USV2)
|
||||
vm.startPrank(ALICE);
|
||||
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(WETH_ADDR, amountIn);
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap =
|
||||
encodeSingleSwap(address(usv2Executor), protocolData);
|
||||
|
||||
tychoRouter.singleSwapPermit2(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
2659881924818443699786,
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
swap
|
||||
);
|
||||
|
||||
uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertEq(daiBalance, 2659881924818443699787);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(ALICE), 0);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSingleSwapNoPermit2() public {
|
||||
// Trade 1 WETH for DAI with 1 swap on Uniswap V2
|
||||
// Checks amount out at the end
|
||||
uint256 amountIn = 1 ether;
|
||||
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
// Approve the tokenIn to be transferred to the router
|
||||
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), amountIn);
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap =
|
||||
encodeSingleSwap(address(usv2Executor), protocolData);
|
||||
|
||||
uint256 minAmountOut = 2600 * 1e18;
|
||||
uint256 amountOut = tychoRouter.singleSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
minAmountOut,
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
swap
|
||||
);
|
||||
|
||||
uint256 expectedAmount = 2659881924818443699787;
|
||||
assertEq(amountOut, expectedAmount);
|
||||
uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertEq(daiBalance, expectedAmount);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(ALICE), 0);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSingleSwapUndefinedMinAmount() public {
|
||||
// Trade 1 WETH for DAI with 1 swap on Uniswap V2
|
||||
// Checks amount out at the end
|
||||
uint256 amountIn = 1 ether;
|
||||
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), amountIn);
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap =
|
||||
encodeSingleSwap(address(usv2Executor), protocolData);
|
||||
|
||||
vm.expectRevert(TychoRouter__UndefinedMinAmountOut.selector);
|
||||
tychoRouter.singleSwap(
|
||||
amountIn, WETH_ADDR, DAI_ADDR, 0, false, false, ALICE, swap
|
||||
);
|
||||
}
|
||||
|
||||
function testSingleSwapInsufficientApproval() public {
|
||||
// Trade 1 WETH for DAI with 1 swap on Uniswap V2
|
||||
// Checks amount out at the end
|
||||
uint256 amountIn = 1 ether;
|
||||
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), amountIn - 1);
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap =
|
||||
encodeSingleSwap(address(usv2Executor), protocolData);
|
||||
|
||||
uint256 minAmountOut = 2600 * 1e18;
|
||||
vm.expectRevert();
|
||||
tychoRouter.singleSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
minAmountOut,
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
swap
|
||||
);
|
||||
}
|
||||
|
||||
function testSingleSwapNegativeSlippageFailure() public {
|
||||
// Trade 1 WETH for DAI with 1 swap on Uniswap V2
|
||||
// Checks amount out at the end
|
||||
uint256 amountIn = 1 ether;
|
||||
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
// Approve the tokenIn to be transferred to the router
|
||||
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), amountIn);
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap =
|
||||
encodeSingleSwap(address(usv2Executor), protocolData);
|
||||
|
||||
uint256 minAmountOut = 5600 * 1e18;
|
||||
|
||||
vm.expectRevert(
|
||||
abi.encodeWithSelector(
|
||||
TychoRouter__NegativeSlippage.selector,
|
||||
2659881924818443699787, // actual amountOut
|
||||
minAmountOut
|
||||
)
|
||||
);
|
||||
tychoRouter.singleSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
minAmountOut,
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
swap
|
||||
);
|
||||
}
|
||||
|
||||
function testSingleSwapFee() public {
|
||||
// Trade 1 WETH for DAI with 1 swap on Uniswap V2
|
||||
// Takes 1% fee at the end
|
||||
|
||||
vm.startPrank(FEE_SETTER);
|
||||
tychoRouter.setFee(100);
|
||||
tychoRouter.setFeeReceiver(FEE_RECEIVER);
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 amountIn = 1 ether;
|
||||
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
// Approve the tokenIn to be transferred to the router
|
||||
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), amountIn);
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap =
|
||||
encodeSingleSwap(address(usv2Executor), protocolData);
|
||||
|
||||
uint256 minAmountOut = 2600 * 1e18;
|
||||
uint256 amountOut = tychoRouter.singleSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
minAmountOut,
|
||||
false,
|
||||
false,
|
||||
ALICE,
|
||||
swap
|
||||
);
|
||||
|
||||
uint256 expectedAmount = 2633283105570259262790;
|
||||
assertEq(amountOut, expectedAmount);
|
||||
uint256 usdcBalance = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertEq(usdcBalance, expectedAmount);
|
||||
assertEq(IERC20(DAI_ADDR).balanceOf(FEE_RECEIVER), 26598819248184436997);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSingleSwapWrapETH() public {
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
|
||||
IAllowanceTransfer.PermitSingle memory emptyPermitSingle =
|
||||
IAllowanceTransfer.PermitSingle({
|
||||
details: IAllowanceTransfer.PermitDetails({
|
||||
token: address(0),
|
||||
amount: 0,
|
||||
expiration: 0,
|
||||
nonce: 0
|
||||
}),
|
||||
spender: address(0),
|
||||
sigDeadline: 0
|
||||
});
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap =
|
||||
encodeSingleSwap(address(usv2Executor), protocolData);
|
||||
|
||||
uint256 amountOut = tychoRouter.singleSwapPermit2{value: amountIn}(
|
||||
amountIn,
|
||||
address(0),
|
||||
DAI_ADDR,
|
||||
1000_000000,
|
||||
true,
|
||||
false,
|
||||
ALICE,
|
||||
emptyPermitSingle,
|
||||
"",
|
||||
swap
|
||||
);
|
||||
uint256 expectedAmount = 2659881924818443699787;
|
||||
assertEq(amountOut, expectedAmount);
|
||||
uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertEq(daiBalance, expectedAmount);
|
||||
assertEq(ALICE.balance, 0);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSingleSwapUnwrapETH() public {
|
||||
// DAI -> WETH with unwrapping to ETH
|
||||
uint256 amountIn = 3000 ether;
|
||||
deal(DAI_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(DAI_ADDR, amountIn);
|
||||
|
||||
bytes memory protocolData =
|
||||
encodeUniswapV2Swap(DAI_ADDR, WETH_DAI_POOL, tychoRouterAddr, true);
|
||||
|
||||
bytes memory swap =
|
||||
encodeSingleSwap(address(usv2Executor), protocolData);
|
||||
|
||||
uint256 amountOut = tychoRouter.singleSwapPermit2(
|
||||
amountIn,
|
||||
DAI_ADDR,
|
||||
address(0),
|
||||
1000_000000,
|
||||
false,
|
||||
true,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
swap
|
||||
);
|
||||
|
||||
uint256 expectedAmount = 1120007305574805922;
|
||||
assertEq(amountOut, expectedAmount);
|
||||
assertEq(ALICE.balance, expectedAmount);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSingleSwapIntegration() public {
|
||||
// Tests swapping WETH -> DAI on a USV2 pool with regular approvals
|
||||
deal(WETH_ADDR, ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(tychoRouterAddr, type(uint256).max);
|
||||
// Encoded solution generated using `test_single_swap_strategy_encoder_no_permit2`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"20144a070000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000008f1d5c1cae3740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000515615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d0139500000000000000000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 2659881924818443699787);
|
||||
}
|
||||
|
||||
function testSingleSwapIntegrationPermit2() public {
|
||||
// Tests swapping WETH -> DAI on a USV2 pool with permit2
|
||||
deal(WETH_ADDR, ALICE, 1 ether);
|
||||
uint256 balancerBefore = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(PERMIT2_ADDRESS, type(uint256).max);
|
||||
// Encoded solution generated using `test_single_swap_strategy_encoder`
|
||||
(bool success,) = tychoRouterAddr.call(
|
||||
hex"30ace1b10000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000903146e5f6c59c064b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000006817833200000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067effd3a00000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000026000000000000000000000000000000000000000000000000000000000000000417efea09004d5d40d8d072e1ce0a425507717ea485c765eb90c170859197d362b502fb039b4f5cdce57318ecfe3ab276d1ac87771eb5d017b253a8f4107e6a20b1b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000515615deb798bb3e4dfa0139dfa1b3d433cc23b72fc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d0139500000000000000000000000000000000"
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 balancerAfter = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertTrue(success, "Call Failed");
|
||||
assertEq(balancerAfter - balancerBefore, 2659881924818443699787);
|
||||
}
|
||||
}
|
||||
683
foundry/test/TychoRouterSplitSwap.t.sol
Normal file
683
foundry/test/TychoRouterSplitSwap.t.sol
Normal file
@@ -0,0 +1,683 @@
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
import "@src/executors/UniswapV4Executor.sol";
|
||||
import {TychoRouter} from "@src/TychoRouter.sol";
|
||||
import "./TychoRouterTestSetup.sol";
|
||||
import "./executors/UniswapV4Utils.sol";
|
||||
import {SafeCallback} from "@uniswap/v4-periphery/src/base/SafeCallback.sol";
|
||||
|
||||
contract TychoRouterSplitSwapTest is TychoRouterTestSetup {
|
||||
bytes32 public constant FEE_SETTER_ROLE =
|
||||
0xe6ad9a47fbda1dc18de1eb5eeb7d935e5e81b4748f3cfc61e233e64f88182060;
|
||||
|
||||
function _getSplitSwaps() private view returns (bytes[] memory) {
|
||||
// Trade 1 WETH for USDC through DAI and WBTC with 4 swaps on Uniswap V2
|
||||
// -> DAI ->
|
||||
// 1 WETH USDC
|
||||
// -> WBTC ->
|
||||
// (univ2) (univ2)
|
||||
bytes[] memory swaps = new bytes[](4);
|
||||
// WETH -> WBTC (60%)
|
||||
swaps[0] = encodeSplitSwap(
|
||||
uint8(0),
|
||||
uint8(1),
|
||||
(0xffffff * 60) / 100, // 60%
|
||||
address(usv2Executor),
|
||||
encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_WBTC_POOL, tychoRouterAddr, false
|
||||
)
|
||||
);
|
||||
// WBTC -> USDC
|
||||
swaps[1] = encodeSplitSwap(
|
||||
uint8(1),
|
||||
uint8(2),
|
||||
uint24(0),
|
||||
address(usv2Executor),
|
||||
encodeUniswapV2Swap(
|
||||
WBTC_ADDR, USDC_WBTC_POOL, tychoRouterAddr, true
|
||||
)
|
||||
);
|
||||
// WETH -> DAI
|
||||
swaps[2] = encodeSplitSwap(
|
||||
uint8(0),
|
||||
uint8(3),
|
||||
uint24(0),
|
||||
address(usv2Executor),
|
||||
encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
)
|
||||
);
|
||||
|
||||
// DAI -> USDC
|
||||
swaps[3] = encodeSplitSwap(
|
||||
uint8(3),
|
||||
uint8(2),
|
||||
uint24(0),
|
||||
address(usv2Executor),
|
||||
encodeUniswapV2Swap(DAI_ADDR, DAI_USDC_POOL, tychoRouterAddr, true)
|
||||
);
|
||||
|
||||
return swaps;
|
||||
}
|
||||
|
||||
function testSplitSwapInternalMethod() public {
|
||||
// Trade 1 WETH for USDC through DAI and WBTC - see _getSplitSwaps for more info
|
||||
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, tychoRouterAddr, amountIn);
|
||||
bytes[] memory swaps = _getSplitSwaps();
|
||||
tychoRouter.exposedSplitSwap(amountIn, 4, pleEncode(swaps));
|
||||
|
||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(tychoRouterAddr);
|
||||
assertEq(usdcBalance, 2615491639);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testSplitSwapPermit2() public {
|
||||
// Trade 1 WETH for USDC through DAI and WBTC - see _getSplitSwaps for more info
|
||||
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(WETH_ADDR, amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSplitSwaps();
|
||||
|
||||
tychoRouter.splitSwapPermit2(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
1, // min amount
|
||||
false,
|
||||
false,
|
||||
4,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
assertEq(usdcBalance, 2615491639);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testSplitSwapNoPermit2() public {
|
||||
// Trade 1 WETH for USDC through DAI and WBTC - see _getSplitSwaps for more info
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSplitSwaps();
|
||||
|
||||
tychoRouter.splitSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
1000_000000, // min amount
|
||||
false,
|
||||
false,
|
||||
4,
|
||||
ALICE,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
uint256 usdcBalance = IERC20(USDC_ADDR).balanceOf(ALICE);
|
||||
assertEq(usdcBalance, 2615491639);
|
||||
assertEq(IERC20(WETH_ADDR).balanceOf(tychoRouterAddr), 0);
|
||||
}
|
||||
|
||||
function testSplitSwapUndefinedMinAmount() public {
|
||||
// Min amount should always be non-zero. If zero, swap attempt should revert.
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSplitSwaps();
|
||||
|
||||
vm.expectRevert(TychoRouter__UndefinedMinAmountOut.selector);
|
||||
tychoRouter.splitSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
0, // min amount
|
||||
false,
|
||||
false,
|
||||
4,
|
||||
ALICE,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitSwapInsufficientApproval() public {
|
||||
// Trade 1 WETH for USDC through DAI and WBTC - see _getSplitSwaps for more info
|
||||
uint256 amountIn = 1 ether;
|
||||
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
// Approve less than the amountIn
|
||||
IERC20(WETH_ADDR).approve(address(tychoRouterAddr), amountIn - 1);
|
||||
bytes[] memory swaps = _getSplitSwaps();
|
||||
|
||||
vm.expectRevert();
|
||||
tychoRouter.splitSwap(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
USDC_ADDR,
|
||||
1000_000000, // min amount
|
||||
false,
|
||||
false,
|
||||
2,
|
||||
ALICE,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitSwapNegativeSlippageFailure() public {
|
||||
// Trade 1 WETH for USDC through DAI and WBTC - see _getSplitSwaps for more info
|
||||
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
vm.startPrank(ALICE);
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(WETH_ADDR, amountIn);
|
||||
|
||||
bytes[] memory swaps = _getSplitSwaps();
|
||||
|
||||
uint256 minAmountOut = 3000 * 1e18;
|
||||
|
||||
vm.expectRevert(
|
||||
abi.encodeWithSelector(
|
||||
TychoRouter__NegativeSlippage.selector,
|
||||
2615491639, // actual amountOut
|
||||
minAmountOut
|
||||
)
|
||||
);
|
||||
tychoRouter.splitSwapPermit2(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
minAmountOut,
|
||||
false,
|
||||
false,
|
||||
4,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitSwapFee() public {
|
||||
// Trade 1 WETH for DAI with 1 swap on Uniswap V2
|
||||
// Does permit2 token approval and transfer
|
||||
// Takes fee at the end
|
||||
|
||||
vm.startPrank(FEE_SETTER);
|
||||
tychoRouter.setFee(100);
|
||||
tychoRouter.setFeeReceiver(FEE_RECEIVER);
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(WETH_ADDR, amountIn);
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap = encodeSplitSwap(
|
||||
uint8(0), uint8(1), uint24(0), address(usv2Executor), protocolData
|
||||
);
|
||||
bytes[] memory swaps = new bytes[](1);
|
||||
swaps[0] = swap;
|
||||
|
||||
uint256 amountOut = tychoRouter.splitSwapPermit2(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
2633283105570259262780,
|
||||
false,
|
||||
false,
|
||||
2,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
uint256 expectedAmount = 2633283105570259262790;
|
||||
assertEq(amountOut, expectedAmount);
|
||||
uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertEq(daiBalance, expectedAmount);
|
||||
assertEq(IERC20(DAI_ADDR).balanceOf(FEE_RECEIVER), 26598819248184436997);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitSwapWrapETH() public {
|
||||
// Trade 1 ETH (and wrap it) for DAI with 1 swap on Uniswap V2
|
||||
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
|
||||
IAllowanceTransfer.PermitSingle memory emptyPermitSingle =
|
||||
IAllowanceTransfer.PermitSingle({
|
||||
details: IAllowanceTransfer.PermitDetails({
|
||||
token: address(0),
|
||||
amount: 0,
|
||||
expiration: 0,
|
||||
nonce: 0
|
||||
}),
|
||||
spender: address(0),
|
||||
sigDeadline: 0
|
||||
});
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap = encodeSplitSwap(
|
||||
uint8(0), uint8(1), uint24(0), address(usv2Executor), protocolData
|
||||
);
|
||||
bytes[] memory swaps = new bytes[](1);
|
||||
swaps[0] = swap;
|
||||
|
||||
uint256 amountOut = tychoRouter.splitSwapPermit2{value: amountIn}(
|
||||
amountIn,
|
||||
address(0),
|
||||
DAI_ADDR,
|
||||
2659881924818443699780,
|
||||
true,
|
||||
false,
|
||||
2,
|
||||
ALICE,
|
||||
emptyPermitSingle,
|
||||
"",
|
||||
pleEncode(swaps)
|
||||
);
|
||||
uint256 expectedAmount = 2659881924818443699787;
|
||||
assertEq(amountOut, expectedAmount);
|
||||
uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertEq(daiBalance, expectedAmount);
|
||||
assertEq(ALICE.balance, 0);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitSwapUnwrapETH() public {
|
||||
// Trade 3k DAI for WETH with 1 swap on Uniswap V2 and unwrap it at the end
|
||||
|
||||
uint256 amountIn = 3_000 * 10 ** 18;
|
||||
deal(DAI_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(DAI_ADDR, amountIn);
|
||||
|
||||
bytes memory protocolData =
|
||||
encodeUniswapV2Swap(DAI_ADDR, WETH_DAI_POOL, tychoRouterAddr, true);
|
||||
|
||||
bytes memory swap = encodeSplitSwap(
|
||||
uint8(0), uint8(1), uint24(0), address(usv2Executor), protocolData
|
||||
);
|
||||
bytes[] memory swaps = new bytes[](1);
|
||||
swaps[0] = swap;
|
||||
|
||||
uint256 amountOut = tychoRouter.splitSwapPermit2(
|
||||
amountIn,
|
||||
DAI_ADDR,
|
||||
address(0),
|
||||
1120007305574805920,
|
||||
false,
|
||||
true,
|
||||
2,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
uint256 expectedAmount = 1120007305574805922; // 1.12 ETH
|
||||
assertEq(amountOut, expectedAmount);
|
||||
assertEq(ALICE.balance, expectedAmount);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitSwapSingleUSV3Permit2() public {
|
||||
// Trade 1 WETH for DAI with 1 swap on Uniswap V3 using Permit2
|
||||
// Tests entire USV3 flow including callback
|
||||
// 1 WETH -> DAI
|
||||
// (USV3)
|
||||
vm.startPrank(ALICE);
|
||||
uint256 amountIn = 10 ** 18;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(WETH_ADDR, amountIn);
|
||||
|
||||
uint256 expAmountOut = 1205_128428842122129186; //Swap 1 WETH for 1205.12 DAI
|
||||
bool zeroForOne = false;
|
||||
bytes memory protocolData = encodeUniswapV3Swap(
|
||||
WETH_ADDR, DAI_ADDR, tychoRouterAddr, DAI_WETH_USV3, zeroForOne
|
||||
);
|
||||
bytes memory swap = encodeSplitSwap(
|
||||
uint8(0), uint8(1), uint24(0), address(usv3Executor), protocolData
|
||||
);
|
||||
|
||||
bytes[] memory swaps = new bytes[](1);
|
||||
swaps[0] = swap;
|
||||
|
||||
tychoRouter.splitSwapPermit2(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
expAmountOut - 1,
|
||||
false,
|
||||
false,
|
||||
2,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
uint256 finalBalance = IERC20(DAI_ADDR).balanceOf(ALICE);
|
||||
assertGe(finalBalance, expAmountOut);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testEmptySwapsRevert() public {
|
||||
uint256 amountIn = 10 ** 18;
|
||||
bytes memory swaps = "";
|
||||
vm.expectRevert(TychoRouter__EmptySwaps.selector);
|
||||
tychoRouter.exposedSplitSwap(amountIn, 2, swaps);
|
||||
}
|
||||
|
||||
function testSplitSwapAmountInNotFullySpent() public {
|
||||
// Trade 1 WETH for DAI with 1 swap on Uniswap V2
|
||||
// Has invalid data as input! There is only one swap with 60% of the input amount
|
||||
uint256 amountIn = 1 ether;
|
||||
deal(WETH_ADDR, ALICE, amountIn);
|
||||
|
||||
vm.startPrank(ALICE);
|
||||
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(WETH_ADDR, amountIn);
|
||||
|
||||
bytes memory protocolData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes memory swap = encodeSplitSwap(
|
||||
uint8(0),
|
||||
uint8(1),
|
||||
(0xffffff * 60) / 100, // 60%
|
||||
address(usv2Executor),
|
||||
protocolData
|
||||
);
|
||||
|
||||
bytes[] memory swaps = new bytes[](1);
|
||||
swaps[0] = swap;
|
||||
|
||||
vm.expectRevert(
|
||||
abi.encodeWithSelector(
|
||||
TychoRouter__AmountInDiffersFromConsumed.selector,
|
||||
1000000000000000000,
|
||||
600000000000000000
|
||||
)
|
||||
);
|
||||
|
||||
tychoRouter.splitSwapPermit2(
|
||||
amountIn,
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
1,
|
||||
false,
|
||||
false,
|
||||
2,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitSwapSingleUSV4CallbackPermit2() public {
|
||||
vm.startPrank(ALICE);
|
||||
uint256 amountIn = 100 ether;
|
||||
deal(USDE_ADDR, ALICE, amountIn);
|
||||
(
|
||||
IAllowanceTransfer.PermitSingle memory permitSingle,
|
||||
bytes memory signature
|
||||
) = handlePermit2Approval(USDE_ADDR, amountIn);
|
||||
|
||||
UniswapV4Executor.UniswapV4Pool[] memory pools =
|
||||
new UniswapV4Executor.UniswapV4Pool[](1);
|
||||
pools[0] = UniswapV4Executor.UniswapV4Pool({
|
||||
intermediaryToken: USDT_ADDR,
|
||||
fee: uint24(100),
|
||||
tickSpacing: int24(1)
|
||||
});
|
||||
|
||||
bytes memory protocolData = UniswapV4Utils.encodeExactInput(
|
||||
USDE_ADDR, USDT_ADDR, true, address(usv4Executor), pools
|
||||
);
|
||||
|
||||
bytes memory swap = encodeSplitSwap(
|
||||
uint8(0), uint8(1), uint24(0), address(usv4Executor), protocolData
|
||||
);
|
||||
|
||||
bytes[] memory swaps = new bytes[](1);
|
||||
swaps[0] = swap;
|
||||
|
||||
tychoRouter.splitSwapPermit2(
|
||||
amountIn,
|
||||
USDE_ADDR,
|
||||
USDT_ADDR,
|
||||
99943850,
|
||||
false,
|
||||
false,
|
||||
2,
|
||||
ALICE,
|
||||
permitSingle,
|
||||
signature,
|
||||
pleEncode(swaps)
|
||||
);
|
||||
|
||||
assertEq(IERC20(USDT_ADDR).balanceOf(ALICE), 99943852);
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function testSplitSwapMultipleUSV4Callback() public {
|
||||
// This test has two uniswap v4 hops that will be executed inside of the V4 pool manager
|
||||
// USDE -> USDT -> WBTC
|
||||
uint256 amountIn = 100 ether;
|
||||
deal(USDE_ADDR, tychoRouterAddr, amountIn);
|
||||
|
||||
UniswapV4Executor.UniswapV4Pool[] memory pools =
|
||||
new UniswapV4Executor.UniswapV4Pool[](2);
|
||||
pools[0] = UniswapV4Executor.UniswapV4Pool({
|
||||
intermediaryToken: USDT_ADDR,
|
||||
fee: uint24(100),
|
||||
tickSpacing: int24(1)
|
||||
});
|
||||
pools[1] = UniswapV4Executor.UniswapV4Pool({
|
||||
intermediaryToken: WBTC_ADDR,
|
||||
fee: uint24(3000),
|
||||
tickSpacing: int24(60)
|
||||
});
|
||||
|
||||
bytes memory protocolData = UniswapV4Utils.encodeExactInput(
|
||||
USDE_ADDR, WBTC_ADDR, true, address(usv4Executor), pools
|
||||
);
|
||||
|
||||
bytes memory swap = encodeSplitSwap(
|
||||
uint8(0), uint8(1), uint24(0), address(usv4Executor), protocolData
|
||||
);
|
||||
|
||||
bytes[] memory swaps = new bytes[](1);
|
||||
swaps[0] = swap;
|
||||
|
||||
tychoRouter.exposedSplitSwap(amountIn, 2, pleEncode(swaps));
|
||||
|
||||
assertEq(IERC20(WBTC_ADDR).balanceOf(tychoRouterAddr), 102718);
|
||||
}
|
||||
|
||||
function testSplitInputCyclicSwapInternalMethod() public {
|
||||
// This test has start and end tokens that are the same
|
||||
// The flow is:
|
||||
// ┌─ (USV3, 60% split) ──> WETH ─┐
|
||||
// │ │
|
||||
// USDC ──────┤ ├──(USV2)──> USDC
|
||||
// │ │
|
||||
// └─ (USV3, 40% split) ──> WETH ─┘
|
||||
uint256 amountIn = 100 * 10 ** 6;
|
||||
deal(USDC_ADDR, tychoRouterAddr, amountIn);
|
||||
|
||||
bytes memory usdcWethV3Pool1ZeroOneData = encodeUniswapV3Swap(
|
||||
USDC_ADDR, WETH_ADDR, tychoRouterAddr, USDC_WETH_USV3, true
|
||||
);
|
||||
|
||||
bytes memory usdcWethV3Pool2ZeroOneData = encodeUniswapV3Swap(
|
||||
USDC_ADDR, WETH_ADDR, tychoRouterAddr, USDC_WETH_USV3_2, true
|
||||
);
|
||||
|
||||
bytes memory wethUsdcV2OneZeroData = encodeUniswapV2Swap(
|
||||
WETH_ADDR, USDC_WETH_USV2, tychoRouterAddr, false
|
||||
);
|
||||
|
||||
bytes[] memory swaps = new bytes[](3);
|
||||
// USDC -> WETH (60% split)
|
||||
swaps[0] = encodeSplitSwap(
|
||||
uint8(0),
|
||||
uint8(1),
|
||||
(0xffffff * 60) / 100, // 60%
|
||||
address(usv3Executor),
|
||||
usdcWethV3Pool1ZeroOneData
|
||||
);
|
||||
// USDC -> WETH (40% remainder)
|
||||
swaps[1] = encodeSplitSwap(
|
||||
uint8(0),
|
||||
uint8(1),
|
||||
uint24(0),
|
||||
address(usv3Executor),
|
||||
usdcWethV3Pool2ZeroOneData
|
||||
);
|
||||
// WETH -> USDC
|
||||
swaps[2] = encodeSplitSwap(
|
||||
uint8(1),
|
||||
uint8(0),
|
||||
uint24(0),
|
||||
address(usv2Executor),
|
||||
wethUsdcV2OneZeroData
|
||||
);
|
||||
tychoRouter.exposedSplitSwap(amountIn, 2, pleEncode(swaps));
|
||||
assertEq(IERC20(USDC_ADDR).balanceOf(tychoRouterAddr), 99574171);
|
||||
}
|
||||
|
||||
function testSplitOutputCyclicSwapInternalMethod() public {
|
||||
// This test has start and end tokens that are the same
|
||||
// The flow is:
|
||||
// ┌─── (USV3, 60% split) ───┐
|
||||
// │ │
|
||||
// USDC ──(USV2) ── WETH──| ├─> USDC
|
||||
// │ │
|
||||
// └─── (USV3, 40% split) ───┘
|
||||
|
||||
uint256 amountIn = 100 * 10 ** 6;
|
||||
deal(USDC_ADDR, tychoRouterAddr, amountIn);
|
||||
|
||||
bytes memory usdcWethV2Data = encodeUniswapV2Swap(
|
||||
USDC_ADDR, USDC_WETH_USV2, tychoRouterAddr, true
|
||||
);
|
||||
|
||||
bytes memory usdcWethV3Pool1OneZeroData = encodeUniswapV3Swap(
|
||||
WETH_ADDR, USDC_ADDR, tychoRouterAddr, USDC_WETH_USV3, false
|
||||
);
|
||||
|
||||
bytes memory usdcWethV3Pool2OneZeroData = encodeUniswapV3Swap(
|
||||
WETH_ADDR, USDC_ADDR, tychoRouterAddr, USDC_WETH_USV3_2, false
|
||||
);
|
||||
|
||||
bytes[] memory swaps = new bytes[](3);
|
||||
// USDC -> WETH
|
||||
swaps[0] = encodeSplitSwap(
|
||||
uint8(0), uint8(1), uint24(0), address(usv2Executor), usdcWethV2Data
|
||||
);
|
||||
// WETH -> USDC
|
||||
swaps[1] = encodeSplitSwap(
|
||||
uint8(1),
|
||||
uint8(0),
|
||||
(0xffffff * 60) / 100,
|
||||
address(usv3Executor),
|
||||
usdcWethV3Pool1OneZeroData
|
||||
);
|
||||
|
||||
// WETH -> USDC
|
||||
swaps[2] = encodeSplitSwap(
|
||||
uint8(1),
|
||||
uint8(0),
|
||||
uint24(0),
|
||||
address(usv3Executor),
|
||||
usdcWethV3Pool2OneZeroData
|
||||
);
|
||||
|
||||
tychoRouter.exposedSplitSwap(amountIn, 2, pleEncode(swaps));
|
||||
assertEq(IERC20(USDC_ADDR).balanceOf(tychoRouterAddr), 99525908);
|
||||
}
|
||||
|
||||
// Base Network Tests
|
||||
// Make sure to set the RPC_URL to base network
|
||||
function testSplitSwapInternalMethodBase() 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 = encodeSplitSwap(
|
||||
uint8(0), uint8(1), uint24(0), address(usv2Executor), protocolData
|
||||
);
|
||||
bytes[] memory swaps = new bytes[](1);
|
||||
swaps[0] = swap;
|
||||
|
||||
tychoRouter.exposedSplitSwap(amountIn, 2, pleEncode(swaps));
|
||||
assertGt(IERC20(BASE_MAG7).balanceOf(tychoRouterAddr), 1379830606);
|
||||
}
|
||||
}
|
||||
@@ -25,16 +25,23 @@ contract TychoRouterExposed is TychoRouter {
|
||||
return _unwrapETH(amount);
|
||||
}
|
||||
|
||||
function exposedSwap(
|
||||
function exposedSplitSwap(
|
||||
uint256 amountIn,
|
||||
uint256 nTokens,
|
||||
bytes calldata swaps
|
||||
) external returns (uint256) {
|
||||
return _swap(amountIn, nTokens, swaps);
|
||||
return _splitSwap(amountIn, nTokens, swaps);
|
||||
}
|
||||
|
||||
function exposedSequentialSwap(uint256 amountIn, bytes calldata swaps)
|
||||
external
|
||||
returns (uint256)
|
||||
{
|
||||
return _sequentialSwap(amountIn, swaps);
|
||||
}
|
||||
}
|
||||
|
||||
contract TychoRouterTestSetup is Test, Constants {
|
||||
contract TychoRouterTestSetup is Constants {
|
||||
TychoRouterExposed tychoRouter;
|
||||
address tychoRouterAddr;
|
||||
UniswapV2Executor public usv2Executor;
|
||||
@@ -214,7 +221,23 @@ contract TychoRouterTestSetup is Test, Constants {
|
||||
}
|
||||
}
|
||||
|
||||
function encodeSwap(
|
||||
function encodeSingleSwap(address executor, bytes memory protocolData)
|
||||
internal
|
||||
pure
|
||||
returns (bytes memory)
|
||||
{
|
||||
return abi.encodePacked(executor, protocolData);
|
||||
}
|
||||
|
||||
function encodeSequentialSwap(address executor, bytes memory protocolData)
|
||||
internal
|
||||
pure
|
||||
returns (bytes memory)
|
||||
{
|
||||
return abi.encodePacked(executor, protocolData);
|
||||
}
|
||||
|
||||
function encodeSplitSwap(
|
||||
uint8 tokenInIndex,
|
||||
uint8 tokenOutIndex,
|
||||
uint24 split,
|
||||
|
||||
@@ -3,7 +3,9 @@ use std::io::{self, Read};
|
||||
use clap::{Parser, Subcommand};
|
||||
use tycho_common::{hex_bytes::Bytes, models::Chain};
|
||||
use tycho_execution::encoding::{
|
||||
evm::encoder_builder::EVMEncoderBuilder, models::Solution, tycho_encoder::TychoEncoder,
|
||||
evm::encoder_builders::{TychoExecutorEncoderBuilder, TychoRouterEncoderBuilder},
|
||||
models::Solution,
|
||||
tycho_encoder::TychoEncoder,
|
||||
};
|
||||
|
||||
#[derive(Parser)]
|
||||
@@ -45,19 +47,16 @@ pub struct Cli {
|
||||
executors_file_path: Option<String>,
|
||||
#[arg(short, long)]
|
||||
router_address: Option<Bytes>,
|
||||
#[arg(short, long)]
|
||||
swapper_pk: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
/// Use the Tycho router encoding strategy
|
||||
/// Use Tycho router encoding
|
||||
TychoRouter,
|
||||
/// Use the Tycho router encoding strategy with Permit2 approval and token in transfer
|
||||
TychoRouterPermit2 {
|
||||
#[arg(short, long)]
|
||||
swapper_pk: String,
|
||||
},
|
||||
/// Use the direct execution encoding strategy
|
||||
DirectExecution,
|
||||
/// Use direct execution encoding
|
||||
TychoExecutor,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@@ -75,24 +74,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
let solution: Solution = serde_json::from_str(&buffer)?;
|
||||
|
||||
let mut builder = EVMEncoderBuilder::new().chain(chain);
|
||||
|
||||
if let Some(config_path) = cli.executors_file_path {
|
||||
builder = builder.executors_file_path(config_path);
|
||||
}
|
||||
if let Some(router_address) = cli.router_address {
|
||||
builder = builder.router_address(router_address);
|
||||
}
|
||||
|
||||
builder = match cli.command {
|
||||
Commands::TychoRouter => builder.initialize_tycho_router()?,
|
||||
Commands::TychoRouterPermit2 { swapper_pk } => {
|
||||
builder.initialize_tycho_router_with_permit2(swapper_pk)?
|
||||
let encoder: Box<dyn TychoEncoder> = match cli.command {
|
||||
Commands::TychoRouter => {
|
||||
let mut builder = TychoRouterEncoderBuilder::new().chain(chain);
|
||||
if let Some(config_path) = cli.executors_file_path {
|
||||
builder = builder.executors_file_path(config_path);
|
||||
}
|
||||
if let Some(router_address) = cli.router_address {
|
||||
builder = builder.router_address(router_address);
|
||||
}
|
||||
if let Some(swapper_pk) = cli.swapper_pk {
|
||||
builder = builder.swapper_pk(swapper_pk);
|
||||
}
|
||||
builder.build()?
|
||||
}
|
||||
Commands::DirectExecution => builder.initialize_direct_execution()?,
|
||||
Commands::TychoExecutor => TychoExecutorEncoderBuilder::new()
|
||||
.chain(chain)
|
||||
.build()?,
|
||||
};
|
||||
let encoder = builder.build()?;
|
||||
let transactions = encoder.encode_router_calldata(vec![solution])?;
|
||||
|
||||
let transactions = encoder.encode_calldata(vec![solution])?;
|
||||
let encoded = serde_json::json!({
|
||||
"to": format!("0x{}", hex::encode(&transactions[0].to)),
|
||||
"value": format!("0x{}", hex::encode(transactions[0].value.to_bytes_be())),
|
||||
|
||||
@@ -172,7 +172,7 @@ mod tests {
|
||||
|
||||
use alloy_primitives::Uint;
|
||||
use num_bigint::BigUint;
|
||||
use tycho_common::models::Chain as TychoCoreChain;
|
||||
use tycho_common::models::Chain as TychoCommonChain;
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -208,7 +208,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn eth_chain() -> Chain {
|
||||
TychoCoreChain::Ethereum.into()
|
||||
TychoCommonChain::Ethereum.into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,150 +0,0 @@
|
||||
use tycho_common::{models::Chain, Bytes};
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::{
|
||||
strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder},
|
||||
swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
|
||||
tycho_encoder::EVMTychoEncoder,
|
||||
},
|
||||
strategy_encoder::StrategyEncoder,
|
||||
};
|
||||
|
||||
/// Builder pattern for constructing an `EVMTychoEncoder` with customizable options.
|
||||
///
|
||||
/// This struct allows setting a chain and strategy encoder before building the final encoder.
|
||||
pub struct EVMEncoderBuilder {
|
||||
strategy: Option<Box<dyn StrategyEncoder>>,
|
||||
chain: Option<Chain>,
|
||||
executors_file_path: Option<String>,
|
||||
router_address: Option<Bytes>,
|
||||
}
|
||||
|
||||
impl Default for EVMEncoderBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl EVMEncoderBuilder {
|
||||
pub fn new() -> Self {
|
||||
EVMEncoderBuilder {
|
||||
chain: None,
|
||||
strategy: None,
|
||||
executors_file_path: None,
|
||||
router_address: None,
|
||||
}
|
||||
}
|
||||
pub fn chain(mut self, chain: Chain) -> Self {
|
||||
self.chain = Some(chain);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the `executors_file_path` manually.
|
||||
/// If it's not set, the default path will be used (config/executor_addresses.json)
|
||||
pub fn executors_file_path(mut self, executors_file_path: String) -> Self {
|
||||
self.executors_file_path = Some(executors_file_path);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the `router_address` manually.
|
||||
/// If it's not set, the default router address will be used (config/router_addresses.json)
|
||||
pub fn router_address(mut self, router_address: Bytes) -> Self {
|
||||
self.router_address = Some(router_address);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the `strategy_encoder` manually.
|
||||
///
|
||||
/// **Note**: This method should not be used in combination with `tycho_router` or
|
||||
/// `direct_execution`.
|
||||
pub fn strategy_encoder(mut self, strategy: Box<dyn StrategyEncoder>) -> Self {
|
||||
self.strategy = Some(strategy);
|
||||
self
|
||||
}
|
||||
|
||||
/// Shortcut method to initialize a `SplitSwapStrategyEncoder` without any approval nor token in
|
||||
/// transfer. **Note**: Should not be used at the same time as `strategy_encoder`.
|
||||
pub fn initialize_tycho_router(self) -> Result<Self, EncodingError> {
|
||||
if let Some(chain) = self.chain {
|
||||
let swap_encoder_registry =
|
||||
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
|
||||
let strategy = Box::new(SplitSwapStrategyEncoder::new(
|
||||
chain,
|
||||
swap_encoder_registry,
|
||||
None,
|
||||
self.router_address.clone(),
|
||||
)?);
|
||||
Ok(EVMEncoderBuilder {
|
||||
chain: Some(chain),
|
||||
strategy: Some(strategy),
|
||||
executors_file_path: self.executors_file_path,
|
||||
router_address: self.router_address,
|
||||
})
|
||||
} else {
|
||||
Err(EncodingError::FatalError(
|
||||
"Please set the chain before setting the tycho router".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Shortcut method to initialize a `SplitSwapStrategyEncoder` with Permit2 approval and token
|
||||
/// in transfer. **Note**: Should not be used at the same time as `strategy_encoder`.
|
||||
pub fn initialize_tycho_router_with_permit2(
|
||||
self,
|
||||
swapper_pk: String,
|
||||
) -> Result<Self, EncodingError> {
|
||||
if let Some(chain) = self.chain {
|
||||
let swap_encoder_registry =
|
||||
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
|
||||
let strategy = Box::new(SplitSwapStrategyEncoder::new(
|
||||
chain,
|
||||
swap_encoder_registry,
|
||||
Some(swapper_pk),
|
||||
self.router_address.clone(),
|
||||
)?);
|
||||
Ok(EVMEncoderBuilder {
|
||||
chain: Some(chain),
|
||||
strategy: Some(strategy),
|
||||
executors_file_path: self.executors_file_path,
|
||||
router_address: self.router_address,
|
||||
})
|
||||
} else {
|
||||
Err(EncodingError::FatalError(
|
||||
"Please set the chain before setting the tycho router".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Shortcut method to initialize an `ExecutorStrategyEncoder`.
|
||||
/// **Note**: Should not be used at the same time as `strategy_encoder`.
|
||||
pub fn initialize_direct_execution(self) -> Result<Self, EncodingError> {
|
||||
if let Some(chain) = self.chain {
|
||||
let swap_encoder_registry =
|
||||
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
|
||||
let strategy = Box::new(ExecutorStrategyEncoder::new(swap_encoder_registry));
|
||||
Ok(EVMEncoderBuilder {
|
||||
chain: Some(chain),
|
||||
strategy: Some(strategy),
|
||||
executors_file_path: self.executors_file_path,
|
||||
router_address: self.router_address,
|
||||
})
|
||||
} else {
|
||||
Err(EncodingError::FatalError(
|
||||
"Please set the chain before setting the strategy".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the `EVMTychoEncoder` instance using the configured chain and strategy.
|
||||
/// Returns an error if either the chain or strategy has not been set.
|
||||
pub fn build(self) -> Result<EVMTychoEncoder, EncodingError> {
|
||||
if let (Some(chain), Some(strategy)) = (self.chain, self.strategy) {
|
||||
EVMTychoEncoder::new(chain, strategy)
|
||||
} else {
|
||||
Err(EncodingError::FatalError(
|
||||
"Please set the chain and strategy before building the encoder".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
141
src/encoding/evm/encoder_builders.rs
Normal file
141
src/encoding/evm/encoder_builders.rs
Normal file
@@ -0,0 +1,141 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use tycho_common::{models::Chain as TychoCommonChain, Bytes};
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::{
|
||||
constants::DEFAULT_ROUTERS_JSON,
|
||||
swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
|
||||
tycho_encoders::{TychoExecutorEncoder, TychoRouterEncoder},
|
||||
},
|
||||
models::Chain,
|
||||
tycho_encoder::TychoEncoder,
|
||||
};
|
||||
|
||||
/// Builder pattern for constructing a `TychoRouterEncoder` with customizable options.
|
||||
///
|
||||
/// This struct allows setting a chain and strategy encoder before building the final encoder.
|
||||
pub struct TychoRouterEncoderBuilder {
|
||||
swapper_pk: Option<String>,
|
||||
chain: Option<Chain>,
|
||||
executors_file_path: Option<String>,
|
||||
router_address: Option<Bytes>,
|
||||
}
|
||||
|
||||
impl Default for TychoRouterEncoderBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TychoRouterEncoderBuilder {
|
||||
pub fn new() -> Self {
|
||||
TychoRouterEncoderBuilder {
|
||||
swapper_pk: None,
|
||||
chain: None,
|
||||
executors_file_path: None,
|
||||
router_address: None,
|
||||
}
|
||||
}
|
||||
pub fn chain(mut self, chain: TychoCommonChain) -> Self {
|
||||
self.chain = Some(chain.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the `executors_file_path` manually.
|
||||
/// If it's not set, the default path will be used (config/executor_addresses.json)
|
||||
pub fn executors_file_path(mut self, executors_file_path: String) -> Self {
|
||||
self.executors_file_path = Some(executors_file_path);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the `router_address` manually.
|
||||
/// If it's not set, the default router address will be used (config/router_addresses.json)
|
||||
pub fn router_address(mut self, router_address: Bytes) -> Self {
|
||||
self.router_address = Some(router_address);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn swapper_pk(mut self, swapper_pk: String) -> Self {
|
||||
self.swapper_pk = Some(swapper_pk);
|
||||
self
|
||||
}
|
||||
|
||||
/// Builds the `TychoRouterEncoder` instance using the configured chain.
|
||||
/// Returns an error if either the chain has not been set.
|
||||
pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> {
|
||||
if let Some(chain) = self.chain {
|
||||
let tycho_router_address;
|
||||
if let Some(address) = self.router_address {
|
||||
tycho_router_address = address;
|
||||
} else {
|
||||
let default_routers: HashMap<String, Bytes> =
|
||||
serde_json::from_str(DEFAULT_ROUTERS_JSON)?;
|
||||
tycho_router_address = default_routers
|
||||
.get(&chain.name)
|
||||
.ok_or(EncodingError::FatalError(
|
||||
"No default router address found for chain".to_string(),
|
||||
))?
|
||||
.to_owned();
|
||||
}
|
||||
|
||||
let swap_encoder_registry =
|
||||
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain.clone())?;
|
||||
|
||||
Ok(Box::new(TychoRouterEncoder::new(
|
||||
chain,
|
||||
swap_encoder_registry,
|
||||
self.swapper_pk,
|
||||
tycho_router_address,
|
||||
)?))
|
||||
} else {
|
||||
Err(EncodingError::FatalError(
|
||||
"Please set the chain and router address before building the encoder".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Builder pattern for constructing a `TychoExecutorEncoder` with customizable options.
|
||||
pub struct TychoExecutorEncoderBuilder {
|
||||
chain: Option<Chain>,
|
||||
executors_file_path: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for TychoExecutorEncoderBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TychoExecutorEncoderBuilder {
|
||||
pub fn new() -> Self {
|
||||
TychoExecutorEncoderBuilder { chain: None, executors_file_path: None }
|
||||
}
|
||||
pub fn chain(mut self, chain: TychoCommonChain) -> Self {
|
||||
self.chain = Some(chain.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the `executors_file_path` manually.
|
||||
/// If it's not set, the default path will be used (config/executor_addresses.json)
|
||||
pub fn executors_file_path(mut self, executors_file_path: String) -> Self {
|
||||
self.executors_file_path = Some(executors_file_path);
|
||||
self
|
||||
}
|
||||
|
||||
/// Builds the `TychoExecutorEncoder` instance using the configured chain and strategy.
|
||||
/// Returns an error if either the chain or strategy has not been set.
|
||||
pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> {
|
||||
if let Some(chain) = self.chain {
|
||||
let swap_encoder_registry =
|
||||
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain.clone())?;
|
||||
Ok(Box::new(TychoExecutorEncoder::new(chain, swap_encoder_registry)?))
|
||||
} else {
|
||||
Err(EncodingError::FatalError(
|
||||
"Please set the chain and strategy before building the encoder".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
pub mod approvals;
|
||||
mod constants;
|
||||
pub mod encoder_builder;
|
||||
pub mod encoder_builders;
|
||||
mod group_swaps;
|
||||
pub mod strategy_encoder;
|
||||
mod swap_encoder;
|
||||
pub mod tycho_encoder;
|
||||
pub mod tycho_encoders;
|
||||
pub mod utils;
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
mod group_swaps;
|
||||
pub mod strategy_encoders;
|
||||
mod strategy_validators;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,92 +7,10 @@ use crate::encoding::{
|
||||
models::{NativeAction, Solution, Swap},
|
||||
};
|
||||
|
||||
/// Validates whether a sequence of split swaps represents a valid solution.
|
||||
#[derive(Clone)]
|
||||
pub struct SplitSwapValidator;
|
||||
|
||||
impl SplitSwapValidator {
|
||||
/// Raises an error if the split percentages are invalid.
|
||||
///
|
||||
/// Split percentages are considered valid if all the following conditions are met:
|
||||
/// * Each split amount is < 1 (100%)
|
||||
/// * There is exactly one 0% split for each token, and it's the last swap specified, signifying
|
||||
/// to the router to send the remainder of the token to the designated protocol
|
||||
/// * The sum of all non-remainder splits for each token is < 1 (100%)
|
||||
/// * There are no negative split amounts
|
||||
pub fn validate_split_percentages(&self, swaps: &[Swap]) -> Result<(), EncodingError> {
|
||||
let mut swaps_by_token: HashMap<Bytes, Vec<&Swap>> = HashMap::new();
|
||||
for swap in swaps {
|
||||
if swap.split >= 1.0 {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"Split percentage must be less than 1 (100%), got {}",
|
||||
swap.split
|
||||
)));
|
||||
}
|
||||
swaps_by_token
|
||||
.entry(swap.token_in.clone())
|
||||
.or_default()
|
||||
.push(swap);
|
||||
}
|
||||
|
||||
for (token, token_swaps) in swaps_by_token {
|
||||
// Single swaps don't need remainder handling
|
||||
if token_swaps.len() == 1 {
|
||||
if token_swaps[0].split != 0.0 {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"Single swap must have 0% split for token {:?}",
|
||||
token
|
||||
)));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut found_zero_split = false;
|
||||
let mut total_percentage = 0.0;
|
||||
for (i, swap) in token_swaps.iter().enumerate() {
|
||||
match (swap.split == 0.0, i == token_swaps.len() - 1) {
|
||||
(true, false) => {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"The 0% split for token {:?} must be the last swap",
|
||||
token
|
||||
)))
|
||||
}
|
||||
(true, true) => found_zero_split = true,
|
||||
(false, _) => {
|
||||
if swap.split < 0.0 {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"All splits must be >= 0% for token {:?}",
|
||||
token
|
||||
)));
|
||||
}
|
||||
total_percentage += swap.split;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !found_zero_split {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"Token {:?} must have exactly one 0% split for remainder handling",
|
||||
token
|
||||
)));
|
||||
}
|
||||
|
||||
// Total must be <100% to leave room for remainder
|
||||
if total_percentage >= 1.0 {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"Total of non-remainder splits for token {:?} must be <100%, got {}%",
|
||||
token,
|
||||
total_percentage * 100.0
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub trait SwapValidator {
|
||||
/// Raises an error if the solution does not have checked amount set or slippage with checked
|
||||
/// amount set.
|
||||
pub fn validate_solution_min_amounts(&self, solution: &Solution) -> Result<(), EncodingError> {
|
||||
fn validate_solution_min_amounts(&self, solution: &Solution) -> Result<(), EncodingError> {
|
||||
if solution.checked_amount.is_none() &&
|
||||
(solution.slippage.is_none() || solution.expected_amount.is_none())
|
||||
{
|
||||
@@ -113,7 +31,7 @@ impl SplitSwapValidator {
|
||||
/// If the given token is the native token and the native action is WRAP, it will be converted
|
||||
/// to the wrapped token before validating the swap path. The same principle applies for the
|
||||
/// checked token and the UNWRAP action.
|
||||
pub fn validate_swap_path(
|
||||
fn validate_swap_path(
|
||||
&self,
|
||||
swaps: &[Swap],
|
||||
given_token: &Bytes,
|
||||
@@ -197,6 +115,98 @@ impl SplitSwapValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates whether a sequence of split swaps represents a valid solution.
|
||||
#[derive(Clone)]
|
||||
pub struct SplitSwapValidator;
|
||||
|
||||
impl SwapValidator for SplitSwapValidator {}
|
||||
|
||||
impl SplitSwapValidator {
|
||||
/// Raises an error if the split percentages are invalid.
|
||||
///
|
||||
/// Split percentages are considered valid if all the following conditions are met:
|
||||
/// * Each split amount is < 1 (100%)
|
||||
/// * There is exactly one 0% split for each token, and it's the last swap specified, signifying
|
||||
/// to the router to send the remainder of the token to the designated protocol
|
||||
/// * The sum of all non-remainder splits for each token is < 1 (100%)
|
||||
/// * There are no negative split amounts
|
||||
pub fn validate_split_percentages(&self, swaps: &[Swap]) -> Result<(), EncodingError> {
|
||||
let mut swaps_by_token: HashMap<Bytes, Vec<&Swap>> = HashMap::new();
|
||||
for swap in swaps {
|
||||
if swap.split >= 1.0 {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"Split percentage must be less than 1 (100%), got {}",
|
||||
swap.split
|
||||
)));
|
||||
}
|
||||
swaps_by_token
|
||||
.entry(swap.token_in.clone())
|
||||
.or_default()
|
||||
.push(swap);
|
||||
}
|
||||
|
||||
for (token, token_swaps) in swaps_by_token {
|
||||
// Single swaps don't need remainder handling
|
||||
if token_swaps.len() == 1 {
|
||||
if token_swaps[0].split != 0.0 {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"Single swap must have 0% split for token {:?}",
|
||||
token
|
||||
)));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut found_zero_split = false;
|
||||
let mut total_percentage = 0.0;
|
||||
for (i, swap) in token_swaps.iter().enumerate() {
|
||||
match (swap.split == 0.0, i == token_swaps.len() - 1) {
|
||||
(true, false) => {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"The 0% split for token {:?} must be the last swap",
|
||||
token
|
||||
)))
|
||||
}
|
||||
(true, true) => found_zero_split = true,
|
||||
(false, _) => {
|
||||
if swap.split < 0.0 {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"All splits must be >= 0% for token {:?}",
|
||||
token
|
||||
)));
|
||||
}
|
||||
total_percentage += swap.split;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !found_zero_split {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"Token {:?} must have exactly one 0% split for remainder handling",
|
||||
token
|
||||
)));
|
||||
}
|
||||
|
||||
// Total must be <100% to leave room for remainder
|
||||
if total_percentage >= 1.0 {
|
||||
return Err(EncodingError::InvalidInput(format!(
|
||||
"Total of non-remainder splits for token {:?} must be <100%, got {}%",
|
||||
token,
|
||||
total_percentage * 100.0
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates whether a sequence of sequential swaps represents a valid solution.
|
||||
#[derive(Clone)]
|
||||
pub struct SequentialSwapValidator;
|
||||
|
||||
impl SwapValidator for SequentialSwapValidator {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::str::FromStr;
|
||||
|
||||
@@ -20,11 +20,7 @@ pub struct SwapEncoderRegistry {
|
||||
impl SwapEncoderRegistry {
|
||||
/// Populates the registry with the `SwapEncoders` for the given blockchain by parsing the
|
||||
/// executors' addresses in the file at the given path.
|
||||
pub fn new(
|
||||
executors_file_path: Option<String>,
|
||||
blockchain: tycho_common::models::Chain,
|
||||
) -> Result<Self, EncodingError> {
|
||||
let chain = Chain::from(blockchain);
|
||||
pub fn new(executors_file_path: Option<String>, chain: Chain) -> Result<Self, EncodingError> {
|
||||
let config_str = if let Some(ref path) = executors_file_path {
|
||||
fs::read_to_string(path).map_err(|e| {
|
||||
EncodingError::FatalError(format!(
|
||||
|
||||
@@ -1,720 +0,0 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use num_bigint::BigUint;
|
||||
use tycho_common::Bytes;
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
models::{Chain, NativeAction, Solution, Transaction},
|
||||
strategy_encoder::StrategyEncoder,
|
||||
tycho_encoder::TychoEncoder,
|
||||
};
|
||||
|
||||
/// Represents an encoder for a swap using any strategy supported by the strategy registry.
|
||||
///
|
||||
/// # Fields
|
||||
/// * `strategy_encoder`: Strategy encoder to follow for encoding the solution
|
||||
/// * `native_address`: Address of the chain's native token
|
||||
/// * `wrapped_address`: Address of the chain's wrapped native token
|
||||
pub struct EVMTychoEncoder {
|
||||
strategy_encoder: Box<dyn StrategyEncoder>,
|
||||
native_address: Bytes,
|
||||
wrapped_address: Bytes,
|
||||
}
|
||||
|
||||
impl Clone for EVMTychoEncoder {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
strategy_encoder: self.strategy_encoder.clone_box(),
|
||||
native_address: self.native_address.clone(),
|
||||
wrapped_address: self.wrapped_address.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EVMTychoEncoder {
|
||||
pub fn new(
|
||||
chain: tycho_common::models::Chain,
|
||||
strategy_encoder: Box<dyn StrategyEncoder>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
let chain: Chain = Chain::from(chain);
|
||||
let native_address = chain.native_token()?;
|
||||
let wrapped_address = chain.wrapped_token()?;
|
||||
Ok(EVMTychoEncoder { strategy_encoder, native_address, wrapped_address })
|
||||
}
|
||||
}
|
||||
|
||||
impl EVMTychoEncoder {
|
||||
/// Raises an `EncodingError` if the solution is not considered valid.
|
||||
///
|
||||
/// A solution is considered valid if all the following conditions are met:
|
||||
/// * The solution is not exact out.
|
||||
/// * The solution has at least one swap.
|
||||
/// * If the solution is wrapping, the given token is the chain's native token and the first
|
||||
/// swap's input is the chain's wrapped token.
|
||||
/// * If the solution is unwrapping, the checked token is the chain's native token and the last
|
||||
/// swap's output is the chain's wrapped token.
|
||||
/// * The token cannot appear more than once in the solution unless it is the first and last
|
||||
/// token (i.e. a true cyclical swap).
|
||||
fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError> {
|
||||
if solution.exact_out {
|
||||
return Err(EncodingError::FatalError(
|
||||
"Currently only exact input solutions are supported".to_string(),
|
||||
));
|
||||
}
|
||||
if solution.swaps.is_empty() {
|
||||
return Err(EncodingError::FatalError("No swaps found in solution".to_string()));
|
||||
}
|
||||
if let Some(native_action) = solution.clone().native_action {
|
||||
if native_action == NativeAction::Wrap {
|
||||
if solution.given_token != self.native_address {
|
||||
return Err(EncodingError::FatalError(
|
||||
"Native token must be the input token in order to wrap".to_string(),
|
||||
));
|
||||
}
|
||||
if let Some(first_swap) = solution.swaps.first() {
|
||||
if first_swap.token_in != self.wrapped_address {
|
||||
return Err(EncodingError::FatalError(
|
||||
"Wrapped token must be the first swap's input in order to wrap"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
} else if native_action == NativeAction::Unwrap {
|
||||
if solution.checked_token != self.native_address {
|
||||
return Err(EncodingError::FatalError(
|
||||
"Native token must be the output token in order to unwrap".to_string(),
|
||||
));
|
||||
}
|
||||
if let Some(last_swap) = solution.swaps.last() {
|
||||
if last_swap.token_out != self.wrapped_address {
|
||||
return Err(EncodingError::FatalError(
|
||||
"Wrapped token must be the last swap's output in order to unwrap"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut solution_tokens = vec![];
|
||||
let mut split_tokens_already_considered = HashSet::new();
|
||||
for (i, swap) in solution.swaps.iter().enumerate() {
|
||||
// so we don't count the split tokens more than once
|
||||
if swap.split != 0.0 {
|
||||
if !split_tokens_already_considered.contains(&swap.token_in) {
|
||||
solution_tokens.push(swap.token_in.clone());
|
||||
split_tokens_already_considered.insert(swap.token_in.clone());
|
||||
}
|
||||
} else {
|
||||
// it might be the last swap of the split or a regular swap
|
||||
if !split_tokens_already_considered.contains(&swap.token_in) {
|
||||
solution_tokens.push(swap.token_in.clone());
|
||||
}
|
||||
}
|
||||
if i == solution.swaps.len() - 1 {
|
||||
solution_tokens.push(swap.token_out.clone());
|
||||
}
|
||||
}
|
||||
|
||||
if solution_tokens.len() !=
|
||||
solution_tokens
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect::<HashSet<Bytes>>()
|
||||
.len()
|
||||
{
|
||||
if let Some(last_swap) = solution.swaps.last() {
|
||||
if solution.swaps[0].token_in != last_swap.token_out {
|
||||
return Err(EncodingError::FatalError(
|
||||
"Cyclical swaps are only allowed if they are the first and last token of a solution".to_string(),
|
||||
));
|
||||
} else {
|
||||
// it is a valid cyclical swap
|
||||
// we don't support any wrapping or unwrapping in this case
|
||||
if let Some(_native_action) = solution.clone().native_action {
|
||||
return Err(EncodingError::FatalError(
|
||||
"Wrapping/Unwrapping is not available in cyclical swaps".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl TychoEncoder for EVMTychoEncoder {
|
||||
fn encode_router_calldata(
|
||||
&self,
|
||||
solutions: Vec<Solution>,
|
||||
) -> Result<Vec<Transaction>, EncodingError> {
|
||||
let mut transactions: Vec<Transaction> = Vec::new();
|
||||
for solution in solutions.iter() {
|
||||
self.validate_solution(solution)?;
|
||||
|
||||
let (contract_interaction, target_address) = self
|
||||
.strategy_encoder
|
||||
.encode_strategy(solution.clone())?;
|
||||
|
||||
let value = if solution.given_token == self.native_address {
|
||||
solution.given_amount.clone()
|
||||
} else {
|
||||
BigUint::ZERO
|
||||
};
|
||||
|
||||
transactions.push(Transaction {
|
||||
value,
|
||||
data: contract_interaction,
|
||||
to: target_address,
|
||||
});
|
||||
}
|
||||
Ok(transactions)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::str::FromStr;
|
||||
|
||||
use tycho_common::models::{protocol::ProtocolComponent, Chain as TychoCoreChain};
|
||||
|
||||
use super::*;
|
||||
use crate::encoding::{
|
||||
models::Swap, strategy_encoder::StrategyEncoder, swap_encoder::SwapEncoder,
|
||||
};
|
||||
|
||||
fn dai() -> Bytes {
|
||||
Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap()
|
||||
}
|
||||
|
||||
fn eth() -> Bytes {
|
||||
Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap()
|
||||
}
|
||||
|
||||
fn weth() -> Bytes {
|
||||
Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap()
|
||||
}
|
||||
|
||||
fn usdc() -> Bytes {
|
||||
Bytes::from_str("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48").unwrap()
|
||||
}
|
||||
|
||||
fn wbtc() -> Bytes {
|
||||
Bytes::from_str("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599").unwrap()
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct MockStrategy;
|
||||
|
||||
impl StrategyEncoder for MockStrategy {
|
||||
fn encode_strategy(&self, _solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
Ok((
|
||||
Bytes::from_str("0x1234")
|
||||
.unwrap()
|
||||
.to_vec(),
|
||||
Bytes::from_str("0xabcd").unwrap(),
|
||||
))
|
||||
}
|
||||
|
||||
fn get_swap_encoder(&self, _protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
|
||||
None
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn StrategyEncoder> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn get_mocked_tycho_encoder() -> EVMTychoEncoder {
|
||||
let strategy_encoder = Box::new(MockStrategy {});
|
||||
EVMTychoEncoder::new(TychoCoreChain::Ethereum, strategy_encoder).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_router_calldata() {
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let eth_amount_in = BigUint::from(1000u32);
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_amount: eth_amount_in.clone(),
|
||||
given_token: eth(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let transactions = encoder.encode_router_calldata(vec![solution]);
|
||||
|
||||
assert!(transactions.is_ok());
|
||||
let transactions = transactions.unwrap();
|
||||
assert_eq!(transactions.len(), 1);
|
||||
assert_eq!(transactions[0].value, eth_amount_in);
|
||||
assert_eq!(transactions[0].data, Bytes::from_str("0x1234").unwrap());
|
||||
assert_eq!(transactions[0].to, Bytes::from_str("0xabcd").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_fails_for_exact_out() {
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let solution = Solution {
|
||||
exact_out: true, // This should cause an error
|
||||
..Default::default()
|
||||
};
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.err().unwrap(),
|
||||
EncodingError::FatalError(
|
||||
"Currently only exact input solutions are supported".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_passes_for_wrap() {
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: eth(),
|
||||
checked_token: dai(),
|
||||
checked_amount: None,
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_fails_for_wrap_wrong_input() {
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: weth(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.err().unwrap(),
|
||||
EncodingError::FatalError(
|
||||
"Native token must be the input token in order to wrap".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_fails_for_wrap_wrong_first_swap() {
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: eth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: eth(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.err().unwrap(),
|
||||
EncodingError::FatalError(
|
||||
"Wrapped token must be the first swap's input in order to wrap".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_fails_no_swaps() {
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: eth(),
|
||||
swaps: vec![],
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.err().unwrap(),
|
||||
EncodingError::FatalError("No swaps found in solution".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_passes_for_unwrap() {
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: weth(),
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
checked_token: eth(),
|
||||
checked_amount: None,
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Unwrap),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_fails_for_unwrap_wrong_output() {
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: weth(),
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: dai(),
|
||||
checked_token: weth(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Unwrap),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.err().unwrap(),
|
||||
EncodingError::FatalError(
|
||||
"Native token must be the output token in order to unwrap".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_fails_for_unwrap_wrong_last_swap() {
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: eth(),
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
checked_token: eth(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Unwrap),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.err().unwrap(),
|
||||
EncodingError::FatalError(
|
||||
"Wrapped token must be the last swap's output in order to unwrap".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_cyclical_swap() {
|
||||
// This validation passes because the cyclical swap is the first and last token
|
||||
// 50% -> WETH
|
||||
// DAI - -> DAI
|
||||
// 50% -> WETH
|
||||
// (some of the pool addresses in this test are fake)
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swaps = vec![
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: weth(),
|
||||
split: 0.5f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0x0000000000000000000000000000000000000000".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: weth(),
|
||||
split: 0f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0x0000000000000000000000000000000000000000".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
},
|
||||
];
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: dai(),
|
||||
checked_token: dai(),
|
||||
swaps,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_cyclical_swap_fail() {
|
||||
// This test should fail because the cyclical swap is not the first and last token
|
||||
// DAI -> WETH -> USDC -> DAI -> WBTC
|
||||
// (some of the pool addresses in this test are fake)
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swaps = vec![
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: weth(),
|
||||
split: 0f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: usdc(),
|
||||
split: 0f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0x0000000000000000000000000000000000000000".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: usdc(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0x0000000000000000000000000000000000000000".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: wbtc(),
|
||||
split: 0f64,
|
||||
},
|
||||
];
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: dai(),
|
||||
checked_token: wbtc(),
|
||||
swaps,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.err().unwrap(),
|
||||
EncodingError::FatalError(
|
||||
"Cyclical swaps are only allowed if they are the first and last token of a solution".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_validate_cyclical_swap_split_output() {
|
||||
// This validation passes because it is a valid cyclical swap
|
||||
// -> WETH
|
||||
// WETH -> DAI
|
||||
// -> WETH
|
||||
// (some of the pool addresses in this test are fake)
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swaps = vec![
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0x0000000000000000000000000000000000000000".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: weth(),
|
||||
split: 0.5f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0x0000000000000000000000000000000000000000".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: weth(),
|
||||
split: 0f64,
|
||||
},
|
||||
];
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: weth(),
|
||||
checked_token: weth(),
|
||||
swaps,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_cyclical_swap_native_action_fail() {
|
||||
// This validation fails because there is a native action with a valid cyclical swap
|
||||
// ETH -> WETH -> DAI -> WETH
|
||||
// (some of the pool addresses in this test are fake)
|
||||
let encoder = get_mocked_tycho_encoder();
|
||||
let swaps = vec![
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
},
|
||||
Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0x0000000000000000000000000000000000000000".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: dai(),
|
||||
token_out: weth(),
|
||||
split: 0f64,
|
||||
},
|
||||
];
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: eth(),
|
||||
checked_token: weth(),
|
||||
swaps,
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = encoder.validate_solution(&solution);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
result.err().unwrap(),
|
||||
EncodingError::FatalError(
|
||||
"Wrapping/Unwrapping is not available in cyclical swaps"
|
||||
.to_string()
|
||||
.to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
1065
src/encoding/evm/tycho_encoders.rs
Normal file
1065
src/encoding/evm/tycho_encoders.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,7 @@ use alloy::{
|
||||
transports::BoxTransport,
|
||||
};
|
||||
use alloy_primitives::{aliases::U24, keccak256, Address, FixedBytes, Keccak256, U256, U8};
|
||||
use alloy_sol_types::SolValue;
|
||||
use num_bigint::BigUint;
|
||||
use tokio::runtime::{Handle, Runtime};
|
||||
use tycho_common::Bytes;
|
||||
@@ -150,6 +151,22 @@ pub async fn get_client() -> Result<Arc<RootProvider<BoxTransport>>, EncodingErr
|
||||
Ok(Arc::new(client))
|
||||
}
|
||||
|
||||
|
||||
/// Uses prefix-length encoding to efficient encode action data.
|
||||
///
|
||||
/// Prefix-length encoding is a data encoding method where the beginning of a data segment
|
||||
/// (the "prefix") contains information about the length of the following data.
|
||||
pub fn ple_encode(action_data_array: Vec<Vec<u8>>) -> Vec<u8> {
|
||||
let mut encoded_action_data: Vec<u8> = Vec::new();
|
||||
|
||||
for action_data in action_data_array {
|
||||
let args = (encoded_action_data, action_data.len() as u16, action_data);
|
||||
encoded_action_data = args.abi_encode_packed();
|
||||
}
|
||||
|
||||
encoded_action_data
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use num_bigint::BigUint;
|
||||
|
||||
@@ -2,7 +2,7 @@ use hex;
|
||||
use num_bigint::BigUint;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tycho_common::{
|
||||
models::{protocol::ProtocolComponent, Chain as TychoCoreChain},
|
||||
models::{protocol::ProtocolComponent, Chain as TychoCommonChain},
|
||||
Bytes,
|
||||
};
|
||||
|
||||
@@ -121,15 +121,15 @@ pub struct Chain {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl From<TychoCoreChain> for Chain {
|
||||
fn from(chain: TychoCoreChain) -> Self {
|
||||
impl From<TychoCommonChain> for Chain {
|
||||
fn from(chain: TychoCommonChain) -> Self {
|
||||
match chain {
|
||||
TychoCoreChain::Ethereum => Chain { id: 1, name: chain.to_string() },
|
||||
TychoCoreChain::ZkSync => Chain { id: 324, name: chain.to_string() },
|
||||
TychoCoreChain::Arbitrum => Chain { id: 42161, name: chain.to_string() },
|
||||
TychoCoreChain::Starknet => Chain { id: 0, name: chain.to_string() },
|
||||
TychoCoreChain::Base => Chain { id: 8453, name: chain.to_string() },
|
||||
TychoCoreChain::Unichain => Chain { id: 130, name: chain.to_string() },
|
||||
TychoCommonChain::Ethereum => Chain { id: 1, name: chain.to_string() },
|
||||
TychoCommonChain::ZkSync => Chain { id: 324, name: chain.to_string() },
|
||||
TychoCommonChain::Arbitrum => Chain { id: 42161, name: chain.to_string() },
|
||||
TychoCommonChain::Starknet => Chain { id: 0, name: chain.to_string() },
|
||||
TychoCommonChain::Base => Chain { id: 8453, name: chain.to_string() },
|
||||
TychoCommonChain::Unichain => Chain { id: 130, name: chain.to_string() },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,7 @@ pub trait TychoEncoder {
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<Vec<Transaction>, EncodingError>` - Vector of executable transactions
|
||||
fn encode_router_calldata(
|
||||
&self,
|
||||
solutions: Vec<Solution>,
|
||||
) -> Result<Vec<Transaction>, EncodingError>;
|
||||
fn encode_calldata(&self, solutions: Vec<Solution>) -> Result<Vec<Transaction>, EncodingError>;
|
||||
|
||||
fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user