start using Amount

This commit is contained in:
julian 2023-03-27 08:41:59 -06:00
parent 7b353be350
commit 4bd87e8dce
3 changed files with 64 additions and 57 deletions

View file

@ -1,5 +1,8 @@
import 'dart:convert'; import 'dart:convert';
import 'package:stackwallet/utilities/amount.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
class EthTokenTxExtraDTO { class EthTokenTxExtraDTO {
EthTokenTxExtraDTO({ EthTokenTxExtraDTO({
required this.blockHash, required this.blockHash,
@ -27,20 +30,16 @@ class EthTokenTxExtraDTO {
timestamp: map['timestamp'] as int, timestamp: map['timestamp'] as int,
from: map['from'] as String, from: map['from'] as String,
to: map['to'] as String, to: map['to'] as String,
value: int.parse(map['value'] as String), value: Amount(
gas: map['gas'] as int, rawValue: BigInt.parse(map['value'] as String),
gasPrice: map['gasPrice'] as int, fractionDigits: Coin.ethereum.decimals,
),
gas: _amountFromJsonNum(map['gas']),
gasPrice: _amountFromJsonNum(map['gasPrice']),
nonce: map['nonce'] as int, nonce: map['nonce'] as int,
input: map['input'] as String, input: map['input'] as String,
gasCost: map['gasCost'] as int, gasCost: _amountFromJsonNum(map['gasCost']),
gasUsed: map['gasUsed'] as int, gasUsed: _amountFromJsonNum(map['gasUsed']),
);
factory EthTokenTxExtraDTO.fromJsonString(String jsonString) =>
EthTokenTxExtraDTO.fromMap(
Map<String, dynamic>.from(
jsonDecode(jsonString) as Map,
),
); );
final String hash; final String hash;
@ -50,13 +49,20 @@ class EthTokenTxExtraDTO {
final int timestamp; final int timestamp;
final String from; final String from;
final String to; final String to;
final int value; final Amount value;
final int gas; final Amount gas;
final int gasPrice; final Amount gasPrice;
final String input; final String input;
final int nonce; final int nonce;
final int gasCost; final Amount gasCost;
final int gasUsed; final Amount gasUsed;
static Amount _amountFromJsonNum(dynamic json) {
return Amount(
rawValue: BigInt.from(json as num),
fractionDigits: Coin.ethereum.decimals,
);
}
EthTokenTxExtraDTO copyWith({ EthTokenTxExtraDTO copyWith({
String? hash, String? hash,
@ -66,13 +72,13 @@ class EthTokenTxExtraDTO {
int? timestamp, int? timestamp,
String? from, String? from,
String? to, String? to,
int? value, Amount? value,
int? gas, Amount? gas,
int? gasPrice, Amount? gasPrice,
int? nonce, int? nonce,
String? input, String? input,
int? gasCost, Amount? gasCost,
int? gasUsed, Amount? gasUsed,
}) => }) =>
EthTokenTxExtraDTO( EthTokenTxExtraDTO(
hash: hash ?? this.hash, hash: hash ?? this.hash,

View file

@ -1,5 +1,8 @@
import 'dart:convert'; import 'dart:convert';
import 'package:stackwallet/utilities/amount.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
class EthTxDTO { class EthTxDTO {
EthTxDTO({ EthTxDTO({
required this.hash, required this.hash,
@ -29,22 +32,16 @@ class EthTxDTO {
timestamp: map['timestamp'] as int, timestamp: map['timestamp'] as int,
from: map['from'] as String, from: map['from'] as String,
to: map['to'] as String, to: map['to'] as String,
value: map['value'] as int, value: _amountFromJsonNum(map['value']),
gas: map['gas'] as int, gas: _amountFromJsonNum(map['gas']),
gasPrice: map['gasPrice'] as int, gasPrice: _amountFromJsonNum(map['gasPrice']),
maxFeePerGas: map['maxFeePerGas'] as int, maxFeePerGas: _amountFromJsonNum(map['maxFeePerGas']),
maxPriorityFeePerGas: map['maxPriorityFeePerGas'] as int, maxPriorityFeePerGas: _amountFromJsonNum(map['maxPriorityFeePerGas']),
isError: map['isError'] as int, isError: map['isError'] as int,
hasToken: map['hasToken'] as int, hasToken: map['hasToken'] as int,
compressedTx: map['compressedTx'] as String, compressedTx: map['compressedTx'] as String,
gasCost: map['gasCost'] as int, gasCost: _amountFromJsonNum(map['gasCost']),
gasUsed: map['gasUsed'] as int, gasUsed: _amountFromJsonNum(map['gasUsed']),
);
factory EthTxDTO.fromJsonString(String jsonString) => EthTxDTO.fromMap(
Map<String, dynamic>.from(
jsonDecode(jsonString) as Map,
),
); );
final String hash; final String hash;
@ -54,16 +51,23 @@ class EthTxDTO {
final int timestamp; final int timestamp;
final String from; final String from;
final String to; final String to;
final int value; final Amount value;
final int gas; final Amount gas;
final int gasPrice; final Amount gasPrice;
final int maxFeePerGas; final Amount maxFeePerGas;
final int maxPriorityFeePerGas; final Amount maxPriorityFeePerGas;
final int isError; final int isError;
final int hasToken; final int hasToken;
final String compressedTx; final String compressedTx;
final int gasCost; final Amount gasCost;
final int gasUsed; final Amount gasUsed;
static Amount _amountFromJsonNum(dynamic json) {
return Amount(
rawValue: BigInt.from(json as num),
fractionDigits: Coin.ethereum.decimals,
);
}
EthTxDTO copyWith({ EthTxDTO copyWith({
String? hash, String? hash,
@ -73,16 +77,16 @@ class EthTxDTO {
int? timestamp, int? timestamp,
String? from, String? from,
String? to, String? to,
int? value, Amount? value,
int? gas, Amount? gas,
int? gasPrice, Amount? gasPrice,
int? maxFeePerGas, Amount? maxFeePerGas,
int? maxPriorityFeePerGas, Amount? maxPriorityFeePerGas,
int? isError, int? isError,
int? hasToken, int? hasToken,
String? compressedTx, String? compressedTx,
int? gasCost, Amount? gasCost,
int? gasUsed, Amount? gasUsed,
}) => }) =>
EthTxDTO( EthTxDTO(
hash: hash ?? this.hash, hash: hash ?? this.hash,

View file

@ -854,7 +854,7 @@ class EthereumWallet extends CoinServiceAPI with WalletCache, WalletDB {
final allTxs = txsResponse.value!; final allTxs = txsResponse.value!;
final List<Tuple2<Transaction, Address?>> txnsData = []; final List<Tuple2<Transaction, Address?>> txnsData = [];
for (final element in allTxs) { for (final element in allTxs) {
int transactionAmount = element.value; Amount transactionAmount = element.value;
bool isIncoming; bool isIncoming;
bool txFailed = false; bool txFailed = false;
@ -869,7 +869,7 @@ class EthereumWallet extends CoinServiceAPI with WalletCache, WalletDB {
//Calculate fees (GasLimit * gasPrice) //Calculate fees (GasLimit * gasPrice)
// int txFee = element.gasPrice * element.gasUsed; // int txFee = element.gasPrice * element.gasUsed;
int txFee = element.gasCost; Amount txFee = element.gasCost;
final String addressString = checksumEthereumAddress(element.to); final String addressString = checksumEthereumAddress(element.to);
final int height = element.blockNumber; final int height = element.blockNumber;
@ -881,12 +881,9 @@ class EthereumWallet extends CoinServiceAPI with WalletCache, WalletDB {
type: type:
isIncoming ? TransactionType.incoming : TransactionType.outgoing, isIncoming ? TransactionType.incoming : TransactionType.outgoing,
subType: TransactionSubType.none, subType: TransactionSubType.none,
amount: transactionAmount, amount: transactionAmount.raw.toInt(),
amountString: Amount( amountString: transactionAmount.toJsonString(),
rawValue: BigInt.from(transactionAmount), fee: txFee.raw.toInt(),
fractionDigits: coin.decimals,
).toJsonString(),
fee: txFee,
height: height, height: height,
isCancelled: txFailed, isCancelled: txFailed,
isLelantus: false, isLelantus: false,