cake_wallet/cw_zano/lib/zano_formatter.dart
Omar Hatem 141a7ebfca
v4.23.0 release candidate ()
* v4.23.0 release candidate

* - Fix restoring zano from QR
- Fix Zano confirmations count
- Fix birdpay
- Fix balance display

* Fix Zano assets showing amount before they are added

* - handle fetching token data while the API is busy
- potential fix for duplicate transactions

* fix receive confirmations, maybe

* revert onChangeWallet cleanup

* Fix confirmations not updating

* improve zano wallet opening, fix CI commands and messages on slack ()

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* Cache wallet when creating/restoring as well

* - hardcode Trocador Maximum limit for Zano temporarily
- Configure Cake Zano node to use SSL

* reformatting [skip ci]

* revert to non-ssl

* update build numbers [skip ci]

* disable zano for desktop [skip ci]

---------

Co-authored-by: cyan <cyjan@mrcyjanek.net>
2025-01-28 23:53:43 +02:00

78 lines
2.6 KiB
Dart

import 'dart:math';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_zano/zano_wallet_api.dart';
import 'package:decimal/decimal.dart';
import 'package:decimal/intl.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:intl/intl.dart';
class ZanoFormatter {
static const defaultDecimalPoint = 12;
//static final numberFormat = NumberFormat()
// ..maximumFractionDigits = defaultDecimalPoint
// ..minimumFractionDigits = 1;
static Decimal _bigIntDivision({required BigInt amount, required BigInt divider}) {
return (Decimal.fromBigInt(amount) / Decimal.fromBigInt(divider)).toDecimal();
}
static String intAmountToString(int amount, [int decimalPoint = defaultDecimalPoint]) {
final numberFormat = NumberFormat()..maximumFractionDigits = decimalPoint
..minimumFractionDigits = 1;
return numberFormat.format(
DecimalIntl(
_bigIntDivision(
amount: BigInt.from(amount),
divider: BigInt.from(pow(10, decimalPoint)),
),
),
)
.replaceAll(',', '');
}
static String bigIntAmountToString(BigInt amount, [int decimalPoint = defaultDecimalPoint]) {
if (decimalPoint == 0) {
return '0';
}
final numberFormat = NumberFormat()..maximumFractionDigits = decimalPoint
..minimumFractionDigits = 1;
return numberFormat.format(
DecimalIntl(
_bigIntDivision(
amount: amount,
divider: BigInt.from(pow(10, decimalPoint)),
),
),
)
.replaceAll(',', '');
}
static double intAmountToDouble(int amount, [int decimalPoint = defaultDecimalPoint]) => _bigIntDivision(
amount: BigInt.from(amount),
divider: BigInt.from(pow(10, decimalPoint)),
).toDouble();
static int parseAmount(String amount, [int decimalPoint = defaultDecimalPoint]) {
final resultBigInt = (Decimal.parse(amount) * Decimal.fromBigInt(BigInt.from(10).pow(decimalPoint))).toBigInt();
if (!resultBigInt.isValidInt) {
Fluttertoast.showToast(msg: 'Cannot transfer $amount. Maximum is ${intAmountToString(resultBigInt.toInt(), decimalPoint)}.');
}
return resultBigInt.toInt();
}
static BigInt bigIntFromDynamic(dynamic d) {
if (d is int) {
return BigInt.from(d);
} else if (d is BigInt) {
return d;
} else if (d == null) {
return BigInt.zero;
} else {
printV(('cannot cast value of type ${d.runtimeType} to BigInt'));
throw 'cannot cast value of type ${d.runtimeType} to BigInt';
//return BigInt.zero;
}
}
}