2023-12-02 02:26:43 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2023-08-04 17:01:49 +00:00
|
|
|
import 'package:cw_core/format_amount.dart';
|
|
|
|
import 'package:cw_core/transaction_direction.dart';
|
|
|
|
import 'package:cw_core/transaction_info.dart';
|
|
|
|
|
|
|
|
class EthereumTransactionInfo extends TransactionInfo {
|
|
|
|
EthereumTransactionInfo({
|
|
|
|
required this.id,
|
|
|
|
required this.height,
|
|
|
|
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-09-14 19:12:41 +00:00
|
|
|
required this.to,
|
2023-08-04 17:01:49 +00:00
|
|
|
}) : this.amount = ethAmount.toInt(),
|
|
|
|
this.fee = ethFee.toInt();
|
|
|
|
|
|
|
|
final String id;
|
|
|
|
final int height;
|
|
|
|
final int amount;
|
|
|
|
final BigInt ethAmount;
|
|
|
|
final int exponent;
|
|
|
|
final TransactionDirection direction;
|
|
|
|
final DateTime date;
|
|
|
|
final bool isPending;
|
|
|
|
final int fee;
|
|
|
|
final BigInt ethFee;
|
|
|
|
final int confirmations;
|
|
|
|
final String tokenSymbol;
|
|
|
|
String? _fiatAmount;
|
2023-09-14 19:12:41 +00:00
|
|
|
final String? to;
|
2023-08-04 17:01:49 +00:00
|
|
|
|
|
|
|
@override
|
2023-12-02 02:26:43 +00:00
|
|
|
String amountFormatted() {
|
|
|
|
final amount = formatAmount((ethAmount / BigInt.from(10).pow(exponent)).toString());
|
|
|
|
return '${amount.substring(0, min(10, amount.length))} $tokenSymbol';
|
|
|
|
}
|
2023-08-04 17:01:49 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String fiatAmount() => _fiatAmount ?? '';
|
|
|
|
|
|
|
|
@override
|
|
|
|
void changeFiatAmount(String amount) => _fiatAmount = formatAmount(amount);
|
|
|
|
|
|
|
|
@override
|
2023-12-02 02:26:43 +00:00
|
|
|
String feeFormatted() {
|
|
|
|
final amount = (ethFee / BigInt.from(10).pow(18)).toString();
|
|
|
|
return '${amount.substring(0, min(10, amount.length))} ETH';
|
|
|
|
}
|
2023-08-04 17:01:49 +00:00
|
|
|
|
|
|
|
factory EthereumTransactionInfo.fromJson(Map<String, dynamic> data) {
|
|
|
|
return EthereumTransactionInfo(
|
|
|
|
id: data['id'] as String,
|
|
|
|
height: data['height'] as int,
|
|
|
|
ethAmount: BigInt.parse(data['amount']),
|
|
|
|
exponent: data['exponent'] as int,
|
|
|
|
ethFee: BigInt.parse(data['fee']),
|
|
|
|
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,
|
2023-09-14 19:12:41 +00:00
|
|
|
to: data['to'],
|
2023-08-04 17:01:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
'id': id,
|
|
|
|
'height': height,
|
|
|
|
'amount': ethAmount.toString(),
|
|
|
|
'exponent': exponent,
|
|
|
|
'fee': ethFee.toString(),
|
|
|
|
'direction': direction.index,
|
|
|
|
'date': date.millisecondsSinceEpoch,
|
|
|
|
'isPending': isPending,
|
|
|
|
'confirmations': confirmations,
|
|
|
|
'tokenSymbol': tokenSymbol,
|
2023-09-14 19:12:41 +00:00
|
|
|
'to': to,
|
2023-08-04 17:01:49 +00:00
|
|
|
};
|
|
|
|
}
|