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
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:
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:
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).
State Mutability: payable
profitReceiver
profitReceiver
Returns the address that receives profit from positive slippage and fees.
Signature:
function profitReceiver() external view returns (address)
Returns:
address
The address of the profit receiver.
State Mutability: view
singleHopFee
singleHopFee
Returns the fee charged for single-hop swaps (routes with only one adapter).
Signature:
function singleHopFee() external view returns (uint128)
Returns:
uint128
The single hop fee as a percentage (scaled by PERCENTAGE_BASE
).
State Mutability: view
multiHopFee
multiHopFee
Returns the fee charged for multi-hop swaps (routes with more than one adapter).
Signature:
function multiHopFee() external view returns (uint128)
Returns:
uint128
The multi hop fee as a percentage (scaled by PERCENTAGE_BASE
).
State Mutability: view
activeAdapters
activeAdapters
Returns whether a specific adapter is active and can be used for swaps.
Signature:
function activeAdapters(address adapter) external view returns (bool)
Parameters:
adapter
address
The address of the adapter to check.
Returns:
bool
True if the adapter is active, false otherwise.
State Mutability: view
freeOfChargeFeeAdapters
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)
Parameters:
adapter
address
The address of the adapter to check.
Returns:
bool
True if the adapter is free of charge for single-hop swaps, false otherwise.
State Mutability: view
Note: Free of charge adapters do not charge any fees for swaps if routes contain only them (routes.length == 1
).
PERCENTAGE_BASE
PERCENTAGE_BASE
Returns the percentage base used for fee calculations.
Signature:
function PERCENTAGE_BASE() external view returns (uint256)
Returns:
uint256
The percentage base constant (1e18), representing 100% in fee calculations.
State Mutability: view
Events
SwapExecuted
SwapExecuted
Emitted when a swap is successfully executed.
Signature:
event SwapExecuted(address tokenFrom, uint256 tokenFromAmount, address tokenTo, uint256 tokenToAmount)
Parameters:
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
SingleHopFeeUpdated
Emitted when the single hop fee is updated.
Signature:
event SingleHopFeeUpdated(uint256 singleHopFee)
Parameters:
singleHopFee
uint256
The new single hop fee.
MultiHopFeeUpdated
MultiHopFeeUpdated
Emitted when the multi hop fee is updated.
Signature:
event MultiHopFeeUpdated(uint256 multiHopFee)
Parameters:
multiHopFee
uint256
The new multi hop fee.
FreeOfChargeFeeAdapterUpdated
FreeOfChargeFeeAdapterUpdated
Emitted when an adapter's free of charge status is updated.
Signature:
event FreeOfChargeFeeAdapterUpdated(address indexed adapter, bool isFreeOfCharge)
Parameters:
adapter
address
The address of the adapter.
isFreeOfCharge
bool
True if the adapter is free of charge.
Errors
SingleHopFeeTooHigh
SingleHopFeeTooHigh
Thrown when attempting to set a single hop fee greater than 100% (PERCENTAGE_BASE
).
Signature:
error SingleHopFeeTooHigh()
SingleHopFeeAlreadySet
SingleHopFeeAlreadySet
Thrown when attempting to set the single hop fee to its current value.
Signature:
error SingleHopFeeAlreadySet()
MultiHopFeeTooHigh
MultiHopFeeTooHigh
Thrown when attempting to set a multi hop fee greater than 100% (PERCENTAGE_BASE
).
Signature:
error MultiHopFeeTooHigh()
MultiHopFeeAlreadySet
MultiHopFeeAlreadySet
Thrown when attempting to set the multi hop fee to its current value.
Signature:
error MultiHopFeeAlreadySet()
ProfitReceiverAddressZero
ProfitReceiverAddressZero
Thrown when attempting to set the profit receiver address to the zero address.
Signature:
error ProfitReceiverAddressZero()
RoutesLengthZero
RoutesLengthZero
Thrown when the routes array is empty during a swap operation.
Signature:
error RoutesLengthZero()
TokenFromCannotBeEqualToTokenTo
TokenFromCannotBeEqualToTokenTo
Thrown when a route specifies the same token for both tokenFrom
and tokenTo
.
Signature:
error TokenFromCannotBeEqualToTokenTo()
TokenFromAddressMismatch
TokenFromAddressMismatch
Thrown when the first route's tokenFrom address doesn't match the swap's tokenFromAddress parameter.
Signature:
error TokenFromAddressMismatch()
TokenToAddressMismatch
TokenToAddressMismatch
Thrown when the last route's tokenTo
address doesn't match the swap's tokenToAddress
parameter.
Signature:
error TokenToAddressMismatch()
TokenFromAmountZero
TokenFromAmountZero
Thrown when the tokenFromAmount is zero.
Signature:
error TokenFromAmountZero()
TokenToAmountLessThanMinAmount
TokenToAmountLessThanMinAmount
Thrown when the expected tokenToAmount is less than the minimum amount (tokenToMinAmount
).
Signature:
error TokenToAmountLessThanMinAmount()
TokenFromAmountExceedsBalance
TokenFromAmountExceedsBalance
Thrown when the user's token balance is insufficient for the swap amount.
Signature:
error TokenFromAmountExceedsBalance()
ReceivedAmountLessThanMinAmount
ReceivedAmountLessThanMinAmount
Thrown when the actual received amount is less than the minimum acceptable amount.
Signature:
error ReceivedAmountLessThanMinAmount()
AdapterNotActive
AdapterNotActive
Thrown when attempting to use an inactive adapter in a swap route.
Signature:
error AdapterNotActive(address adapter)
Parameters:
adapter
address
The address of the inactive adapter.
NativeTokenAmountMismatch
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:
expected
uint256
The expected native token amount.
received
uint256
The actual native token amount.
UnexpectedNativeTokenSent
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