added different digits (decimal points) for formatters, some refactoring

This commit is contained in:
leo 2024-03-20 11:17:48 +00:00
parent 662aba5d72
commit 9d9fe4a5a6
9 changed files with 42 additions and 132 deletions

View file

@ -11,24 +11,35 @@ class ZanoFormatter {
..maximumFractionDigits = defaultDecimalPoint ..maximumFractionDigits = defaultDecimalPoint
..minimumFractionDigits = 1; ..minimumFractionDigits = 1;
static Decimal _intDivision({required int amount, required BigInt divider}) => (Decimal.fromInt(amount) / Decimal.fromBigInt(divider)).toDecimal();
static Decimal _bigIntDivision({required BigInt amount, required BigInt divider}) => static Decimal _bigIntDivision({required BigInt amount, required BigInt divider}) =>
(Decimal.fromBigInt(amount) / Decimal.fromBigInt(divider)).toDecimal(); (Decimal.fromBigInt(amount) / Decimal.fromBigInt(divider)).toDecimal();
static String intAmountToString(int amount, [int decimalPoint = defaultDecimalPoint]) => numberFormat.format( static String intAmountToString(int amount, [int decimalPoint = defaultDecimalPoint]) => numberFormat
.format(
DecimalIntl( DecimalIntl(
_intDivision( _bigIntDivision(
amount: amount, amount: BigInt.from(amount),
divider: BigInt.from(pow(10, decimalPoint)), divider: BigInt.from(pow(10, decimalPoint)),
), ),
), ),
).replaceAll(',', ''); )
static String bigIntAmountToString(BigInt amount, [int decimalPoint = defaultDecimalPoint]) => numberFormat.format( .replaceAll(',', '');
static String bigIntAmountToString(BigInt amount, [int decimalPoint = defaultDecimalPoint]) => numberFormat
.format(
DecimalIntl( DecimalIntl(
_bigIntDivision( _bigIntDivision(
amount: amount, amount: amount,
divider: BigInt.from(pow(10, decimalPoint)), divider: BigInt.from(pow(10, decimalPoint)),
), ),
), ),
).replaceAll(',', ''); )
.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]) =>
(Decimal.parse(amount) * Decimal.fromBigInt(BigInt.from(10).pow(decimalPoint))).toBigInt().toInt();
} }

View file

@ -1,7 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'package:cw_zano/api/api_calls.dart'; import 'package:cw_zano/api/api_calls.dart';
import 'package:cw_zano/api/model/get_address_info_result.dart'; import 'package:cw_zano/api/model/get_address_info_result.dart';
import 'package:cw_zano/zano_wallet_api.dart';
class ZanoUtils { class ZanoUtils {
static bool validateAddress(String address) { static bool validateAddress(String address) {

View file

@ -42,7 +42,6 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
static const int _autoSaveInterval = 30; static const int _autoSaveInterval = 30;
List<Transfer> transfers = []; List<Transfer> transfers = [];
//String defaultAsssetId = '';
@override @override
ZanoWalletAddresses walletAddresses; ZanoWalletAddresses walletAddresses;
@ -200,6 +199,7 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
comment: outputs.first.note ?? '', comment: outputs.first.note ?? '',
assetId: assetId, assetId: assetId,
ticker: credentials.currency.title, ticker: credentials.currency.title,
decimalPoint: credentials.currency.decimals,
amount: totalAmount, amount: totalAmount,
); );
} }

View file

