tronic 0.5.0

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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
use std::collections::HashMap;

use derivative::Derivative;
use serde::Deserialize;
use serde::Serialize;
use time::OffsetDateTime;

use super::account::AccountType;
use crate::domain::{address::TronAddress, permission::Permission, trx::Trx};

use super::HexMessage;
use super::Message;

pub mod methods;

#[derive(Default, Debug, Clone, PartialEq)]
#[repr(i32)]
pub enum ContractType {
    AccountCreateContract(AccountCreateContract) = 0,
    TransferContract(TransferContract) = 1,
    TransferAssetContract(TransferAssetContract) = 2,
    VoteAssetContract = 3,
    VoteWitnessContract(VoteWitnessContract) = 4,
    WitnessCreateContract(WitnessCreateContract) = 5,
    AssetIssueContract(AssetIssueContract) = 6,
    WitnessUpdateContract(WitnessUpdateContract) = 8,
    ParticipateAssetIssueContract(ParticipateAssetIssueContract) = 9,
    AccountUpdateContract(AccountUpdateContract) = 10,
    FreezeBalanceContract(FreezeBalanceContract) = 11,
    UnfreezeBalanceContract(UnfreezeBalanceContract) = 12,
    WithdrawBalanceContract(WithdrawBalanceContract) = 13,
    UnfreezeAssetContract(UnfreezeAssetContract) = 14,
    UpdateAssetContract(UpdateAssetContract) = 15,
    ProposalCreateContract(ProposalCreateContract) = 16,
    ProposalApproveContract(ProposalApproveContract) = 17,
    ProposalDeleteContract(ProposalDeleteContract) = 18,
    SetAccountIdContract(SetAccountIdContract) = 19,
    #[default]
    CustomContract = 20,
    CreateSmartContract(CreateSmartContract) = 30,
    TriggerSmartContract(TriggerSmartContract) = 31,
    GetContract = 32,
    UpdateSettingContract(UpdateSettingContract) = 33,
    ExchangeCreateContract(ExchangeCreateContract) = 41,
    ExchangeInjectContract(ExchangeInjectContract) = 42,
    ExchangeWithdrawContract(ExchangeWithdrawContract) = 43,
    ExchangeTransactionContract(ExchangeTransactionContract) = 44,
    UpdateEnergyLimitContract(UpdateEnergyLimitContract) = 45,
    AccountPermissionUpdateContract(AccountPermissionUpdateContract) = 46,
    ClearAbiContract(ClearAbiContract) = 48,
    UpdateBrokerageContract(UpdateBrokerageContract) = 49,
    ShieldedTransferContract(ShieldedTransferContract) = 51,
    MarketSellAssetContract(MarketSellAssetContract) = 52,
    MarketCancelOrderContract(MarketCancelOrderContract) = 53,
    FreezeBalanceV2Contract(FreezeBalanceV2Contract) = 54,
    UnfreezeBalanceV2Contract(UnfreezeBalanceV2Contract) = 55,
    WithdrawExpireUnfreezeContract(WithdrawExpireUnfreezeContract) = 56,
    DelegateResourceContract(DelegateResourceContract) = 57,
    UnDelegateResourceContract(UnDelegateResourceContract) = 58,
    CancelAllUnfreezeV2Contract(CancelAllUnfreezeV2Contract) = 59,
}

