mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-26 06:08:40 +01:00
refactor: remove toDouble method and reverted other changes (#3531)
This commit is contained in:
@@ -202,10 +202,6 @@ class Money implements Comparable<Money> {
|
||||
@override
|
||||
int get hashCode => amount.hashCode ^ currency.hashCode;
|
||||
|
||||
// Added this to reduce the hops we do to convert Money to double
|
||||
// for fiat conversion and display
|
||||
double toDouble() => amount / multiplierOf(currency.decimals);
|
||||
|
||||
@override
|
||||
String toString() => formatFixed(amount, currency.decimals);
|
||||
|
||||
|
||||
@@ -4,31 +4,44 @@ String formatFixed(BigInt value, int? decimals, {int? fractionalDigits, bool tri
|
||||
decimals ??= 0;
|
||||
fractionalDigits ??= decimals;
|
||||
|
||||
final multiplier = multiplierOf(decimals);
|
||||
var negative = value.isNegative;
|
||||
if (negative) value = -value;
|
||||
final multiplier = getMultiplier(decimals);
|
||||
// Make sure wei is a big number (convert as necessary)
|
||||
final negative = value.isNegative;
|
||||
if (negative) {
|
||||
value = -value;
|
||||
}
|
||||
|
||||
var fraction = (value % multiplier).toString().padLeft(decimals, "0");
|
||||
var fraction =
|
||||
value.modPow(BigInt.one, BigInt.parse(multiplier)).toString().padLeft(decimals, "0");
|
||||
|
||||
if (fractionalDigits < 0) fractionalDigits = 0;
|
||||
if (fractionalDigits > decimals) fractionalDigits = decimals;
|
||||
if (fractionalDigits < 0) {
|
||||
fractionalDigits = 0;
|
||||
}
|
||||
if (fractionalDigits > decimals) {
|
||||
fractionalDigits = decimals;
|
||||
}
|
||||
fraction = fraction.substring(0, fractionalDigits);
|
||||
|
||||
if (trimZeros) {
|
||||
fraction = removeTrailing("0", fraction);
|
||||
}
|
||||
|
||||
final whole = value ~/ multiplier;
|
||||
final whole = value ~/ BigInt.parse(multiplier);
|
||||
|
||||
final valString = fraction.isEmpty ? "$whole" : "$whole.$fraction";
|
||||
|
||||
if (negative) return "-$valString";
|
||||
if (negative) {
|
||||
return "-$valString";
|
||||
}
|
||||
|
||||
return valString;
|
||||
}
|
||||
|
||||
String removeTrailing(String pattern, String from) {
|
||||
if (pattern.isEmpty) return from;
|
||||
if (pattern.isEmpty) {
|
||||
return from;
|
||||
}
|
||||
|
||||
var i = from.length;
|
||||
while (i > 0 && from.startsWith(pattern, i - pattern.length)) {
|
||||
i -= pattern.length;
|
||||
|
||||
@@ -37,40 +37,44 @@ BigInt parseFixed(String value, int decimals) {
|
||||
}
|
||||
|
||||
final negative = value.startsWith("-");
|
||||
if (negative) value = value.substring(1);
|
||||
if (negative) {
|
||||
value = value.substring(1);
|
||||
}
|
||||
|
||||
if (value == ".") throw FormatException("missing value, value, $value");
|
||||
if (value == ".") {
|
||||
throw FormatException("missing value, value, $value");
|
||||
}
|
||||
|
||||
if (value.startsWith(".")) value = "0$value";
|
||||
if (value.startsWith(".")) {
|
||||
value = "0$value";
|
||||
}
|
||||
|
||||
final comps = value.split(".");
|
||||
if (comps.length > 2) {
|
||||
throw FormatException("too many decimal points, value, $value");
|
||||
}
|
||||
|
||||
var whole = comps.isNotEmpty ? comps[0] : "0";
|
||||
var fraction = (comps.length == 2 ? comps[1] : "0").padRight(decimals, "0");
|
||||
final whole = comps.isNotEmpty ? comps[0] : "0";
|
||||
final fraction = (comps.length == 2 ? comps[1] : "0").padRight(decimals, "0");
|
||||
|
||||
if (fraction.length > multiplier.length - 1) {
|
||||
throw FormatException(
|
||||
"fractional component(${fraction.length}) exceeds decimals(${decimals}), underflow, parseFixed");
|
||||
"fractional component(${fraction.length}) exceeds decimals(${decimals}), underflow, parseFixed",
|
||||
);
|
||||
}
|
||||
|
||||
final wholeValue = BigInt.parse(whole);
|
||||
final fractionValue = BigInt.parse(fraction);
|
||||
final multiplierValue = multiplierOf(decimals);
|
||||
final multiplierValue = BigInt.parse(multiplier);
|
||||
|
||||
var wei = (wholeValue * multiplierValue) + fractionValue;
|
||||
|
||||
if (negative) wei *= BigInt.from(-1);
|
||||
if (negative) {
|
||||
wei *= BigInt.from(-1);
|
||||
}
|
||||
|
||||
return wei;
|
||||
}
|
||||
|
||||
// Returns a string "1" followed by decimal "0"s
|
||||
String getMultiplier(int decimals) => "1".padRight(decimals + 1, "0");
|
||||
|
||||
final _multipliers = <int, BigInt>{};
|
||||
|
||||
// this is more direct and faster than having it as string then parsing everytime to get the number
|
||||
BigInt multiplierOf(int decimals) => _multipliers[decimals] ??= BigInt.from(10).pow(decimals);
|
||||
|
||||
@@ -203,7 +203,7 @@ class TransactionListItem extends ActionListItem with Keyable {
|
||||
case WalletType.decred:
|
||||
case WalletType.zcash:
|
||||
amount = calculateFiatAmountRaw(
|
||||
cryptoAmount: transaction.amount.toDouble(),
|
||||
cryptoAmount: double.parse(transaction.amount.toString()),
|
||||
price: price,
|
||||
).withLocalSeperator(_appStore.settingsStore.languageCode);
|
||||
case WalletType.ethereum:
|
||||
@@ -214,15 +214,15 @@ class TransactionListItem extends ActionListItem with Keyable {
|
||||
final asset = assetOfTransaction;
|
||||
final price = balanceViewModel.fiatConversionStore.prices[asset];
|
||||
amount = calculateFiatAmountRaw(
|
||||
cryptoAmount: transaction.amount.toDouble(),
|
||||
cryptoAmount: double.parse(transaction.amount.toString()),
|
||||
price: price,
|
||||
).withLocalSeperator(_appStore.settingsStore.languageCode);
|
||||
break;
|
||||
case WalletType.solana:
|
||||
final asset = assetOfTransaction;
|
||||
final asset = solana!.assetOfTransaction(balanceViewModel.wallet, transaction);
|
||||
final price = balanceViewModel.fiatConversionStore.prices[asset];
|
||||
amount = calculateFiatAmountRaw(
|
||||
cryptoAmount: transaction.amount.toDouble(),
|
||||
cryptoAmount: double.parse(transaction.amount.toString()),
|
||||
price: price,
|
||||
).withLocalSeperator(_appStore.settingsStore.languageCode);
|
||||
break;
|
||||
@@ -230,7 +230,7 @@ class TransactionListItem extends ActionListItem with Keyable {
|
||||
final asset = tron!.assetOfTransaction(balanceViewModel.wallet, transaction);
|
||||
final price = balanceViewModel.fiatConversionStore.prices[asset];
|
||||
amount = calculateFiatAmountRaw(
|
||||
cryptoAmount: transaction.amount.toDouble(),
|
||||
cryptoAmount: double.parse(transaction.amount.toString()),
|
||||
price: price,
|
||||
).withLocalSeperator(_appStore.settingsStore.languageCode);
|
||||
break;
|
||||
@@ -242,7 +242,7 @@ class TransactionListItem extends ActionListItem with Keyable {
|
||||
}
|
||||
final price = balanceViewModel.fiatConversionStore.prices[asset];
|
||||
amount = calculateFiatAmountRaw(
|
||||
cryptoAmount: transaction.amount.toDouble(),
|
||||
cryptoAmount: double.parse(transaction.amount.toString()),
|
||||
price: price,
|
||||
).withLocalSeperator(_appStore.settingsStore.languageCode);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user