MulticallLibraryV2

The MulticallLibraryV2 provides efficient batch execution of multiple function calls in a single transaction, optimized for gas efficiency and error handling.

Overview

MulticallLibraryV2 enables developers to execute multiple contract calls atomically, reducing gas costs and improving user experience by bundling operations together.

Library Interface

library MulticallLibraryV2 {
    struct Call {
        address target;
        bytes callData;
        uint256 gasLimit;
    }
    
    struct CallWithValue {
        address target;
        bytes callData;
        uint256 value;
        uint256 gasLimit;
    }
    
    struct Result {
        bool success;
        bytes returnData;
        uint256 gasUsed;
    }
    
    function multicall(Call[] memory calls) external returns (Result[] memory results);
    function multicallWithValue(CallWithValue[] memory calls) external payable returns (Result[] memory results);
    function tryMulticall(Call[] memory calls) external returns (Result[] memory results);
    function aggregate(Call[] memory calls) external returns (uint256 blockNumber, bytes[] memory returnData);
}

Core Functions

multicall

Executes multiple calls and reverts if any call fails.

Parameters:

  • calls: Array of Call structs containing target addresses and call data

Returns:

  • results: Array of Result structs with success status, return data, and gas used

Example:

multicallWithValue

Executes multiple calls with ETH value, reverting if any call fails.

Parameters:

  • calls: Array of CallWithValue structs including ETH value for each call

Returns:

  • results: Array of Result structs with execution details

Example:

tryMulticall

Executes multiple calls without reverting on individual call failures.

Parameters:

  • calls: Array of Call structs to execute

Returns:

  • results: Array of Result structs, including failed calls

Example:

aggregate

Legacy function for basic multicall aggregation.

Parameters:

  • calls: Array of Call structs to execute

Returns:

  • blockNumber: Current block number

  • returnData: Array of return data from each call

Advanced Usage Patterns

1. DeFi Batch Operations

2. NFT Batch Minting

3. Cross-Chain Batch Operations

Gas Optimization Features

Dynamic Gas Limit Adjustment

Gas Estimation

Error Handling

Custom Error Types

Enhanced Error Reporting

Security Considerations

Reentrancy Protection

Access Control

Integration Examples

Frontend JavaScript

React Hook

Performance Metrics

Operation Type
Gas Savings
Execution Time
Max Batch Size

Token Transfers

60-80%

40-60% faster

50-100 calls

NFT Operations

50-70%

30-50% faster

20-50 calls

DeFi Interactions

40-60%

50-70% faster

10-30 calls

Cross-Chain Calls

30-50%

60-80% faster

5-20 calls

Best Practices

1. Gas Optimization

  • Set appropriate gas limits for each call

  • Use tryMulticall for non-critical operations

  • Batch similar operations together

  • Monitor gas usage patterns

2. Error Handling

  • Always check individual call results

  • Implement proper fallback mechanisms

  • Use descriptive error messages

  • Log failed operations for debugging

3. Security

  • Validate all target addresses

  • Implement access controls where needed

  • Use reentrancy protection

  • Audit multicall implementations

4. User Experience

  • Provide clear transaction previews

  • Show individual operation status

  • Implement retry mechanisms

  • Optimize for mobile gas limits

Resources

Last updated