CrossChainAggregator

The CrossChainAggregator contract is the core component of IU2U's DEX aggregation system, enabling optimal token swaps across 37+ protocols and seamless cross-chain operations.

Contract Interface

interface ICrossChainAggregator {
    // Quote functions
    function getOptimalQuote(
        address tokenIn,
        address tokenOut,
        uint256 amountIn,
        uint256[] memory routerTypes
    ) external view returns (uint256 bestAmount, uint256 bestRouter);
    
    function getAllQuotes(
        address tokenIn,
        address tokenOut,
        uint256 amountIn
    ) external view returns (QuoteResult[] memory);
    
    // Swap execution
    function executeSwap(
        address tokenIn,
        address tokenOut,
        uint256 amountIn,
        uint256 minAmountOut,
        uint256 routerType,
        bytes calldata swapData
    ) external payable returns (uint256 amountOut);
    
    // Cross-chain operations
    function crossChainSwap(
        SwapData calldata swapData,
        string calldata destinationChain,
        address destinationToken,
        bytes calldata destinationSwapData,
        address destinationRouter,
        uint256 amountOutMin
    ) external;
    
    // Batch operations
    function multiSwap(
        SwapParams[] memory swaps
    ) external payable returns (uint256[] memory amountsOut);
}

Data Structures

SwapData

SwapParams

QuoteResult

CrossChainSwapRequest

Core Functions

getOptimalQuote

Get the best quote from specified DEX protocols.

Parameters:

  • tokenIn: Input token contract address

  • tokenOut: Output token contract address

  • amountIn: Amount of input tokens to swap

  • routerTypes: Array of router type IDs to check (0-36)

Returns:

  • bestAmount: Maximum output tokens from best route

  • bestRouter: Router type ID of the best quote

Usage Example:

getAllQuotes

Get quotes from all 37 supported DEX protocols.

Parameters:

  • tokenIn: Input token contract address

  • tokenOut: Output token contract address

  • amountIn: Amount of input tokens to swap

Returns:

  • Array of QuoteResult structs with quotes from each protocol

Usage Example:

executeSwap

Execute a token swap through the specified DEX protocol.

Parameters:

  • tokenIn: Input token address (use address(0) for ETH)

  • tokenOut: Output token address

  • amountIn: Exact amount of input tokens

  • minAmountOut: Minimum acceptable output (slippage protection)

  • routerType: DEX protocol to use (0-36)

  • swapData: Protocol-specific calldata for the swap

Returns:

  • amountOut: Actual amount of output tokens received

Usage Example:

crossChainSwap

Execute a cross-chain token swap: TokenA (Chain A) → IU2U → TokenB (Chain B).

Parameters:

  • swapData: Source chain swap parameters (TokenA → IU2U)

  • destinationChain: Target chain name ("ethereum", "bsc", "polygon", etc.)

  • destinationToken: Target token address on destination chain

  • destinationSwapData: Encoded swap data for destination chain

  • destinationRouter: Router address on destination chain

  • amountOutMin: Minimum output on destination chain

Usage Example:

multiSwap

Execute multiple swaps in a single transaction for gas efficiency.

Parameters:

  • swaps: Array of swap parameters

Returns:

  • amountsOut: Array of output amounts for each swap

Usage Example:

View Functions

getSupportedTokens

Get all supported tokens on a specific chain.

getRouterAddress

Get the router contract address for a specific protocol and chain.

isRouterSupported

Check if a router is supported on a specific chain.

getSwapFee

Get the protocol fee for a specific router type.

Events

SwapExecuted

Emitted when a single swap is executed.

CrossChainSwapInitiated

Emitted when a cross-chain swap is initiated on the source chain.

CrossChainSwapCompleted

Emitted when a cross-chain swap is completed on the destination chain.

SwapFailed

Emitted when a swap fails and requires manual intervention.

Error Codes

SwapErrors

Usage Examples

Gas Optimization

Batch Operations

Calldata Optimization

Security Best Practices

Input Validation

Slippage Protection

Deadline Management

Integration Patterns

Basic Integration

Advanced Integration with MEV Protection

Next Steps

Last updated