diff --git a/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart b/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart index 721773da6d..5e18f1e315 100644 --- a/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart +++ b/lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart @@ -35,6 +35,7 @@ import '../../crypto_currency/crypto_currency.dart'; import '../../models/tx_data.dart'; import '../intermediate/bip39_wallet.dart'; import '../supporting/mimblewimblecoin_wallet_info_extension.dart'; +import '../supporting/mimblewimblecoin_wallet_math.dart'; import '../supporting/restore_progress.dart'; class MimblewimblecoinWallet extends Bip39Wallet { @@ -614,9 +615,15 @@ class MimblewimblecoinWallet extends Bip39Wallet { int satoshiAmount, { bool ifErrorEstimateFee = false, }) async { + final int available = info.cachedBalance.spendable.raw.toInt(); + // With nothing to spend, libmwc treats amount == available as send all, + // retries with amount minus the required fee and aborts the app on the + // negative amount. There is no fee to estimate, so skip the call. + if (shouldSkipMwcFeeEstimate(amount: satoshiAmount, available: available)) { + return 0; + } final wallet = await _ensureWalletOpen(); try { - final available = info.cachedBalance.spendable.raw.toInt(); final transactionFees = await libMwc.getTransactionFees( wallet: wallet, amount: satoshiAmount, @@ -837,16 +844,7 @@ class MimblewimblecoinWallet extends Bip39Wallet { } int _calculateRestoreHeightFrom({required DateTime date}) { - final int secondsSinceEpoch = date.millisecondsSinceEpoch ~/ 1000; - const int mimblewimblecoinFirstBlock = 1573462800; - const double overestimateSecondsPerBlock = 61; - final int chosenSeconds = secondsSinceEpoch - mimblewimblecoinFirstBlock; - final int approximateHeight = chosenSeconds ~/ overestimateSecondsPerBlock; - int height = approximateHeight; - if (height < 0) { - height = 0; - } - return height; + return estimateMwcRestoreHeight(date); } // ============== Overrides ================================================== diff --git a/lib/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart b/lib/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart new file mode 100644 index 0000000000..fa9b36fb83 --- /dev/null +++ b/lib/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart @@ -0,0 +1,11 @@ +bool shouldSkipMwcFeeEstimate({required int amount, required int available}) => + amount <= 0 || available <= 0; + +int estimateMwcRestoreHeight(DateTime date) { + final int secondsSinceEpoch = date.millisecondsSinceEpoch ~/ 1000; + const int mimblewimblecoinFirstBlock = 1573462801; + const double overestimateSecondsPerBlock = 61; + final int chosenSeconds = secondsSinceEpoch - mimblewimblecoinFirstBlock; + final int approximateHeight = chosenSeconds ~/ overestimateSecondsPerBlock; + return approximateHeight < 0 ? 0 : approximateHeight; +} diff --git a/test/wallets/mimblewimblecoin_wallet_math_test.dart b/test/wallets/mimblewimblecoin_wallet_math_test.dart new file mode 100644 index 0000000000..c24bfb9371 --- /dev/null +++ b/test/wallets/mimblewimblecoin_wallet_math_test.dart @@ -0,0 +1,33 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:stackwallet/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart'; + +void main() { + test("MWC fee estimation is skipped only for unusable inputs", () { + expect(shouldSkipMwcFeeEstimate(amount: 0, available: 1), isTrue); + expect(shouldSkipMwcFeeEstimate(amount: -1, available: 1), isTrue); + expect(shouldSkipMwcFeeEstimate(amount: 1, available: 0), isTrue); + expect(shouldSkipMwcFeeEstimate(amount: 1, available: -1), isTrue); + expect(shouldSkipMwcFeeEstimate(amount: 1, available: 1), isFalse); + }); + + test("MWC restore height uses its exact first-block timestamp", () { + final genesis = DateTime.fromMillisecondsSinceEpoch( + 1573462801 * 1000, + isUtc: true, + ); + + expect(estimateMwcRestoreHeight(genesis), 0); + expect( + estimateMwcRestoreHeight(genesis.add(const Duration(seconds: 60))), + 0, + ); + expect( + estimateMwcRestoreHeight(genesis.add(const Duration(seconds: 61))), + 1, + ); + expect( + estimateMwcRestoreHeight(genesis.subtract(const Duration(days: 1))), + 0, + ); + }); +}