Rust libraries for working with the TON blockchain: cells, TLB, addresses, wallets, contracts, and tonlibjson integration.
Rust libraries for working with the TON blockchain: cells, TLB, addresses, wallets, contracts, and tonlibjson integration.
Set of general-purpose rust libraries to interact with TON blockchain. This crate is heavily based on the tonlib-rs repository and also uses tonlib-sys underneath for the tonlibjson_client implementation.
TLB Derive macros: Automatically derive TLB trait for your types based on it's membersEnum support using TLBPrefix: Automatically match underlying variant by it's prefix (check enum.rs example). Provides powerful enums, but use them carefully; read the Enum with TLB macros chapter.ton_contract!: Generate a TonContract wrapper type and optionally implement method traits for it.#[ton_methods]: Generate async get-method implementations for contract traits or impl blocks, with optional block-level name_format conversion and per-method exact names.serde feature: provides few mods to ser/de core types, check ton_core/src/serde.rs. Disabled by default.lite-client feature: Disabled by default. Enable it for the ADNL-based LiteClient; it also enables the networking dependencies required by that client.tonlibjson feature: Disabled by default. Enable it for the native TLClient, emulator implementations, TLStateProvider, and ton::emulators::tl_emulation_provider::TLEmulationProvider. ContractClient and TonContract can be used without it by supplying custom providers.
This feature includes lite-client because TLClient uses it to refresh the network configuration's init block.WalletVersion and LiteNodeFilter serialize using their Rust variant names; TVMGetMethodID serializes as an integer or string according to its variant.TON_NET_CONF_MAINNET_PATH or TON_NET_CONF_TESTNET_PATH env variables to override netconfig.json and use your own TON nodes.tonlibjson to interact with TON networkContractClient::builder(state_provider, emulation_provider) to get data or execute methods on TON contractscontracts::tep, grouped into public modules by standard and implementation. For example, use contracts::tep::jetton::jetton_master_contract::JettonMasterContract or contracts::tep::ton_wallet::TonWalletContract.contracts::tep::metadata::meta_loader::MetaLoader resolves ipfs:// metadata through the IPFS Foundation's best-effort public gateway by default. Production applications should configure their own gateway with MetaLoader::builder().with_ipfs_base_url(...).ContractClient::builder(...).with_default_caches() configures state caches. When using the native adapter, configure emulator library caches independently with ton::emulators::tl_emulation_provider::TLEmulationProvider::with_default_caches().
State caches require an active Tokio runtime when the client is built and start a background refresh task. Dropping the client does not cancel an in-flight provider call; initial sequence discovery also keeps retrying provider errors until it succeeds.
TonContract::new() synchronously stores the contract address and optional transaction ID. load_state() and load_parsed_data() load and retain state; emulation uses retained state when available. Otherwise, it lets the emulation provider resolve the address and transaction ID unless EmulationProvider::requires_resolved_state() returns true, in which case the configured state provider loads it first.
Mnemonic clears its owned words and password on drop, and KeyPair clears its
secret key bytes. Caller-owned mnemonic strings and copies read from the public
KeyPair::secret_key field remain the caller's responsibility.
The minimum supported Rust version (MSRV) is 1.94. CI verifies every published crate, target, and feature against the declared MSRV and checks weekly whether the latest dependency releases still support it. MSRV increases are released as minor compatibility changes.
Examples can be found in examples folder (feel free to add your own)
Interesting one:
// Build and read custom cells
fn main() -> anyhow::Result {
use ton_lib::cell::ton_cell::TonCell;
let mut builder = TonCell::builder();
builder.write_bits([1,2,3], 24).unwrap();
let cell = builder.build().unwrap();
assert_eq!(cell.data, vec![1, 2, 3]);
let mut parser = cell.parser();
let data = parser.read_bits(24).unwrap();
assert_eq!(data, [1, 2, 3]);
}…TLB macros can derive TLB for enums. You can define enums with a common prefix or with no common prefix. Enums without a common prefix are tricky: if you embed such an enum into another enum, its variants are effectively inlined into the outer enum.
#[derive(TLB)]
#[tlb(prefix = 0b010, bits_len = 3)]
struct Variant1(u8);
#[derive(TLB)]
#[tlb(prefix = 0b011, bits_len = 3)]
struct Variant2(u8);
#[derive(TLB)]
enum InnerEnum { // No common prefix
Variant1(Variant1), // Prefix = 0b010
Variant2(Variant2), // Prefix = 0b011
}
#[derive(TLB)]
#[tlb(prefix = 0b1, bits_len = 1)] // Common prefix
enum OuterEnum {
OuterVariant1(u16), // Prefix overall = 0b101
OuterVariant2(InnerEnum),
}This is effectively parsed as:
enum OuterEnum {
OuterVariant1(u16), // Prefix overall = 0b101
Variant1(u8), // Prefix overall = 0b1010
Variant2(u16), // Prefix overall = 0b1011
}Be careful with null (zero-length) prefixes. A null prefix acts like a wildcard; during parsing, variants are tried in declaration order, so a null-prefix variant placed earlier can consume the input before later variants are considered. See tests in ton_core/src/traits/tlb/test_tlb_enum.rs for the shadowing and the safe-prefix example.
Repository and crate-specific guidance lives in AGENTS.md.
Public API changes must preserve wire formats, document compatibility impact,
and keep one supported construction path. Open enums and returned records use
#[non_exhaustive]; fixed TLB records remain exhaustive when their fields are
the serialization contract.
If you face with some unclear parts or bugs, your can add a new example or improve documentation.
If you implemented some general feature, please make sure it's covered by tests (unit tests if possible)
No open issues yet, or sync has not completed.