ton-rs
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.
ton_macros
TLBDerive macros: Automatically derive TLB trait for your types based on it's members- Native
Enumsupport using TLBPrefix: Automatically match underlying variant by it's prefix (check ton_core_enum.rs example). Provides powerful enums, but use them carefully; read the Enum with TLB macros chapter. #[ton_methods]: Generate async get-method implementations for contract traits or impl blocks, with optional block-levelname_formatconversion and per-method exact names.
ton_core
serdefeature: provides few mods to ser/de core types, check ton_core/src/serde.rs. Disabled by default.- EmulationProvider - Provider-neutral interface used to execute TVM get methods
- TonCell
- TonAddress
- TLB - Trait allows you read/write arbitrary objects in BOC format
- Types - Few basic types, common and stable enough to be in core
ton
ton_contract!: Generate aTonContractwrapper type and optionally implement method traits for it.lite-clientfeature: Disabled by default. Enable it for the ADNL-basedLiteClient; it also enables the networking dependencies required by that client.tonlibjsonfeature: Disabled by default. Enable it for the nativeTLClient, emulator implementations,TLStateProvider, andton::emulators::tl_emulation_provider::TLEmulationProvider.ContractClientandTonContractcan be used without it by supplying custom providers. This feature includeslite-clientbecauseTLClientuses it to refresh the network configuration's init block.WalletVersionandLiteNodeFilterserialize using their Rust variant names;TVMGetMethodIDserializes as an integer or string according to its variant.- Use
TON_NET_CONF_MAINNET_PATHorTON_NET_CONF_TESTNET_PATHenv variables to overridenetconfig.jsonand use your own TON nodes. - TLBAdapters - Allows you to work with rust types like HashMap, and still serialize it properly for TON
- BlockTLB - Bunch of types to interact with raw blockchain data (However it's not fully covered)
- TonWallet - Wrapper of wallet to sign and create external messages
- TLClient - Using
tonlibjsonto interact with TON network - TonContract - Use it with
ContractClient::builder(state_provider, emulation_provider)to get data or execute methods on TON contracts - Standard Jetton, NFT, SBT, and TON wallet contract wrappers live under
contracts::tep, grouped into public modules by standard and implementation. For example, usecontracts::tep::jetton::jetton_master_contract::JettonMasterContractorcontracts::tep::ton_wallet::TonWalletContract. contracts::tep::metadata::meta_loader::MetaLoaderresolvesipfs://metadata through the IPFS Foundation's best-effort public gateway by default. Production applications should configure their own gateway withMetaLoader::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.
Ledger wallets
[!WARNING] Risk of permanent loss of funds — use at your own risk.
ton_ledgeris provided “as is”, without warranties. To the extent permitted by applicable law and subject to the Apache-2.0 license, the authors, maintainers, and contributors are not responsible for lost funds or other damages arising from its use. You are responsible for reviewing and testing your integration and every transaction you approve.Always test with a small amount you can afford to lose before using larger amounts. Use a dedicated test wallet with a minimal balance, allow for network fees, and confirm that the recipient received the funds on-chain before proceeding. Repeat these checks after changing your integration, device, firmware, or TON app version. A successful test does not guarantee future safety.
Verify the destination, amount, and all available transaction details on the Ledger screen before approving. Do not approve blind signing or opaque payloads unless you independently understand and verify what they authorize. Hardware signing and local signature checks do not guarantee that a transaction is safe or that it succeeded on-chain. Confirmed blockchain transfers cannot be undone by this library or its maintainers.
ton_ledger provides V3R2/V4R2 signing through
USB HID (default), optional Bluetooth, or an exclusive custom transport.
Signatures and exact-message hashes are checked locally. The library owns no
network provider. Its firmware profile is pinned to TON app 2.9.1; hardware
acceptance remains separate from deterministic tests. A manual
USB-first self-transfer example
shows how to sign and broadcast 0.01 TON to the same deployed mainnet wallet. See the crate README for platform setup and limits.
Rust version
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.
Getting started
Examples can be found in examples folder (feel free to add your own)
- ton_emulate_get_method: network-backed contract emulation; requires
--features tonlibjson. - ton_transfer: signs and broadcasts a transfer; requires
--features tonlibjsonand deliberate account/recipient configuration. - ton_ledger_self_transfer: USB-first Ledger signing with Bluetooth fallback; requires
--features ledger-bleand spends mainnet fees.
Run examples through cargo run -p examples --example <name> --features <features>.
Compilation is separate from execution; do not run transfer examples as smoke tests.
Basic usage
Build and read a cell with ton_core:
use TonCell;
Derive a TLB codec and round-trip a typed record:
use TLB;
use TLB;
Enum with TLB macros
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.
use TLB;
;
;
// Common prefix
This is effectively parsed as:
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.
Contribution
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)