ton_macros 0.2.2

A collection of types and utilities for interacting with the TON network
Documentation

ton_macros

Automatically derive TLB and TonContract traits for your types

TLB Derive

use ton_macros::TLB;

#[derive(Debug, Clone, PartialEq, TLB)]
#[tlb(prefix = 0xc4, bits_len = 8)]
pub struct GlobalVersion {
    pub version: u32,
    pub capabilities: u64,
}

// specify custom adapter (ser/de functions for TLB)
#[derive(Debug, Clone, PartialEq, TLB)]
pub struct StateInit {
    #[tlb(bits_len = 5)]
    pub split_depth: Option<u8>,
    pub tick_tock: Option<TickTock>,
    pub code: Option<TLBRef<TonCell>>,
    pub data: Option<TLBRef<TonCell>>,
    #[tlb(adapter = "TLBHashMapE::<DictKeyAdapterTonHash, DictValAdapterTLB<_>>::new(256)")]
    pub library: HashMap<TonHash, SimpleLib>,
}

TonContract and ton_methods

use ton::contracts::TonContract;
use ton::errors::TonResult;
use ton::ton_contract;
use ton_macros::ton_methods;

#[async_trait::async_trait]
#[ton_methods]
pub trait JettonMasterMethods: TonContract {
    async fn get_jetton_data(&self) -> TonResult<u32>;
}

ton_contract!(JettonMaster: JettonMasterMethods);

#[ton_methods] generates default get-method implementations for traits or inherent impl blocks. By default, it passes the Rust function name to emulate_get_method unchanged.

Use name_format to convert Rust method names before emulation:

use ton::contracts::TonContract;
use ton::errors::TonResult;
use ton::ton_contract;
use ton_macros::ton_methods;

#[async_trait::async_trait]
#[ton_methods(name_format = "camelCase")]
pub trait OrderContractMethods: TonContract {
    // Emulates getOrderData.
    async fn get_order_data(&self) -> TonResult<u32>;
}

ton_contract!(OrderContract: OrderContractMethods);

Supported format names are based on convert_case::Case. Common values include snake_case, camelCase, PascalCase, CamelCase, kebab-case, and CONSTANT_CASE.

Use #[ton_method(name = "...")] when one method does not follow the enclosing format. The exact name takes precedence over name_format:

#[async_trait::async_trait]
#[ton_methods(name_format = "camelCase")]
pub trait OrderContractMethods: TonContract {
    #[ton_method(name = "getUIVariables")]
    async fn get_ui_variables(&self) -> TonResult<u32>;
}