EVM Router Swap Functions
EVM Router is the advanced router contract for executing token swaps across multiple protocols on EVM-compatible chains. It supports native token handling, multi-hop swaps, integrator fee/surplus sharing, and permit-based approvals.Overview
EVM Router provides three main swap functions:swap- Standard swap function for normal token exchangesswapIntegrator- Swap with integrator fee or surplus sharingswapWithPermit- Swap with ERC-20 permit signature (gasless approval)
- Multi-hop swaps across different protocols
- Native token handling (MONAD ↔ WMONAD conversion)
- Rate-based amount distribution for splitting input across multiple swaps
- Surplus handling to cap output at expected amount
- Minimum received protection to ensure slippage tolerance
Contract ABI
Router Contract ABI: The complete Application Binary Interface (ABI) for EVM Router is available on GitHub:🔗 View Router ABI on GitHubThe ABI includes all function signatures, event definitions, and error types needed for integration.
Function: swap
Performs a standard token swap using the provided route and swap parameters.
Signature
Parameters
route (RouteParam)
The route parameters defining the overall swap path.
swap_parameters (SwapParams[])
Array of swap parameters for each hop in the multi-hop swap.
Returns
uint256: Amount of output tokens received by the destination address
Behavior
-
Validation: Checks that
token_in != token_out,amount_in > 0,min_received > 0, andswap_parameters.length > 0 -
Token Transfer:
- For
tokenToTokenandtokenToEth: Transfersamount_infrommsg.senderto contract - For
ethToToken: Expects ETH sent with transaction (msg.value)
- For
-
Multi-Hop Execution:
- For first swap: Uses
route.amount_inmultiplied by first swap’srate - For subsequent swaps: Uses previous swap’s output multiplied by current swap’s
rate - Handles native token conversion (MONAD ↔ WMONAD) if protocol supports it
- For first swap: Uses
-
Surplus Handling:
- If actual output >
route.amount_out, caps output atroute.amount_out - User receives capped amount, surplus stays in contract
- If actual output >
-
Slippage Protection:
- Verifies
actual_output >= route.min_received - Reverts with
MinReceivedAmountNotReachedif check fails
- Verifies
-
Token Transfer: Sends output tokens to
destination(ormsg.senderifdestination == address(0))
Example
See EVM Router Events for complete event documentation.
Errors
Function: swapIntegrator
Performs a swap with integrator fee or surplus sharing. Integrators can monetize by taking a percentage fee from output or sharing in surplus profits.
Signature
Parameters
route (RouteParam)
Same as swap function. See swap route parameters.
swap_parameters (SwapParams[])
Same as swap function. See swap parameters.
integrator_data (bytes)
Encoded IntegratorParams struct:
Important: You cannot use both
fee_percentage and surplus_percentage simultaneously. Choose one monetization model.
Returns
uint256: Amount of output tokens received by the user (after integrator fee/surplus)
Behavior
Fee-Based Model (fee_percentage > 0)
- Executes swaps normally
- Calculates total amount received
- Calculates integrator fee:
fee_amount = (total_received * fee_percentage) / 10000 - User receives:
total_received - fee_amount - Integrator receives:
fee_amount
- Total received: 1000 tokens
- Fee percentage: 100 bips (1%)
- Integrator fee: 10 tokens
- User receives: 990 tokens
Surplus-Based Model (surplus_percentage > 0)
- Executes swaps normally
- Calculates total amount received
- Calculates surplus:
surplus = max(0, total_received - route.amount_out) - If surplus > 0:
- Integrator share:
(surplus * surplus_percentage) / 10000 - User receives:
route.amount_out(capped at expected) - Integrator receives: integrator share of surplus
- Integrator share:
- If surplus == 0:
- User receives:
total_received - Integrator receives: 0
- User receives:
- Expected output: 1000 tokens
- Actual received: 1050 tokens
- Surplus: 50 tokens
- Surplus percentage: 2000 bips (20%)
- Integrator share: 10 tokens
- User receives: 1000 tokens (capped)
- Integrator receives: 10 tokens
min_received Calculation
Formula:Example
See EVM Router Events for complete event documentation, including
IntegratorFeeDistribution event details.Errors
All errors fromswap function, plus:
Function: swapWithPermit
Performs a swap using ERC-20 permit signature for gasless token approval. This allows users to approve tokens without a separate transaction.
Signature
Parameters
route (RouteParam)
Same as swap function. See swap route parameters.
Note: route.token_in must be an ERC-20 token that supports permit (ERC-2612).
swap_parameters (SwapParams[])
Same as swap function. See swap parameters.
deadline (uint256)
Unix timestamp after which the permit signature expires. Must be >= block.timestamp.
v, r, s (uint8, bytes32, bytes32)
ECDSA signature components for the permit. Generated by signing:
Returns
uint256: Amount of output tokens received by the destination address
Behavior
- Permit Validation: Checks
deadline >= block.timestamp - Permit Execution: Calls
IERC20Permit(route.token_in).permit(...)with signature - Swap Execution: Executes swap using same logic as
swapfunction
Example
Errors
All errors fromswap function, plus:
Advanced Features
Native Token Support
EVM Router supports native token swaps through automatic wrapped token conversion. How it works:- Protocols can support native token pairs (e.g., ETH/TOKEN) or wrapped pairs (e.g., WETH/TOKEN)
- EVM Router automatically converts native ↔ wrapped tokens when needed
- Conversion is controlled by
shouldConvertInputflag inextra_data
-
MONAD → USDC (using WMONAD/USDC pool):
- User sends MONAD
- Router wraps to WMONAD
- Swaps WMONAD → USDC
-
WMONAD → USDC (using MONAD/USDC pool):
- User sends WMONAD
- Router unwraps to MONAD
- Swaps MONAD → USDC
Rate-Based Amount Distribution
Each swap inswap_parameters has a rate field that determines what percentage of input to use.
Rate Calculation:
- First swap:
rate = 1000000(100%) → Uses all input - Second swap:
rate = 500000(50%) → Uses 50% of first swap’s output - Third swap:
rate = 1000000(100%) → Uses all of second swap’s output
- Splitting input across multiple pools for better execution
- Partial swaps with different protocols
- Complex routing strategies
Surplus Handling
EVM Router caps output atroute.amount_out to prevent users from receiving unexpected excess.
Surplus Calculation:
- Protects against unexpected price improvements
- Ensures predictable output amounts
- Enables integrator surplus sharing
Common Patterns
Simple Token-to-Token Swap
Multi-Hop Swap
Integrator Fee Swap
Error Reference
Contract Address
EVM Router is deployed on multiple EVM-compatible networks. Check Contract Addresses for the latest deployment addresses. Contract ABI: View on GitHubRelated Documentation
- EVM Router Events - Complete event reference and monitoring guide
- Integration Guide - Best practices for integrating with Fibrous API
- V2 Migration Guide - Migrating from V1 to V2 API
- Error Codes - Complete error reference