Contract Calls

Cross-chain contract calls enable smart contracts on one blockchain to execute functions on contracts deployed on different blockchains, creating truly interoperable decentralized applications.

Overview

Contract calls extend beyond simple token transfers by allowing arbitrary function execution across chains. This enables complex multi-chain workflows, cross-chain governance, synchronized state updates, and distributed application logic.

Architecture

Implementation Patterns

1. Simple Function Call

Execute a function on another chain without parameters:

contract CrossChainNotifier {
    IIU2UGateway public gateway;
    
    function notifyOnChain(string memory targetChain, address targetContract) external {
        bytes memory payload = abi.encodeWithSignature("notify()");
        
        gateway.callContract(
            targetChain,
            Strings.toHexString(uint160(targetContract), 20),
            payload
        );
    }
}

contract NotificationReceiver is IU2UExecutable {
    event NotificationReceived(string sourceChain, address sourceContract);
    
    function execute(
        bytes32 commandId,
        string memory sourceChain,
        string memory sourceAddress,
        bytes memory payload
    ) external override onlyGateway {
        // Decode the function selector
        bytes4 selector = bytes4(payload);
        
        if (selector == this.notify.selector) {
            notify();
            emit NotificationReceived(sourceChain, _parseAddress(sourceAddress));
        }
    }
    
    function notify() public {
        // Notification logic here
    }
}

2. Parameterized Function Call

Execute functions with complex parameters:

3. Conditional Execution

Execute functions based on on-chain conditions:

4. Multi-Step Workflow

Chain multiple function calls across different contracts:

Advanced Patterns

Callback Mechanism

Implement callbacks for bidirectional communication:

State Synchronization

Keep state synchronized across multiple chains:

Cross-Chain Governance

Implement governance decisions across multiple chains:

Error Handling

Robust Error Recovery

Circuit Breaker Pattern

Security Considerations

Input Validation

Reentrancy Protection

Gas Optimization

Batch Contract Calls

Optimized Payload Encoding

Testing

Mock Contracts for Testing

Integration Tests

Best Practices

1. Design for Failure

Always implement proper error handling and recovery mechanisms.

2. Validate Inputs

Thoroughly validate all parameters before making cross-chain calls.

3. Use Access Control

Implement proper access control for sensitive cross-chain operations.

4. Optimize Gas Usage

Minimize payload size and use efficient encoding methods.

5. Test Thoroughly

Test all cross-chain interactions on testnets before mainnet deployment.

6. Monitor Operations

Implement monitoring and alerting for cross-chain contract calls.

Resources

Last updated