The Aggregation Router is a swap router that can perform trades across multiple DeFi protocols in a single transaction.
It can be aggregated with:
- Uniswap V3 pools
- Uniswap V2 pools
- Balancer V2 pools
- Curve pools
- DODO V2 pools
- DODO V1 pools
- FraxSwap V2 pools
- PancakeSwap V3 pools
- PancakeSwap V2 pools
- SushiSwap V3 pools
- SushiSwap V2 pools
- Aave V2 & V3 aTokens
- Compound cTokens
- Frax: frxETH & sfrxETH
- Lido: stETH & wstETH
The AggregationRouter contract was inspired by Synthetix: CoreRouter. The constant address variables in the AggregationRouter were obtained via CREATE3. These address variables must be updated before the deployment.
aggregate: Performs swaps, delegating all encoded calls to the adapters mapped by function selectors.
function aggregate(
bytes[] calldata calls,
address recipient,
Currency currencyIn,
Currency currencyOut,
uint256 amountIn,
uint256 amountOutMin
) external payable returns (uint256 amountOut);bytes call:
┌─────────────────┬──────────────┐
│ bytes4 selector │ bytes32 path │
└─────────────────┴──────────────┘
Path is the encoding of 5 parameters and optional flags (up to 8).
| Name | Type | Description |
|---|---|---|
pool |
address |
The address of the pool |
i |
uint8 |
The index of currency to be swapped from |
j |
uint8 |
The index of currency to be swapped for |
wrapIn |
uint8 |
The wrapping command for currency to be swapped from |
wrapOut |
uint8 |
The wrapping command of currency to be swapped for |
| Value | Description |
|---|---|
0 |
No action |
1 |
Wrap native currency |
2 |
Unwrap wrapped native currency |
query: Returns the encoded path and expected amount of currency-out to be received in exchange of currency-in.
function query(
Currency currencyIn,
Currency currencyOut,
uint256 amountIn
) external view returns (bytes32 path, uint256 amountOut);quote: Returns expected amount of currency-out to be received in exchange of currency-in.
function quote(bytes32 path, uint256 amountIn) external view returns (uint256 amountOut);uniswapV3Swap: Performs a swap on Uniswap V3 pool. Wraps and unwraps both currency in and out if requested.
function uniswapV3Swap(bytes32 path) external payable returns (uint256);uniswapV3SwapCallback: A callback executed by Uniswap V3 pool after executing a swap.
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data) external;uniswapV2Swap: Performs a swap on Uniswap V2 pool. Wraps and unwraps both currency in and out if requested.
function uniswapV2Swap(bytes32 path) external payable returns (uint256);balancerV2Swap: Performs a swap on Balancer V2 pool. Wraps and unwraps both currency in and out if requested.
function balancerV2Swap(bytes32 path) external payable returns (uint256);curveSwap: Performs a swap on Curve pool. Wraps and unwraps both currency in and out if requested.
function curveSwap(bytes32 path) external payable returns (uint256);dodoV2Swap: Performs a swap on DODO V2 pool. Wraps and unwraps both currency in and out if requested.
function dodoV2Swap(bytes32 path) external payable returns (uint256);dodoV1Swap: Performs a swap on DODO V1 pool. Wraps and unwraps both currency in and out if requested.
function dodoV1Swap(bytes32 path) external payable returns (uint256);fraxV2Swap: Performs a swap on FraxSwap V2 pool. Wraps and unwraps both currency in and out if requested.
function fraxV2Swap(bytes32 path) external payable returns (uint256);pancakeV3Swap: Performs a swap on PancakeSwap V3 pool. Wraps and unwraps both currency in and out if requested.
function pancakeV3Swap(bytes32 path) external payable returns (uint256);pancakeV3SwapCallback: A callback executed by PancakeSwap V3 pool after executing a swap.
function pancakeV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data) external;pancakeV2Swap: Performs a swap on PancakeSwap V2 pool. Wraps and unwraps both currency in and out if requested.
function pancakeV2Swap(bytes32 path) external payable returns (uint256);sushiV3Swap: Performs a swap on SushiSwap V3 pool. Wraps and unwraps both currency in and out if requested.
function sushiV3Swap(bytes32 path) external payable returns (uint256);uniswapV3SwapCallback: A callback executed by SushiSwap V3 pool after executing a swap.
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data) external;sushiV2Swap: Performs a swap on SushiSwap V2 pool. Wraps and unwraps both currency in and out if requested.
function sushiV2Swap(bytes32 path) external payable returns (uint256);query: Returns the encoded path and expected amount of currency-out to be received in exchange of currency-in.
function query(
Currency wrapped,
Currency underlying,
uint256 amountIn,
bool direction // true for wrapping and false for unwrapping
) external view returns (bytes32 path, uint256 amountOut);quote: Returns expected amount of currency-out to be received in exchange of currency-in.
function quote(bytes32 path, uint256 amountIn) external view returns (uint256 amountOut);wrapAToken: Supplies the underlying currency into Aave and receives overlying aToken in return. Wraps ETH beforehand if requested.
function wrapAToken(bytes32 path) external payable returns (uint256);unwrapAToken: Withdraws the underlying currency by redeeming aToken. Unwraps WETH afterward if requested.
function unwrapAToken(bytes32 path) external payable returns (uint256);wrapCToken: Supplies the underlying currency into Compound and receives overlying cToken in return. Unwraps WETH beforehand if requested.
function wrapCToken(bytes32 path) external payable returns (uint256);unwrapCToken: Withdraws the underlying currency by redeeming the cToken. Wraps ETH afterward if requested.
function unwrapCToken(bytes32 path) external payable returns (uint256);wrapFRXETH: Stakes ETH and receives frxETH in return. Unwraps WETH beforehand if requested.
function wrapFRXETH(bytes32 path) external payable returns (uint256);wrapSFRXETH: Wraps frxETH and receives sfrxETH in return. Stakes ETH beforehand if requested.
function wrapSFRXETH(bytes32 path) external payable returns (uint256);unwrapSFRXETH: Unwraps sfrxETH and receives frxETH in return.
function unwrapSFRXETH(bytes32 path) external payable returns (uint256);wrapSTETH: Stakes ETH and receives stETH in return. Unwraps WETH beforehand if requested.
function wrapSTETH(bytes32 path) external payable returns (uint256);wrapWSTETH: Wraps stETH and receives wstETH in return. Stakes ETH beforehand if requested.
function wrapWSTETH(bytes32 path) external payable returns (uint256);unwrapWSTETH: Unwraps wstETH and receives stETH in return.
function unwrapWSTETH(bytes32 path) external payable returns (uint256);Create .env file with the following content:
INFURA_API_KEY=YOUR_INFURA_API_KEY
RPC_ETHEREUM="https://mainnet.infura.io/v3/${INFURA_API_KEY}"
ETHERSCAN_API_KEY=YOUR_ETHERSCAN_API_KEY
ETHERSCAN_URL="https://api.etherscan.io/api"
$ forge build$ forge test