Fallback to building a tx when estimateFee does not return a fee (eg sweep)
What changed, and why it matters
This change fixes how the wallet calculates transaction fees when sending the entire balance (a 'sweep'). Previously, the fee estimator could fail or return nothing for sweep transactions. Now, when no fee estimate is available, the wallet falls back to actually building the transaction to get the real fee. This is a correctness/reliability fix rather than a typical security bug, but it prevents users from being shown wrong or missing fee information before sending money.
Review and merge. The change improves fee estimation reliability for sweep transactions and adds test coverage. No immediate security response is required, but ensure the fallback createTx path does not produce side effects or persist unintended transaction state.
Security signals we found
Incorrect fee display could lead to user confusion or failed transactions
Sweep transactions previously may have shown no fee estimate
Fixes a functional bug in transaction fee estimation
Adds unit tests for the corrected behavior
Evidence from the diff
The commit modifies the Monero wallet adapter’s estimateFee method to accept an isSweepAll flag. When isSweepAll is true, or when the native _wallet.estimateFee returns null, the adapter now calls _wallet.createTx and returns the fee from the built transaction. The send screen is updated to pass the sweep flag and to run priority fee calculations sequentially instead of in parallel, because the fallback path builds transactions that share wallet2 state. Tests are added covering normal sends, null estimate fallback, and sweep-all pricing.
Changed components
lib/models/app_wallet.dartlib/models/monero_wallet_adapter.dartlib/screens/send.darttest/monero_sweep_fee_test.dartInspect captured patch +100 / −14
### lib/models/app_wallet.dart
@@ -121,7 +121,12 @@ abstract interface class AppWallet implements Listenable {
/// can't estimate it (typically insufficient balance for that priority).
/// [amount] is exact decimal text, never a double. A spend amount that has
/// been through a double is no longer the amount the user asked for.
- Future<int?> estimateFee(String destinationAddress, String amount, {int priority});
+ Future<int?> estimateFee(
+ String destinationAddress,
+ String amount, {
+ int priority,
+ bool isSweepAll,
+ });
Future<AppPendingTx> createTx(
String destinationAddress,
String amount,
### lib/models/monero_wallet_adapter.dart
@@ -214,13 +214,25 @@ class MoneroWalletAdapter extends ChangeNotifier implements AppWallet {
bool isAddressValid(String address) => _wallet.isAddressValid(address);
@override
- Future<int?> estimateFee(String destinationAddress, String amount, {int priority = 0}) async {
- final fee = await _wallet.estimateFee(
- destinationAddress,
- domain.decimalToBaseUnits(amount, _moneroDecimals),
- priority: priority,
- );
- return fee?.toInt();
+ Future<int?> estimateFee(
+ String destinationAddress,
+ String amount, {
+ int priority = 0,
+ bool isSweepAll = false,
+ }) async {
+ final amountBaseUnits = domain.decimalToBaseUnits(amount, _moneroDecimals);
+ if (!isSweepAll) {
+ final fee = await _wallet.estimateFee(destinationAddress, amountBaseUnits, priority: priority);
+ if (fee != null) return fee.toInt();
+ }
+ // A sweep can't be estimated; build the tx (also the fallback when no
+ // estimate comes back).
+ try {
+ final tx = await _wallet.createTx(destinationAddress, amountBaseUnits, isSweepAll, priority: priority);
+ return tx.feeBaseUnits.toInt();
+ } catch (_) {
+ return null;
+ }
}
@override
### lib/screens/send.dart
@@ -368,12 +368,20 @@ class _SendScreenState extends State<SendScreen> {
final amountText = _amount.coinText;
try {
- // Estimate the fee per priority natively (no full tx build).
- final fees = await Future.wait([
- wallet.estimateFee(destinationAddress, amountText, priority: 1),
- wallet.estimateFee(destinationAddress, amountText, priority: 2),
- wallet.estimateFee(destinationAddress, amountText, priority: 3),
- ]);
+ // Sequential: estimateFee builds the tx when no estimate comes back (a
+ // sweep, or some states), and the builds share one wallet2.
+ final fees = <int?>[];
+ for (var priority = 1; priority <= 3; priority++) {
+ if (currentRequest != _feeCalculationCounter) return;
+ fees.add(
+ await wallet.estimateFee(
+ destinationAddress,
+ amountText,
+ priority: priority,
+ isSweepAll: _isSweepAll,
+ ),
+ );
+ }
// Only update state if this is still the latest request
if (currentRequest == _feeCalculationCounter && mounted) {
### test/monero_sweep_fee_test.dart
@@ -0,0 +1,61 @@
+import 'dart:io';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:wallet_domain/wallet_domain.dart';
+import 'package:wallet_infra/testing.dart';
+import 'package:wallet_infra/wallet_infra.dart';
+import 'package:wallet_monero/wallet_monero.dart';
+import 'package:skylight_wallet/models/monero_wallet_adapter.dart';
+
+/// estimateFee prices a normal send; when no estimate comes back (a sweep, or
+/// some states) the adapter falls back to building the tx.
+void main() {
+ late Directory tmp;
+ late FakeMoneroBackend backend;
+ late MoneroWalletAdapter adapter;
+
+ final dest = '4${'a' * 94}';
+
+ setUpAll(() => WalletFileCrypto.kdf = const FastTestPbkdf2());
+ tearDownAll(() => WalletFileCrypto.kdf = const WebCryptoPbkdf2());
+
+ setUp(() async {
+ tmp = Directory.systemTemp.createTempSync('monero_sweep_fee');
+ SharedPreferencesService.store = MemoryPreferenceStore();
+ WalletSecrets.store = MemorySecretStore();
+ WalletAppConfig.install(WalletAppConfig.skylight, directories: FixedDirectories(tmp));
+ useTestCaBundle();
+ WalletLog.sink = MemoryLogSink();
+ backend = FakeMoneroBackend();
+ final wallet = MoneroWallet(backend: backend);
+ backend.existingWalletPaths.add(await wallet.walletPathForType('lws'));
+ await wallet.openExisting(password: 'pw');
+ adapter = MoneroWalletAdapter(wallet, readStoredSeed: () async => null);
+ });
+
+ tearDown(() {
+ WalletAppConfig.resetForTesting();
+ CaBundle.resetForTesting();
+ SharedPreferencesService.resetForTesting();
+ tmp.deleteSync(recursive: true);
+ });
+
+ test('normal send: fee comes from the native estimate', () async {
+ backend.feeEstimate = BigInt.from(30000000);
+ backend.pendingFee = BigInt.from(12345);
+ // Estimate is used; the tx is not built.
+ expect(await adapter.estimateFee(dest, '1.0', priority: 1), 30000000);
+ });
+
+ test('no estimate returned: falls back to the built-tx fee', () async {
+ backend.feeEstimate = null;
+ backend.pendingFee = BigInt.from(12345);
+ expect(await adapter.estimateFee(dest, '1.0', priority: 1), 12345);
+ });
+
+ test('sweep: always priced from the built tx', () async {
+ backend.feeEstimate = BigInt.from(30000000);
+ backend.pendingFee = BigInt.from(12345);
+ expect(await adapter.estimateFee(dest, '1.0', priority: 1, isSweepAll: true), 12345);
+ });
+}Why this scored 33/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.