#coin #address #fields #int #comments #uint #bool

tonstruct

TON blockchain types serialization tool for Rust

1 unstable release

new 0.0.1 Feb 15, 2025

#23 in #uint

Download history 83/week @ 2025-02-10

83 downloads per month

MIT and LGPL-3.0+

25KB
538 lines

TonStruct

Work in progress

ℹ️ The Open Network deserialization crate for Rust language.

❤️ Any contributions are welcome. Feel free to open pull requests, issues, bug reports, feature proposals or anything else

Example

To parse transaction body you need to derive FromCell macro. In example below we parse transaction body from jetton transfer

use tonstruct::{FromCell, fields::{Uint, Coins, Address, Int, CellRef, Comment}};

#[derive(FromCell, Debug, PartialEq)]
struct ForwardPayload {
    is_right: bool,
    text_comment: CellRef<Comment>,
}

#[derive(FromCell, Debug, PartialEq)]
struct JettonTransfer {
    op_code: Uint<32>,
    query_id: Uint<64>,
    amount: Coins,
    destination: Address,
    response_destination: Address,
    custom_payload: Option<Int<0>>,
    forward_ton_amount: Coins,
    forward_payload: ForwardPayload,
}

fn main() {
    // Transaction body Cell
    let cell = ...;
    // Parsed body
    let message = <Message as FromCell>::from_cell(cell).unwrap();
}

To serialize structure into Cell use derived macro ToCell and call to_cell() method.

use tonstruct::ToCell;

#[derive(ToCell)]
struct JettonTransfer {
    /// Fields
}

fn main() {
    // Message body
    let message = Message {
        /// Fields 
    };

    let cell = message.to_cell().unwrap();
    /// OR
    let cell = ToCell::to_cell(&message).unwrap();
}

Dependencies

~3.5–4.5MB
~83K SLoC