Amount fixes

This commit is contained in:
julian 2023-04-06 17:49:13 -06:00
parent 16a6619746
commit 94896dfd60
11 changed files with 48 additions and 58 deletions

View file

@ -1533,7 +1533,10 @@ class _SendViewState extends ConsumerState<SendView> {
? 0.toAmountAsRaw( ? 0.toAmountAsRaw(
fractionDigits: coin.decimals) fractionDigits: coin.decimals)
: (baseAmount.decimal / _price) : (baseAmount.decimal / _price)
.toDouble() .toDecimal(
scaleOnInfinitePrecision:
coin.decimals,
)
.toAmount( .toAmount(
fractionDigits: coin.decimals); fractionDigits: coin.decimals);
} }

View file

@ -1341,11 +1341,9 @@ class BitcoinWallet extends CoinServiceAPI
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
type: isar_models.TransactionType.outgoing, type: isar_models.TransactionType.outgoing,
subType: isar_models.TransactionSubType.none, subType: isar_models.TransactionSubType.none,
amount: txData["recipientAmt"] as int, // precision may be lost here hence the following amountString
amountString: Amount( amount: (txData["recipientAmt"] as Amount).raw.toInt(),
rawValue: BigInt.from(txData["recipientAmt"] as int), amountString: (txData["recipientAmt"] as Amount).toJsonString(),
fractionDigits: coin.decimals,
).toJsonString(),
fee: txData["fee"] as int, fee: txData["fee"] as int,
height: null, height: null,
isCancelled: false, isCancelled: false,

View file

@ -1250,11 +1250,9 @@ class BitcoinCashWallet extends CoinServiceAPI
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
type: isar_models.TransactionType.outgoing, type: isar_models.TransactionType.outgoing,
subType: isar_models.TransactionSubType.none, subType: isar_models.TransactionSubType.none,
amount: txData["recipientAmt"] as int, // precision may be lost here hence the following amountString
amountString: Amount( amount: (txData["recipientAmt"] as Amount).raw.toInt(),
rawValue: BigInt.from(txData["recipientAmt"] as int), amountString: (txData["recipientAmt"] as Amount).toJsonString(),
fractionDigits: coin.decimals,
).toJsonString(),
fee: txData["fee"] as int, fee: txData["fee"] as int,
height: null, height: null,
isCancelled: false, isCancelled: false,

View file

@ -1116,11 +1116,9 @@ class DogecoinWallet extends CoinServiceAPI
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
type: isar_models.TransactionType.outgoing, type: isar_models.TransactionType.outgoing,
subType: isar_models.TransactionSubType.none, subType: isar_models.TransactionSubType.none,
amount: txData["recipientAmt"] as int, // precision may be lost here hence the following amountString
amountString: Amount( amount: (txData["recipientAmt"] as Amount).raw.toInt(),
rawValue: BigInt.from(txData["recipientAmt"] as int), amountString: (txData["recipientAmt"] as Amount).toJsonString(),
fractionDigits: coin.decimals,
).toJsonString(),
fee: txData["fee"] as int, fee: txData["fee"] as int,
height: null, height: null,
isCancelled: false, isCancelled: false,

View file

