Aggregator Smart Contract Interface

The Aggregator contract provides a swap function which allows users to perform token swaps across multiple routes and adapters.

Functions

swap

Performs a token swap from one token to another, potentially using multiple routes and adapters.

Signature

function swap(
    address tokenFromAddress,
    uint256 tokenFromAmount,
    address tokenToAddress,
    uint256 tokenToAmount,
    uint256 tokenToMinAmount,
    IAggregator.Route[] routes
) external payable;

Parameters

Name
Type
Description

tokenFromAddress

address

The address of the token to swap from.

tokenFromAmount

uint256

The amount of the input token to swap.

tokenToAddress

address

The address of the token to receive.

tokenToAmount

uint256

The expected amount of the output token.

tokenToMinAmount

uint256

The minimum amount of the output token after slippage.

routes

IAggregator.Route[]

An array of routes describing the swap path and adapters to use.

Route Structure

Each Route in the routes array is a struct with the following fields:

Name
Type
Description

tokenFrom

address

The address of the token to swap from in this route.

tokenTo

address

The address of the token to swap to in this route.

adapter

address

The address of the adapter contract to use for this route.

additionalParams

bytes

Additional parameters for the adapter (if needed).

profitReceiver

Returns the address that receives profit from positive slippage and fees.

Signature

function profitReceiver() external view returns (address receiver);

Returns

Name
Type
Description

receiver

address

The address of the profit receiver.

singleHopFee

Returns the fee charged for single-hop swaps (routes with only one adapter).

Signature

function singleHopFee() external view returns (uint128 fee);

Returns

Name
Type
Description

fee

uint128

The single hop fee as a percentage (scaled by PERCENTAGE_BASE).

multiHopFee

Returns the fee charged for multi-hop swaps (routes with more than one adapter).

Signature

function multiHopFee() external view returns (uint128 fee);

Returns

Name
Type
Description

fee

uint128

The multi hop fee as a percentage (scaled by PERCENTAGE_BASE).

activeAdapters

Returns whether a specific adapter is active and can be used for swaps.

Signature

function activeAdapters(address adapter) external view returns (bool isActive);

Parameters

Name
Type
Description

adapter

address

The address of the adapter to check.

Returns

Name
Type
Description

isActive

bool

True if the adapter is active, false otherwise.

freeOfChargeFeeAdapters

Returns whether a specific adapter is designated as free of charge for single-hop swaps.

Signature

function freeOfChargeFeeAdapters(address adapter) external view returns (bool isFreeOfCharge);

Parameters

Name
Type
Description

adapter

address

The address of the adapter to check.

Returns

Name
Type
Description

isFreeOfCharge

bool

True if the adapter is free of charge for single-hop swaps, false otherwise.

Note

Free of charge adapters do not charge any fees for swaps if routes contain only them (routes.length == 1).

PERCENTAGE_BASE

Returns the percentage base used for fee calculations.

Signature

function PERCENTAGE_BASE() external view returns (uint256 percentageBase);

Returns

Name
Type
Description

percentageBase

uint256

The percentage base constant (1e18), representing 100% in fee calculations.

Events

SwapExecuted

Emitted when a swap is successfully executed.

Signature

event SwapExecuted(address tokenFrom, uint256 tokenFromAmount, address tokenTo, uint256 tokenToAmount);

Parameters

Name
Type
Description

tokenFrom

address

The address of the token swapped from.

tokenFromAmount

uint256

The amount of tokenFrom swapped.

tokenTo

address

The address of the token swapped to.

tokenToAmount

uint256

The amount of tokenTo received.

SingleHopFeeUpdated

Emitted when the single hop fee is updated.

Signature

event SingleHopFeeUpdated(uint256 singleHopFee);

Parameters

Name
Type
Description

singleHopFee

uint256

The new single hop fee.

MultiHopFeeUpdated

Emitted when the multi-hop fee is updated.

Signature

event MultiHopFeeUpdated(uint256 multiHopFee);

Parameters

Name
Type
Description

multiHopFee

uint256

The new multi hop fee.

FreeOfChargeFeeAdapterUpdated

Emitted when an adapter's free-of-charge status is updated.

