chore: merge main
Took 3 minutes
This commit is contained in:
@@ -35,6 +35,11 @@ module.exports = {
|
||||
url: process.env.RPC_URL,
|
||||
accounts: [process.env.PRIVATE_KEY],
|
||||
chainId: 8453
|
||||
},
|
||||
unichain: {
|
||||
url: process.env.RPC_URL,
|
||||
accounts: [process.env.PRIVATE_KEY],
|
||||
chainId: 130
|
||||
}
|
||||
},
|
||||
|
||||
@@ -46,5 +51,15 @@ module.exports = {
|
||||
|
||||
etherscan: {
|
||||
apiKey: process.env.BLOCKCHAIN_EXPLORER_API_KEY,
|
||||
customChains: [
|
||||
{
|
||||
network: "unichain",
|
||||
chainId: 130,
|
||||
urls: {
|
||||
apiURL: "https://api.uniscan.xyz/api",
|
||||
browserURL: "https://www.uniscan.xyz/"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
16
foundry/lib/ekubo/interfaces/ICore.sol
Normal file
16
foundry/lib/ekubo/interfaces/ICore.sol
Normal file
@@ -0,0 +1,16 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
import {IFlashAccountant} from "./IFlashAccountant.sol";
|
||||
import {EkuboPoolKey} from "../types/poolKey.sol";
|
||||
import {SqrtRatio} from "../types/sqrtRatio.sol";
|
||||
|
||||
interface ICore is IFlashAccountant {
|
||||
function swap_611415377(
|
||||
EkuboPoolKey memory poolKey,
|
||||
int128 amount,
|
||||
bool isToken1,
|
||||
SqrtRatio sqrtRatioLimit,
|
||||
uint256 skipAhead
|
||||
) external payable returns (int128 delta0, int128 delta1);
|
||||
}
|
||||
16
foundry/lib/ekubo/interfaces/IFlashAccountant.sol
Normal file
16
foundry/lib/ekubo/interfaces/IFlashAccountant.sol
Normal file
@@ -0,0 +1,16 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
interface ILocker {
|
||||
function locked(uint256 id) external;
|
||||
}
|
||||
|
||||
interface IPayer {
|
||||
function payCallback(uint256 id, address token) external;
|
||||
}
|
||||
|
||||
interface IFlashAccountant {
|
||||
// Withdraws a token amount from the accountant to the given recipient.
|
||||
// The contract must be locked, as it tracks the withdrawn amount against the current locker's delta.
|
||||
function withdraw(address token, address recipient, uint128 amount) external;
|
||||
}
|
||||
5
foundry/lib/ekubo/math/constants.sol
Normal file
5
foundry/lib/ekubo/math/constants.sol
Normal file
@@ -0,0 +1,5 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
// We use this address to represent the native token within the protocol
|
||||
address constant NATIVE_TOKEN_ADDRESS = address(0);
|
||||
12
foundry/lib/ekubo/types/poolKey.sol
Normal file
12
foundry/lib/ekubo/types/poolKey.sol
Normal file
@@ -0,0 +1,12 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
// address (20 bytes) | fee (8 bytes) | tickSpacing (4 bytes)
|
||||
type Config is bytes32;
|
||||
|
||||
// Each pool has its own state associated with this key
|
||||
struct EkuboPoolKey {
|
||||
address token0;
|
||||
address token1;
|
||||
Config config;
|
||||
}
|
||||
9
foundry/lib/ekubo/types/sqrtRatio.sol
Normal file
9
foundry/lib/ekubo/types/sqrtRatio.sol
Normal file
@@ -0,0 +1,9 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
type SqrtRatio is uint96;
|
||||
|
||||
uint96 constant MIN_SQRT_RATIO_RAW = 4611797791050542631;
|
||||
SqrtRatio constant MIN_SQRT_RATIO = SqrtRatio.wrap(MIN_SQRT_RATIO_RAW);
|
||||
uint96 constant MAX_SQRT_RATIO_RAW = 79227682466138141934206691491;
|
||||
SqrtRatio constant MAX_SQRT_RATIO = SqrtRatio.wrap(MAX_SQRT_RATIO_RAW);
|
||||
1
foundry/lib/solady
Submodule
1
foundry/lib/solady
Submodule
Submodule foundry/lib/solady added at c9e079c0ca
@@ -1,41 +0,0 @@
|
||||
// Updated v3 lib to solidity >=0.7.6
|
||||
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
pragma solidity >=0.7.6;
|
||||
|
||||
import "./PoolAddressV2.sol";
|
||||
import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
|
||||
|
||||
/// @notice Provides validation for callbacks from Uniswap V3 Pools
|
||||
library CallbackValidationV2 {
|
||||
/// @notice Returns the address of a valid Uniswap V3 Pool
|
||||
/// @param factory The contract address of the Uniswap V3 factory
|
||||
/// @param tokenA The contract address of either token0 or token1
|
||||
/// @param tokenB The contract address of the other token
|
||||
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
|
||||
/// @return pool The V3 pool contract address
|
||||
function verifyCallback(
|
||||
address factory,
|
||||
address tokenA,
|
||||
address tokenB,
|
||||
uint24 fee
|
||||
) internal view returns (IUniswapV3Pool pool) {
|
||||
return
|
||||
verifyCallback(
|
||||
factory,
|
||||
PoolAddressV2.getPoolKey(tokenA, tokenB, fee)
|
||||
);
|
||||
}
|
||||
|
||||
/// @notice Returns the address of a valid Uniswap V3 Pool
|
||||
/// @param factory The contract address of the Uniswap V3 factory
|
||||
/// @param poolKey The identifying key of the V3 pool
|
||||
/// @return pool The V3 pool contract address
|
||||
function verifyCallback(
|
||||
address factory,
|
||||
PoolAddressV2.PoolKey memory poolKey
|
||||
) internal view returns (IUniswapV3Pool pool) {
|
||||
pool = IUniswapV3Pool(PoolAddressV2.computeAddress(factory, poolKey));
|
||||
require(msg.sender == address(pool), "CV");
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
// Updated v3 lib to solidity >=0.7.6
|
||||
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
pragma solidity >=0.5.0;
|
||||
|
||||
/// @title Provides functions for deriving a pool address from the factory, tokens, and the fee
|
||||
library PoolAddressV2 {
|
||||
bytes32 internal constant POOL_INIT_CODE_HASH =
|
||||
0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
|
||||
|
||||
/// @notice The identifying key of the pool
|
||||
struct PoolKey {
|
||||
address token0;
|
||||
address token1;
|
||||
uint24 fee;
|
||||
}
|
||||
|
||||
/// @notice Returns PoolKey: the ordered tokens with the matched fee levels
|
||||
/// @param tokenA The first token of a pool, unsorted
|
||||
/// @param tokenB The second token of a pool, unsorted
|
||||
/// @param fee The fee level of the pool
|
||||
/// @return Poolkey The pool details with ordered token0 and token1 assignments
|
||||
function getPoolKey(address tokenA, address tokenB, uint24 fee)
|
||||
internal
|
||||
pure
|
||||
returns (PoolKey memory)
|
||||
{
|
||||
if (tokenA > tokenB) (tokenA, tokenB) = (tokenB, tokenA);
|
||||
return PoolKey({token0: tokenA, token1: tokenB, fee: fee});
|
||||
}
|
||||
|
||||
/// @notice Deterministically computes the pool address given the factory and PoolKey
|
||||
/// @param factory The Uniswap V3 factory contract address
|
||||
/// @param key The PoolKey
|
||||
/// @return pool The contract address of the V3 pool
|
||||
function computeAddress(address factory, PoolKey memory key)
|
||||
internal
|
||||
pure
|
||||
returns (address pool)
|
||||
{
|
||||
require(key.token0 < key.token1);
|
||||
pool = address(
|
||||
uint160(
|
||||
uint256(
|
||||
keccak256(
|
||||
abi.encodePacked(
|
||||
hex"ff",
|
||||
factory,
|
||||
keccak256(
|
||||
abi.encode(key.token0, key.token1, key.fee)
|
||||
),
|
||||
POOL_INIT_CODE_HASH
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,6 @@
|
||||
@uniswap/v3-updated/=lib/v3-updated/
|
||||
@uniswap/v3-core/=lib/v3-core/
|
||||
@uniswap/v4-core/=lib/v4-core/
|
||||
@uniswap/v4-periphery/=lib/v4-periphery/
|
||||
@uniswap/v4-periphery/=lib/v4-periphery/
|
||||
@solady=lib/solady/src/
|
||||
@ekubo=lib/ekubo/
|
||||
|
||||
@@ -38,7 +38,7 @@ For each of the following, you must select one of `tenderly_ethereum`, `tenderly
|
||||
|
||||
1. Deploy router: `npx hardhat run scripts/deploy-router.js --network NETWORK`
|
||||
2. Define the accounts to grant roles to in `scripts/roles.json`
|
||||
3. Export the router address to the environment variable `export ROUTER=<router-address>`
|
||||
3. Export the router address to the environment variable `export ROUTER_ADDRESS=<router-address>`
|
||||
4. Grant roles: `npx hardhat run scripts/set-roles.js --network NETWORK`
|
||||
5. Set executors: `npx hardhat run scripts/set-executors.js --network NETWORK`. Make sure you change the
|
||||
DEPLOY_WALLET to the executor deployer wallet. If you need to deploy executors, follow the instructions below.
|
||||
@@ -47,4 +47,4 @@ For each of the following, you must select one of `tenderly_ethereum`, `tenderly
|
||||
|
||||
1. In `scripts/deploy-executors.js` define the executors to be deployed
|
||||
2. Deploy executors: `npx hardhat run scripts/deploy-executors.js --network NETWORK`
|
||||
3. Fill in the executor addresses in `config/executors.json`
|
||||
3. Fill in the executor addresses in `config/executor_addresses.json`
|
||||
|
||||
@@ -5,18 +5,83 @@ const hre = require("hardhat");
|
||||
// Comment out the executors you don't want to deploy
|
||||
const executors_to_deploy = {
|
||||
"ethereum":[
|
||||
{exchange: "UniswapV2Executor", args: ["0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"]},
|
||||
{exchange: "UniswapV3Executor", args: ["0x1F98431c8aD98523631AE4a59f267346ea31F984"]},
|
||||
// USV2 - Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV2Executor", args: [
|
||||
"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
|
||||
"0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f"
|
||||
]},
|
||||
// SUSHISWAP - Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV2Executor", args: [
|
||||
"0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac",
|
||||
"0xe18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303"
|
||||
]},
|
||||
// PANCAKESWAP V2 - Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV2Executor", args: [
|
||||
"0x1097053Fd2ea711dad45caCcc45EfF7548fCB362",
|
||||
"0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d"
|
||||
]},
|
||||
// USV3 -Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV3Executor", args: [
|
||||
"0x1F98431c8aD98523631AE4a59f267346ea31F984",
|
||||
"0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54"
|
||||
]},
|
||||
// PANCAKESWAP V3 - Args: Deployer, Pool Init Code Hash
|
||||
{exchange: "UniswapV3Executor", args: [
|
||||
"0x41ff9AA7e16B8B1a8a8dc4f0eFacd93D02d071c9",
|
||||
"0x6ce8eb472fa82df5469c6ab6d485f17c3ad13c8cd7af59b3d4a8026c5ce0f7e2"
|
||||
]},
|
||||
// Args: Pool manager
|
||||
{exchange: "UniswapV4Executor", args: ["0x000000000004444c5dc75cB358380D2e3dE08A90"]},
|
||||
{exchange: "BalancerV2Executor", args: []},
|
||||
// Args: Ekubo core contract
|
||||
{exchange: "EkuboExecutor", args: [
|
||||
"0xe0e0e08A6A4b9Dc7bD67BCB7aadE5cF48157d444"
|
||||
]}
|
||||
],
|
||||
"base":[
|
||||
{exchange: "UniswapV2Executor", args: ["0x8909Dc15e40173Ff4699343b6eB8132c65e18eC6"]},
|
||||
{exchange: "UniswapV3Executor", args: ["0x33128a8fC17869897dcE68Ed026d694621f6FDfD"]},
|
||||
// Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV2Executor", args: [
|
||||
"0x8909Dc15e40173Ff4699343b6eB8132c65e18eC6",
|
||||
"0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f"
|
||||
]},
|
||||
// SUSHISWAP V2 - Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV2Executor", args: [
|
||||
"0x71524B4f93c58fcbF659783284E38825f0622859",
|
||||
"0xe18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303"
|
||||
]},
|
||||
// PANCAKESWAP V2 - Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV2Executor", args: [
|
||||
"0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
|
||||
"0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d"
|
||||
]},
|
||||
// USV3 - Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV3Executor", args: [
|
||||
"0x33128a8fC17869897dcE68Ed026d694621f6FDfD",
|
||||
"0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54"
|
||||
]},
|
||||
// PANCAKESWAP V3 - Args: Deployer, Pool Init Code Hash
|
||||
{exchange: "UniswapV3Executor", args: [
|
||||
"0x41ff9AA7e16B8B1a8a8dc4f0eFacd93D02d071c9",
|
||||
"0x6ce8eb472fa82df5469c6ab6d485f17c3ad13c8cd7af59b3d4a8026c5ce0f7e2"
|
||||
]},
|
||||
// Args: Pool manager
|
||||
{exchange: "UniswapV4Executor", args: ["0x498581ff718922c3f8e6a244956af099b2652b2b"]},
|
||||
{exchange: "BalancerV2Executor", args: []},
|
||||
],
|
||||
|
||||
"unichain":[
|
||||
// Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV2Executor", args: [
|
||||
"0x1f98400000000000000000000000000000000002",
|
||||
"0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f"
|
||||
]},
|
||||
// USV3 - Args: Factory, Pool Init Code Hash
|
||||
{exchange: "UniswapV3Executor", args: [
|
||||
"0x1f98400000000000000000000000000000000003",
|
||||
"0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54"
|
||||
]},
|
||||
// Args: Pool manager
|
||||
{exchange: "UniswapV4Executor", args: ["0x1f98400000000000000000000000000000000004"]},
|
||||
],
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
||||
@@ -13,6 +13,10 @@ async function main() {
|
||||
// permit2 address is the same as on ethereum
|
||||
permit2 = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
|
||||
weth = "0x4200000000000000000000000000000000000006";
|
||||
} else if (network === "unichain") {
|
||||
// permit2 address is the same as on ethereum
|
||||
permit2 = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
|
||||
weth = "0x4200000000000000000000000000000000000006";
|
||||
} else {
|
||||
throw new Error(`Unsupported network: ${network}`);
|
||||
}
|
||||
|
||||
@@ -58,5 +58,20 @@
|
||||
"FUND_RESCUER_ROLE": [
|
||||
"0xb0A77f867Fcec1e9b271Ee17354bC6bBC0dD5662"
|
||||
]
|
||||
},
|
||||
"unichain": {
|
||||
"EXECUTOR_SETTER_ROLE": [
|
||||
"0x810A00Fa9287700871ba0f870Cd89D7Eac08D48C"
|
||||
],
|
||||
"FEE_SETTER_ROLE": [],
|
||||
"PAUSER_ROLE": [
|
||||
"0x810A00Fa9287700871ba0f870Cd89D7Eac08D48C"
|
||||
],
|
||||
"UNPAUSER_ROLE": [
|
||||
"0x810A00Fa9287700871ba0f870Cd89D7Eac08D48C"
|
||||
],
|
||||
"FUND_RESCUER_ROLE": [
|
||||
"0x810A00Fa9287700871ba0f870Cd89D7Eac08D48C"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ async function main() {
|
||||
// Set executors
|
||||
const executorAddresses = executorsToSet.map(executor => executor.executor);
|
||||
const tx = await router.setExecutors(executorAddresses, {
|
||||
gasLimit: 200000 // should be around 50k per executor
|
||||
gasLimit: 300000 // should be around 50k per executor
|
||||
});
|
||||
await tx.wait(); // Wait for the transaction to be mined
|
||||
console.log(`Executors set at transaction: ${tx.hash}`);
|
||||
|
||||
@@ -111,8 +111,7 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
/**
|
||||
* @notice Executes a swap operation based on a predefined swap graph, supporting internal token amount splits.
|
||||
* This function enables multi-step swaps, optional ETH wrapping/unwrapping, and validates the output amount
|
||||
* against a user-specified minimum. This function expects the input tokens to already be in the router at
|
||||
* the time of calling.
|
||||
* 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.
|
||||
@@ -144,7 +143,11 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
address receiver,
|
||||
bytes calldata swaps
|
||||
) public payable whenNotPaused nonReentrant returns (uint256 amountOut) {
|
||||
IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
|
||||
if (address(tokenIn) != address(0)) {
|
||||
IERC20(tokenIn).safeTransferFrom(
|
||||
msg.sender, address(this), amountIn
|
||||
);
|
||||
}
|
||||
return _splitSwapChecked(
|
||||
amountIn,
|
||||
tokenIn,
|
||||
@@ -897,6 +900,25 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
_handleCallback(fullData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Called by PancakeV3 pool when swapping on it.
|
||||
*/
|
||||
function pancakeV3SwapCallback(
|
||||
int256, /* amount0Delta */
|
||||
int256, /* amount1Delta */
|
||||
bytes calldata data
|
||||
) external {
|
||||
if (data.length < 24) revert TychoRouter__InvalidDataLength();
|
||||
// We are taking advantage of the fact that the data we need is already encoded in the correct format inside msg.data
|
||||
// This way we preserve the bytes calldata (and don't need to convert it to bytes memory)
|
||||
uint256 dataOffset = 4 + 32 + 32 + 32; // Skip selector + 2 ints + data_offset
|
||||
uint256 dataLength =
|
||||
uint256(bytes32(msg.data[dataOffset:dataOffset + 32]));
|
||||
|
||||
bytes calldata fullData = msg.data[4:dataOffset + 32 + dataLength];
|
||||
_handleCallback(fullData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Called by UniswapV4 pool manager after achieving unlock state.
|
||||
*/
|
||||
@@ -908,4 +930,44 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
||||
_handleCallback(data);
|
||||
return "";
|
||||
}
|
||||
|
||||
function locked(uint256) external {
|
||||
address executor = address(0x4f88f6630a33dB05BEa1FeF7Dc7ff7508D1c531D);
|
||||
|
||||
// slither-disable-next-line controlled-delegatecall,low-level-calls
|
||||
(bool success, bytes memory result) = executor.delegatecall(msg.data);
|
||||
|
||||
if (!success) {
|
||||
revert(
|
||||
string(
|
||||
result.length > 0
|
||||
? result
|
||||
: abi.encodePacked("Callback failed")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// slither-disable-next-line assembly
|
||||
assembly ("memory-safe") {
|
||||
// Propagate the swappedAmount
|
||||
return(add(result, 32), 16)
|
||||
}
|
||||
}
|
||||
|
||||
function payCallback(uint256, address /*token*/ ) external {
|
||||
address executor = address(0x4f88f6630a33dB05BEa1FeF7Dc7ff7508D1c531D);
|
||||
|
||||
// slither-disable-next-line controlled-delegatecall,low-level-calls
|
||||
(bool success, bytes memory result) = executor.delegatecall(msg.data);
|
||||
|
||||
if (!success) {
|
||||
revert(
|
||||
string(
|
||||
result.length > 0
|
||||
? result
|
||||
: abi.encodePacked("Callback failed")
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
154
foundry/src/executors/EkuboExecutor.sol
Normal file
154
foundry/src/executors/EkuboExecutor.sol
Normal file
@@ -0,0 +1,154 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import {IExecutor} from "@interfaces/IExecutor.sol";
|
||||
import {ICore} from "@ekubo/interfaces/ICore.sol";
|
||||
import {ILocker, IPayer} from "@ekubo/interfaces/IFlashAccountant.sol";
|
||||
import {NATIVE_TOKEN_ADDRESS} from "@ekubo/math/constants.sol";
|
||||
import {SafeTransferLib} from "@solady/utils/SafeTransferLib.sol";
|
||||
import {LibBytes} from "@solady/utils/LibBytes.sol";
|
||||
import {Config, EkuboPoolKey} from "@ekubo/types/poolKey.sol";
|
||||
import {MAX_SQRT_RATIO, MIN_SQRT_RATIO} from "@ekubo/types/sqrtRatio.sol";
|
||||
|
||||
contract EkuboExecutor is IExecutor, ILocker, IPayer {
|
||||
error EkuboExecutor__InvalidDataLength();
|
||||
error EkuboExecutor__CoreOnly();
|
||||
error EkuboExecutor__UnknownCallback();
|
||||
|
||||
ICore immutable core;
|
||||
|
||||
uint256 constant POOL_DATA_OFFSET = 92;
|
||||
uint256 constant HOP_BYTE_LEN = 52;
|
||||
|
||||
constructor(address _core) {
|
||||
core = ICore(_core);
|
||||
}
|
||||
|
||||
function swap(uint256 amountIn, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 calculatedAmount)
|
||||
{
|
||||
if (data.length < 92) revert EkuboExecutor__InvalidDataLength();
|
||||
|
||||
// amountIn must be at most type(int128).MAX
|
||||
calculatedAmount =
|
||||
uint256(_lock(bytes.concat(bytes16(uint128(amountIn)), data)));
|
||||
}
|
||||
|
||||
function locked(uint256) external coreOnly {
|
||||
int128 nextAmountIn = int128(uint128(bytes16(msg.data[36:52])));
|
||||
uint128 tokenInDebtAmount = uint128(nextAmountIn);
|
||||
|
||||
address receiver = address(bytes20(msg.data[52:72]));
|
||||
address tokenIn = address(bytes20(msg.data[72:POOL_DATA_OFFSET]));
|
||||
|
||||
address nextTokenIn = tokenIn;
|
||||
|
||||
uint256 hopsLength = (msg.data.length - POOL_DATA_OFFSET) / HOP_BYTE_LEN;
|
||||
|
||||
uint256 offset = POOL_DATA_OFFSET;
|
||||
|
||||
for (uint256 i = 0; i < hopsLength; i++) {
|
||||
address nextTokenOut =
|
||||
address(bytes20(LibBytes.loadCalldata(msg.data, offset)));
|
||||
Config poolConfig =
|
||||
Config.wrap(LibBytes.loadCalldata(msg.data, offset + 20));
|
||||
|
||||
(address token0, address token1, bool isToken1) = nextTokenIn
|
||||
> nextTokenOut
|
||||
? (nextTokenOut, nextTokenIn, true)
|
||||
: (nextTokenIn, nextTokenOut, false);
|
||||
|
||||
// slither-disable-next-line calls-loop
|
||||
(int128 delta0, int128 delta1) = core.swap_611415377(
|
||||
EkuboPoolKey(token0, token1, poolConfig),
|
||||
nextAmountIn,
|
||||
isToken1,
|
||||
isToken1 ? MAX_SQRT_RATIO : MIN_SQRT_RATIO,
|
||||
0
|
||||
);
|
||||
|
||||
nextTokenIn = nextTokenOut;
|
||||
nextAmountIn = -(isToken1 ? delta0 : delta1);
|
||||
|
||||
offset += HOP_BYTE_LEN;
|
||||
}
|
||||
|
||||
_pay(tokenIn, tokenInDebtAmount);
|
||||
|
||||
core.withdraw(nextTokenIn, receiver, uint128(nextAmountIn));
|
||||
|
||||
// slither-disable-next-line assembly
|
||||
assembly ("memory-safe") {
|
||||
mstore(0, nextAmountIn)
|
||||
return(0x10, 16)
|
||||
}
|
||||
}
|
||||
|
||||
function payCallback(uint256, address token) external coreOnly {
|
||||
uint128 amount = uint128(bytes16(msg.data[68:84]));
|
||||
|
||||
SafeTransferLib.safeTransfer(token, address(core), amount);
|
||||
}
|
||||
|
||||
function _lock(bytes memory data)
|
||||
internal
|
||||
returns (uint128 swappedAmount)
|
||||
{
|
||||
address target = address(core);
|
||||
|
||||
// slither-disable-next-line assembly
|
||||
assembly ("memory-safe") {
|
||||
let args := mload(0x40)
|
||||
|
||||
// Selector of lock()
|
||||
mstore(args, shl(224, 0xf83d08ba))
|
||||
|
||||
// We only copy the data, not the length, because the length is read from the calldata size
|
||||
let len := mload(data)
|
||||
mcopy(add(args, 4), add(data, 32), len)
|
||||
|
||||
// If the call failed, pass through the revert
|
||||
if iszero(call(gas(), target, 0, args, add(len, 36), 0, 0)) {
|
||||
returndatacopy(0, 0, returndatasize())
|
||||
revert(0, returndatasize())
|
||||
}
|
||||
|
||||
returndatacopy(0, 0, 16)
|
||||
swappedAmount := shr(128, mload(0))
|
||||
}
|
||||
}
|
||||
|
||||
function _pay(address token, uint128 amount) internal {
|
||||
address target = address(core);
|
||||
|
||||
if (token == NATIVE_TOKEN_ADDRESS) {
|
||||
SafeTransferLib.safeTransferETH(target, amount);
|
||||
} else {
|
||||
// slither-disable-next-line assembly
|
||||
assembly ("memory-safe") {
|
||||
let free := mload(0x40)
|
||||
// selector of pay(address)
|
||||
mstore(free, shl(224, 0x0c11dedd))
|
||||
mstore(add(free, 4), token)
|
||||
mstore(add(free, 36), shl(128, amount))
|
||||
|
||||
// if it failed, pass through revert
|
||||
if iszero(call(gas(), target, 0, free, 52, 0, 0)) {
|
||||
returndatacopy(0, 0, returndatasize())
|
||||
revert(0, returndatasize())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// To receive withdrawals from Core
|
||||
receive() external payable {}
|
||||
|
||||
modifier coreOnly() {
|
||||
if (msg.sender != address(core)) revert EkuboExecutor__CoreOnly();
|
||||
_;
|
||||
}
|
||||
}
|
||||
@@ -8,21 +8,24 @@ import "@uniswap-v2/contracts/interfaces/IUniswapV2Pair.sol";
|
||||
error UniswapV2Executor__InvalidDataLength();
|
||||
error UniswapV2Executor__InvalidTarget();
|
||||
error UniswapV2Executor__InvalidFactory();
|
||||
error UniswapV2Executor__InvalidInitCode();
|
||||
|
||||
contract UniswapV2Executor is IExecutor {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
bytes32 internal constant POOL_INIT_CODE_HASH =
|
||||
0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f;
|
||||
|
||||
address public immutable factory;
|
||||
bytes32 public immutable initCode;
|
||||
address private immutable self;
|
||||
|
||||
constructor(address _factory) {
|
||||
constructor(address _factory, bytes32 _initCode) {
|
||||
if (_factory == address(0)) {
|
||||
revert UniswapV2Executor__InvalidFactory();
|
||||
}
|
||||
if (_initCode == bytes32(0)) {
|
||||
revert UniswapV2Executor__InvalidInitCode();
|
||||
}
|
||||
factory = _factory;
|
||||
initCode = _initCode;
|
||||
self = address(this);
|
||||
}
|
||||
|
||||
@@ -102,9 +105,7 @@ contract UniswapV2Executor is IExecutor {
|
||||
uint160(
|
||||
uint256(
|
||||
keccak256(
|
||||
abi.encodePacked(
|
||||
hex"ff", factory, salt, POOL_INIT_CODE_HASH
|
||||
)
|
||||
abi.encodePacked(hex"ff", factory, salt, initCode)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -4,30 +4,33 @@ pragma solidity ^0.8.26;
|
||||
import "@interfaces/IExecutor.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
|
||||
import "@uniswap/v3-updated/CallbackValidationV2.sol";
|
||||
import "@interfaces/ICallback.sol";
|
||||
|
||||
error UniswapV3Executor__InvalidDataLength();
|
||||
error UniswapV3Executor__InvalidFactory();
|
||||
error UniswapV3Executor__InvalidTarget();
|
||||
error UniswapV3Executor__InvalidInitCode();
|
||||
|
||||
contract UniswapV3Executor is IExecutor, ICallback {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
bytes32 internal constant POOL_INIT_CODE_HASH =
|
||||
0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
|
||||
uint160 private constant MIN_SQRT_RATIO = 4295128739;
|
||||
uint160 private constant MAX_SQRT_RATIO =
|
||||
1461446703485210103287273052203988822378723970342;
|
||||
|
||||
address public immutable factory;
|
||||
bytes32 public immutable initCode;
|
||||
address private immutable self;
|
||||
|
||||
constructor(address _factory) {
|
||||
constructor(address _factory, bytes32 _initCode) {
|
||||
if (_factory == address(0)) {
|
||||
revert UniswapV3Executor__InvalidFactory();
|
||||
}
|
||||
if (_initCode == bytes32(0)) {
|
||||
revert UniswapV3Executor__InvalidInitCode();
|
||||
}
|
||||
factory = _factory;
|
||||
initCode = _initCode;
|
||||
self = address(this);
|
||||
}
|
||||
|
||||
@@ -102,8 +105,7 @@ contract UniswapV3Executor is IExecutor, ICallback {
|
||||
address tokenOut = address(bytes20(data[20:40]));
|
||||
uint24 poolFee = uint24(bytes3(data[40:43]));
|
||||
|
||||
// slither-disable-next-line unused-return
|
||||
CallbackValidationV2.verifyCallback(factory, tokenIn, tokenOut, poolFee);
|
||||
_verifyPairAddress(tokenIn, tokenOut, poolFee, msg.sender);
|
||||
}
|
||||
|
||||
function uniswapV3SwapCallback(
|
||||
@@ -167,7 +169,7 @@ contract UniswapV3Executor is IExecutor, ICallback {
|
||||
hex"ff",
|
||||
factory,
|
||||
keccak256(abi.encode(token0, token1, fee)),
|
||||
POOL_INIT_CODE_HASH
|
||||
initCode
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -47,19 +47,53 @@ contract Constants is Test, BaseConstants {
|
||||
address USDC_WBTC_POOL = 0x004375Dff511095CC5A197A54140a24eFEF3A416;
|
||||
address USDC_WETH_USV2 = 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc;
|
||||
|
||||
// Sushiswap v2
|
||||
address SUSHISWAP_WBTC_WETH_POOL =
|
||||
0xCEfF51756c56CeFFCA006cD410B03FFC46dd3a58;
|
||||
|
||||
// Pancakeswap v2
|
||||
address PANCAKESWAP_WBTC_WETH_POOL =
|
||||
0x4AB6702B3Ed3877e9b1f203f90cbEF13d663B0e8;
|
||||
|
||||
// Uniswap v3
|
||||
address USV3_FACTORY_ETHEREUM = 0x1F98431c8aD98523631AE4a59f267346ea31F984;
|
||||
address USV2_FACTORY_ETHEREUM = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
|
||||
address DAI_WETH_USV3 = 0xC2e9F25Be6257c210d7Adf0D4Cd6E3E881ba25f8;
|
||||
address USDC_WETH_USV3 = 0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640; // 0.05% fee
|
||||
address USDC_WETH_USV3_2 = 0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8; // 0.3% fee
|
||||
|
||||
// Pancakeswap v3
|
||||
address PANCAKESWAPV3_WETH_USDT_POOL =
|
||||
0x6CA298D2983aB03Aa1dA7679389D955A4eFEE15C;
|
||||
|
||||
// Factories
|
||||
address USV3_FACTORY_ETHEREUM = 0x1F98431c8aD98523631AE4a59f267346ea31F984;
|
||||
address USV2_FACTORY_ETHEREUM = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
|
||||
address SUSHISWAPV2_FACTORY_ETHEREUM =
|
||||
0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac;
|
||||
address PANCAKESWAPV2_FACTORY_ETHEREUM =
|
||||
0x1097053Fd2ea711dad45caCcc45EfF7548fCB362;
|
||||
|
||||
// Pancakeswap uses their deployer instead of their factory for target verification
|
||||
address PANCAKESWAPV3_DEPLOYER_ETHEREUM =
|
||||
0x41ff9AA7e16B8B1a8a8dc4f0eFacd93D02d071c9;
|
||||
|
||||
// Uniswap universal router
|
||||
address UNIVERSAL_ROUTER = 0x66a9893cC07D91D95644AEDD05D03f95e1dBA8Af;
|
||||
|
||||
// Permit2
|
||||
address PERMIT2_ADDRESS = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
|
||||
|
||||
// Pool Code Init Hashes
|
||||
bytes32 USV2_POOL_CODE_INIT_HASH =
|
||||
0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f;
|
||||
bytes32 USV3_POOL_CODE_INIT_HASH =
|
||||
0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
|
||||
bytes32 SUSHIV2_POOL_CODE_INIT_HASH =
|
||||
0xe18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303;
|
||||
bytes32 PANCAKEV2_POOL_CODE_INIT_HASH =
|
||||
0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d;
|
||||
bytes32 PANCAKEV3_POOL_CODE_INIT_HASH =
|
||||
0x6ce8eb472fa82df5469c6ab6d485f17c3ad13c8cd7af59b3d4a8026c5ce0f7e2;
|
||||
|
||||
/**
|
||||
* @dev Deploys a dummy contract with non-empty bytecode
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
import "../src/executors/BalancerV2Executor.sol";
|
||||
import "../src/executors/EkuboExecutor.sol";
|
||||
import "../src/executors/UniswapV2Executor.sol";
|
||||
import "../src/executors/UniswapV3Executor.sol";
|
||||
import "../src/executors/UniswapV4Executor.sol";
|
||||
@@ -8,8 +10,8 @@ import "./Constants.sol";
|
||||
import "./mock/MockERC20.sol";
|
||||
import "@src/TychoRouter.sol";
|
||||
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
|
||||
import {WETH} from "../lib/permit2/lib/solmate/src/tokens/WETH.sol";
|
||||
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
|
||||
import {WETH} from "../lib/permit2/lib/solmate/src/tokens/WETH.sol";
|
||||
|
||||
contract TychoRouterExposed is TychoRouter {
|
||||
constructor(address _permit2, address weth) TychoRouter(_permit2, weth) {}
|
||||
@@ -43,7 +45,10 @@ contract TychoRouterTestSetup is Constants {
|
||||
address tychoRouterAddr;
|
||||
UniswapV2Executor public usv2Executor;
|
||||
UniswapV3Executor public usv3Executor;
|
||||
UniswapV3Executor public pancakev3Executor;
|
||||
UniswapV4Executor public usv4Executor;
|
||||
BalancerV2Executor public balancerv2Executor;
|
||||
EkuboExecutor public ekuboExecutor;
|
||||
MockERC20[] tokens;
|
||||
|
||||
function setUp() public {
|
||||
@@ -51,10 +56,23 @@ contract TychoRouterTestSetup is Constants {
|
||||
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
||||
|
||||
vm.startPrank(ADMIN);
|
||||
address factoryV3 = USV3_FACTORY_ETHEREUM;
|
||||
address factoryV2 = USV2_FACTORY_ETHEREUM;
|
||||
address poolManagerAddress = 0x000000000004444c5dc75cB358380D2e3dE08A90;
|
||||
IPoolManager poolManager = IPoolManager(poolManagerAddress);
|
||||
tychoRouter = deployRouter();
|
||||
deployDummyContract();
|
||||
vm.stopPrank();
|
||||
|
||||
address[] memory executors = deployExecutors();
|
||||
vm.startPrank(EXECUTOR_SETTER);
|
||||
tychoRouter.setExecutors(executors);
|
||||
vm.stopPrank();
|
||||
|
||||
vm.startPrank(BOB);
|
||||
tokens.push(new MockERC20("Token A", "A"));
|
||||
tokens.push(new MockERC20("Token B", "B"));
|
||||
tokens.push(new MockERC20("Token C", "C"));
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
function deployRouter() public returns (TychoRouterExposed) {
|
||||
tychoRouter = new TychoRouterExposed(PERMIT2_ADDRESS, WETH_ADDR);
|
||||
tychoRouterAddr = address(tychoRouter);
|
||||
tychoRouter.grantRole(keccak256("FUND_RESCUER_ROLE"), FUND_RESCUER);
|
||||
@@ -64,25 +82,36 @@ contract TychoRouterTestSetup is Constants {
|
||||
tychoRouter.grantRole(
|
||||
keccak256("EXECUTOR_SETTER_ROLE"), EXECUTOR_SETTER
|
||||
);
|
||||
deployDummyContract();
|
||||
vm.stopPrank();
|
||||
return tychoRouter;
|
||||
}
|
||||
|
||||
usv2Executor = new UniswapV2Executor(factoryV2);
|
||||
usv3Executor = new UniswapV3Executor(factoryV3);
|
||||
function deployExecutors() public returns (address[] memory) {
|
||||
address factoryV2 = USV2_FACTORY_ETHEREUM;
|
||||
address factoryV3 = USV3_FACTORY_ETHEREUM;
|
||||
address factoryPancakeV3 = PANCAKESWAPV3_DEPLOYER_ETHEREUM;
|
||||
bytes32 initCodeV2 = USV2_POOL_CODE_INIT_HASH;
|
||||
bytes32 initCodeV3 = USV3_POOL_CODE_INIT_HASH;
|
||||
bytes32 initCodePancakeV3 = PANCAKEV3_POOL_CODE_INIT_HASH;
|
||||
address poolManagerAddress = 0x000000000004444c5dc75cB358380D2e3dE08A90;
|
||||
address ekuboCore = 0xe0e0e08A6A4b9Dc7bD67BCB7aadE5cF48157d444;
|
||||
|
||||
IPoolManager poolManager = IPoolManager(poolManagerAddress);
|
||||
usv2Executor = new UniswapV2Executor(factoryV2, initCodeV2);
|
||||
usv3Executor = new UniswapV3Executor(factoryV3, initCodeV3);
|
||||
usv4Executor = new UniswapV4Executor(poolManager);
|
||||
vm.startPrank(EXECUTOR_SETTER);
|
||||
address[] memory executors = new address[](3);
|
||||
pancakev3Executor =
|
||||
new UniswapV3Executor(factoryPancakeV3, initCodePancakeV3);
|
||||
balancerv2Executor = new BalancerV2Executor();
|
||||
ekuboExecutor = new EkuboExecutor(ekuboCore);
|
||||
|
||||
address[] memory executors = new address[](6);
|
||||
executors[0] = address(usv2Executor);
|
||||
executors[1] = address(usv3Executor);
|
||||
executors[2] = address(usv4Executor);
|
||||
tychoRouter.setExecutors(executors);
|
||||
vm.stopPrank();
|
||||
|
||||
vm.startPrank(BOB);
|
||||
tokens.push(new MockERC20("Token A", "A"));
|
||||
tokens.push(new MockERC20("Token B", "B"));
|
||||
tokens.push(new MockERC20("Token C", "C"));
|
||||
vm.stopPrank();
|
||||
executors[2] = address(pancakev3Executor);
|
||||
executors[3] = address(usv4Executor);
|
||||
executors[4] = address(balancerv2Executor);
|
||||
executors[5] = address(ekuboExecutor);
|
||||
return executors;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
158
foundry/test/executors/EkuboExecutor.t.sol
Normal file
158
foundry/test/executors/EkuboExecutor.t.sol
Normal file
@@ -0,0 +1,158 @@
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
import {EkuboExecutor} from "@src/executors/EkuboExecutor.sol";
|
||||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import {Constants} from "../Constants.sol";
|
||||
import {Test, console} from "forge-std/Test.sol";
|
||||
import {NATIVE_TOKEN_ADDRESS} from "@ekubo/math/constants.sol";
|
||||
import {ICore} from "@ekubo/interfaces/ICore.sol";
|
||||
|
||||
contract EkuboExecutorTest is Test, Constants {
|
||||
address constant EXECUTOR_ADDRESS =
|
||||
0xcA4F73Fe97D0B987a0D12B39BBD562c779BAb6f6; // Same address as in swap_encoder.rs tests
|
||||
EkuboExecutor executor;
|
||||
|
||||
IERC20 USDC = IERC20(USDC_ADDR);
|
||||
IERC20 USDT = IERC20(USDT_ADDR);
|
||||
|
||||
address constant CORE_ADDRESS = 0xe0e0e08A6A4b9Dc7bD67BCB7aadE5cF48157d444;
|
||||
|
||||
bytes32 constant ORACLE_CONFIG =
|
||||
0x51d02a5948496a67827242eabc5725531342527c000000000000000000000000;
|
||||
|
||||
function setUp() public {
|
||||
vm.createSelectFork(vm.rpcUrl("mainnet"), 22082754);
|
||||
|
||||
deployCodeTo(
|
||||
"executors/EkuboExecutor.sol",
|
||||
abi.encode(CORE_ADDRESS),
|
||||
EXECUTOR_ADDRESS
|
||||
);
|
||||
executor = EkuboExecutor(payable(EXECUTOR_ADDRESS));
|
||||
}
|
||||
|
||||
function testSingleSwapEth() public {
|
||||
uint256 amountIn = 1 ether;
|
||||
|
||||
deal(address(executor), amountIn);
|
||||
|
||||
uint256 ethBalanceBeforeCore = CORE_ADDRESS.balance;
|
||||
uint256 ethBalanceBeforeExecutor = address(executor).balance;
|
||||
|
||||
uint256 usdcBalanceBeforeCore = USDC.balanceOf(CORE_ADDRESS);
|
||||
uint256 usdcBalanceBeforeExecutor = USDC.balanceOf(address(executor));
|
||||
|
||||
bytes memory data = abi.encodePacked(
|
||||
address(executor), // receiver
|
||||
NATIVE_TOKEN_ADDRESS, // tokenIn
|
||||
USDC_ADDR, // tokenOut
|
||||
ORACLE_CONFIG // poolConfig
|
||||
);
|
||||
|
||||
uint256 gasBefore = gasleft();
|
||||
uint256 amountOut = executor.swap(amountIn, data);
|
||||
console.log(gasBefore - gasleft());
|
||||
|
||||
console.log(amountOut);
|
||||
|
||||
assertEq(CORE_ADDRESS.balance, ethBalanceBeforeCore + amountIn);
|
||||
assertEq(address(executor).balance, ethBalanceBeforeExecutor - amountIn);
|
||||
|
||||
assertEq(
|
||||
USDC.balanceOf(CORE_ADDRESS), usdcBalanceBeforeCore - amountOut
|
||||
);
|
||||
assertEq(
|
||||
USDC.balanceOf(address(executor)),
|
||||
usdcBalanceBeforeExecutor + amountOut
|
||||
);
|
||||
}
|
||||
|
||||
function testSingleSwapERC20() public {
|
||||
uint256 amountIn = 1_000_000_000;
|
||||
|
||||
deal(USDC_ADDR, address(executor), amountIn);
|
||||
|
||||
uint256 usdcBalanceBeforeCore = USDC.balanceOf(CORE_ADDRESS);
|
||||
uint256 usdcBalanceBeforeExecutor = USDC.balanceOf(address(executor));
|
||||
|
||||
uint256 ethBalanceBeforeCore = CORE_ADDRESS.balance;
|
||||
uint256 ethBalanceBeforeExecutor = address(executor).balance;
|
||||
|
||||
bytes memory data = abi.encodePacked(
|
||||
address(executor), // receiver
|
||||
USDC_ADDR, // tokenIn
|
||||
NATIVE_TOKEN_ADDRESS, // tokenOut
|
||||
ORACLE_CONFIG // config
|
||||
);
|
||||
|
||||
uint256 gasBefore = gasleft();
|
||||
uint256 amountOut = executor.swap(amountIn, data);
|
||||
console.log(gasBefore - gasleft());
|
||||
|
||||
console.log(amountOut);
|
||||
|
||||
assertEq(USDC.balanceOf(CORE_ADDRESS), usdcBalanceBeforeCore + amountIn);
|
||||
assertEq(
|
||||
USDC.balanceOf(address(executor)),
|
||||
usdcBalanceBeforeExecutor - amountIn
|
||||
);
|
||||
|
||||
assertEq(CORE_ADDRESS.balance, ethBalanceBeforeCore - amountOut);
|
||||
assertEq(
|
||||
address(executor).balance, ethBalanceBeforeExecutor + amountOut
|
||||
);
|
||||
}
|
||||
|
||||
// Expects input that encodes the same test case as swap_encoder::tests::ekubo::test_encode_swap_multi
|
||||
function multiHopSwap(bytes memory data) internal {
|
||||
uint256 amountIn = 1 ether;
|
||||
|
||||
deal(address(executor), amountIn);
|
||||
|
||||
uint256 ethBalanceBeforeCore = CORE_ADDRESS.balance;
|
||||
uint256 ethBalanceBeforeExecutor = address(executor).balance;
|
||||
|
||||
uint256 usdtBalanceBeforeCore = USDT.balanceOf(CORE_ADDRESS);
|
||||
uint256 usdtBalanceBeforeExecutor = USDT.balanceOf(address(executor));
|
||||
|
||||
uint256 gasBefore = gasleft();
|
||||
uint256 amountOut = executor.swap(amountIn, data);
|
||||
console.log(gasBefore - gasleft());
|
||||
|
||||
console.log(amountOut);
|
||||
|
||||
assertEq(CORE_ADDRESS.balance, ethBalanceBeforeCore + amountIn);
|
||||
assertEq(address(executor).balance, ethBalanceBeforeExecutor - amountIn);
|
||||
|
||||
assertEq(
|
||||
USDT.balanceOf(CORE_ADDRESS), usdtBalanceBeforeCore - amountOut
|
||||
);
|
||||
assertEq(
|
||||
USDT.balanceOf(address(executor)),
|
||||
usdtBalanceBeforeExecutor + amountOut
|
||||
);
|
||||
}
|
||||
|
||||
// Same test case as in swap_encoder::tests::ekubo::test_encode_swap_multi
|
||||
function testMultiHopSwap() public {
|
||||
bytes memory data = abi.encodePacked(
|
||||
address(executor), // receiver
|
||||
NATIVE_TOKEN_ADDRESS, // tokenIn
|
||||
USDC_ADDR, // tokenOut of 1st swap
|
||||
ORACLE_CONFIG, // config of 1st swap
|
||||
USDT_ADDR, // tokenOut of 2nd swap
|
||||
bytes32(
|
||||
0x00000000000000000000000000000000000000000001a36e2eb1c43200000032
|
||||
) // config of 2nd swap (0.0025% fee & 0.005% base pool)
|
||||
);
|
||||
multiHopSwap(data);
|
||||
}
|
||||
|
||||
// Data is generated by test case in swap_encoder::tests::ekubo::test_encode_swap_multi
|
||||
function testMultiHopSwapIntegration() public {
|
||||
multiHopSwap(
|
||||
hex"ca4f73fe97d0b987a0d12b39bbd562c779bab6f60000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4851d02a5948496a67827242eabc5725531342527c000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000001a36e2eb1c43200000032"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,9 @@ import {Test} from "../../lib/forge-std/src/Test.sol";
|
||||
import {Constants} from "../Constants.sol";
|
||||
|
||||
contract UniswapV2ExecutorExposed is UniswapV2Executor {
|
||||
constructor(address _factory) UniswapV2Executor(_factory) {}
|
||||
constructor(address _factory, bytes32 _initCode)
|
||||
UniswapV2Executor(_factory, _initCode)
|
||||
{}
|
||||
|
||||
function decodeParams(bytes calldata data)
|
||||
external
|
||||
@@ -48,13 +50,23 @@ contract UniswapV2ExecutorTest is Test, Constants {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
UniswapV2ExecutorExposed uniswapV2Exposed;
|
||||
UniswapV2ExecutorExposed sushiswapV2Exposed;
|
||||
UniswapV2ExecutorExposed pancakeswapV2Exposed;
|
||||
IERC20 WETH = IERC20(WETH_ADDR);
|
||||
IERC20 DAI = IERC20(DAI_ADDR);
|
||||
|
||||
function setUp() public {
|
||||
uint256 forkBlock = 17323404;
|
||||
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
||||
uniswapV2Exposed = new UniswapV2ExecutorExposed(USV2_FACTORY_ETHEREUM);
|
||||
uniswapV2Exposed = new UniswapV2ExecutorExposed(
|
||||
USV2_FACTORY_ETHEREUM, USV2_POOL_CODE_INIT_HASH
|
||||
);
|
||||
sushiswapV2Exposed = new UniswapV2ExecutorExposed(
|
||||
SUSHISWAPV2_FACTORY_ETHEREUM, SUSHIV2_POOL_CODE_INIT_HASH
|
||||
);
|
||||
pancakeswapV2Exposed = new UniswapV2ExecutorExposed(
|
||||
PANCAKESWAPV2_FACTORY_ETHEREUM, PANCAKEV2_POOL_CODE_INIT_HASH
|
||||
);
|
||||
}
|
||||
|
||||
function testDecodeParams() public view {
|
||||
@@ -82,6 +94,14 @@ contract UniswapV2ExecutorTest is Test, Constants {
|
||||
uniswapV2Exposed.verifyPairAddress(WETH_DAI_POOL);
|
||||
}
|
||||
|
||||
function testVerifyPairAddressSushi() public view {
|
||||
sushiswapV2Exposed.verifyPairAddress(SUSHISWAP_WBTC_WETH_POOL);
|
||||
}
|
||||
|
||||
function testVerifyPairAddressPancake() public view {
|
||||
pancakeswapV2Exposed.verifyPairAddress(PANCAKESWAP_WBTC_WETH_POOL);
|
||||
}
|
||||
|
||||
function testInvalidTarget() public {
|
||||
address fakePool = address(new FakeUniswapV2Pool(WETH_ADDR, DAI_ADDR));
|
||||
vm.expectRevert(UniswapV2Executor__InvalidTarget.selector);
|
||||
|
||||
@@ -6,7 +6,9 @@ import {Test} from "../../lib/forge-std/src/Test.sol";
|
||||
import {Constants} from "../Constants.sol";
|
||||
|
||||
contract UniswapV3ExecutorExposed is UniswapV3Executor {
|
||||
constructor(address _factory) UniswapV3Executor(_factory) {}
|
||||
constructor(address _factory, bytes32 _initCode)
|
||||
UniswapV3Executor(_factory, _initCode)
|
||||
{}
|
||||
|
||||
function decodeData(bytes calldata data)
|
||||
external
|
||||
@@ -37,6 +39,7 @@ contract UniswapV3ExecutorTest is Test, Constants {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
UniswapV3ExecutorExposed uniswapV3Exposed;
|
||||
UniswapV3ExecutorExposed pancakeV3Exposed;
|
||||
IERC20 WETH = IERC20(WETH_ADDR);
|
||||
IERC20 DAI = IERC20(DAI_ADDR);
|
||||
|
||||
@@ -44,7 +47,12 @@ contract UniswapV3ExecutorTest is Test, Constants {
|
||||
uint256 forkBlock = 17323404;
|
||||
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
||||
|
||||
uniswapV3Exposed = new UniswapV3ExecutorExposed(USV3_FACTORY_ETHEREUM);
|
||||
uniswapV3Exposed = new UniswapV3ExecutorExposed(
|
||||
USV3_FACTORY_ETHEREUM, USV3_POOL_CODE_INIT_HASH
|
||||
);
|
||||
pancakeV3Exposed = new UniswapV3ExecutorExposed(
|
||||
PANCAKESWAPV3_DEPLOYER_ETHEREUM, PANCAKEV3_POOL_CODE_INIT_HASH
|
||||
);
|
||||
}
|
||||
|
||||
function testDecodeParams() public view {
|
||||
@@ -84,6 +92,12 @@ contract UniswapV3ExecutorTest is Test, Constants {
|
||||
);
|
||||
}
|
||||
|
||||
function testVerifyPairAddressPancake() public view {
|
||||
pancakeV3Exposed.verifyPairAddress(
|
||||
WETH_ADDR, USDT_ADDR, 500, PANCAKESWAPV3_WETH_USDT_POOL
|
||||
);
|
||||
}
|
||||
|
||||
function testUSV3Callback() public {
|
||||
uint24 poolFee = 3000;
|
||||
uint256 amountOwed = 1000000000000000000;
|
||||
|
||||
Reference in New Issue
Block a user