@ -221,15 +221,14 @@ class EthereumWallet extends CoinServiceAPI with WalletCache, WalletDB {
Future<void> updateBalance() async { Future<void> updateBalance() async {
web3.Web3Client client = getEthClient(); web3.Web3Client client = getEthClient();
web3.EtherAmount ethBalance = await client.getBalance(_credentials.address); web3.EtherAmount ethBalance = await client.getBalance(_credentials.address);
// TODO: check if toInt() is ok and if getBalance actually returns enough balance data
_balance = Balance( _balance = Balance(
coin: coin, coin: coin,
total: Amount.fromDouble( total: Amount(
ethBalance.getValueInUnit(web3.EtherUnit.ether), rawValue: ethBalance.getInWei,
fractionDigits: coin.decimals, fractionDigits: coin.decimals,
), ),
spendable: Amount.fromDouble( spendable: Amount(
ethBalance.getValueInUnit(web3.EtherUnit.ether), rawValue: ethBalance.getInWei,
fractionDigits: coin.decimals, fractionDigits: coin.decimals,
), ),
blockedTotal: Amount( blockedTotal: Amount(
@ -922,11 +921,9 @@ class EthereumWallet extends CoinServiceAPI with WalletCache, WalletDB {
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
type: TransactionType.outgoing, type: TransactionType.outgoing,
subType: TransactionSubType.none, subType: TransactionSubType.none,
amount: txData["recipientAmt"] as int, // precision may be lost here hence the following amountString
amountString: Amount( amount: (txData["recipientAmt"] as Amount).raw.toInt(),
rawValue: BigInt.from(txData["recipientAmt"] as int), amountString: (txData["recipientAmt"] as Amount).toJsonString(),
fractionDigits: coin.decimals,
).toJsonString(),
fee: txData["fee"] as int, fee: txData["fee"] as int,
height: null, height: null,
isCancelled: false, isCancelled: false,

View file

@ -910,11 +910,9 @@ class FiroWallet extends CoinServiceAPI with WalletCache, WalletDB, FiroHive {
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
type: isar_models.TransactionType.outgoing, type: isar_models.TransactionType.outgoing,
subType: isar_models.TransactionSubType.none, subType: isar_models.TransactionSubType.none,
amount: txData["recipientAmt"] as int, // precision may be lost here hence the following amountString
amountString: Amount( amount: (txData["recipientAmt"] as Amount).raw.toInt(),
rawValue: BigInt.from(txData["recipientAmt"] as int), amountString: (txData["recipientAmt"] as Amount).toJsonString(),
fractionDigits: Coin.firo.decimals,
).toJsonString(),
fee: txData["fee"] as int, fee: txData["fee"] as int,
height: null, height: null,
isCancelled: false, isCancelled: false,

View file

@ -1248,11 +1248,9 @@ class LitecoinWallet extends CoinServiceAPI
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
type: isar_models.TransactionType.outgoing, type: isar_models.TransactionType.outgoing,
subType: isar_models.TransactionSubType.none, subType: isar_models.TransactionSubType.none,
amount: txData["recipientAmt"] as int, // precision may be lost here hence the following amountString
amountString: Amount( amount: (txData["recipientAmt"] as Amount).raw.toInt(),
rawValue: BigInt.from(txData["recipientAmt"] as int), amountString: (txData["recipientAmt"] as Amount).toJsonString(),
fractionDigits: coin.decimals,
).toJsonString(),
fee: txData["fee"] as int, fee: txData["fee"] as int,
height: null, height: null,
isCancelled: false, isCancelled: false,

View file

@ -1235,11 +1235,9 @@ class NamecoinWallet extends CoinServiceAPI
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
type: isar_models.TransactionType.outgoing, type: isar_models.TransactionType.outgoing,
subType: isar_models.TransactionSubType.none, subType: isar_models.TransactionSubType.none,
amount: txData["recipientAmt"] as int, // precision may be lost here hence the following amountString
amountString: Amount( amount: (txData["recipientAmt"] as Amount).raw.toInt(),
rawValue: BigInt.from(txData["recipientAmt"] as int), amountString: (txData["recipientAmt"] as Amount).toJsonString(),
fractionDigits: coin.decimals,
).toJsonString(),
fee: txData["fee"] as int, fee: txData["fee"] as int,
height: null, height: null,
isCancelled: false, isCancelled: false,

View file

@ -1163,11 +1163,9 @@ class ParticlWallet extends CoinServiceAPI
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
type: isar_models.TransactionType.outgoing, type: isar_models.TransactionType.outgoing,
subType: isar_models.TransactionSubType.none, subType: isar_models.TransactionSubType.none,
amount: txData["recipientAmt"] as int, // precision may be lost here hence the following amountString
amountString: Amount( amount: (txData["recipientAmt"] as Amount).raw.toInt(),
rawValue: BigInt.from(txData["recipientAmt"] as int), amountString: (txData["recipientAmt"] as Amount).toJsonString(),
fractionDigits: coin.decimals,
).toJsonString(),
fee: txData["fee"] as int, fee: txData["fee"] as int,
height: null, height: null,
isCancelled: false, isCancelled: false,

View file

@ -178,11 +178,9 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
type: TransactionType.outgoing, type: TransactionType.outgoing,
subType: TransactionSubType.ethToken, subType: TransactionSubType.ethToken,
amount: txData["recipientAmt"] as int, // precision may be lost here hence the following amountString
amountString: Amount( amount: (txData["recipientAmt"] as Amount).raw.toInt(),
rawValue: BigInt.from(txData["recipientAmt"] as int), amountString: (txData["recipientAmt"] as Amount).toJsonString(),
fractionDigits: tokenContract.decimals,
).toJsonString(),
fee: txData["fee"] as int, fee: txData["fee"] as int,
height: null, height: null,
isCancelled: false, isCancelled: false,

View file

@ -1,5 +1,3 @@
import 'dart:math';
import 'package:bip32/bip32.dart' as bip32; import 'package:bip32/bip32.dart' as bip32;
import 'package:bip39/bip39.dart' as bip39; import 'package:bip39/bip39.dart' as bip39;
import 'package:decimal/decimal.dart'; import 'package:decimal/decimal.dart';
@ -64,11 +62,19 @@ String getPrivateKey(String mnemonic, String mnemonicPassphrase) {
} }
Amount estimateFee(int feeRate, int gasLimit, int decimals) { Amount estimateFee(int feeRate, int gasLimit, int decimals) {
final gweiAmount = feeRate / (pow(10, 9)); final gweiAmount = feeRate.toDecimal() / (Decimal.ten.pow(9).toDecimal());
final fee = gasLimit * gweiAmount; final fee = gasLimit.toDecimal() *
gweiAmount.toDecimal(
scaleOnInfinitePrecision: Coin.ethereum.decimals,
);
//Convert gwei to ETH //Convert gwei to ETH
final feeInWei = fee * (pow(10, 9)); final feeInWei = fee * Decimal.ten.pow(9).toDecimal();
final ethAmount = feeInWei / (pow(10, decimals)); final ethAmount = feeInWei / Decimal.ten.pow(decimals).toDecimal();
return Amount.fromDouble(ethAmount, fractionDigits: decimals); return Amount.fromDecimal(
ethAmount.toDecimal(
scaleOnInfinitePrecision: Coin.ethereum.decimals,
),
fractionDigits: decimals,
);
} }