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(
fractionDigits: coin.decimals)
: (baseAmount.decimal / _price)
.toDouble()
.toDecimal(
scaleOnInfinitePrecision:
coin.decimals,
)
.toAmount(
fractionDigits: coin.decimals);
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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