@ -23,7 +23,6 @@ import 'package:cw_zano/zano_formatter.dart';
import 'package:cw_zano/zano_wallet.dart'; import 'package:cw_zano/zano_wallet.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
class ZanoNewWalletCredentials extends WalletCredentials { class ZanoNewWalletCredentials extends WalletCredentials {
ZanoNewWalletCredentials({required String name, String? password}) : super(name: name, password: password); ZanoNewWalletCredentials({required String name, String? password}) : super(name: name, password: password);

View file

@ -119,7 +119,7 @@ class TransactionListItem extends ActionListItem with Keyable {
final asset = zano!.assetOfTransaction(balanceViewModel.wallet, transaction); final asset = zano!.assetOfTransaction(balanceViewModel.wallet, transaction);
final price = balanceViewModel.fiatConvertationStore.prices[asset]; final price = balanceViewModel.fiatConvertationStore.prices[asset];
amount = calculateFiatAmountRaw( amount = calculateFiatAmountRaw(
cryptoAmount: zano!.formatterMoneroAmountToDouble(amount: transaction.amount), cryptoAmount: zano!.formatterIntAmountToDouble(amount: transaction.amount, currency: asset),
price: price); price: price);
break; break;
default: default:

View file

@ -98,7 +98,7 @@ abstract class OutputBase with Store {
_amount = polygon!.formatterPolygonParseAmount(_cryptoAmount); _amount = polygon!.formatterPolygonParseAmount(_cryptoAmount);
break; break;
case WalletType.zano: case WalletType.zano:
_amount = zano!.formatterMoneroParseAmount(amount: _cryptoAmount); _amount = zano!.formatterParseAmount(amount: _cryptoAmount, currency: cryptoCurrencyHandler());
break; break;
default: default:
break; break;
@ -144,7 +144,7 @@ abstract class OutputBase with Store {
} }
if (_wallet.type == WalletType.zano) { if (_wallet.type == WalletType.zano) {
return zano!.formatterMoneroAmountToDouble(amount: fee); return zano!.formatterIntAmountToDouble(amount: fee, currency: cryptoCurrencyHandler());
} }
} catch (e) { } catch (e) {
print(e.toString()); print(e.toString());

View file

@ -373,6 +373,12 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo
addressList.add(WalletAddressListItem(isPrimary: true, name: null, address: primaryAddress)); addressList.add(WalletAddressListItem(isPrimary: true, name: null, address: primaryAddress));
} }
if (wallet.type == WalletType.zano) {
final primaryAddress = zano!.getAddress(wallet);
addressList.add(WalletAddressListItem(isPrimary: true, name: null, address: primaryAddress));
}
if (searchText.isNotEmpty) { if (searchText.isNotEmpty) {
return ObservableList.of(addressList.where((item) { return ObservableList.of(addressList.where((item) {
if (item is WalletAddressListItem) { if (item is WalletAddressListItem) {

View file

@ -186,37 +186,16 @@ class CWZano extends Zano {
); );
} }
// @override
// String formatterMoneroAmountToString({required int amount}) {
// return moneroAmountToString(amount: amount);
// }
@override @override
double formatterMoneroAmountToDouble({required int amount}) { double formatterIntAmountToDouble({required int amount, required CryptoCurrency currency}) {
return moneroAmountToDouble(amount: amount); if (currency is ZanoAsset) return ZanoFormatter.intAmountToDouble(amount, currency.decimalPoint);
return ZanoFormatter.intAmountToDouble(amount);
} }
@override @override
int formatterMoneroParseAmount({required String amount}) { int formatterParseAmount({required String amount, required CryptoCurrency currency}) {
return moneroParseAmount(amount: amount); if (currency is ZanoAsset) return ZanoFormatter.parseAmount(amount, currency.decimalPoint);
} return ZanoFormatter.parseAmount(amount);
// @override
// Account getCurrentAccount(Object wallet) {
// final zanoWallet = wallet as ZanoWallet;
// final acc = zanoWallet.walletAddresses.account as monero_account.Account;
// return Account(id: acc.id, label: acc.label);
// }
// @override
// void setCurrentAccount(Object wallet, int id, String label) {
// final zanoWallet = wallet as ZanoWallet;
// zanoWallet.walletAddresses.account = monero_account.Account(id: id, label: label);
// }
@override
void onStartup() {
debugPrint("onStartup");
} }
@override @override
@ -230,12 +209,6 @@ class CWZano extends Zano {
return ZanoWalletService(walletInfoSource); return ZanoWalletService(walletInfoSource);
} }
// @override
// String getTransactionAddress(Object wallet, int accountIndex, int addressIndex) {
// final zanoWallet = wallet as ZanoWallet;
// return zanoWallet.getTransactionAddress(accountIndex, addressIndex);
// }
@override @override
CryptoCurrency assetOfTransaction(WalletBase wallet, TransactionInfo transaction) { CryptoCurrency assetOfTransaction(WalletBase wallet, TransactionInfo transaction) {
transaction as ZanoTransactionInfo; transaction as ZanoTransactionInfo;
@ -247,4 +220,7 @@ class CWZano extends Zano {
} }
String getZanoAssetAddress(CryptoCurrency asset) => (asset as ZanoAsset).assetId; String getZanoAssetAddress(CryptoCurrency asset) => (asset as ZanoAsset).assetId;
@override
String getAddress(WalletBase wallet) => (wallet as ZanoWallet).walletAddresses.address;
} }

View file

@ -2,6 +2,7 @@ import 'package:cake_wallet/utils/language_list.dart';
import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_base.dart';
import 'package:cw_zano/model/zano_asset.dart'; import 'package:cw_zano/model/zano_asset.dart';
import 'package:cw_zano/model/zano_transaction_credentials.dart'; import 'package:cw_zano/model/zano_transaction_credentials.dart';
import 'package:cw_zano/zano_formatter.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:cw_core/wallet_credentials.dart'; import 'package:cw_core/wallet_credentials.dart';
@ -26,68 +27,8 @@ part 'cw_zano.dart';
Zano? zano = CWZano(); Zano? zano = CWZano();
// class Account {
// Account({required this.id, required this.label});
// final int id;
// final String label;
// }
// class Subaddress {
// Subaddress({
// required this.id,
// required this.label,
// required this.address});
// final int id;
// final String label;
// final String address;
// }
/*class ZanoBalance extends Balance {
ZanoBalance({required this.fullBalance, required this.unlockedBalance})
: formattedFullBalance = zano!.formatterMoneroAmountToString(amount: fullBalance),
formattedUnlockedBalance =
zano!.formatterMoneroAmountToString(amount: unlockedBalance),
super(unlockedBalance, fullBalance);
ZanoBalance.fromString(
{required this.formattedFullBalance,
required this.formattedUnlockedBalance})
: fullBalance = zano!.formatterMoneroParseAmount(amount: formattedFullBalance),
unlockedBalance = zano!.formatterMoneroParseAmount(amount: formattedUnlockedBalance),
super(zano!.formatterMoneroParseAmount(amount: formattedUnlockedBalance),
zano!.formatterMoneroParseAmount(amount: formattedFullBalance));
final int fullBalance;
final int unlockedBalance;
final String formattedFullBalance;
final String formattedUnlockedBalance;
@override
String get formattedAvailableBalance => formattedUnlockedBalance;
@override
String get formattedAdditionalBalance => formattedFullBalance;
}*/
/*abstract class ZanoWalletDetails {
// FIX-ME: it's abstruct class
// @observable
// late Account account;
// FIX-ME: it's abstruct class
@observable
late ZanoBalance balance;
}*/
abstract class Zano { abstract class Zano {
/**ZanoAccountList getAccountList(Object wallet);*/
TransactionHistoryBase getTransactionHistory(Object wallet); TransactionHistoryBase getTransactionHistory(Object wallet);
//ZanoWalletDetails getZanoWalletDetails(Object wallet);
// String getTransactionAddress(Object wallet, int accountIndex, int addressIndex);
TransactionPriority getDefaultTransactionPriority(); TransactionPriority getDefaultTransactionPriority();
TransactionPriority deserializeMoneroTransactionPriority({required int raw}); TransactionPriority deserializeMoneroTransactionPriority({required int raw});
List<TransactionPriority> getTransactionPriorities(); List<TransactionPriority> getTransactionPriorities();
@ -105,12 +46,8 @@ abstract class Zano {
WalletCredentials createZanoNewWalletCredentials({required String name, String password}); WalletCredentials createZanoNewWalletCredentials({required String name, String password});
Map<String, String> getKeys(Object wallet); Map<String, String> getKeys(Object wallet);
Object createZanoTransactionCredentials({required List<Output> outputs, required TransactionPriority priority, required CryptoCurrency currency}); Object createZanoTransactionCredentials({required List<Output> outputs, required TransactionPriority priority, required CryptoCurrency currency});
// String formatterMoneroAmountToString({required int amount}); double formatterIntAmountToDouble({required int amount, required CryptoCurrency currency});
double formatterMoneroAmountToDouble({required int amount}); int formatterParseAmount({required String amount, required CryptoCurrency currency});
int formatterMoneroParseAmount({required String amount});
// Account getCurrentAccount(Object wallet);
// void setCurrentAccount(Object wallet, int id, String label);
void onStartup();
int getTransactionInfoAccountId(TransactionInfo tx); int getTransactionInfoAccountId(TransactionInfo tx);
WalletService createZanoWalletService(Box<WalletInfo> walletInfoSource); WalletService createZanoWalletService(Box<WalletInfo> walletInfoSource);
CryptoCurrency assetOfTransaction(WalletBase wallet, TransactionInfo tx); CryptoCurrency assetOfTransaction(WalletBase wallet, TransactionInfo tx);
@ -120,24 +57,5 @@ abstract class Zano {
Future<CryptoCurrency> addZanoAssetById(WalletBase wallet, String assetId); Future<CryptoCurrency> addZanoAssetById(WalletBase wallet, String assetId);
Future<void> deleteZanoAsset(WalletBase wallet, CryptoCurrency token); Future<void> deleteZanoAsset(WalletBase wallet, CryptoCurrency token);
Future<CryptoCurrency?> getZanoAsset(WalletBase wallet, String contractAddress); Future<CryptoCurrency?> getZanoAsset(WalletBase wallet, String contractAddress);
String getAddress(WalletBase wallet);
} }
// abstract class MoneroSubaddressList {
// ObservableList<Subaddress> get subaddresses;
// void update(Object wallet, {required int accountIndex});
// void refresh(Object wallet, {required int accountIndex});
// List<Subaddress> getAll(Object wallet);
// Future<void> addSubaddress(Object wallet, {required int accountIndex, required String label});
// Future<void> setLabelSubaddress(Object wallet,
// {required int accountIndex, required int addressIndex, required String label});
// }
// abstract class ZanoAccountList {
// ObservableList<Account> get accounts;
// void update(Object wallet);
// void refresh(Object wallet);
// List<Account> getAll(Object wallet);
// Future<void> addAccount(Object wallet, {required String label});
// Future<void> setLabelAccount(Object wallet, {required int accountIndex, required String label});
// }