cake_wallet/cw_ethereum/lib/ethereum_transaction_info.dart

75 lines
2.2 KiB
Dart
Raw Normal View History

import 'package:cw_core/format_amount.dart';
import 'package:cw_core/transaction_direction.dart';
2022-12-28 15:02:04 +00:00
import 'package:cw_core/transaction_info.dart';
class EthereumTransactionInfo extends TransactionInfo {
EthereumTransactionInfo({
required this.id,
required this.height,
2023-07-22 00:39:10 +00:00
required this.ethAmount,
required this.ethFee,
this.tokenSymbol = "ETH",
this.exponent = 18,
required this.direction,
required this.isPending,
required this.date,
required this.confirmations,
2023-07-22 00:39:10 +00:00
}) : this.amount = ethAmount.toInt(),
this.fee = ethFee.toInt();
final String id;
final int height;
final int amount;
2023-07-22 00:39:10 +00:00
final BigInt ethAmount;
final int exponent;
final TransactionDirection direction;
final DateTime date;
final bool isPending;
final int fee;
2023-07-22 00:39:10 +00:00
final BigInt ethFee;
final int confirmations;
final String tokenSymbol;
String? _fiatAmount;
2022-12-28 15:02:04 +00:00
@override
String amountFormatted() =>
2023-07-22 00:39:10 +00:00
'${formatAmount((ethAmount / BigInt.from(10).pow(exponent)).toString())} $tokenSymbol';
2022-12-28 15:02:04 +00:00
@override
String fiatAmount() => _fiatAmount ?? '';
2022-12-28 15:02:04 +00:00
@override
void changeFiatAmount(String amount) => _fiatAmount = formatAmount(amount);
2022-12-28 15:02:04 +00:00
@override
2023-07-22 00:39:10 +00:00
String feeFormatted() => '${(ethFee / BigInt.from(10).pow(18)).toString()} ETH';
factory EthereumTransactionInfo.fromJson(Map<String, dynamic> data) {
return EthereumTransactionInfo(
2023-07-21 15:37:33 +00:00
id: data['id'] as String,
height: data['height'] as int,
2023-07-22 00:39:10 +00:00
ethAmount: data['amount'] as BigInt,
2023-07-21 15:37:33 +00:00
exponent: data['exponent'] as int,
2023-07-22 00:39:10 +00:00
ethFee: data['fee'] as BigInt,
2023-07-21 15:37:33 +00:00
direction: parseTransactionDirectionFromInt(data['direction'] as int),
date: DateTime.fromMillisecondsSinceEpoch(data['date'] as int),
isPending: data['isPending'] as bool,
confirmations: data['confirmations'] as int,
tokenSymbol: data['tokenSymbol'] as String,
);
2022-12-28 15:02:04 +00:00
}
2023-07-21 15:37:33 +00:00
Map<String, dynamic> toJson() => {
'id': id,
'height': height,
2023-07-22 00:39:10 +00:00
'amount': ethAmount,
2023-07-21 15:37:33 +00:00
'exponent': exponent,
2023-07-22 00:39:10 +00:00
'fee': ethFee,
2023-07-21 15:37:33 +00:00
'direction': direction.index,
'date': date.millisecondsSinceEpoch,
'isPending': isPending,
'confirmations': confirmations,
'tokenSymbol': tokenSymbol,
};
2022-12-28 15:02:04 +00:00
}