impl ContractType {
    #[must_use]
    pub const fn id(&self) -> i32 {
        // SAFETY: This is safe because `Foo` is repr(i32).
        #[allow(unsafe_code)]
        unsafe {
            *std::ptr::from_ref::<Self>(self).cast::<i32>()
        }
    }
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct Contract {
    pub contract_type: ContractType,
    pub provider: Vec<u8>,
    pub contract_name: Message,
    pub permission_id: i32,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct TriggerSmartContract {
    pub owner_address: TronAddress,
    pub contract_address: TronAddress,
    pub call_value: Trx,
    pub data: HexMessage,
    pub call_token_value: Trx,
    pub token_id: i64,
}

#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct FrozenSupply {
    pub frozen_amount: Trx,
    pub frozen_days: i64,
}

#[derive(Derivative, Debug, Clone, PartialEq)]
#[derivative(Default)]
pub struct AssetIssueContract {
    pub id: String,
    pub owner_address: TronAddress,
    pub name: Message,
    pub abbr: Message,
    pub total_supply: i64,
    pub frozen_supply: Vec<FrozenSupply>,
    pub trx_num: Trx,
    pub precision: i32,
    pub num: i32,
    #[derivative(Default(value = "OffsetDateTime::UNIX_EPOCH"))]
    pub start_time: OffsetDateTime,
    #[derivative(Default(value = "OffsetDateTime::UNIX_EPOCH"))]
    pub end_time: OffsetDateTime,
    /// useless
    pub order: i64,
    pub vote_score: i32,
    pub description: Message,
    pub url: Message,
    pub free_asset_net_limit: i64,
    pub public_free_asset_net_limit: i64,
    pub public_free_asset_net_usage: i64,
    #[derivative(Default(value = "OffsetDateTime::UNIX_EPOCH"))]
    pub public_latest_free_net_time: OffsetDateTime,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct TransferAssetContract {
    /// this field is token name before the proposal ALLOW_SAME_TOKEN_NAME is active, otherwise it is token id and token is should be in string format.
    pub asset_name: Message,
    pub owner_address: TronAddress,
    pub to_address: TronAddress,
    pub amount: Trx,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct UnfreezeAssetContract {
    pub owner_address: TronAddress,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct UpdateAssetContract {
    pub owner_address: TronAddress,
    pub description: Message,
    pub url: Message,
    pub new_limit: i64,        // Not TRX
    pub new_public_limit: i64, // Not TRX
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ParticipateAssetIssueContract {
    pub owner_address: TronAddress,
    pub to_address: TronAddress,
    /// this field is token name before the proposal ALLOW_SAME_TOKEN_NAME is active, otherwise it is token id and token is should be in string format.
    pub asset_name: Message,
    /// the amount of drops
    pub amount: Trx,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct AccountCreateContract {
    pub owner_address: TronAddress,
    pub account_address: TronAddress,
    pub account_type: AccountType,
}

// Update account name. Account name is not unique now.
#[derive(Default, Debug, Clone, PartialEq)]
pub struct AccountUpdateContract {
    pub account_name: Message,
    pub owner_address: TronAddress,
}

// Set account id if the account has no id. Account id is unique and case insensitive.
#[derive(Default, Debug, Clone, PartialEq)]
pub struct SetAccountIdContract {
    pub account_id: Message,
    pub owner_address: TronAddress,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct AccountPermissionUpdateContract {
    pub owner_address: TronAddress,
    /// Empty is invalidate
    pub owner: Option<Permission>,
    /// Can be empty
    pub witness: Option<Permission>,
    /// Empty is invalidate
    pub actives: Vec<Permission>,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct WitnessCreateContract {
    pub owner_address: TronAddress,
    pub url: Message,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct WitnessUpdateContract {
    pub owner_address: TronAddress,
    pub update_url: Message,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct Vote {
    pub vote_address: TronAddress,
    pub vote_count: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct VoteWitnessContract {
    pub owner_address: TronAddress,
    pub votes: Vec<Vote>,
    pub support: bool,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct FreezeBalanceContract {
    pub owner_address: TronAddress,
    pub frozen_balance: Trx,
    pub frozen_duration: time::Duration,
    pub resource: ResourceCode,
    pub receiver_address: TronAddress,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct UnfreezeBalanceContract {
    pub owner_address: TronAddress,
    pub resource: ResourceCode,
    pub receiver_address: TronAddress,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct WithdrawBalanceContract {
    pub owner_address: TronAddress,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct TransferContract {
    pub owner_address: TronAddress,
    pub to_address: TronAddress,
    pub amount: Trx,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct TransactionBalanceTrace {
    pub transaction_identifier: HexMessage,
    pub operation: Vec<Operation>,
    pub r#type: String,
    pub status: String,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct Operation {
    pub operation_identifier: i64,
    pub address: TronAddress,
    pub amount: Trx,
}

#[derive(Derivative, Debug, Clone, PartialEq)]
#[derivative(Default)]
pub struct BlockBalanceTrace {
    pub block_identifier: BlockIdentifier,
    #[derivative(Default(value = "OffsetDateTime::UNIX_EPOCH"))]
    pub timestamp: OffsetDateTime,
    ///   BlockIdentifier parent_block_identifier = 4;
    pub transaction_balance_trace: Vec<TransactionBalanceTrace>,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct BlockIdentifier {
    pub hash: HexMessage,
    pub number: i64,
}

#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct AccountTrace {
    pub balance: Trx,
    pub placeholder: Trx,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct AccountIdentifier {
    pub address: TronAddress,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct AccountBalanceRequest {
    pub account_identifier: AccountIdentifier,
    pub block_identifier: BlockIdentifier,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct AccountBalanceResponse {
    pub balance: Trx,
    pub block_identifier: BlockIdentifier,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct FreezeBalanceV2Contract {
    pub owner_address: TronAddress,
    pub frozen_balance: Trx,
    pub resource: ResourceCode,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct UnfreezeBalanceV2Contract {
    pub owner_address: TronAddress,
    pub unfreeze_balance: Trx,
    pub resource: ResourceCode,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct WithdrawExpireUnfreezeContract {
    pub owner_address: TronAddress,
}

#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub enum ResourceCode {
    #[default]
    Bandwidth = 0,
    Energy = 1,
    #[doc = "Deprecated in FreezeV2. Use `tron_power` field instead."]
    TronPower = 2,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct DelegateResourceContract {
    pub owner_address: TronAddress,
    pub resource: ResourceCode,
    pub balance: Trx,
    pub receiver_address: TronAddress,
    pub lock_period: Option<time::Duration>,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct UnDelegateResourceContract {
    pub owner_address: TronAddress,
    pub resource: ResourceCode,
    pub balance: Trx,
    pub receiver_address: TronAddress,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct CancelAllUnfreezeV2Contract {
    pub owner_address: TronAddress,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ProposalApproveContract {
    pub owner_address: TronAddress,
    pub proposal_id: i64,
    /// add or remove approval
    pub is_add_approval: bool,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ProposalCreateContract {
    pub owner_address: TronAddress,
    pub parameters: HashMap<i64, i64>,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ProposalDeleteContract {
    pub owner_address: TronAddress,
    pub proposal_id: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct BuyStorageBytesContract {
    pub owner_address: TronAddress,
    /// storage bytes for buy
    pub bytes: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct BuyStorageContract {
    pub owner_address: TronAddress,
    /// trx quantity for buy storage (sun)
    pub quant: Trx,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct SellStorageContract {
    pub owner_address: TronAddress,
    pub storage_bytes: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct UpdateBrokerageContract {
    pub owner_address: TronAddress,
    /// 1 mean 1%
    pub brokerage: i32,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ExchangeCreateContract {
    pub owner_address: TronAddress,
    pub first_token_id: Message,
    pub first_token_balance: i64,
    pub second_token_id: Message,
    pub second_token_balance: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ExchangeInjectContract {
    pub owner_address: TronAddress,
    pub exchange_id: i64,
    pub token_id: Message,
    pub quant: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ExchangeWithdrawContract {
    pub owner_address: TronAddress,
    pub exchange_id: i64,
    pub token_id: Message,
    pub quant: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ExchangeTransactionContract {
    pub owner_address: TronAddress,
    pub exchange_id: i64,
    pub token_id: Message,
    pub quant: i64,
    pub expected: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct MarketSellAssetContract {
    pub owner_address: TronAddress,
    pub sell_token_id: Message,
    pub sell_token_quantity: i64,
    pub buy_token_id: Message,
    /// min to receive
    pub buy_token_quantity: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct MarketCancelOrderContract {
    pub owner_address: TronAddress,
    pub order_id: HexMessage,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct SmartContract {
    pub origin_address: TronAddress,
    pub contract_address: TronAddress,
    pub abi: Abi,
    pub bytecode: Vec<u8>,
    pub call_value: Trx,
    pub consume_user_resource_percent: i64,
    pub name: String,
    pub origin_energy_limit: i64,
    pub code_hash: HexMessage,
    pub trx_hash: HexMessage,
    pub version: i32,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct Abi {
    pub entrys: Vec<Entry>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Entry {
    #[serde(default)]
    pub anonymous: bool,
    #[serde(default)]
    pub constant: bool,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub inputs: Vec<Param>,
    #[serde(default)]
    pub outputs: Vec<Param>,
    #[serde(rename = "type")]
    pub entry_type: EntryType,
    #[serde(default)]
    pub payable: bool,
    #[serde(default)]
    pub state_mutability: StateMutabilityType,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Param {
    #[serde(default)]
    pub indexed: bool,
    #[serde(default)]
    pub name: String,
    /// SolidityType type = 3;
    #[serde(rename = "type")]
    pub param_type: String,
}

#[derive(
    Default,
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PartialOrd,
    Ord,
    Serialize,
    Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum EntryType {
    #[default]
    UnknownEntryType = 0,
    Constructor = 1,
    Function = 2,
    Event = 3,
    Fallback = 4,
    Receive = 5,
    Error = 6,
}

#[derive(
    Default,
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    PartialOrd,
    Ord,
    Serialize,
    Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum StateMutabilityType {
    #[default]
    UnknownMutabilityType = 0,
    Pure = 1,
    View = 2,
    Nonpayable = 3,
    Payable = 4,
}

#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct ContractState {
    pub energy_usage: i64,
    pub energy_factor: i64,
    pub update_cycle: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct CreateSmartContract {
    pub owner_address: TronAddress,
    pub new_contract: SmartContract,
    pub call_token_value: Trx,
    pub token_id: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ClearAbiContract {
    pub owner_address: TronAddress,
    pub contract_address: TronAddress,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct UpdateSettingContract {
    pub owner_address: TronAddress,
    pub contract_address: TronAddress,
    pub consume_user_resource_percent: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct UpdateEnergyLimitContract {
    pub owner_address: TronAddress,
    pub contract_address: TronAddress,
    pub origin_energy_limit: i64,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct SpendDescription {
    pub value_commitment: Vec<u8>,
    /// merkle root
    pub anchor: Vec<u8>,
    /// used for check double spend
    pub nullifier: Vec<u8>,
    /// used for check spend authority signature
    pub rk: Vec<u8>,
    pub zkproof: Vec<u8>,
    pub spend_authority_signature: Vec<u8>,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ReceiveDescription {
    pub value_commitment: Vec<u8>,
    pub note_commitment: Vec<u8>,
    /// for Encryption
    pub epk: Vec<u8>,
    /// Encryption for incoming, decrypt it with ivk
    pub c_enc: Vec<u8>,
    /// Encryption for audit, decrypt it with ovk
    pub c_out: Vec<u8>,
    pub zkproof: Vec<u8>,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ShieldedTransferContract {
    /// transparent address
    pub transparent_from_address: TronAddress,
    pub from_amount: i64,
    pub spend_description: Vec<SpendDescription>,
    pub receive_description: Vec<ReceiveDescription>,
    pub binding_signature: Vec<u8>,
    /// transparent address
    pub transparent_to_address: TronAddress,
    /// the amount to transparent to_address
    pub to_amount: i64,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContractArtifact {
    pub contract_name: String,
    pub abi: Vec<Entry>,
    pub bytecode: String,
    pub deployed_bytecode: String,
    pub source_map: Option<String>,
    pub deployed_source_map: Option<String>,
    pub source: Option<String>,
    pub source_path: Option<String>,
    pub schema_version: String,
    pub updated_at: String,
}