| 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 | |
| 6 | class SolanaTransactionInfo extends TransactionInfo { |
| 7 | SolanaTransactionInfo({ |
| 8 | required this.id, |
| 9 | required this.date, |
| 10 | required this.to, |
| 11 | required this.from, |
| 12 | required this.direction, |
| 13 | required this.amount, |
| 14 | required this.isPending, |
| 15 | required this.fee, |
| 16 | }); |
| 17 | |
| 18 | @override |
| 19 | final String id; |
| 20 | @override |
| 21 | final String? to; |
| 22 | @override |
| 23 | final String? from; |
| 24 | |
| 25 | @override |
| 26 | String get txHash => id.replaceFirst(RegExp(r'_(outgoing|incoming)$'), ''); |
| 27 | |
| 28 | @override |
| 29 | final Money amount; |
| 30 | @override |
| 31 | final bool isPending; |
| 32 | @override |
| 33 | final Money fee; |
| 34 | @override |
| 35 | final TransactionDirection direction; |
| 36 | @override |
| 37 | final DateTime date; |
| 38 | |
| 39 | factory SolanaTransactionInfo.fromJson(Map<String, dynamic> data) { |
| 40 | final symbol = data['tokenSymbol'] as String? ?? "SOL"; |
| 41 | final decimals = data['tokenDecimals'] as int? ?? 6; |
| 42 | |
| 43 | final currency = CryptoCurrency(name: symbol, title: symbol, decimals: decimals); |
| 44 | |
| 45 | return SolanaTransactionInfo( |
| 46 | id: data['id'] as String, |
| 47 | amount: Money.parse(data['solAmount'].toString(), currency), |
| 48 | direction: parseTransactionDirectionFromInt(data['direction'] as int), |
| 49 | date: DateTime.fromMillisecondsSinceEpoch(data['blockTime'] as int), |
| 50 | isPending: data['isPending'] as bool, |
| 51 | to: data['to'], |
| 52 | from: data['from'], |
| 53 | fee: Money.parse(data['txFee'], CryptoCurrency.sol), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | Map<String, dynamic> toJson() => { |
| 58 | 'id': id, |
| 59 | 'solAmount': amount.toString(), |
| 60 | 'direction': direction.index, |
| 61 | 'blockTime': date.millisecondsSinceEpoch, |
| 62 | 'isPending': isPending, |
| 63 | 'tokenSymbol': amount.currency.symbol, |
| 64 | 'tokenDecimals': amount.currency.decimals, |
| 65 | 'to': to, |
| 66 | 'from': from, |
| 67 | 'txFee': fee.toString(), |
| 68 | }; |
| 69 | } |