tonlib_core/
message.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
mod error;
pub use error::*;
use lazy_static::lazy_static;
use num_bigint::BigUint;
use num_traits::Zero;

mod common_msg_info;
pub use common_msg_info::*;

use crate::cell::{ArcCell, Cell};

mod common;
mod jetton;
mod nft;
mod sbt;
mod transfer;
pub use common::*;
pub use jetton::*;
pub use nft::*;
pub use sbt::*;
pub use transfer::*;

lazy_static! {
    pub(crate) static ref ZERO_COINS: BigUint = BigUint::zero();
}

pub trait TonMessage: Sized {
    fn build(&self) -> Result<Cell, TonMessageError>;

    fn parse(cell: &Cell) -> Result<Self, TonMessageError>;
}

pub trait HasOpcode: TonMessage {
    fn verify_opcode(&self, opcode: u32) -> Result<(), TonMessageError> {
        let expected_opcode = Self::opcode();
        if opcode != expected_opcode {
            let invalid = InvalidMessage {
                opcode: Some(opcode),
                query_id: Some(self.query_id()),
                message: format!("Unexpected opcode.  {0:08x} expected", expected_opcode),
            };
            Err(TonMessageError::InvalidMessage(invalid))
        } else {
            Ok(())
        }
    }

    fn opcode() -> u32;
    fn with_query_id(&mut self, query_id: u64) -> &mut Self {
        self.set_query_id(query_id);
        self
    }
    fn set_query_id(&mut self, query_id: u64);

    fn query_id(&self) -> u64;
}

impl TonMessage for Cell {
    fn build(&self) -> Result<Cell, TonMessageError> {
        Ok(self.clone())
    }

    fn parse(cell: &Cell) -> Result<Self, TonMessageError> {
        Ok(cell.clone())
    }
}

pub trait WithForwardPayload: TonMessage {
    fn with_forward_payload(
        &mut self,
        forward_ton_amount: BigUint,
        forward_payload: ArcCell,
    ) -> &mut Self {
        self.set_forward_payload(forward_payload, forward_ton_amount);
        self
    }

    fn set_forward_payload(&mut self, forward_payload: ArcCell, forward_ton_amount: BigUint);
}