tronic 0.5.2

A modular, async-first Rust client for the Tron blockchain.
Documentation
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use std::marker::PhantomData;

use alloy_primitives::U256;
use eyre::eyre;

use crate::{
    client::{
        Client,
        pending::{ActivationFeeCheck, PendingTransaction},
    },
    contracts::{AbiDecode, AbiEncode, token::Token},
    domain::{
        Message,
        address::TronAddress,
        contract::{Contract, TriggerSmartContract},
        transaction::Transaction,
        trx::Trx,
    },
    error::Error,
    provider::TronProvider,
    signer::PrehashSigner,
    trx,
};

use super::TryFromData;

#[allow(non_camel_case_types, non_snake_case)]
pub mod Trc20 {
    use crate::{
        contracts::{AbiDecode, AbiEncode},
        domain::address::TronAddress,
    };
    use alloy_primitives::U256;
    use alloy_sol_macro::sol;
    use alloy_sol_types::SolCall;

    sol! {
        #[derive(Debug)]
        contract Erc20 {
             /// Send amount of tokens from sender to recipient
            function transfer(address recipient, uint256 amount) external returns (bool);

            /// Check the token balance of a given account
            function balanceOf(address account) external view returns (uint256);

            /// Allow spender to withdraw from the sender's account multiple times
            function approve(address spender, uint256 amount) external returns (bool);

            /// Check the remaining amount spender is allowed to spend on behalf of owner
            function allowance(address owner, address spender) external view returns (uint256);

            /// Transfer tokens from one account to another, using the allowance
            function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

            /// Events to emit for Transfer and Approval actions
            event Transfer(address indexed from, address indexed to, uint256 value);
            event Approval(address indexed owner, address indexed spender, uint256 value);
        }
    }

    macro_rules! generate_trc20_structs {
        // Handle non-generic structs
        ($($name:ident { $($field:ident: $ty:ty),+ }),+) => {
            $(
                #[derive(Clone, Debug)]
                pub struct $name {
                    $(pub $field: $ty),+
                }
            )+
        };
        // Handle generic structs
        ($($name:ident<$generic:ident> { $($field:ident: $ty:ty),+ }),+) => {
            $(
                #[derive(Clone, Debug)]
                pub struct $name<$generic> {
                    $(pub $field: $ty),+
                }
            )+
        };
        // Mixed case (both generic and non-generic)
        ($(
            $name:ident $(<$generic:ident>)? { $($field:ident: $ty:ty),+ }
        ),+) => {
            $(
                #[derive(Clone, Debug)]
                pub struct $name $(<$generic>)? {
                    $(pub $field: $ty),+
                }
            )+
        };
    }

    // Usage with generic parameters
    generate_trc20_structs! {
        transferCall<T> { recipient: TronAddress, amount: T },
        balanceOfCall { account: TronAddress },
        approveCall<T> { spender: TronAddress, amount: T },
        allowanceCall { owner: TronAddress, spender: TronAddress },
        transferFromCall<T> { sender: TronAddress, recipient: TronAddress, amount: T }
    }

    // Macro for non-generic types
    macro_rules! impl_from_erc20 {
        ($trc20_type:ident, $erc20_type:path { $($field:ident),+ }) => {
            impl From<$erc20_type> for $trc20_type {
                fn from(call: $erc20_type) -> Self {
                    $trc20_type {
                        $($field: call.$field.into()),+
                    }
                }
            }
        };
    }

    // Macro for non-generic types (reverse direction)
    macro_rules! impl_from_trc20 {
        ($trc20_type:ident, $erc20_type:path { $($field:ident),+ }) => {
            impl From<$trc20_type> for $erc20_type {
                fn from(call: $trc20_type) -> Self {
                    $erc20_type {
                        $($field: call.$field.into()),+
                    }
                }
            }
        };
    }

