2020-09-21 11:50:26 +00:00
|
|
|
import 'package:intl/intl.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
|
|
|
|
class AmountConverter {
|
|
|
|
static const _moneroAmountLength = 12;
|
|
|
|
static const _moneroAmountDivider = 1000000000000;
|
2024-06-06 21:36:54 +00:00
|
|
|
static const _wowneroAmountLength = 11;
|
|
|
|
static const _wowneroAmountDivider = 100000000000;
|
2020-09-21 11:50:26 +00:00
|
|
|
static const _bitcoinAmountDivider = 100000000;
|
|
|
|
static const _bitcoinAmountLength = 8;
|
|
|
|
static final _bitcoinAmountFormat = NumberFormat()
|
|
|
|
..maximumFractionDigits = _bitcoinAmountLength
|
|
|
|
..minimumFractionDigits = 1;
|
|
|
|
static final _moneroAmountFormat = NumberFormat()
|
|
|
|
..maximumFractionDigits = _moneroAmountLength
|
|
|
|
..minimumFractionDigits = 1;
|
2024-06-06 21:36:54 +00:00
|
|
|
static final _wowneroAmountFormat = NumberFormat()
|
|
|
|
..maximumFractionDigits = _wowneroAmountLength
|
|
|
|
..minimumFractionDigits = 1;
|
2020-09-21 11:50:26 +00:00
|
|
|
|
|
|
|
static int amountStringToInt(CryptoCurrency cryptoCurrency, String amount) {
|
|
|
|
switch (cryptoCurrency) {
|
|
|
|
case CryptoCurrency.xmr:
|
|
|
|
return _moneroParseAmount(amount);
|
2022-03-30 15:57:04 +00:00
|
|
|
case CryptoCurrency.xhv:
|
|
|
|
case CryptoCurrency.xag:
|
|
|
|
case CryptoCurrency.xau:
|
|
|
|
case CryptoCurrency.xaud:
|
|
|
|
case CryptoCurrency.xbtc:
|
|
|
|
case CryptoCurrency.xcad:
|
|
|
|
case CryptoCurrency.xchf:
|
|
|
|
case CryptoCurrency.xcny:
|
|
|
|
case CryptoCurrency.xeur:
|
|
|
|
case CryptoCurrency.xgbp:
|
|
|
|
case CryptoCurrency.xjpy:
|
|
|
|
case CryptoCurrency.xnok:
|
|
|
|
case CryptoCurrency.xnzd:
|
|
|
|
case CryptoCurrency.xusd:
|
|
|
|
return _moneroParseAmount(amount);
|
2020-09-21 11:50:26 +00:00
|
|
|
default:
|
2022-10-12 17:09:57 +00:00
|
|
|
return 0;
|
2020-09-21 11:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static String amountIntToString(CryptoCurrency cryptoCurrency, int amount) {
|
|
|
|
switch (cryptoCurrency) {
|
|
|
|
case CryptoCurrency.xmr:
|
|
|
|
return _moneroAmountToString(amount);
|
2024-06-06 21:36:54 +00:00
|
|
|
case CryptoCurrency.wow:
|
|
|
|
return _wowneroAmountToString(amount);
|
2020-09-21 11:50:26 +00:00
|
|
|
case CryptoCurrency.btc:
|
2023-10-12 22:50:16 +00:00
|
|
|
case CryptoCurrency.bch:
|
2024-01-23 05:15:24 +00:00
|
|
|
case CryptoCurrency.ltc:
|
2020-09-21 11:50:26 +00:00
|
|
|
return _bitcoinAmountToString(amount);
|
2024-02-29 20:39:49 +00:00
|
|
|
case CryptoCurrency.btcln:
|
|
|
|
return _lightningAmountToString(amount);
|
2022-03-30 15:57:04 +00:00
|
|
|
case CryptoCurrency.xhv:
|
|
|
|
case CryptoCurrency.xag:
|
|
|
|
case CryptoCurrency.xau:
|
|
|
|
case CryptoCurrency.xaud:
|
|
|
|
case CryptoCurrency.xbtc:
|
|
|
|
case CryptoCurrency.xcad:
|
|
|
|
case CryptoCurrency.xchf:
|
|
|
|
case CryptoCurrency.xcny:
|
|
|
|
case CryptoCurrency.xeur:
|
|
|
|
case CryptoCurrency.xgbp:
|
|
|
|
case CryptoCurrency.xjpy:
|
|
|
|
case CryptoCurrency.xnok:
|
|
|
|
case CryptoCurrency.xnzd:
|
|
|
|
case CryptoCurrency.xusd:
|
|
|
|
return _moneroAmountToString(amount);
|
2020-09-21 11:50:26 +00:00
|
|
|
default:
|
2022-10-12 17:09:57 +00:00
|
|
|
return '';
|
2020-09-21 11:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
static double cryptoAmountToDouble({required num amount, required num divider}) =>
|
2020-09-21 11:50:26 +00:00
|
|
|
amount / divider;
|
|
|
|
|
2024-06-06 21:36:54 +00:00
|
|
|
static String _moneroAmountToString(int amount) => _moneroAmountFormat
|
|
|
|
.format(cryptoAmountToDouble(amount: amount, divider: _moneroAmountDivider));
|
2020-09-21 11:50:26 +00:00
|
|
|
|
2024-06-06 21:36:54 +00:00
|
|
|
static String _bitcoinAmountToString(int amount) => _bitcoinAmountFormat
|
|
|
|
.format(cryptoAmountToDouble(amount: amount, divider: _bitcoinAmountDivider));
|
2020-09-21 11:50:26 +00:00
|
|
|
|
2024-02-29 20:39:49 +00:00
|
|
|
static String _lightningAmountToString(int amount) {
|
|
|
|
String formattedAmount = _bitcoinAmountFormat.format(amount);
|
|
|
|
return formattedAmount.substring(0, formattedAmount.length - 2);
|
|
|
|
}
|
|
|
|
|
2024-06-06 21:36:54 +00:00
|
|
|
static String _wowneroAmountToString(int amount) => _wowneroAmountFormat
|
|
|
|
.format(cryptoAmountToDouble(amount: amount, divider: _wowneroAmountDivider));
|
2024-06-30 02:43:52 +00:00
|
|
|
|
|
|
|
static int _moneroParseAmount(String amount) =>
|
|
|
|
_moneroAmountFormat.parse(amount).toInt();
|
2020-09-21 11:50:26 +00:00
|
|
|
}
|