From deec0cdddbd350e07ad2a06227565974b67e69f3 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 11 Oct 2022 19:54:50 -0600 Subject: [PATCH] fix monero and wownero fee estimates for sending. fix some ui bugs --- .../transaction_fee_selection_sheet.dart | 4 +- lib/services/coins/monero/monero_wallet.dart | 39 ++++++++++++++++--- .../coins/wownero/wownero_wallet.dart | 35 +++++++++++++++-- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/lib/pages/send_view/sub_widgets/transaction_fee_selection_sheet.dart b/lib/pages/send_view/sub_widgets/transaction_fee_selection_sheet.dart index ff26fe782..05644cf70 100644 --- a/lib/pages/send_view/sub_widgets/transaction_fee_selection_sheet.dart +++ b/lib/pages/send_view/sub_widgets/transaction_fee_selection_sheet.dart @@ -410,8 +410,8 @@ class _TransactionFeeSelectionSheetState FutureBuilder( future: feeFor( coin: manager.coin, - feeRateType: FeeRateType.fast, - feeRate: feeObject!.fast, + feeRateType: FeeRateType.average, + feeRate: feeObject!.medium, amount: Format .decimalAmountToSatoshis( amount)), diff --git a/lib/services/coins/monero/monero_wallet.dart b/lib/services/coins/monero/monero_wallet.dart index 378295cee..b0ebac4e6 100644 --- a/lib/services/coins/monero/monero_wallet.dart +++ b/lib/services/coins/monero/monero_wallet.dart @@ -1451,7 +1451,7 @@ class MoneroWallet extends CoinServiceAPI { bool isSendAll = false; final balance = await availableBalance; final satInDecimal = ((Decimal.fromInt(satoshiAmount) / - Decimal.fromInt(Constants.satsPerCoinWownero)) + Decimal.fromInt(Constants.satsPerCoinMonero)) .toDecimal() * Decimal.fromInt(10000)); if (satInDecimal == balance) { @@ -1471,7 +1471,9 @@ class MoneroWallet extends CoinServiceAPI { Object tmp = monero.createMoneroTransactionCreationCredentials( outputs: outputs, priority: feePriority); - awaitPendingTransaction = walletBase!.createTransaction(tmp); + await prepareSendMutex.protect(() async { + awaitPendingTransaction = walletBase!.createTransaction(tmp); + }); } catch (e, s) { Logging.instance.log("Exception rethrown from prepareSend(): $e\n$s", level: LogLevel.Warning); @@ -1510,30 +1512,57 @@ class MoneroWallet extends CoinServiceAPI { } } + Mutex prepareSendMutex = Mutex(); + Mutex estimateFeeMutex = Mutex(); + @override Future estimateFeeFor(int satoshiAmount, int feeRate) async { MoneroTransactionPriority? priority; + FeeRateType feeRateType = FeeRateType.slow; switch (feeRate) { case 1: priority = MoneroTransactionPriority.regular; + feeRateType = FeeRateType.slow; break; case 2: priority = MoneroTransactionPriority.medium; + feeRateType = FeeRateType.average; break; case 3: priority = MoneroTransactionPriority.fast; + feeRateType = FeeRateType.average; break; case 4: priority = MoneroTransactionPriority.fastest; + feeRateType = FeeRateType.fast; break; case 0: default: priority = MoneroTransactionPriority.slow; + feeRateType = FeeRateType.slow; break; } - final fee = - (walletBase?.calculateEstimatedFee(priority, satoshiAmount) ?? 0) ~/ - 10000; + var aprox; + + await estimateFeeMutex.protect(() async { + { + try { + aprox = (await prepareSend( + // This address is only used for getting an approximate fee, never for sending + address: + "8347huhmj6Ggzr1BpZPJAD5oa96ob5Fe8GtQdGZDYVVYVsCgtUNH3pEEzExDuaAVZdC16D4FkAb24J6wUfsKkcZtC8EPXB7", + satoshiAmount: satoshiAmount, + args: {"feeRate": feeRateType}))['fee']; + await Future.delayed(const Duration(milliseconds: 1000)); + } catch (e, s) { + Logging.instance.log("$feeRateType $e $s", level: LogLevel.Error); + aprox = -9999999999999999; + } + } + }); + + print("this is the aprox fee $aprox for $satoshiAmount"); + final fee = (aprox as int); return fee; } diff --git a/lib/services/coins/wownero/wownero_wallet.dart b/lib/services/coins/wownero/wownero_wallet.dart index 687c25764..afdc49117 100644 --- a/lib/services/coins/wownero/wownero_wallet.dart +++ b/lib/services/coins/wownero/wownero_wallet.dart @@ -1477,7 +1477,9 @@ class WowneroWallet extends CoinServiceAPI { Object tmp = wownero.createWowneroTransactionCreationCredentials( outputs: outputs, priority: feePriority); - awaitPendingTransaction = walletBase!.createTransaction(tmp); + await prepareSendMutex.protect(() async { + awaitPendingTransaction = walletBase!.createTransaction(tmp); + }); } catch (e, s) { Logging.instance.log("Exception rethrown from prepareSend(): $e\n$s", level: LogLevel.Warning); @@ -1516,30 +1518,55 @@ class WowneroWallet extends CoinServiceAPI { } } + Mutex prepareSendMutex = Mutex(); + Mutex estimateFeeMutex = Mutex(); + @override Future estimateFeeFor(int satoshiAmount, int feeRate) async { MoneroTransactionPriority? priority; + FeeRateType feeRateType = FeeRateType.slow; switch (feeRate) { case 1: priority = MoneroTransactionPriority.regular; + feeRateType = FeeRateType.slow; break; case 2: priority = MoneroTransactionPriority.medium; + feeRateType = FeeRateType.average; break; case 3: priority = MoneroTransactionPriority.fast; + feeRateType = FeeRateType.average; break; case 4: priority = MoneroTransactionPriority.fastest; + feeRateType = FeeRateType.fast; break; case 0: default: priority = MoneroTransactionPriority.slow; + feeRateType = FeeRateType.slow; break; } - final fee = - (walletBase?.calculateEstimatedFee(priority, satoshiAmount) ?? 0) ~/ - 10000; + var aprox; + await estimateFeeMutex.protect(() async { + { + try { + aprox = (await prepareSend( + // This address is only used for getting an approximate fee, never for sending + address: + "WW3iVcnoAY6K9zNdU4qmdvZELefx6xZz4PMpTwUifRkvMQckyadhSPYMVPJhBdYE8P9c27fg9RPmVaWNFx1cDaj61HnetqBiy", + satoshiAmount: satoshiAmount, + args: {"feeRate": feeRateType}))['fee']; + await Future.delayed(const Duration(milliseconds: 100)); + } catch (e, s) { + aprox = -9999999999999999; + } + } + }); + + print("this is the aprox fee $aprox for $satoshiAmount"); + final fee = (aprox as int); return fee; }