    // Macro for generic types (ERC20 → TRC20)
    macro_rules! impl_from_erc20_generic {
        ($trc20_type:ident<$generic:ident>, $erc20_type:path { $($field:ident),+ }) => {
            impl<$generic: From<U256>> From<$erc20_type> for $trc20_type<$generic> {
                fn from(call: $erc20_type) -> Self {
                    $trc20_type {
                        $($field: call.$field.into()),+
                    }
                }
            }
        };
    }

    // Macro for generic types (TRC20 → ERC20)
    macro_rules! impl_from_trc20_generic {
        ($trc20_type:ident<$generic:ident>, $erc20_type:path { $($field:ident),+ }) => {
            impl<$generic: Into<U256>> From<$trc20_type<$generic>> for $erc20_type {
                fn from(call: $trc20_type<$generic>) -> Self {
                    $erc20_type {
                        $($field: call.$field.into()),+
                    }
                }
            }
        };
    }

    // Implement conversions for non-generic structs
    impl_from_erc20!(balanceOfCall, Erc20::balanceOfCall { account });
    impl_from_erc20!(allowanceCall, Erc20::allowanceCall { owner, spender });
    impl_from_trc20!(balanceOfCall, Erc20::balanceOfCall { account });
    impl_from_trc20!(allowanceCall, Erc20::allowanceCall { owner, spender });

    // Implement conversions for generic structs
    impl_from_erc20_generic!(
        transferCall<T>,
        Erc20::transferCall { recipient, amount }
    );
    impl_from_erc20_generic!(
        approveCall<T>,
        Erc20::approveCall { spender, amount }
    );
    impl_from_erc20_generic!(
        transferFromCall<T>,
        Erc20::transferFromCall {
            sender,
            recipient,
            amount
        }
    );

    impl_from_trc20_generic!(
        transferCall<T>,
        Erc20::transferCall { recipient, amount }
    );
    impl_from_trc20_generic!(
        approveCall<T>,
        Erc20::approveCall { spender, amount }
    );
    impl_from_trc20_generic!(
        transferFromCall<T>,
        Erc20::transferFromCall {
            sender,
            recipient,
            amount
        }
    );

    #[macro_export]
    macro_rules! impl_abi_encode_decode_new {
        // For non-generic types
        ($struct_name:ident, $sol_type:path) => {
            impl AbiEncode for $struct_name {
                fn encode(self) -> Vec<u8> {
                    let sol_type: $sol_type = self.into();
                    sol_type.abi_encode()
                }
            }

            impl AbiDecode for $struct_name {
                type Error = String;

                fn decode(data: &[u8]) -> Result<Self, Self::Error> {
                    <$sol_type>::abi_decode(data)
                        .map(|decoded| decoded.into())
                        .map_err(|e| format!("Failed to decode: {}", e))
                }
            }
        };

        // For generic types
        ($struct_name:ident<$generic:ident>, $sol_type:path) => {
            impl<$generic: Into<U256> + From<U256>> AbiEncode
                for $struct_name<$generic>
            {
                fn encode(self) -> Vec<u8> {
                    let sol_type: $sol_type = self.into();
                    sol_type.abi_encode()
                }
            }

            impl<$generic: Into<U256> + From<U256>> AbiDecode
                for $struct_name<$generic>
            {
                type Error = String;

                fn decode(data: &[u8]) -> Result<Self, Self::Error> {
                    <$sol_type>::abi_decode(data)
                        .map(|decoded| decoded.into())
                        .map_err(|e| format!("Failed to decode: {}", e))
                }
            }
        };
    }

    impl_abi_encode_decode_new!(transferCall<T>, Erc20::transferCall);
    impl_abi_encode_decode_new!(balanceOfCall, Erc20::balanceOfCall);
    impl_abi_encode_decode_new!(approveCall<T>, Erc20::approveCall);
    impl_abi_encode_decode_new!(allowanceCall, Erc20::allowanceCall);
    impl_abi_encode_decode_new!(transferFromCall<T>, Erc20::transferFromCall);
}

