| 1 | // ignore_for_file: overridden_fields, annotate_overrides |
| 2 | import 'package:cw_core/amount/money.dart'; |
| 3 | import 'package:cw_core/crypto_currency.dart'; |
| 4 | import 'package:cw_core/erc20_token.dart'; |
| 5 | import 'package:cw_core/format_amount.dart'; |
| 6 | import 'package:cw_core/transaction_direction.dart'; |
| 7 | import 'package:cw_core/transaction_info.dart'; |
| 8 | import 'package:cw_evm/evm_chain_registry.dart'; |
| 9 | import 'package:cw_evm/utils/evm_chain_utils.dart'; |
| 10 | |
| 11 | class EVMChainTransactionInfo extends TransactionInfo { |
| 12 | EVMChainTransactionInfo({ |
| 13 | required this.id, |
| 14 | required this.height, |
| 15 | required this.amount, |
| 16 | required this.fee, |
| 17 | required this.tokenSymbol, |
| 18 | this.exponent = 18, |
| 19 | required this.direction, |
| 20 | required this.isPending, |
| 21 | required this.date, |
| 22 | required this.confirmations, |
| 23 | required this.to, |
| 24 | required this.from, |
| 25 | this.evmSignatureName, |
| 26 | this.contractAddress, |
| 27 | required this.chainId, |
| 28 | }); |
| 29 | |
| 30 | final String id; |
| 31 | final int height; |
| 32 | final Money amount; |
| 33 | final int exponent; |
| 34 | final TransactionDirection direction; |
| 35 | final DateTime date; |
| 36 | final bool isPending; |
| 37 | final Money fee; |
| 38 | final int confirmations; |
| 39 | final String tokenSymbol; |
| 40 | String? _fiatAmount; |
| 41 | final String? to; |
| 42 | final String? from; |
| 43 | final String? evmSignatureName; |
| 44 | final String? contractAddress; |
| 45 | final int chainId; |
| 46 | |
| 47 | /// Get fee currency symbol based on wallet type |
| 48 | String get feeCurrency => EVMChainUtils.getFeeCurrency(chainId); |
| 49 | |
| 50 | @override |
| 51 | String fiatAmount() => _fiatAmount ?? ''; |
| 52 | |
| 53 | @override |
| 54 | void changeFiatAmount(String amount) => _fiatAmount = formatAmount(amount); |
| 55 | |
| 56 | factory EVMChainTransactionInfo.fromJson(Map<String, dynamic> data, int chainId) { |
| 57 | final decimals = data['exponent'] as int? ?? 18; |
| 58 | final tokenSymbol = data['tokenSymbol'] as String; |
| 59 | final currency = |
| 60 | Erc20Token(name: '', symbol: tokenSymbol, contractAddress: '', decimal: decimals); |
| 61 | |
| 62 | final feeCurrency = |
| 63 | EvmChainRegistry().getChainConfig(chainId)?.nativeCurrency ?? CryptoCurrency.eth; |
| 64 | |
| 65 | return EVMChainTransactionInfo( |
| 66 | id: data['id'] as String, |
| 67 | height: data['height'] as int, |
| 68 | amount: Money(BigInt.parse(data['amount'] as String), currency), |
| 69 | exponent: decimals, |
| 70 | fee: Money(BigInt.parse(data['fee'] as String), feeCurrency), |
| 71 | direction: TransactionDirection.values[data['direction'] as int], |
| 72 | date: DateTime.fromMillisecondsSinceEpoch(data['date'] as int), |
| 73 | isPending: data['isPending'] as bool? ?? false, |
| 74 | confirmations: data['confirmations'] as int, |
| 75 | tokenSymbol: tokenSymbol, |
| 76 | to: data['to'] as String?, |
| 77 | from: data['from'] as String?, |
| 78 | evmSignatureName: data['evmSignatureName'] as String?, |
| 79 | contractAddress: data['contractAddress'] as String?, |
| 80 | chainId: chainId, |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | Map<String, dynamic> toJson() => { |
| 85 | 'id': id, |
| 86 | 'height': height, |
| 87 | 'amount': amount.amount.toString(), |
| 88 | 'exponent': exponent, |
| 89 | 'fee': fee.amount.toString(), |
| 90 | 'direction': direction.index, |
| 91 | 'date': date.millisecondsSinceEpoch, |
| 92 | 'isPending': isPending, |
| 93 | 'confirmations': confirmations, |
| 94 | 'tokenSymbol': tokenSymbol, |
| 95 | 'to': to, |
| 96 | 'from': from, |
| 97 | 'evmSignatureName': evmSignatureName, |
| 98 | 'contractAddress': contractAddress, |
| 99 | 'chainId': chainId, |
| 100 | }; |
| 101 | } |