tonlib_core_anychain/message/jetton/
transfer.rs

1use num_bigint::BigUint;
2use num_traits::Zero;
3
4use super::JETTON_TRANSFER;
5use crate::cell::{ArcCell, Cell, CellBuilder, EitherCellLayout, EMPTY_ARC_CELL};
6use crate::message::{HasOpcode, TonMessage, TonMessageError, WithForwardPayload, ZERO_COINS};
7use crate::TonAddress;
8
9/// Creates a body for jetton transfer according to TL-B schema:
10///
11/// ```raw
12/// transfer#0f8a7ea5 query_id:uint64 amount:(VarUInteger 16) destination:MsgAddress
13///                  response_destination:MsgAddress custom_payload:(Maybe ^Cell)
14///                  forward_ton_amount:(VarUInteger 16) forward_payload:(Either Cell ^Cell)
15///                  = InternalMsgBody;
16/// ```
17#[derive(Clone, Debug, PartialEq)]
18pub struct JettonTransferMessage {
19    /// arbitrary request number.
20    pub query_id: u64,
21    /// amount of transferred jettons in elementary units.
22    pub amount: BigUint,
23    /// address of the new owner of the jettons.
24    pub destination: TonAddress,
25    /// address where to send a response with confirmation of a successful transfer and the rest of the incoming message Toncoins.
26    pub response_destination: TonAddress,
27    /// optional custom data (which is used by either sender or receiver jetton wallet for inner logic).
28    pub custom_payload: Option<ArcCell>,
29    ///  the amount of nanotons to be sent to the destination address.
30    pub forward_ton_amount: BigUint,
31    ///  optional custom data that should be sent to the destination address.
32    pub forward_payload: ArcCell,
33
34    pub forward_payload_layout: EitherCellLayout,
35}
36
37impl JettonTransferMessage {
38    pub fn new(destination: &TonAddress, amount: &BigUint) -> Self {
39        JettonTransferMessage {
40            query_id: 0,
41            amount: amount.clone(),
42            destination: destination.clone(),
43            response_destination: TonAddress::null(),
44            custom_payload: None,
45            forward_ton_amount: ZERO_COINS.clone(),
46            forward_payload: EMPTY_ARC_CELL.clone(),
47            forward_payload_layout: EitherCellLayout::Native,
48        }
49    }
50
51    pub fn with_response_destination(&mut self, response_destination: &TonAddress) -> &mut Self {
52        self.response_destination = response_destination.clone();
53        self
54    }
55
56    pub fn with_custom_payload(&mut self, custom_payload: ArcCell) -> &mut Self {
57        self.custom_payload = Some(custom_payload);
58        self
59    }
60
61    pub fn set_either_cell_layout(&mut self, layout: EitherCellLayout) -> &mut Self {
62        self.forward_payload_layout = layout;
63        self
64    }
65}
66
67impl WithForwardPayload for JettonTransferMessage {
68    fn set_forward_payload(&mut self, forward_payload: ArcCell, forward_ton_amount: BigUint) {
69        self.forward_payload = forward_payload;
70        self.forward_ton_amount = forward_ton_amount;
71    }
72}
73
74impl TonMessage for JettonTransferMessage {
75    fn build(&self) -> Result<Cell, TonMessageError> {
76        if self.forward_ton_amount.is_zero() && self.forward_payload != *EMPTY_ARC_CELL {
77            return Err(TonMessageError::ForwardTonAmountIsNegative);
78        }
79
80        let mut builder = CellBuilder::new();
81        builder.store_u32(32, Self::opcode())?;
82        builder.store_u64(64, self.query_id)?;
83        builder.store_coins(&self.amount)?;
84        builder.store_address(&self.destination)?;
85        builder.store_address(&self.response_destination)?;
86        builder.store_maybe_cell_ref(&self.custom_payload)?;
87        builder.store_coins(&self.forward_ton_amount)?;
88        builder
89            .store_either_cell_or_cell_ref(&self.forward_payload, self.forward_payload_layout)?;
90        Ok(builder.build()?)
91    }
92
93    fn parse(cell: &Cell) -> Result<Self, TonMessageError> {
94        let mut parser = cell.parser();
95
96        let opcode: u32 = parser.load_u32(32)?;
97        let query_id = parser.load_u64(64)?;
98
99        let amount = parser.load_coins()?;
100        let destination = parser.load_address()?;
101        let response_destination = parser.load_address()?;
102        let custom_payload = parser.load_maybe_cell_ref()?;
103        let forward_ton_amount = parser.load_coins()?;
104        let forward_payload = parser.load_either_cell_or_cell_ref()?;
105        parser.ensure_empty()?;
106
107        let result = JettonTransferMessage {
108            query_id,
109            amount,
110            destination,
111            response_destination,
112            custom_payload,
113            forward_ton_amount,
114            forward_payload,
115            forward_payload_layout: EitherCellLayout::Native,
116        };
117        result.verify_opcode(opcode)?;
118
119        Ok(result)
120    }
121}
122
123impl HasOpcode for JettonTransferMessage {
124    fn set_query_id(&mut self, query_id: u64) {
125        self.query_id = query_id;
126    }
127
128    fn query_id(&self) -> u64 {
129        self.query_id
130    }
131
132    fn opcode() -> u32 {
133        JETTON_TRANSFER
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use std::str::FromStr;
140    use std::sync::Arc;
141
142    use num_bigint::BigUint;
143    use num_traits::Zero;
144
145    use crate::cell::{BagOfCells, Cell, CellBuilder, EitherCellLayout, EMPTY_ARC_CELL};
146    use crate::message::{JettonTransferMessage, TonMessage, TonMessageError, WithForwardPayload};
147    use crate::TonAddress;
148
149    const JETTON_TRANSFER_MSG : &str="b5ee9c720101020100a800016d0f8a7ea5001f5512dab844d643b9aca00800ef3b9902a271b2a01c8938a523cfe24e71847aaeb6a620001ed44a77ac0e709c1033428f030100d7259385618009dd924373a9aad41b28cec02da9384d67363af2034fc2a7ccc067e28d4110de86e66deb002365dfa32dfd419308ebdf35e0f6ba7c42534bbb5dab5e89e28ea3e0455cc2d2f00257a672371a90e149b7d25864dbfd44827cc1e8a30df1b1e0c4338502ade2ad96";
150    const TRANSFER_PAYLOAD: &str = "259385618009DD924373A9AAD41B28CEC02DA9384D67363AF2034FC2A7CCC067E28D4110DE86E66DEB002365DFA32DFD419308EBDF35E0F6BA7C42534BBB5DAB5E89E28EA3E0455CC2D2F00257A672371A90E149B7D25864DBFD44827CC1E8A30DF1B1E0C4338502ADE2AD94";
151
152    #[test]
153    fn test_jetton_transfer_parser() -> Result<(), TonMessageError> {
154        let boc = BagOfCells::parse_hex(JETTON_TRANSFER_MSG).unwrap();
155        let cell = boc.single_root().unwrap();
156
157        let result_jetton_transfer_msg = JettonTransferMessage::parse(cell)?;
158
159        let transfer_message_cell = Arc::new(Cell::new(
160            hex::decode(TRANSFER_PAYLOAD).unwrap(),
161            862,
162            vec![],
163            false,
164        )?);
165
166        let expected_jetton_transfer_msg = JettonTransferMessage {
167            query_id: 8819263745311958,
168            amount: BigUint::from(1000000000u64),
169            destination: TonAddress::from_str("EQB3ncyBUTjZUA5EnFKR5_EnOMI9V1tTEAAPaiU71gc4TiUt")
170                .unwrap(),
171            response_destination: TonAddress::from_str(
172                "EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c",
173            )
174            .unwrap(),
175            custom_payload: None,
176            forward_ton_amount: BigUint::from(215000000u64),
177            forward_payload: transfer_message_cell,
178            forward_payload_layout: EitherCellLayout::Native,
179        };
180
181        assert_eq!(expected_jetton_transfer_msg, result_jetton_transfer_msg);
182        Ok(())
183    }
184
185    #[test]
186    fn test_jetton_transfer_builder() -> Result<(), TonMessageError> {
187        let jetton_transfer_msg = JettonTransferMessage {
188            query_id: 8819263745311958,
189            amount: BigUint::from(1000000000u64),
190            destination: TonAddress::from_str("EQB3ncyBUTjZUA5EnFKR5_EnOMI9V1tTEAAPaiU71gc4TiUt")
191                .unwrap(),
192            response_destination: TonAddress::from_str(
193                "EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c",
194            )
195            .unwrap(),
196            custom_payload: None,
197            forward_ton_amount: BigUint::from(215000000u64),
198            forward_payload: Arc::new(
199                Cell::new(hex::decode(TRANSFER_PAYLOAD).unwrap(), 862, vec![], false).unwrap(),
200            ),
201            forward_payload_layout: EitherCellLayout::Native,
202        };
203
204        let result_cell = jetton_transfer_msg.build()?;
205
206        let result_boc_serialized = BagOfCells::from_root(result_cell).serialize(false).unwrap();
207        let expected_boc_serialized = hex::decode(JETTON_TRANSFER_MSG).unwrap();
208
209        assert_eq!(expected_boc_serialized, result_boc_serialized);
210        Ok(())
211    }
212
213    #[test]
214    fn test_jetton_transfer_builder_bad_forward_amount() -> Result<(), TonMessageError> {
215        let forward_payload =
216            Arc::new(CellBuilder::new().store_byte(123).unwrap().build().unwrap());
217
218        let mut jetton_transfer_msg = JettonTransferMessage::new(
219            &TonAddress::from_str("EQB3ncyBUTjZUA5EnFKR5_EnOMI9V1tTEAAPaiU71gc4TiUt").unwrap(),
220            &BigUint::from(300u32),
221        );
222
223        jetton_transfer_msg.with_forward_payload(BigUint::zero(), forward_payload.clone());
224        assert!(jetton_transfer_msg.build().is_err());
225
226        jetton_transfer_msg.with_forward_payload(BigUint::from(300u32), forward_payload.clone());
227        assert!(jetton_transfer_msg.build().is_ok());
228
229        jetton_transfer_msg.with_forward_payload(BigUint::zero(), EMPTY_ARC_CELL.clone());
230        assert!(jetton_transfer_msg.build().is_ok());
231
232        Ok(())
233    }
234}