tongo Go Library for TON Blockchain

repository·master·Indexed 43 days ago

https://github.com/tonkeeper/tongo

A comprehensive Go implementation for interacting with the TON blockchain. It provides low-level ADNL and Bag-of-Cells (BOC) handling, TL and TL-B serialization, and high-level tools for wallet deployment, smart contract interaction (including Jettons and NFTs), and Tolk ABI code generation. It also includes Go bindings for the C++ TON Virtual Machine (TVM) and a pure-Golang Lite client for node communication.

Tokens
10.1K
Snippets
53
Records
65
Agent score
55%

What's inside tongo

  1. Overview of Tolk ABI for Golang

    master

    The abi-tolk package enables interaction with TON smart contracts using their Tolk-based ABI. It bridges the gap between Tolk source code and Go by generating code from the ABI JSON produced by the Tolk compiler.

    Key capabilities:

    • Marshaling/Unmarshaling: Automatically generates code to convert Go types to/from TON cells for all types defined in the ABI.
    • Typed State Builders: Provides typed init state builders for contract deployment.
    • Get Method Wrappers: Generates Go wrappers for each contract get method, allowing for easy interaction with contract state.
    • Message Handling: Supports internal and external message types defined in the contract.
  2. Understand the tongo library structure

    master

    tongo is a modular Go implementation for the TON blockchain. The library is organized into several specialized packages:

    • ADNL: Low-level implementation of the ADNL protocol.
    • Lite client: Interaction with TON nodes as a lite client.
    • BOC: Methods and primitives for Cells and Bag-of-Cells (BOC).
    • TL: Interaction with binary data described by TL (Type Language) schemas.
    • TLB: Interaction with binary data (in Cells) described by TL-B (Typed Language - Binary) schemas.
    • TVM: Interaction with the TON Virtual Machine (TVM).
    • Wallet: Tools for deploying and interacting with wallet smart contracts.
    • Contract: Tools for interacting with smart contracts like Jettons and NFTs.
    • Tolk ABI: Data structures and methods for smart contract interaction using Tolk ABI.
  3. Serialize and deserialize TL-B data with the tlb library

    master

    The tlb library provides Marshaling and Unmarshaling capabilities for data described by TL-B (Type Language - B) schemas and certain primitives. This allows you to convert between Go structures and the binary format used by the TON blockchain.

    For detailed specifications of the TL-B language, refer to the official documentation at ton.org or the tonstack TL-B repository.

    // See the full implementation example in the repository:
    // examples/tlb/main.go
  4. Serialize and deserialize TL data

    master

    This library provides Marshaling and Unmarshaling capabilities for data described by TL (Type Language) schemas, following the MTProto TL specification. It allows you to convert TL-described structures into binary formats and back.

    // See example for implementation details
    // Example path: ../examples/tl/main.go
  5. Capabilities of the wallet library

    master

    The wallet library provides tools to simplify the deployment and interaction with TON wallet smart contracts. It supports the following operations:

    1. Address Generation: Generate addresses from public or private keys for wallet versions V1 through V4.
    2. Deployment: Generate messages required for deploying new wallets.
    3. Message Generation:
      • GRAM Transfers: Generate messages for transferring GRAM, including support for long comments.
      • Custom Payloads: Generate messages with custom payloads (supports up to 4 payload messages for V1-V4 wallets).
  6. Use Contract Storage in Message Builders

    master

    When a contract defines a storage type in its Tolk ABI, the generated internal and external message builders will require a *tlb.StateInitT[*StorageType] argument. This allows the builder to correctly account for the contract's state during message construction.

    Tolk Definition:

    contract cocoon_root {
        storage: RootStorage
    }
    
    struct RootStorage {
        ownerAddress: address
        data: Cell<RootData>
        params: Cell<CocoonParams>
        version: uint32
    }

    Generated Go Usage:

    func (msg RegisterProxy) ToInternal(
        dest tlb.InternalAddress,
        amount tlb.Grams,
        bounce bool,
        init *tlb.StateInitT[*RootStorage],
    ) (tlb.Message, error) {
        // ...
    }
    func (msg RegisterProxy) ToInternal(
    dest tlb.InternalAddress,
    amount tlb.Grams,
    bounce bool,
    init *tlb.StateInitT[*RootStorage],
    ) (tlb.Message, error) {
        // ...
    }
  7. Understand the Directory Layout for Generated Code

    master

    The package follows a specific directory structure to separate source schemas from generated Go code:

    • schemas/{group}/: Contains the original .tolk source files and the corresponding .json ABI files generated by the Tolk compiler.
    • abiGenerated/{group}/: Contains the generated Go code. Within a group, you will find:
      • {contract}.go: Structs, type aliases, get methods, and the client implementation.
      • {contract}_marshal.go: Marshaling/unmarshaling logic (and unmarshal from stack functions for get method return types).
      • executor.go: Interfaces for interacting with contracts.
      • tolk_consts_generated.go: Constants for the contract interface.
      • shared.go & shared_marshal.go: Shared types and marshaling logic used across multiple contracts in the same group.
    abi-tolk/
      schemas/
        cocoon/      # example: Cocoon contracts
        {group}/     # other contracts
            {contract1}.tolk
            {contract1}.json
      abiGenerated/
        cocoon/      # package abiCocoon
        {name}/      # package abi{Name}
        {group}/     # package abi{Group}
            {contract1}.go
            {contract1}_marshal.go
            executor.go
            tolk_consts_generated.go
            shared.go
            shared_marshal.go
  8. Use the Go bindings for the C++ TVM library

    master

    This library provides Go bindings to interact with the C++ implementation of the TON Virtual Machine (TVM). It allows Go developers to execute and interact with TVM-based smart contracts.

    To use the bindings, you must link or recompile the shared library provided in the /lib directory of this repository, selecting the version appropriate for your operating system.

    // See example for implementation details
    // [Example](../examples/tvm/main.go)
  9. Connect to a TON node

    master

    To connect to a TON node via the Lite client, you need the node's public key and its ip:port address.

    Typically, you use public configuration files. You can download the required configuration files from ton.org:

    • Mainnet: global-config.json
    • Testnet: testnet-global.config.json

    Note: The Lite client supports auto-downloading the mainnet configuration directly from ton.org.

  10. Use the Linux emulator library

    master

    To use the libemulator.so library on Linux, you must ensure the system can locate it. You can either copy the file to /usr/lib or set the LD_LIBRARY_PATH environment variable to point to the directory containing the library.

    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/directory_containing_libemulator_so