Line data Source code
1 : part of '../../../domain/entities/generic_transaction.dart';
2 :
3 : base class TronTransaction extends EVMTransaction {
4 1 : const TronTransaction({
5 : required super.hash,
6 : required super.block,
7 : required super.confirmations,
8 : required super.timeMilli,
9 : required super.amount,
10 : required super.fee,
11 : required super.sender,
12 : required super.recipient,
13 : required super.transferMethod,
14 : required super.token,
15 : required super.status,
16 : required super.gasPrice,
17 : required super.gasUsed,
18 : required super.gas,
19 : required super.decodedInput,
20 : required super.input,
21 : });
22 :
23 1 : static TronTransaction? fromJson(
24 : Json json,
25 : CoinEntity token,
26 : String walletAddress,
27 : ) {
28 : /// TRC20 Transaction TRONSCAN
29 : if (json
30 : case {
31 2 : "hash": String hash,
32 2 : "from": String from,
33 2 : "to": String to,
34 2 : "amount": String value_s,
35 2 : "block_timestamp": int block_timestamp,
36 2 : "block": int block,
37 2 : "confirmed": int confirmed,
38 2 : "direction": int direction,
39 : }) {
40 1 : final value = BigInt.parse(value_s);
41 2 : final amount = Amount(value: value, decimals: token.decimals);
42 :
43 1 : return TronTransaction(
44 : token: token,
45 : hash: hash,
46 : block: block,
47 : timeMilli: block_timestamp,
48 : recipient: to,
49 : sender: from,
50 : decodedInput: null,
51 : gas: null,
52 : gasPrice: null,
53 : gasUsed: null,
54 : amount: amount,
55 : fee: null,
56 : transferMethod: switch (direction) {
57 1 : 2 => TransactionTransferMethod.receive,
58 1 : 1 => TransactionTransferMethod.send,
59 : _ => TransactionTransferMethod.unknown,
60 : },
61 : confirmations: 0,
62 : status: switch (confirmed) {
63 1 : 1 => ConfirmationStatus.confirmed,
64 0 : 0 => ConfirmationStatus.pending,
65 : _ => ConfirmationStatus.failed,
66 : },
67 1 : input: Uint8List(0),
68 : );
69 : }
70 :
71 : /// TRX Transaction TRONSCAN
72 : if (json
73 : case {
74 2 : "block": int block,
75 2 : "hash": String hash,
76 2 : "ownerAddress": String ownerAddress,
77 2 : "toAddress": String toAddress,
78 2 : "fee": String fee_s,
79 2 : "amount": String amount_s,
80 2 : "timestamp": int timestamp,
81 2 : "confirmed": bool _,
82 2 : "riskTransaction": bool riskTransaction,
83 2 : "contractData": JSON contractData,
84 : }) {
85 : if (riskTransaction) {
86 : return null;
87 : }
88 :
89 3 : final input = (contractData['data'] as String?)?.hexToBytes ?? Uint8List(0);
90 :
91 1 : final fee_bi = BigInt.tryParse(fee_s);
92 0 : final feeAmount = fee_bi != null ? Amount(value: fee_bi, decimals: 6) : null;
93 :
94 1 : final amount_bi = BigInt.tryParse(amount_s) ?? BigInt.zero;
95 1 : final amount = Amount(value: amount_bi, decimals: 6);
96 :
97 1 : final transferMethod = walletAddress == ownerAddress
98 : ? TransactionTransferMethod.send
99 : : TransactionTransferMethod.receive;
100 :
101 1 : return TronTransaction(
102 : hash: hash,
103 : block: block,
104 : confirmations: 0,
105 : timeMilli: timestamp,
106 : amount: amount,
107 : fee: feeAmount,
108 : decodedInput: null,
109 : gas: null,
110 : gasPrice: null,
111 : gasUsed: null,
112 : sender: ownerAddress,
113 : recipient: toAddress,
114 : transferMethod: transferMethod,
115 : token: token,
116 : status: ConfirmationStatus.confirmed, // TODO:
117 : input: input,
118 : );
119 : }
120 :
121 : ///
122 : /// TRX Transaction RPC
123 : ///
124 : if (json
125 : case {
126 2 : "txID": String hash,
127 2 : "net_usage": int _,
128 2 : "net_fee": int netFee,
129 2 : "energy_usage": int _,
130 2 : "energy_fee": int energyFee,
131 2 : "blockNumber": int block,
132 2 : "block_timestamp": int block_timestamp,
133 2 : "raw_data_hex": String raw_data_hex,
134 : }) {
135 1 : final fee = Amount(
136 2 : value: BigInt.from(netFee + energyFee),
137 : decimals: 6,
138 : );
139 :
140 1 : final rawData = raw_data_hex.hexToBytes;
141 1 : final Transaction_raw rawTx = Transaction_raw.fromBuffer(rawData);
142 :
143 2 : final contract = rawTx.contract.first;
144 1 : final contractType = contract.type;
145 :
146 : final TronContractData contractData;
147 :
148 : try {
149 2 : contractData = TronContractData.from(contractType, contract.parameter);
150 1 : } on UnsupportedError {
151 : return null;
152 : }
153 :
154 : return switch (contractData) {
155 2 : TronTransferContractData data => TronTransaction(
156 : hash: hash,
157 : block: block,
158 : confirmations: 1,
159 : timeMilli: block_timestamp,
160 1 : amount: Amount(
161 1 : value: data.amount,
162 1 : decimals: token.decimals,
163 : ),
164 : fee: fee,
165 1 : sender: data.from,
166 : gas: null,
167 : gasPrice: null,
168 : gasUsed: null,
169 : decodedInput: null,
170 1 : recipient: data.to,
171 2 : transferMethod: data.from == walletAddress
172 : ? TransactionTransferMethod.send
173 : : TransactionTransferMethod.receive,
174 : token: token,
175 : status: ConfirmationStatus.confirmed,
176 1 : input: Uint8List(0),
177 : ),
178 2 : TronTransferAssetContractData data => TronTransaction(
179 : hash: hash,
180 : block: block,
181 : confirmations: 1,
182 : timeMilli: block_timestamp,
183 1 : amount: Amount(
184 1 : value: data.amount,
185 1 : decimals: token.decimals,
186 : ),
187 : fee: fee,
188 1 : sender: data.from,
189 : gas: null,
190 : gasPrice: null,
191 : gasUsed: null,
192 : decodedInput: null,
193 1 : recipient: data.to,
194 2 : transferMethod: data.from == walletAddress
195 : ? TransactionTransferMethod.send
196 : : TransactionTransferMethod.receive,
197 : token: token,
198 : status: ConfirmationStatus.confirmed,
199 1 : input: Uint8List(0),
200 : ),
201 0 : _ => throw UnimplementedError(),
202 : };
203 : }
204 :
205 0 : throw UnimplementedError();
206 : }
207 :
208 0 : @override
209 : Map<String, dynamic> toJson() {
210 0 : return {
211 0 : 'hash': hash,
212 0 : 'block': block,
213 0 : 'confirmations': confirmations,
214 0 : 'timeMilli': timeMilli,
215 0 : 'amount': amount.toJson(),
216 0 : 'fee': fee?.toJson(),
217 0 : 'sender': sender,
218 0 : 'recipient': recipient,
219 0 : 'transferMethod': transferMethod.index,
220 0 : 'status': status.index,
221 0 : 'input': input.toHex,
222 0 : 'token': token.toJson(),
223 : };
224 : }
225 : }
|