2023-07-28 16:48:52 +00:00
|
|
|
/*
|
2023-05-26 21:21:16 +00:00
|
|
|
* This file is part of Stack Wallet.
|
2023-07-28 16:48:52 +00:00
|
|
|
*
|
2023-05-26 21:21:16 +00:00
|
|
|
* Copyright (c) 2023 Cypher Stack
|
|
|
|
* All Rights Reserved.
|
|
|
|
* The code is distributed under GPLv3 license, see LICENSE file for details.
|
|
|
|
* Generated by Cypher Stack on 2023-05-26
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-04-06 23:27:42 +00:00
|
|
|
import 'dart:math' as math;
|
|
|
|
|
|
|
|
import 'package:decimal/decimal.dart';
|
2024-05-23 00:37:06 +00:00
|
|
|
import '../../models/isar/models/ethereum/eth_contract.dart';
|
|
|
|
import 'amount.dart';
|
|
|
|
import '../util.dart';
|
|
|
|
import '../../wallets/crypto_currency/crypto_currency.dart';
|
|
|
|
import '../../wallets/crypto_currency/intermediate/nano_currency.dart';
|
2023-04-06 23:27:42 +00:00
|
|
|
|
2023-05-29 17:42:11 +00:00
|
|
|
// preserve index order as index is used to store value in preferences
|
2023-04-06 23:27:42 +00:00
|
|
|
enum AmountUnit {
|
|
|
|
normal(0),
|
|
|
|
milli(3),
|
|
|
|
micro(6),
|
|
|
|
nano(9),
|
|
|
|
pico(12),
|
|
|
|
femto(15),
|
|
|
|
atto(18),
|
2023-05-30 15:32:17 +00:00
|
|
|
zepto(21),
|
|
|
|
yocto(24),
|
|
|
|
ronto(27),
|
|
|
|
quecto(30),
|
2023-04-06 23:27:42 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
const AmountUnit(this.shift);
|
|
|
|
final int shift;
|
2023-05-29 22:50:21 +00:00
|
|
|
|
2024-05-15 21:20:45 +00:00
|
|
|
static List<AmountUnit> valuesForCoin(CryptoCurrency coin) {
|
|
|
|
final remainder = coin.fractionDigits % 3;
|
|
|
|
int n = (coin.fractionDigits ~/ 3) + 1;
|
|
|
|
if (remainder > 0) {
|
|
|
|
n++;
|
2023-05-29 22:50:21 +00:00
|
|
|
}
|
2024-05-15 21:20:45 +00:00
|
|
|
|
|
|
|
return AmountUnit.values.sublist(0, n);
|
|
|
|
//
|
|
|
|
// switch (coin) {
|
|
|
|
// case Coin.firo:
|
|
|
|
// case Coin.litecoin:
|
|
|
|
// case Coin.particl:
|
|
|
|
// case Coin.peercoin:
|
|
|
|
// case Coin.namecoin:
|
|
|
|
// case Coin.bitcoinFrost:
|
|
|
|
// case Coin.bitcoinFrostTestNet:
|
|
|
|
// case Coin.bitcoinTestNet:
|
|
|
|
// case Coin.litecoinTestNet:
|
|
|
|
// case Coin.bitcoincashTestnet:
|
|
|
|
// case Coin.dogecoinTestNet:
|
|
|
|
// case Coin.firoTestNet:
|
|
|
|
// case Coin.peercoinTestNet:
|
|
|
|
// case Coin.bitcoin:
|
|
|
|
// case Coin.bitcoincash:
|
|
|
|
// case Coin.dogecoin:
|
|
|
|
// case Coin.eCash:
|
|
|
|
// case Coin.epicCash:
|
|
|
|
// case Coin.stellar: // TODO: check if this is correct
|
|
|
|
// case Coin.stellarTestnet:
|
|
|
|
// case Coin.tezos:
|
|
|
|
// case Coin.solana:
|
|
|
|
// return AmountUnit.values.sublist(0, 4);
|
|
|
|
//
|
|
|
|
// case Coin.monero:
|
|
|
|
// case Coin.wownero:
|
|
|
|
// return AmountUnit.values.sublist(0, 5);
|
|
|
|
//
|
|
|
|
// case Coin.ethereum:
|
|
|
|
// return AmountUnit.values.sublist(0, 7);
|
|
|
|
//
|
|
|
|
// case Coin.nano:
|
|
|
|
// case Coin.banano:
|
|
|
|
// return AmountUnit.values;
|
|
|
|
// }
|
2023-05-29 22:50:21 +00:00
|
|
|
}
|
2023-04-06 23:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension AmountUnitExt on AmountUnit {
|
2024-05-15 21:20:45 +00:00
|
|
|
String unitForCoin(CryptoCurrency coin) {
|
2023-04-06 23:27:42 +00:00
|
|
|
switch (this) {
|
|
|
|
case AmountUnit.normal:
|
|
|
|
return coin.ticker;
|
|
|
|
case AmountUnit.milli:
|
|
|
|
return "m${coin.ticker}";
|
|
|
|
case AmountUnit.micro:
|
|
|
|
return "µ${coin.ticker}";
|
|
|
|
case AmountUnit.nano:
|
2024-05-15 21:20:45 +00:00
|
|
|
if (coin is Ethereum) {
|
2023-04-06 23:27:42 +00:00
|
|
|
return "gwei";
|
2024-05-15 21:20:45 +00:00
|
|
|
} else if (coin is Wownero || coin is Monero || coin is NanoCurrency) {
|
2023-04-06 23:27:42 +00:00
|
|
|
return "n${coin.ticker}";
|
|
|
|
} else {
|
|
|
|
return "sats";
|
|
|
|
}
|
|
|
|
case AmountUnit.pico:
|
2024-05-15 21:20:45 +00:00
|
|
|
if (coin is Ethereum) {
|
2023-04-06 23:27:42 +00:00
|
|
|
return "mwei";
|
2024-05-15 21:20:45 +00:00
|
|
|
} else if (coin is Wownero || coin is Monero || coin is NanoCurrency) {
|
2023-04-06 23:27:42 +00:00
|
|
|
return "p${coin.ticker}";
|
|
|
|
} else {
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
case AmountUnit.femto:
|
2024-05-15 21:20:45 +00:00
|
|
|
if (coin is Ethereum) {
|
2023-04-06 23:27:42 +00:00
|
|
|
return "kwei";
|
2024-05-15 21:20:45 +00:00
|
|
|
} else if (coin is NanoCurrency) {
|
2023-05-30 15:32:17 +00:00
|
|
|
return "f${coin.ticker}";
|
2023-04-06 23:27:42 +00:00
|
|
|
} else {
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
case AmountUnit.atto:
|
2024-05-15 21:20:45 +00:00
|
|
|
if (coin is Ethereum) {
|
2023-04-06 23:27:42 +00:00
|
|
|
return "wei";
|
2024-05-15 21:20:45 +00:00
|
|
|
} else if (coin is NanoCurrency) {
|
2023-05-30 15:32:17 +00:00
|
|
|
return "a${coin.ticker}";
|
|
|
|
} else {
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
case AmountUnit.zepto:
|
2024-05-15 21:20:45 +00:00
|
|
|
if (coin is NanoCurrency) {
|
2023-05-30 15:32:17 +00:00
|
|
|
return "z${coin.ticker}";
|
|
|
|
} else {
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
case AmountUnit.yocto:
|
2024-05-15 21:20:45 +00:00
|
|
|
if (coin is NanoCurrency) {
|
2023-05-30 15:32:17 +00:00
|
|
|
return "y${coin.ticker}";
|
|
|
|
} else {
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
case AmountUnit.ronto:
|
2024-05-15 21:20:45 +00:00
|
|
|
if (coin is NanoCurrency) {
|
2023-05-30 15:32:17 +00:00
|
|
|
return "r${coin.ticker}";
|
|
|
|
} else {
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
case AmountUnit.quecto:
|
2024-05-15 21:20:45 +00:00
|
|
|
if (coin is NanoCurrency) {
|
2023-05-30 15:32:17 +00:00
|
|
|
return "q${coin.ticker}";
|
2023-04-06 23:27:42 +00:00
|
|
|
} else {
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:10:55 +00:00
|
|
|
String unitForContract(EthContract contract) {
|
|
|
|
switch (this) {
|
|
|
|
case AmountUnit.normal:
|
|
|
|
return contract.symbol;
|
|
|
|
case AmountUnit.milli:
|
|
|
|
return "m${contract.symbol}";
|
|
|
|
case AmountUnit.micro:
|
|
|
|
return "µ${contract.symbol}";
|
|
|
|
case AmountUnit.nano:
|
|
|
|
return "gwei";
|
|
|
|
case AmountUnit.pico:
|
|
|
|
return "mwei";
|
|
|
|
case AmountUnit.femto:
|
|
|
|
return "kwei";
|
|
|
|
case AmountUnit.atto:
|
|
|
|
return "wei";
|
2023-05-30 15:32:17 +00:00
|
|
|
default:
|
|
|
|
throw ArgumentError(
|
|
|
|
"Does eth even allow more than 18 decimal places?",
|
|
|
|
);
|
2023-05-29 21:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 16:43:20 +00:00
|
|
|
Amount? tryParse(
|
|
|
|
String value, {
|
|
|
|
required String locale,
|
2024-05-15 21:20:45 +00:00
|
|
|
required CryptoCurrency coin,
|
2023-06-16 16:43:20 +00:00
|
|
|
EthContract? tokenContract,
|
2023-07-19 14:30:15 +00:00
|
|
|
bool overrideWithDecimalPlacesFromString = false,
|
2023-06-16 16:43:20 +00:00
|
|
|
}) {
|
|
|
|
final precisionLost = value.startsWith("~");
|
|
|
|
|
|
|
|
final parts = (precisionLost ? value.substring(1) : value).split(" ");
|
|
|
|
|
|
|
|
if (parts.first.isEmpty) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
String str = parts.first;
|
|
|
|
if (str.startsWith(RegExp(r'[+-]'))) {
|
|
|
|
str = str.substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str.isEmpty) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get number symbols for decimal place and group separator
|
2023-07-12 22:06:06 +00:00
|
|
|
final numberSymbols = Util.getSymbolsFor(locale: locale);
|
2023-06-16 16:43:20 +00:00
|
|
|
|
|
|
|
final groupSeparator = numberSymbols?.GROUP_SEP ?? ",";
|
|
|
|
final decimalSeparator = numberSymbols?.DECIMAL_SEP ?? ".";
|
|
|
|
|
|
|
|
str = str.replaceAll(groupSeparator, "");
|
|
|
|
|
|
|
|
final decimalString = str.replaceFirst(decimalSeparator, ".");
|
|
|
|
final Decimal? decimal = Decimal.tryParse(decimalString);
|
|
|
|
|
|
|
|
if (decimal == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-07-19 14:30:15 +00:00
|
|
|
final decimalPlaces = overrideWithDecimalPlacesFromString
|
|
|
|
? decimal.scale
|
2024-05-15 21:20:45 +00:00
|
|
|
: tokenContract?.decimals ?? coin.fractionDigits;
|
2023-06-16 16:43:20 +00:00
|
|
|
final realShift = math.min(shift, decimalPlaces);
|
|
|
|
|
|
|
|
return decimal.shift(0 - realShift).toAmount(fractionDigits: decimalPlaces);
|
|
|
|
}
|
|
|
|
|
2023-04-06 23:27:42 +00:00
|
|
|
String displayAmount({
|
|
|
|
required Amount amount,
|
|
|
|
required String locale,
|
2024-05-15 21:20:45 +00:00
|
|
|
required CryptoCurrency coin,
|
2023-04-06 23:27:42 +00:00
|
|
|
required int maxDecimalPlaces,
|
2023-05-29 21:10:55 +00:00
|
|
|
bool withUnitName = true,
|
2023-05-30 15:02:09 +00:00
|
|
|
bool indicatePrecisionLoss = true,
|
2023-05-29 21:10:55 +00:00
|
|
|
String? overrideUnit,
|
|
|
|
EthContract? tokenContract,
|
2023-04-06 23:27:42 +00:00
|
|
|
}) {
|
|
|
|
assert(maxDecimalPlaces >= 0);
|
2023-05-29 21:10:55 +00:00
|
|
|
|
2023-04-06 23:27:42 +00:00
|
|
|
// ensure we don't shift past minimum atomic value
|
|
|
|
final realShift = math.min(shift, amount.fractionDigits);
|
|
|
|
|
|
|
|
// shifted to unit
|
|
|
|
final Decimal shifted = amount.decimal.shift(realShift);
|
|
|
|
|
|
|
|
// get shift int value without fractional value
|
|
|
|
final BigInt wholeNumber = shifted.toBigInt();
|
|
|
|
|
|
|
|
// get decimal places to display
|
|
|
|
final int places = math.max(0, amount.fractionDigits - realShift);
|
|
|
|
|
|
|
|
// start building the return value with just the whole value
|
|
|
|
String returnValue = wholeNumber.toString();
|
|
|
|
|
2023-06-16 16:43:20 +00:00
|
|
|
// get number symbols for decimal place and group separator
|
2023-07-12 22:06:06 +00:00
|
|
|
final numberSymbols = Util.getSymbolsFor(locale: locale);
|
2023-06-16 16:43:20 +00:00
|
|
|
|
|
|
|
// insert group separator
|
|
|
|
final regex = RegExp(r'\B(?=(\d{3})+(?!\d))');
|
|
|
|
returnValue = returnValue.replaceAllMapped(
|
|
|
|
regex,
|
|
|
|
(m) => "${m.group(0)}${numberSymbols?.GROUP_SEP ?? ","}",
|
|
|
|
);
|
|
|
|
|
2023-05-29 23:13:45 +00:00
|
|
|
// if true and withUnitName is true, we will show "~" prepended on amount
|
|
|
|
bool didLosePrecision = false;
|
|
|
|
|
2023-04-06 23:27:42 +00:00
|
|
|
// if any decimal places should be shown continue building the return value
|
|
|
|
if (places > 0) {
|
|
|
|
// get the fractional value
|
|
|
|
final Decimal fraction = shifted - shifted.truncate();
|
|
|
|
|
2023-05-29 21:10:55 +00:00
|
|
|
// get final decimal based on max precision wanted while ensuring that
|
|
|
|
// maxDecimalPlaces doesn't exceed the max per coin
|
|
|
|
final int updatedMax;
|
|
|
|
if (tokenContract != null) {
|
|
|
|
updatedMax = maxDecimalPlaces > tokenContract.decimals
|
|
|
|
? tokenContract.decimals
|
|
|
|
: maxDecimalPlaces;
|
|
|
|
} else {
|
2024-05-15 21:20:45 +00:00
|
|
|
updatedMax = maxDecimalPlaces > coin.fractionDigits
|
|
|
|
? coin.fractionDigits
|
|
|
|
: maxDecimalPlaces;
|
2023-05-29 21:10:55 +00:00
|
|
|
}
|
|
|
|
final int actualDecimalPlaces = math.min(places, updatedMax);
|
2023-04-06 23:27:42 +00:00
|
|
|
|
|
|
|
// get remainder string without the prepending "0."
|
2023-05-29 21:10:55 +00:00
|
|
|
final fractionString = fraction.toString();
|
|
|
|
String remainder;
|
|
|
|
if (fractionString.length > 2) {
|
|
|
|
remainder = fraction.toString().substring(2);
|
|
|
|
} else {
|
|
|
|
remainder = "0";
|
|
|
|
}
|
2023-04-06 23:27:42 +00:00
|
|
|
|
|
|
|
if (remainder.length > actualDecimalPlaces) {
|
2023-05-29 23:13:45 +00:00
|
|
|
// check for loss of precision
|
|
|
|
final remainingRemainder =
|
|
|
|
BigInt.tryParse(remainder.substring(actualDecimalPlaces));
|
|
|
|
if (remainingRemainder != null) {
|
|
|
|
didLosePrecision = remainingRemainder > BigInt.zero;
|
|
|
|
}
|
|
|
|
|
2023-04-06 23:27:42 +00:00
|
|
|
// trim unwanted trailing digits
|
|
|
|
remainder = remainder.substring(0, actualDecimalPlaces);
|
|
|
|
} else if (remainder.length < actualDecimalPlaces) {
|
|
|
|
// pad with zeros to achieve requested precision
|
|
|
|
for (int i = remainder.length; i < actualDecimalPlaces; i++) {
|
|
|
|
remainder += "0";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get decimal separator based on locale
|
2023-06-16 16:43:20 +00:00
|
|
|
final String separator = numberSymbols?.DECIMAL_SEP ?? ".";
|
2023-04-06 23:27:42 +00:00
|
|
|
|
|
|
|
// append separator and fractional amount
|
|
|
|
returnValue += "$separator$remainder";
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:02:09 +00:00
|
|
|
if (!withUnitName && !indicatePrecisionLoss) {
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (didLosePrecision && indicatePrecisionLoss) {
|
|
|
|
returnValue = "~$returnValue";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!withUnitName && indicatePrecisionLoss) {
|
2023-05-29 21:10:55 +00:00
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2023-04-06 23:27:42 +00:00
|
|
|
// return the value with the proper unit symbol
|
2023-05-29 21:10:55 +00:00
|
|
|
if (tokenContract != null) {
|
|
|
|
overrideUnit = unitForContract(tokenContract);
|
|
|
|
}
|
2023-05-29 23:13:45 +00:00
|
|
|
|
2023-05-29 21:10:55 +00:00
|
|
|
return "$returnValue ${overrideUnit ?? unitForCoin(coin)}";
|
2023-04-06 23:27:42 +00:00
|
|
|
}
|
|
|
|
}
|