Skip to content

A swap router that can perform trades across multiple DeFi protocols

Notifications You must be signed in to change notification settings

fomoweth/aggregation-router

Repository files navigation

Aggregation Router

The Aggregation Router is a swap router that can perform trades across multiple DeFi protocols in a single transaction.

It can be aggregated with:

Contract Overview

AggregationRouter

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);

Encoding calls

bytes call:
┌─────────────────┬──────────────┐
│ bytes4 selector │ bytes32 path │
└─────────────────┴──────────────┘

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

Wrapping Command

Value Description
0 No action
1 Wrap native currency
2 Unwrap wrapped native currency

Adapters

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);

UniswapV3Adapter

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;

UniswapV2Adapter

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);

BalancerV2Adapter

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);

CurveAdapter

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);

DoDoV2Adapter

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);

DoDoV1Adapter

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);

FraxSwapV2Adapter

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);

PancakeV3Adapter

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;

PancakeV2Adapter

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);

SushiV3Adapter

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;

SushiV2Adapter

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);

Wrappers

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);

ATokenWrapper

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);

CTokenWrapper

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);

FRXETHWrapper

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);

STETHWrapper

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);

Usage

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"

Build

$ forge build

Test

$ forge test

About

A swap router that can perform trades across multiple DeFi protocols

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published