#[derive(Debug, Clone)]
pub struct Trc20Contract<T> {
    contract_address: TronAddress,
    _token: PhantomData<T>,
}

impl<T: Token> Trc20Contract<T> {
    pub fn new(contract_address: TronAddress) -> Self {
        Trc20Contract {
            contract_address,
            _token: Default::default(),
        }
    }

    pub fn address(&self) -> TronAddress {
        self.contract_address
    }

    /// Send amount of tokens from sender to recipient
    pub fn transfer(
        &self,
        recipient: TronAddress,
        amount: T,
    ) -> Trc20::transferCall<T> {
        Trc20::transferCall { recipient, amount }
    }

    /// Check the token balance of a given account
    pub fn balance_of(&self, account: TronAddress) -> Trc20::balanceOfCall {
        Trc20::balanceOfCall { account }
    }

    /// Allow spender to withdraw from the sender's account multiple times
    pub fn approve(
        &self,
        spender: TronAddress,
        amount: u64,
    ) -> Trc20::approveCall<T> {
        Trc20::approveCall {
            spender,
            amount: U256::from(amount).into(),
        }
    }

    /// Check the remaining amount spender is allowed to spend on behalf of owner
    pub fn allowance(
        &self,
        owner: TronAddress,
        spender: TronAddress,
    ) -> Trc20::allowanceCall {
        Trc20::allowanceCall { owner, spender }
    }

    /// Transfer tokens from one account to another, using the allowance
    pub fn transfer_from(
        &self,
        sender: TronAddress,
        recipient: TronAddress,
        amount: u64,
    ) -> Trc20::transferFromCall<T> {
        Trc20::transferFromCall {
            sender,
            recipient,
            amount: U256::from(amount).into(),
        }
    }
}

#[derive(Clone, Debug)]
pub enum Trc20Call<T> {
    BalanceOf(Trc20::balanceOfCall),
    Transfer(Trc20::transferCall<T>),
}

impl<T: Token> TryFromData for Trc20Call<T> {
    type Error = String;
    fn try_from_data(data: &[u8]) -> Result<Self, Self::Error> {
        if let Ok(call) = Trc20::transferCall::<T>::decode(data) {
            Ok(Trc20Call::Transfer(call))
        } else if let Ok(call) = Trc20::balanceOfCall::decode(data) {
            Ok(Trc20Call::BalanceOf(call))
        } else {
            Err("unknown call".into())
        }
    }
}

#[derive(bon::Builder)]
#[builder(start_fn = with_client)]
#[builder(finish_fn(vis = "", name = build_internal))]
pub struct Trc20Transfer<'a, P, S, T> {
    #[builder(start_fn)]
    pub(super) client: &'a Client<P, S>,
    pub(super) contract: Trc20Contract<T>,
    pub(super) to: TronAddress,
    pub(super) amount: T,
    pub(super) owner: Option<TronAddress>,
    pub(super) memo: Option<Message>,
    pub(super) call_value: Option<Trx>,
    pub(super) call_token_value: Option<Trx>,
    pub(super) token_id: Option<i64>,
    #[builder(default = true)]
    pub(super) can_spend_trx_for_fee: bool,
}

impl<'a, P, S, T, State: trc20_transfer_builder::IsComplete>
    Trc20TransferBuilder<'a, P, S, T, State>
