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