Programmatic Gateway

This page explains how to use the Sonic Gateway in your application or script to transfer ERC-20 assets from Ethereum to Sonic and back.

Sonic Bridge: Programmatic Usage Guide

Contract Addresses

// Ethereum (L1)
const ETH_CONTRACTS = {
    TOKEN_DEPOSIT: "0xa1E2481a9CD0Cb0447EeB1cbc26F1b3fff3bec20",
    TOKEN_PAIRS: "0xf2b1510c2709072C88C5b14db90Ec3b6297193e4",
    STATE_ORACLE: "0xB7e8CC3F5FeA12443136f0cc13D81F109B2dEd7f"
};

// Sonic (L2)
const SONIC_CONTRACTS = {
    BRIDGE: "0x9Ef7629F9B930168b76283AdD7120777b3c895b3",
    TOKEN_PAIRS: "0x134E4c207aD5A13549DE1eBF8D43c1f49b00ba94",
    STATE_ORACLE: "0x836664B0c0CB29B7877bCcF94159CC996528F2C3"
};

Setup

// Network RPC endpoints
const ETHEREUM_RPC = "https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY";
const SONIC_RPC = "https://rpc.soniclabs.com";

// Initialize providers
const ethProvider = new ethers.providers.JsonRpcProvider(ETHEREUM_RPC);
const sonicProvider = new ethers.providers.JsonRpcProvider(SONIC_RPC);

// Initialize signer with your private key
const PRIVATE_KEY = "your-private-key";
const ethSigner = new ethers.Wallet(PRIVATE_KEY, ethProvider);
const sonicSigner = new ethers.Wallet(PRIVATE_KEY, sonicProvider);

Bridge Operations

1. Ethereum to Sonic Transfer

2. Claim Tokens on Sonic

3. Sonic to Ethereum Transfer

4. Claim Tokens on Ethereum

Complete Example

Required ABIs

Important Notes

  1. State Updates

    • Ethereum → Sonic: Monitor StateOracle.lastBlockNum until it's >= deposit block

    • Sonic → Ethereum: Monitor StateOracle.lastBlockNum until it's >= withdrawal block

  2. Proofs

    • Required for all claim operations

    • Generated using eth_getProof RPC call with correct storage slots

    • Must be RLP encoded in format: RLP.encode([RLP.encode(accountProof), RLP.encode(storageProof)])

    • Storage slots are calculated using:

      • Deposits: keccak256(abi.encode(depositId, uint8(7)))

      • Withdrawals: keccak256(abi.encode(withdrawalId, uint8(1)))

  3. Gas Fees

    • Keep enough ETH/S for gas on both networks

    • Claim operations typically cost more gas due to proof verification

  4. Security

    • Never share private keys

    • Always verify contract addresses

    • Test with small amounts first

    • Use the same private key for both networks

  5. Monitoring

    • Monitor transaction status on both networks

    • Keep transaction hashes for reference

    • Verify successful claims before proceeding

    • Monitor StateOracle updates for claim timing

Last updated