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 : final input =
90 3 : (contractData['data'] as String?)?.hexToBytes ?? Uint8List(0);
91 :
92 1 : final fee_bi = BigInt.tryParse(fee_s);
93 : final feeAmount =
94 0 : fee_bi != null ? Amount(value: fee_bi, decimals: 6) : null;
95 :
96 1 : final amount_bi = BigInt.tryParse(amount_s) ?? BigInt.zero;
97 1 : final amount = Amount(value: amount_bi, decimals: 6);
98 :
99 1 : final transferMethod = walletAddress == ownerAddress
100 : ? TransactionTransferMethod.send
101 : : TransactionTransferMethod.receive;
102 :
103 1 : return TronTransaction(
104 : hash: hash,
105 : block: block,
106 : confirmations: 0,
107 : timeMilli: timestamp,
108 : amount: amount,
109 : fee: feeAmount,
110 : decodedInput: null,
111 : gas: null,
112 : gasPrice: null,
113 : gasUsed: null,
114 : sender: ownerAddress,
115 : recipient: toAddress,
116 : transferMethod: transferMethod,
117 : token: token,
118 : status: ConfirmationStatus.confirmed, // TODO:
119 : input: input,
120 : );
121 : }
122 :
123 : ///
124 : /// TRX Transaction RPC
125 : ///
126 : if (json
127 : case {
128 2 : "txID": String hash,
129 2 : "net_usage": int _,
130 2 : "net_fee": int netFee,
131 2 : "energy_usage": int _,
132 2 : "energy_fee": int energyFee,
133 2 : "blockNumber": int block,
134 2 : "block_timestamp": int block_timestamp,
135 2 : "raw_data_hex": String raw_data_hex,
136 : }) {
137 1 : final fee = Amount(
138 2 : value: BigInt.from(netFee + energyFee),
139 : decimals: 6,
140 : );
141 :
142 1 : final rawData = raw_data_hex.hexToBytes;
143 1 : final Transaction_raw rawTx = Transaction_raw.fromBuffer(rawData);
144 :
145 2 : final contract = rawTx.contract.first;
146 1 : final contractType = contract.type;
147 :
148 : final TronContractData contractData;
149 :
150 : try {
151 2 : contractData = TronContractData.from(contractType, contract.parameter);
152 1 : } on UnsupportedError {
153 : return null;
154 : }
155 :
156 : return switch (contractData) {
157 2 : TronTransferContractData data => TronTransaction(
158 : hash: hash,
159 : block: block,
160 : confirmations: 1,
161 : timeMilli: block_timestamp,
162 1 : amount: Amount(
163 1 : value: data.amount,
164 1 : decimals: token.decimals,
165 : ),
166 : fee: fee,
167 1 : sender: data.from,
168 : gas: null,
169 : gasPrice: null,
170 : gasUsed: null,
171 : decodedInput: null,
172 1 : recipient: data.to,
173 2 : transferMethod: data.from == walletAddress
174 : ? TransactionTransferMethod.send
175 : : TransactionTransferMethod.receive,
176 : token: token,
177 : status: ConfirmationStatus.confirmed,
178 1 : input: Uint8List(0),
179 : ),
180 2 : TronTransferAssetContractData data => TronTransaction(
181 : hash: hash,
182 : block: block,
183 : confirmations: 1,
184 : timeMilli: block_timestamp,
185 1 : amount: Amount(
186 1 : value: data.amount,
187 1 : decimals: token.decimals,
188 : ),
189 : fee: fee,
190 1 : sender: data.from,
191 : gas: null,
192 : gasPrice: null,
193 : gasUsed: null,
194 : decodedInput: null,
195 1 : recipient: data.to,
196 2 : transferMethod: data.from == walletAddress
197 : ? TransactionTransferMethod.send
198 : : TransactionTransferMethod.receive,
199 : token: token,
200 : status: ConfirmationStatus.confirmed,
201 1 : input: Uint8List(0),
202 : ),
203 0 : _ => throw UnimplementedError(),
204 : };
205 : }
206 :
207 0 : throw UnimplementedError();
208 : }
209 :
210 0 : @override
211 : Map<String, dynamic> toJson() {
212 0 : return {
213 0 : 'hash': hash,
214 0 : 'block': block,
215 0 : 'confirmations': confirmations,
216 0 : 'timeMilli': timeMilli,
217 0 : 'amount': amount.toJson(),
218 0 : 'fee': fee?.toJson(),
219 0 : 'sender': sender,
220 0 : 'recipient': recipient,
221 0 : 'transferMethod': transferMethod.index,
222 0 : 'status': status.index,
223 0 : 'input': input.toHex,
224 0 : 'token': token.toJson(),
225 : };
226 : }
227 : }
|