Amount tweaks and fixes

This commit is contained in:
julian 2023-04-06 14:21:49 -06:00
parent ecc075d5fc
commit 14d68b0743
11 changed files with 110 additions and 36 deletions

View file

@ -683,7 +683,7 @@ class _CoinControlViewState extends ConsumerState<CoinControlView> {
value += element,
);
return Text(
"${selectedSum.toAmount(fractionDigits: coin.decimals).localizedStringAsFixed(
"${selectedSum.toAmountAsRaw(fractionDigits: coin.decimals).localizedStringAsFixed(
locale: ref.watch(
localeServiceChangeNotifierProvider
.select(
@ -731,7 +731,7 @@ class _CoinControlViewState extends ConsumerState<CoinControlView> {
style: STextStyles.w600_14(context),
),
Text(
"${widget.requestedTotal!.toAmount(fractionDigits: coin.decimals).localizedStringAsFixed(
"${widget.requestedTotal!.toAmountAsRaw(fractionDigits: coin.decimals).localizedStringAsFixed(
locale: ref.watch(
localeServiceChangeNotifierProvider
.select(

View file

@ -124,7 +124,7 @@ class _UtxoCardState extends ConsumerState<UtxoCard> {
mainAxisSize: MainAxisSize.min,
children: [
Text(
"${utxo.value.toAmount(fractionDigits: coin.decimals).localizedStringAsFixed(
"${utxo.value.toAmountAsRaw(fractionDigits: coin.decimals).localizedStringAsFixed(
locale: ref.watch(
localeServiceChangeNotifierProvider.select(
(value) => value.locale,

View file

@ -240,7 +240,7 @@ class _UtxoDetailsViewState extends ConsumerState<UtxoDetailsView> {
width: 16,
),
Text(
"${utxo!.value.toAmount(fractionDigits: coin.decimals).localizedStringAsFixed(
"${utxo!.value.toAmountAsRaw(fractionDigits: coin.decimals).localizedStringAsFixed(
locale: ref.watch(
localeServiceChangeNotifierProvider.select(
(value) => value.locale,

View file

@ -359,7 +359,7 @@ class _ConfirmChangeNowSendViewState
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
"${(transactionInfo["fee"] as int).toAmount(
"${(transactionInfo["fee"] as int).toAmountAsRaw(
fractionDigits: ref.watch(
managerProvider
.select((value) => value.coin.decimals),
@ -402,7 +402,7 @@ class _ConfirmChangeNowSendViewState
managerProvider.select((value) => value.coin),
);
final fee =
(transactionInfo["fee"] as int).toAmount(
(transactionInfo["fee"] as int).toAmountAsRaw(
fractionDigits: coin.decimals,
);
final amount =
@ -637,7 +637,7 @@ class _ConfirmChangeNowSendViewState
style: STextStyles.smallMed12(context),
),
Text(
"${(transactionInfo["fee"] as int).toAmount(fractionDigits: ref.watch(
"${(transactionInfo["fee"] as int).toAmountAsRaw(fractionDigits: ref.watch(
managerProvider.select(
(value) => value.coin.decimals,
),
@ -733,7 +733,7 @@ class _ConfirmChangeNowSendViewState
final coin = ref.watch(
managerProvider.select((value) => value.coin),
);
final fee = (transactionInfo["fee"] as int).toAmount(
final fee = (transactionInfo["fee"] as int).toAmountAsRaw(
fractionDigits: coin.decimals,
);
final amount =

View file

@ -160,7 +160,7 @@ class _PaynymDetailsPopupState extends ConsumerState<PaynymDetailsPopup> {
);
},
amount: (preparedTx["amount"] as Amount) +
(preparedTx["fee"] as int).toAmount(
(preparedTx["fee"] as int).toAmountAsRaw(
fractionDigits: manager.coin.decimals,
),
coin: manager.coin,

View file

@ -143,7 +143,7 @@ class _PaynymDetailsPopupState extends ConsumerState<DesktopPaynymDetails> {
);
},
amount: (preparedTx["amount"] as Amount) +
(preparedTx["fee"] as int).toAmount(
(preparedTx["fee"] as int).toAmountAsRaw(
fractionDigits: manager.coin.decimals,
),
coin: manager.coin,

View file

@ -427,7 +427,7 @@ class _ConfirmTransactionViewState
style: STextStyles.smallMed12(context),
),
Text(
"${(transactionInfo["fee"] as int).toAmount(
"${(transactionInfo["fee"] as int).toAmountAsRaw(
fractionDigits: ref.watch(
managerProvider.select(
(value) => value.coin.decimals,
@ -678,8 +678,8 @@ class _ConfirmTransactionViewState
value.getManager(walletId)))
.coin;
final fee =
(transactionInfo["fee"] as int).toAmount(
final fee = (transactionInfo["fee"] as int)
.toAmountAsRaw(
fractionDigits: coin.decimals,
);
@ -857,7 +857,7 @@ class _ConfirmTransactionViewState
.select((value) => value.getManager(walletId)))
.coin;
final fee = (transactionInfo["fee"] as int).toAmount(
final fee = (transactionInfo["fee"] as int).toAmountAsRaw(
fractionDigits: coin.decimals,
);
@ -917,7 +917,7 @@ class _ConfirmTransactionViewState
final coin = ref.watch(walletsChangeNotifierProvider
.select((value) => value.getManager(walletId).coin));
final fee = (transactionInfo["fee"] as int)
.toAmount(fractionDigits: coin.decimals);
.toAmountAsRaw(fractionDigits: coin.decimals);
final locale = ref.watch(
localeServiceChangeNotifierProvider
.select((value) => value.locale),

View file

@ -154,7 +154,7 @@ class _SendViewState extends ConsumerState<SendView> {
setState(() {
_calculateFeesFuture = calculateFees(
_amountToSend == null
? 0.toAmount(fractionDigits: coin.decimals)
? 0.toAmountAsRaw(fractionDigits: coin.decimals)
: _amountToSend!,
);
});
@ -175,7 +175,7 @@ class _SendViewState extends ConsumerState<SendView> {
setState(() {
_calculateFeesFuture = calculateFees(
_amountToSend == null
? 0.toAmount(fractionDigits: coin.decimals)
? 0.toAmountAsRaw(fractionDigits: coin.decimals)
: _amountToSend!,
);
});
@ -560,10 +560,10 @@ class _SendViewState extends ConsumerState<SendView> {
void initState() {
coin = widget.coin;
ref.refresh(feeSheetSessionCacheProvider);
_currentFee = 0.toAmount(fractionDigits: coin.decimals);
_currentFee = 0.toAmountAsRaw(fractionDigits: coin.decimals);
_calculateFeesFuture =
calculateFees(0.toAmount(fractionDigits: coin.decimals));
calculateFees(0.toAmountAsRaw(fractionDigits: coin.decimals));
_data = widget.autoFillData;
walletId = widget.walletId;
clipboard = widget.clipboard;
@ -672,7 +672,7 @@ class _SendViewState extends ConsumerState<SendView> {
if (_amountToSend == null) {
setState(() {
_calculateFeesFuture =
calculateFees(0.toAmount(fractionDigits: coin.decimals));
calculateFees(0.toAmountAsRaw(fractionDigits: coin.decimals));
});
} else {
setState(() {
@ -1526,11 +1526,11 @@ class _SendViewState extends ConsumerState<SendView> {
.item1;
if (_price == Decimal.zero) {
_amountToSend = 0.toAmount(
_amountToSend = 0.toAmountAsRaw(
fractionDigits: coin.decimals);
} else {
_amountToSend = baseAmount <= Amount.zero
? 0.toAmount(
? 0.toAmountAsRaw(
fractionDigits: coin.decimals)
: (baseAmount.decimal / _price)
.toDouble()
@ -1558,8 +1558,8 @@ class _SendViewState extends ConsumerState<SendView> {
cryptoAmountController.text = amountString;
_cryptoAmountChangeLock = false;
} else {
_amountToSend =
0.toAmount(fractionDigits: coin.decimals);
_amountToSend = 0.toAmountAsRaw(
fractionDigits: coin.decimals);
_cryptoAmountChangeLock = true;
cryptoAmountController.text = "";
_cryptoAmountChangeLock = false;

View file

@ -83,7 +83,7 @@ class _TransactionDetailsViewState
coin = widget.coin;
amount = _transaction.realAmount;
fee = _transaction.fee.toAmount(fractionDigits: coin.decimals);
fee = _transaction.fee.toAmountAsRaw(fractionDigits: coin.decimals);
if ((coin == Coin.firo || coin == Coin.firoTestNet) &&
_transaction.subType == TransactionSubType.mint) {

View file

@ -43,9 +43,7 @@ class Amount {
BigInt get raw => _value;
/// actual decimal vale represented
Decimal get decimal =>
(Decimal.fromBigInt(_value) / _ten.pow(fractionDigits).toDecimal())
.toDecimal(scaleOnInfinitePrecision: fractionDigits);
Decimal get decimal => Decimal.fromBigInt(raw).shift(-1 * fractionDigits);
/// convenience getter
@Deprecated("provided for convenience only. Use fractionDigits instead.")
@ -69,16 +67,21 @@ class Amount {
decimalPlaces ??= fractionDigits;
assert(decimalPlaces >= 0);
final wholeNumber = decimal.truncate();
if (decimalPlaces == 0) {
return wholeNumber.toStringAsFixed(0);
}
final String separator =
(numberFormatSymbols[locale] as NumberSymbols?)?.DECIMAL_SEP ??
(numberFormatSymbols[locale.substring(0, 2)] as NumberSymbols?)
?.DECIMAL_SEP ??
".";
final intValue = decimal.shift(fractionDigits).toBigInt();
final fraction = (raw - intValue).toDecimal();
final fraction = decimal - wholeNumber;
return "$intValue$separator${fraction.toStringAsFixed(decimalPlaces).substring(2)}";
return "${wholeNumber.toStringAsFixed(0)}$separator${fraction.toStringAsFixed(decimalPlaces).substring(2)}";
}
// ===========================================================================
@ -95,13 +98,13 @@ class Amount {
// ===========================================================================
// ======= operators =========================================================
bool operator >(Amount other) => raw > other.raw;
bool operator >(Amount other) => decimal > other.decimal;
bool operator <(Amount other) => raw < other.raw;
bool operator <(Amount other) => decimal < other.decimal;
bool operator >=(Amount other) => raw >= other.raw;
bool operator >=(Amount other) => decimal >= other.decimal;
bool operator <=(Amount other) => raw <= other.raw;
bool operator <=(Amount other) => decimal <= other.decimal;
Amount operator +(Amount other) {
if (fractionDigits != other.fractionDigits) {
@ -177,7 +180,7 @@ extension DoubleAmountExt on double {
}
extension IntAmountExtension on int {
Amount toAmount({required int fractionDigits}) {
Amount toAmountAsRaw({required int fractionDigits}) {
return Amount(
rawValue: BigInt.from(this),
fractionDigits: fractionDigits,

View file

@ -79,6 +79,77 @@ void main() {
expect(didThrow, true);
});
group("serialization", () {
test("toMap", () {
expect(
Amount(rawValue: BigInt.two, fractionDigits: 8).toMap(),
{"raw": "2", "fractionDigits": 8},
);
expect(
Amount.fromDecimal(Decimal.fromInt(2), fractionDigits: 8).toMap(),
{"raw": "200000000", "fractionDigits": 8},
);
expect(
Amount.fromDouble(2, fractionDigits: 8).toMap(),
{"raw": "200000000", "fractionDigits": 8},
);
});
test("toJsonString", () {
expect(
Amount(rawValue: BigInt.two, fractionDigits: 8).toJsonString(),
'{"raw":"2","fractionDigits":8}',
);
expect(
Amount.fromDecimal(Decimal.fromInt(2), fractionDigits: 8)
.toJsonString(),
'{"raw":"200000000","fractionDigits":8}',
);
expect(
Amount.fromDouble(2, fractionDigits: 8).toJsonString(),
'{"raw":"200000000","fractionDigits":8}',
);
});
test("localizedStringAsFixed", () {
expect(
Amount(rawValue: BigInt.two, fractionDigits: 8)
.localizedStringAsFixed(locale: "en_US"),
"0.00000002",
);
expect(
Amount(rawValue: BigInt.two, fractionDigits: 8)
.localizedStringAsFixed(locale: "en_US", decimalPlaces: 2),
"0.00",
);
expect(
Amount.fromDecimal(Decimal.fromInt(2), fractionDigits: 8)
.localizedStringAsFixed(locale: "en_US"),
"2.00000000",
);
expect(
Amount.fromDecimal(Decimal.fromInt(2), fractionDigits: 8)
.localizedStringAsFixed(locale: "en_US", decimalPlaces: 4),
"2.0000",
);
expect(
Amount.fromDecimal(Decimal.fromInt(2), fractionDigits: 8)
.localizedStringAsFixed(locale: "en_US", decimalPlaces: 0),
"2",
);
});
});
group("deserialization", () {
test("fromSerializedJsonString", () {
expect(
Amount.fromSerializedJsonString(
'{"raw":"200000000","fractionDigits":8}'),
Amount.fromDouble(2, fractionDigits: 8),
);
});
});
group("operators", () {
final one = Amount(rawValue: BigInt.one, fractionDigits: 0);
final two = Amount(rawValue: BigInt.two, fractionDigits: 0);