Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[Cosmoverse 2023] IBC-Solidity - Programming IBC in Solidity

TOKI
October 05, 2023

[Cosmoverse 2023] IBC-Solidity - Programming IBC in Solidity

This is the deck for Cosmoverse 2023. Susumu, the VP of Product at TOKI, gave a presentation about IBC-Solidity, the IBC implementation in Solidity.

Find our more:
- GitHub: https://github.com/hyperledger-labs/yui-ibc-solidity
- X(Twitter): https://twitter.com/tokifinance

TOKI

October 05, 2023
Tweet

More Decks by TOKI

Other Decks in Technology

Transcript

  1. IBC-Solidity
    Programming IBC in Solidity
    ・・・

    View Slide

  2. Building IBC-bridge in
    non-Cosmos ecosystem
    Enterprise IBC adoption
    IBC-Solidity developer

    View Slide

  3. IBC-Solidity Overview
    IBC implementations in Solidity
    - IBC compatibility: v4.0.0
    - Supports EVM-compatible chains such as Ethereum
    Key Features
    - Implementation of ICS (IBC Core/TAO)
    - Implementation of ICS-20 (protobuf encoding)
    - Enable ERC-20 Token Transfer

    View Slide

  4. Key Specification
    Integration of Light Client (ICS-02)
    - Implements IClient interface to integrate with IBC-Solidity
    - Examples includes IBFT 2.0, Tendermint Light Client and Mock
    Client
    Internal encoding
    - Currently using solidity-protobuf for Packet encoding unlike
    JSON in specs such as ICS-20
    - Currently, v0.1.0 is required

    View Slide

  5. Example: MiniToken 🪙
    IBC-based token transfer between chains
    - Use IBC to create and send packets between blockchains
    - Create a basic token and send tokens to another blockchain
    - Basically, ICS-20 without denomination trace
    https://github.com/hyperledger-labs/yui-docs
    Ledgers used in sample: (MockClient is used for simplicity)
    - Geth
    - Besu (variant of Ethereum client in Java)

    View Slide

  6. Contract relationship
    IBCHandler
    (ICS-25)
    IBCChannel
    (ICS-04)
    IBCConnection
    (ICS-03)
    IBCClient
    (ICS-02)
    MiniToken
    (IBCAppBase)
    IBCModule
    (ICS-05)
    IBCPacket
    (ICS-04)
    MiniToken
    PacketData
    bindPort to
    set callbacks
    delegatecall Encode to send
    through IBCHandler
    ibcHandler.bindPort(PortTransfer, MiniToken.address)
    MiniToken App
    IBC-Solidity

    View Slide

  7. Packet Structure/Schema for MiniToken
    syntax = "proto3";
    option go_package = "pkg/ibc/app";
    message MiniTokenPacketData {
    // the token amount to be transferred
    uint64 amount = 1;
    // the sender address
    bytes sender = 2;
    // the recipient address on the destination chain
    bytes receiver = 3;
    }
    Generated Packet.sol:
    https://github.com/hyperledger-labs/yui-docs/blob/main/contracts/minitoken/solidi
    ty/contracts/lib/Packet.sol

    View Slide

  8. Key Functions for MiniToken: Send data
    function sendTransfer(
    uint64 amount, address receiver,
    string calldata sourcePort,
    string calldata sourceChannel,
    uint64 timeoutHeight
    ) external {
    require(_burn(msg.sender, amount), "MiniToken: failed to burn");
    _sendPacket(
    MiniTokenPacketData.Data({
    amount: amount,
    sender: abi.encodePacked(msg.sender),
    receiver: abi.encodePacked(receiver)
    }),
    sourcePort, sourceChannel, timeoutHeight
    );
    emit SendTransfer(
    msg.sender, receiver, sourcePort, sourceChannel, timeoutHeight, amount
    );
    }

    View Slide

  9. Key Functions for MiniToken: Send data (Contd.)
    function _sendPacket(
    MiniTokenPacketData.Data memory data,
    string memory sourcePort,
    string memory sourceChannel,
    uint64 timeoutHeight
    ) internal virtual {
    ibcHandler.sendPacket(
    sourcePort,
    sourceChannel,
    Height.Data({
    revision_number: 0,
    revision_height: timeoutHeight
    }),
    0,
    MiniTokenPacketData.encode(data)
    );
    }
    interface IIBCPacket {
    function sendPacket(
    string calldata sourcePort,
    string calldata sourceChannel,
    Height.Data calldata timeoutHeight,
    uint64 timeoutTimestamp,
    bytes calldata data
    ) external returns (uint64);

    View Slide

  10. Key Functions for MiniToken: Callbacks
    function onRecvPacket(Packet.Data calldata packet, address /*relayer*/)
    external virtual override
    onlyIBC
    returns (bytes memory acknowledgement)
    {
    MiniTokenPacketData.Data memory data = MiniTokenPacketData.decode(
    packet.data
    );
    return
    _newAcknowledgement(_mint(data.receiver.toAddress(), data.amount));
    }
    function onAcknowledgementPacket(
    Packet.Data calldata packet,
    bytes calldata acknowledgement, address /*relayer*/
    ) external virtual override onlyIBC {
    if (!_isSuccessAcknowledgement(acknowledgement)) {
    _refundTokens(MiniTokenPacketData.decode(packet.data));
    }
    }

    View Slide

  11. MiniToken: Scenario
    Alice@ibc0
    (Besu)
    Bob@ibc1
    (Geth)
    1. Mint 100 token
    2. Send 50 token
    3. Return 25 token

    View Slide

  12. Demo
    https://asciinema.org/a/611589

    View Slide

  13. $ truffle console --network=ibc0
    truffle(ibc0)> MiniToken.deployed().then((instance) => instance.balanceOf(accounts[1]))
    BN { negative: 0, words: [ 0, <1 empty item> ], length: 1, red: null }
    truffle(ibc0)> MiniToken.deployed().then((instance) => instance.balanceOf(accounts[1]))
    BN {
    negative: 0,
    words: [ 100, <1 empty item> ],
    length: 1,
    red: null
    }
    truffle(ibc0)> await MiniToken.deployed().then(instance => instance.mint(accounts[1], 100));
    {
    tx: '0xce...',
    receipt: {
    ...
    },
    logs: [
    {
    ...
    event: 'Mint',
    args: [Result]
    }
    ]
    }

    View Slide

  14. truffle(ibc0)> await MiniToken.deployed().then(instance =>
    instance.sendTransfer(50, accounts[2], "transfer", "channel-0", 1500, {from:
    accounts[1]}));
    {
    tx: '0x011...',
    receipt: {
    ...
    },
    logs: [
    {
    ...
    event: 'Burn',
    args: [Result]
    },
    {
    ...
    event: 'SendTransfer',
    args: [Result]
    }
    ]
    }

    View Slide

  15. truffle(ibc1)> MiniToken.deployed().then((instance) => instance.balanceOf(accounts[2]))
    BN { negative: 0, words: [ 50, <1 empty item> ], length: 1, red: null }
    truffle(ibc1)> await MiniToken.deployed().then(instance => instance.sendTransfer(25,
    accounts[1], "transfer", "channel-0", 1500, {from: accounts[2]}));
    {
    tx: '0xd7f...',
    receipt: {
    ...
    },
    logs: [
    {
    ...
    event: 'Burn',
    args: [Result]
    },
    {
    ...
    event: 'SendTransfer',
    args: [Result]
    }
    ]
    }
    truffle(ibc1)> MiniToken.deployed().then((instance) => instance.balanceOf(accounts[2]))
    BN { negative: 0, words: [ 25, <1 empty item> ], length: 1, red: null }

    View Slide

  16. Related Projects
    YUI-Relayer: https://github.com/hyperledger-labs/yui-relayer
    - Modular architecture to support multiple chains (forked go-relayer)
    - Abstraction of “Chain” and “Prover”
    - Supports non-instant finality chains such as Ethereum
    Cosmos Ethereum LCP:
    https://github.com/datachainlab/cosmos-ethereum-ibc-lcp
    - Presented in keynote from 15:50
    [IBC Gets Ready to Land on Ethereum]
    - PingPong between Cosmos and Ethereum with full-fledged light clients

    View Slide

  17. Roadmap
    - Already on Testnet (Sepolia)
    - Audit -> December
    - Mainnet 🚀 -> Early 2024
    - Bridge to Noble -> 2024
    Other teams also have plans 💝

    View Slide

  18. Key Issues
    - ICS-20: Use JSON for packet data serialization
    - ICS-30: Middleware support
    You can refer to issues:
    https://github.com/hyperledger-labs/yui-ibc-solidity

    View Slide

  19. Contact / Further Information
    Toki Finance
    https://linktr.ee/tokifinance
    Datachain
    https://www.datachain.jp/

    View Slide