dev
dart 86 lines 2.29 KB
Raw
1 import 'package:cw_core/amount/money.dart';
2 import 'package:cw_core/crypto_currency.dart';
3 import 'package:cw_core/transaction_direction.dart';
4 import 'package:cw_core/transaction_info.dart';
5 import 'package:on_chain/tron/tron.dart';
6
7 class TronTransactionInfo extends TransactionInfo {
8 TronTransactionInfo({
9 required this.id,
10 required this.amount,
11 required this.fee,
12 required this.direction,
13 required this.blockTime,
14 required this.to,
15 required this.from,
16 required this.isPending,
17 });
18
19 @override
20 final String id;
21
22 @override
23 final String? to;
24
25 @override
26 final String? from;
27
28 @override
29 final Money amount;
30
31 @override
32 final Money? fee;
33
34 @override
35 final bool isPending;
36
37 @override
38 final TransactionDirection direction;
39
40 final DateTime blockTime;
41
42 factory TronTransactionInfo.fromJson(Map<String, dynamic> data) {
43 final tokenSymbol = data['tokenSymbol'] as String;
44 final decimals = data['decimals'] as int? ?? CryptoCurrency.trx.decimals;
45 final currency = CryptoCurrency(name: tokenSymbol, title: tokenSymbol, decimals: decimals);
46
47 return TronTransactionInfo(
48 id: data['id'] as String,
49 amount: Money(BigInt.parse(data['tronAmount']), currency),
50 fee: Money.tryParse(data['txFee']?.toString() ?? '0', CryptoCurrency.trx, isBaseUnit: true),
51 direction: parseTransactionDirectionFromInt(data['direction'] as int),
52 blockTime: DateTime.fromMillisecondsSinceEpoch(data['blockTime'] as int),
53 to: data['to'],
54 from: data['from'],
55 isPending: data['isPending'],
56 );
57 }
58
59 Map<String, dynamic> toJson() => {
60 'id': id,
61 'tronAmount': amount.amount.toString(),
62 'txFee': fee?.amount.toString(),
63 'direction': direction.index,
64 'blockTime': blockTime.millisecondsSinceEpoch,
65 'to': to,
66 'from': from,
67 'isPending': isPending,
68 'tokenSymbol': amount.currency.symbol,
69 'decimals': amount.currency.decimals
70 };
71
72 @override
73 DateTime get date => blockTime;
74
75 String _rawAmountAsString(BigInt amount) {
76 String formattedAmount = TronHelper.fromSun(amount);
77
78 if (formattedAmount.length >= 8) {
79 formattedAmount = formattedAmount.substring(0, 8);
80 }
81
82 return formattedAmount;
83 }
84
85 String rawTronAmount() => _rawAmountAsString(amount.amount);
86 }