where
    P: TronProvider,
    S: PrehashSigner,
    Error: From<S::Error>,
    T: Token,
{
    pub async fn build<M>(
        self,
    ) -> crate::Result<PendingTransaction<'a, P, S, M>> {
        let transfer = self.build_internal();
        let owner = transfer
            .owner
            .or_else(|| transfer.client.signer_address())
            .ok_or_else(|| Error::Unexpected(eyre!("missing owner address")))?;
        let call = transfer.contract.transfer(transfer.to, transfer.amount);
        let contract_address = transfer.contract.address();

        // Check balance
        {
            let balance = Trc20BalanceOf::with_client(transfer.client)
                .contract(transfer.contract)
                .owner(owner)
                .get()
                .await?;
            if transfer.amount > balance {
                return Err(Error::InsufficientTokenBalance {
                    balance: balance.into(),
                    need: transfer.amount.into(),
                    token: T::symbol(),
                });
            }
        }

        let latest_block = transfer.client.provider.get_now_block().await?;
        let transaction = Transaction::new(
            Contract {
                contract_type:
                    crate::domain::contract::ContractType::TriggerSmartContract(
                        TriggerSmartContract {
                            owner_address: owner,
                            contract_address,
                            call_value: transfer.call_value.unwrap_or_default(),
                            data: call.encode().into(),
                            call_token_value: transfer
                                .call_token_value
                                .unwrap_or_default(),
                            token_id: transfer.token_id.unwrap_or_default(),
                        },
                    ),
                ..Default::default()
            },
            &latest_block,
            transfer.memo.unwrap_or_default(),
        );
        let activation_checks = vec![ActivationFeeCheck {
            address: transfer.to,
            fee: trx!(0.1 TRX),
        }];
        PendingTransaction::new(
            transfer.client,
            transaction,
            owner,
            transfer.call_value.unwrap_or_default(),
            activation_checks,
            transfer.can_spend_trx_for_fee,
        )
        .await
    }
}

#[derive(bon::Builder)]
#[builder(start_fn = with_client)]
#[builder(finish_fn(vis = "", name = build_internal))]
pub struct Trc20BalanceOf<'a, P, S, T> {
    #[builder(start_fn)]
    pub(super) client: &'a Client<P, S>,
    pub(super) contract: Trc20Contract<T>,
    pub(super) owner: Option<TronAddress>,
}

impl<'a, P, S, T, State: trc20_balance_of_builder::IsComplete>
    Trc20BalanceOfBuilder<'a, P, S, T, State>
where
    P: TronProvider,
    S: PrehashSigner,
    T: Token,
{
    pub async fn get(self) -> crate::Result<T> {
        let balance_of = self.build_internal();
        let owner = balance_of
            .owner
            .or_else(|| balance_of.client.signer_address())
            .ok_or_else(|| {
                Error::Unexpected(eyre!(
                    "missing address to check trc20 balance for"
                ))
            })?;

        let call = balance_of.contract.balance_of(owner);
        let trigger = TriggerSmartContract {
            owner_address: owner,
            contract_address: balance_of.contract.address(),
            data: call.encode().into(),
            ..Default::default()
        };
        let mut extention = balance_of
            .client
            .provider()
            .trigger_constant_contract(trigger)
            .await?;
        let balance = if let Some(result) = extention.constant_result.pop() {
            if result.len() == 32 {
                let balance_bytes: [u8; 32] = result.try_into().unwrap(); // We sure in length
                alloy_primitives::U256::from_be_bytes(balance_bytes)
            } else {
                return Err(eyre!("unexpected constant result length").into());
            }
        } else {
            return Err(eyre!("no constant result returned",).into());
        };

        Ok(T::from(balance))
    }
}

pub trait Trc20Calls<P, S, T> {
    fn trc20_balance_of(&self) -> Trc20BalanceOfBuilder<'_, P, S, T>;
    fn trc20_transfer(&self) -> Trc20TransferBuilder<'_, P, S, T>;
}

impl<P, S, T> Trc20Calls<P, S, T> for Client<P, S> {
    fn trc20_balance_of(&self) -> Trc20BalanceOfBuilder<'_, P, S, T> {
        Trc20BalanceOf::with_client(self)
    }

    fn trc20_transfer(&self) -> Trc20TransferBuilder<'_, P, S, T> {
        Trc20Transfer::with_client(self)
    }
}