From 5af75aa7ad8bf0a575e1dec77af23385f1b16b8e Mon Sep 17 00:00:00 2001 From: leo1234 Date: Mon, 18 Mar 2024 20:15:54 +0800 Subject: [PATCH] transfers for multiple destinations and send all, some refactoring --- cw_zano/lib/api/api_calls.dart | 9 ++++-- cw_zano/lib/api/convert_utf8_to_string.dart | 8 ----- cw_zano/lib/api/model/asset_info.dart | 34 --------------------- cw_zano/lib/api/model/balance.dart | 1 - cw_zano/lib/pending_zano_transaction.dart | 2 +- cw_zano/lib/zano_balance.dart | 16 +++++----- cw_zano/lib/zano_formatter.dart | 1 - cw_zano/lib/zano_transaction_info.dart | 10 +++--- cw_zano/lib/zano_wallet.dart | 21 ++++++------- cw_zano/lib/zano_wallet_service.dart | 22 +++++++------ 10 files changed, 43 insertions(+), 81 deletions(-) delete mode 100644 cw_zano/lib/api/convert_utf8_to_string.dart delete mode 100644 cw_zano/lib/api/model/asset_info.dart diff --git a/cw_zano/lib/api/api_calls.dart b/cw_zano/lib/api/api_calls.dart index 65d80b933..428f5afd0 100644 --- a/cw_zano/lib/api/api_calls.dart +++ b/cw_zano/lib/api/api_calls.dart @@ -1,6 +1,5 @@ import 'dart:ffi'; -import 'package:cw_zano/api/convert_utf8_to_string.dart'; import 'package:cw_zano/api/structs/utf8_box.dart'; import 'package:cw_zano/api/zano_api.dart'; import 'package:ffi/ffi.dart'; @@ -55,12 +54,18 @@ typedef _SetPassword = Pointer Function(int hWallet, Pointer passwor typedef _stringFunction = Pointer Function(); class ApiCalls { + static String _convertUTF8ToString({required Pointer pointer}) { + final str = pointer.toDartString(); + calloc.free(pointer); + return str; + } + static String _performApiCall( Pointer Function() apiCall, { List>? pointersToFree, }) { try { - return convertUTF8ToString(pointer: apiCall()); + return _convertUTF8ToString(pointer: apiCall()); } finally { if (pointersToFree != null) { for (var pointer in pointersToFree) { diff --git a/cw_zano/lib/api/convert_utf8_to_string.dart b/cw_zano/lib/api/convert_utf8_to_string.dart deleted file mode 100644 index 41a6b648a..000000000 --- a/cw_zano/lib/api/convert_utf8_to_string.dart +++ /dev/null @@ -1,8 +0,0 @@ -import 'dart:ffi'; -import 'package:ffi/ffi.dart'; - -String convertUTF8ToString({required Pointer pointer}) { - final str = pointer.toDartString(); - calloc.free(pointer); - return str; -} \ No newline at end of file diff --git a/cw_zano/lib/api/model/asset_info.dart b/cw_zano/lib/api/model/asset_info.dart deleted file mode 100644 index 85dc23b6d..000000000 --- a/cw_zano/lib/api/model/asset_info.dart +++ /dev/null @@ -1,34 +0,0 @@ -// class AssetInfo { -// final String assetId; -// final int currentSupply; -// final int decimalPoint; -// final String fullName; -// final bool hiddenSupply; -// final String metaInfo; -// final String owner; -// final String ticker; -// final int totalMaxSupply; - -// AssetInfo( -// {required this.assetId, -// required this.currentSupply, -// required this.decimalPoint, -// required this.fullName, -// required this.hiddenSupply, -// required this.metaInfo, -// required this.owner, -// required this.ticker, -// required this.totalMaxSupply}); - -// factory AssetInfo.fromJson(Map json) => AssetInfo( -// assetId: json['asset_id'] as String? ?? '', -// currentSupply: json['current_supply'] as int? ?? 0, -// decimalPoint: json['decimal_point'] as int? ?? 0, -// fullName: json['full_name'] as String? ?? '', -// hiddenSupply: json['hidden_supply'] as bool, -// metaInfo: json['meta_info'] as String? ?? '', -// owner: json['owner'] as String? ?? '', -// ticker: json['ticker'] as String? ?? '', -// totalMaxSupply: json['total_max_supply'] as int? ?? 0, -// ); -// } diff --git a/cw_zano/lib/api/model/balance.dart b/cw_zano/lib/api/model/balance.dart index 0fa6b6460..e3bda0039 100644 --- a/cw_zano/lib/api/model/balance.dart +++ b/cw_zano/lib/api/model/balance.dart @@ -1,6 +1,5 @@ import 'package:cw_core/amount_converter.dart'; import 'package:cw_core/crypto_currency.dart'; -import 'package:cw_zano/api/model/asset_info.dart'; import 'package:cw_zano/zano_asset.dart'; class Balance { diff --git a/cw_zano/lib/pending_zano_transaction.dart b/cw_zano/lib/pending_zano_transaction.dart index 726dda7f4..98f5ec9cc 100644 --- a/cw_zano/lib/pending_zano_transaction.dart +++ b/cw_zano/lib/pending_zano_transaction.dart @@ -16,7 +16,7 @@ class PendingZanoTransaction with PendingTransaction { required this.comment, required this.assetId, required this.ticker, - this.decimalPoint = 12, + this.decimalPoint = ZanoFormatter.defaultDecimalPoint, required this.amount, }); diff --git a/cw_zano/lib/zano_balance.dart b/cw_zano/lib/zano_balance.dart index c1566a81c..53b4bdc43 100644 --- a/cw_zano/lib/zano_balance.dart +++ b/cw_zano/lib/zano_balance.dart @@ -1,20 +1,18 @@ -import 'package:cw_core/amount_converter.dart'; import 'package:cw_core/balance.dart'; -import 'package:cw_core/crypto_currency.dart'; -import 'package:cw_core/monero_amount_format.dart'; +import 'package:cw_zano/zano_formatter.dart'; class ZanoBalance extends Balance { final int total; final int unlocked; - ZanoBalance({required this.total, required this.unlocked}): super(unlocked, total-unlocked); + final int decimalPoint; + ZanoBalance({required this.total, required this.unlocked, required this.decimalPoint}) : super(unlocked, total - unlocked); @override - String get formattedAdditionalBalance => AmountConverter.amountIntToString(CryptoCurrency.zano, total-unlocked); + String get formattedAdditionalBalance => ZanoFormatter.intAmountToString(total - unlocked, decimalPoint); @override - String get formattedAvailableBalance => AmountConverter.amountIntToString(CryptoCurrency.zano, unlocked); - - @override - String get formattedFrozenBalance => ''; + String get formattedAvailableBalance => ZanoFormatter.intAmountToString(unlocked, decimalPoint); + // @override + // String get formattedFrozenBalance => ''; } diff --git a/cw_zano/lib/zano_formatter.dart b/cw_zano/lib/zano_formatter.dart index 2b8d4977d..9c9690c0d 100644 --- a/cw_zano/lib/zano_formatter.dart +++ b/cw_zano/lib/zano_formatter.dart @@ -7,7 +7,6 @@ import 'package:intl/intl.dart'; class ZanoFormatter { static const defaultDecimalPoint = 12; - //static const _moneroAmountLength = 12; static final numberFormat = NumberFormat() ..maximumFractionDigits = defaultDecimalPoint ..minimumFractionDigits = 1; diff --git a/cw_zano/lib/zano_transaction_info.dart b/cw_zano/lib/zano_transaction_info.dart index 7f3ad1885..594c2b879 100644 --- a/cw_zano/lib/zano_transaction_info.dart +++ b/cw_zano/lib/zano_transaction_info.dart @@ -1,8 +1,8 @@ import 'package:cw_core/format_amount.dart'; -import 'package:cw_core/monero_amount_format.dart'; import 'package:cw_core/transaction_direction.dart'; import 'package:cw_core/transaction_info.dart'; import 'package:cw_zano/api/model/transfer.dart'; +import 'package:cw_zano/zano_formatter.dart'; class ZanoTransactionInfo extends TransactionInfo { ZanoTransactionInfo({ @@ -18,9 +18,10 @@ class ZanoTransactionInfo extends TransactionInfo { required this.assetId, required this.confirmations, required this.tokenSymbol, + required this.decimalPoint, }); - ZanoTransactionInfo.fromTransfer(Transfer transfer, this.tokenSymbol) + ZanoTransactionInfo.fromTransfer(Transfer transfer, this.tokenSymbol, this.decimalPoint) : id = transfer.txHash, height = transfer.height, direction = transfer.subtransfers.first.isIncome ? TransactionDirection.incoming : TransactionDirection.outgoing, @@ -44,6 +45,7 @@ class ZanoTransactionInfo extends TransactionInfo { final int fee; final int addressIndex; final int confirmations; + final int decimalPoint; late String recipientAddress; final String tokenSymbol; late String assetId; @@ -51,7 +53,7 @@ class ZanoTransactionInfo extends TransactionInfo { String? key; @override - String amountFormatted() => '${formatAmount(moneroAmountToString(amount: amount))} $tokenSymbol'; + String amountFormatted() => '${formatAmount(ZanoFormatter.intAmountToString(amount, decimalPoint))} $tokenSymbol'; @override String fiatAmount() => _fiatAmount ?? ''; @@ -60,7 +62,7 @@ class ZanoTransactionInfo extends TransactionInfo { void changeFiatAmount(String amount) => _fiatAmount = formatAmount(amount); @override - String feeFormatted() => '${formatAmount(moneroAmountToString(amount: fee))} $feeCurrency'; + String feeFormatted() => '${formatAmount(ZanoFormatter.intAmountToString(fee))} $feeCurrency'; String get feeCurrency => 'ZANO'; } diff --git a/cw_zano/lib/zano_wallet.dart b/cw_zano/lib/zano_wallet.dart index 15098366b..f19a10dcf 100644 --- a/cw_zano/lib/zano_wallet.dart +++ b/cw_zano/lib/zano_wallet.dart @@ -3,7 +3,6 @@ import 'dart:io'; import 'package:cw_core/cake_hive.dart'; import 'package:cw_core/crypto_currency.dart'; -import 'package:cw_core/monero_amount_format.dart'; import 'package:cw_core/monero_wallet_utils.dart'; import 'package:cw_core/node.dart'; import 'package:cw_core/pathForWallet.dart'; @@ -77,7 +76,7 @@ abstract class ZanoWalletBase extends WalletBase element.assetId == item.subtransfers.first.assetId).ticker; - return ZanoTransactionInfo.fromTransfer(item, tokenSymbol); + final asset = zanoAssets.firstWhere((element) => element.assetId == item.subtransfers.first.assetId); + return ZanoTransactionInfo.fromTransfer(item, asset.ticker, asset.decimalPoint); } }, ); @@ -229,12 +228,12 @@ abstract class ZanoWalletBase extends WalletBase init(String address) async { - final boxName = "${walletInfo.name.replaceAll(" ", "_")}_${ZanoAsset.zanoAssetsBoxName}"; + final boxName = '${walletInfo.name.replaceAll(' ', '_')}_${ZanoAsset.zanoAssetsBoxName}'; zanoAssetsBox = await CakeHive.openBox(boxName); print( 'assets in box total: ${zanoAssetsBox.length} ${zanoAssetsBox.values} active: ${zanoAssetsBox.values.where((element) => element.enabled).length} ${zanoAssetsBox.values.where((element) => element.enabled)}'); for (final asset in zanoAssetsBox.values) { - if (asset.enabled) balance[asset] = ZanoBalance(total: 0, unlocked: 0); + if (asset.enabled) balance[asset] = ZanoBalance(total: 0, unlocked: 0, decimalPoint: asset.decimalPoint); } await walletAddresses.init(); await walletAddresses.updateAddress(address); @@ -336,11 +335,11 @@ abstract class ZanoWalletBase extends WalletBase