Signature

event FreeOfChargeFeeAdapterUpdated(address indexed adapter, bool isFreeOfCharge);

Parameters

Name
Type
Description

adapter

address

The address of the adapter.

isFreeOfCharge

bool

True if the adapter is free of charge.

Errors

SingleHopFeeTooHigh

Thrown when attempting to set a single hop fee greater than 100% (PERCENTAGE_BASE).

Signature

error SingleHopFeeTooHigh();

SingleHopFeeAlreadySet

Thrown when attempting to set the single hop fee to its current value.

Signature

error SingleHopFeeAlreadySet();

MultiHopFeeTooHigh

Thrown when attempting to set a multi-hop fee greater than 100% (PERCENTAGE_BASE).

Signature

error MultiHopFeeTooHigh();

MultiHopFeeAlreadySet

Thrown when attempting to set the multi-hop fee to its current value.

Signature

error MultiHopFeeAlreadySet();

ProfitReceiverAddressZero

Thrown when attempting to set the profit receiver address to the zero address.

Signature

error ProfitReceiverAddressZero();

RoutesLengthZero

Thrown when the routes array is empty during a swap operation.

Signature

error RoutesLengthZero();

TokenFromCannotBeEqualToTokenTo

Thrown when a route specifies the same token for both tokenFrom and tokenTo.

Signature

error TokenFromCannotBeEqualToTokenTo();

TokenFromAddressMismatch

Thrown when the first route's tokenFrom address doesn't match the swap's tokenFromAddress parameter.

Signature

error TokenFromAddressMismatch();

TokenToAddressMismatch

Thrown when the last route's tokenTo address doesn't match the swap's tokenToAddress parameter.

Signature

error TokenToAddressMismatch();

TokenFromAmountZero

Thrown when the tokenFromAmount is zero.

Signature

error TokenFromAmountZero();

TokenToAmountLessThanMinAmount

Thrown when the expected tokenToAmount is less than the minimum amount (tokenToMinAmount).

Signature

error TokenToAmountLessThanMinAmount();

TokenFromAmountExceedsBalance

Thrown when the user's token balance is insufficient for the swap amount.

Signature

error TokenFromAmountExceedsBalance();

ReceivedAmountLessThanMinAmount

Thrown when the actual received amount is less than the minimum acceptable amount.

Signature

error ReceivedAmountLessThanMinAmount();

AdapterNotActive

Thrown when attempting to use an inactive adapter in a swap route.

Signature

error AdapterNotActive(address adapter);

Parameters

Name
Type
Description

adapter

address

The address of the inactive adapter.

NativeTokenAmountMismatch

Thrown when the native token amount sent doesn't match the expected amount for native token swaps.

Signature

error NativeTokenAmountMismatch(uint256 expected, uint256 received);

Parameters

Name
Type
Description

expected

uint256

The expected native token amount.

received

uint256

The actual native token amount.

UnexpectedNativeTokenSent

Thrown when native tokens are sent for non-native token swaps.

Signature

error UnexpectedNativeTokenSent();

Example Usage

Below is an example of swapping 1 WMON for USDC on Monad Testnet.

// Example call to swap 1 WMON for USDC on Monad Testnet
aggregator.swap(
    0x760AfE86e5de5fa0Ee542fc7B7B713e1c5425701, // tokenFromAddress (WMON)
    1000000000000000000,                        // tokenFromAmount (1 WMON, 18 decimals)
    0xf817257fed379853cDe0fa4F97AB987181B1E5Ea, // tokenToAddress (USDC)
    1617900,                                    // tokenToAmount (expected USDC amount out)
    1600000,                                    // tokenToMinAmount (min USDC amount out after slippage)
    [
        {
            tokenFrom: 0x760AfE86e5de5fa0Ee542fc7B7B713e1c5425701, // WMON
            tokenTo: 0xf817257fed379853cDe0fa4F97AB987181B1E5Ea,   // USDC
            adapter: 0xe5a44bc972134f6baa8168adce623f7a33bac4ab,   // Example adapter address
            additionalParams: "0x"                                 // Adapter-specific params (empty in this example)
        }
    ]
);

Last updated