check txn after fee adjustment, simplification
MODIFY
WalletWasabi.Fluent/Helpers/TransactionHelpers.cs
WalletWasabi.Fluent/Helpers/TransactionHelpers.cs
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -60,6 +61,22 @@ namespace WalletWasabi.Fluent.Helpers
}
+ public static bool TryBuildTransaction(Wallet wallet, TransactionInfo transactionInfo, [NotNullWhen(true)]out BuildTransactionResult? transaction, bool subtractFee = false, bool isPayJoin = false)
+ {
+ transaction = null;
+
+ try
+ {
+ transaction = BuildTransaction(wallet, transactionInfo, subtractFee, isPayJoin);
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
public static async Task<SmartTransaction> ParseTransactionAsync(string path, Network network)
{
var psbtBytes = await File.ReadAllBytesAsync(path);

MODIFY
WalletWasabi.Fluent/ViewModels/Wallets/Send/TransactionPreviewViewModel.cs
WalletWasabi.Fluent/ViewModels/Wallets/Send/TransactionPreviewViewModel.cs
@@ -16,7 +16,6 @@ using WalletWasabi.Fluent.Models;
using WalletWasabi.Fluent.ViewModels.Dialogs;
using WalletWasabi.Fluent.ViewModels.Dialogs.Base;
using WalletWasabi.Fluent.ViewModels.Navigation;
-using WalletWasabi.Fluent.ViewModels.TransactionBroadcasting;
using WalletWasabi.Logging;
using WalletWasabi.Wallets;
@@ -154,7 +153,12 @@ namespace WalletWasabi.Fluent.ViewModels.Wallets.Send
}
catch (InsufficientBalanceException)
{
- return await HandleInsufficientBalanceAsync(_wallet, transactionInfo);
+ if (transactionInfo.IsPayJoin)
+ {
+ return await HandleInsufficientBalanceWhenPayJoinAsync(_wallet, transactionInfo);
+ }
+
+ return await HandleInsufficientBalanceWhenNormalAsync(_wallet, transactionInfo);
}
catch (Exception ex)
{
@@ -171,23 +175,20 @@ namespace WalletWasabi.Fluent.ViewModels.Wallets.Send
}
}
- private async Task<BuildTransactionResult?> HandleInsufficientBalanceAsync(Wallet wallet, TransactionInfo transactionInfo)
- {
- if (transactionInfo.IsPayJoin)
- {
- if (wallet.Coins.Sum(x => x.Amount) > transactionInfo.Amount)
+ private async Task<BuildTransactionResult?> HandleInsufficientBalanceWhenNormalAsync(Wallet wallet, TransactionInfo transactionInfo)
{
- await ShowErrorAsync("Transaction Building",
- $"There are not enough {(transactionInfo.IsPrivatePocketUsed ? "private funds" : "funds selected")} to cover the transaction fee",
- "Wasabi was unable to create your transaction.");
+ var dialog = new InsufficientBalanceDialogViewModel(transactionInfo.IsPrivatePocketUsed ? BalanceType.Private : BalanceType.Pocket);
+ var result = await NavigateDialogAsync(dialog, NavigationTarget.DialogScreen);
- var feeDialogResult = await NavigateDialogAsync(new SendFeeViewModel(wallet, transactionInfo, false));
- if (feeDialogResult.Kind == DialogResultKind.Normal)
+ if (result.Result)
{
- transactionInfo.FeeRate = feeDialogResult.Result;
+ return await Task.Run(() => TransactionHelpers.BuildTransaction(wallet, transactionInfo, subtractFee: true));
}
+ if (wallet.Coins.TotalAmount() > transactionInfo.Amount)
+ {
var privacyControlDialogResult = await NavigateDialogAsync(new PrivacyControlViewModel(wallet, transactionInfo));
+
if (privacyControlDialogResult.Kind == DialogResultKind.Normal && privacyControlDialogResult.Result is { })
{
transactionInfo.Coins = privacyControlDialogResult.Result;
@@ -195,30 +196,31 @@ namespace WalletWasabi.Fluent.ViewModels.Wallets.Send
return await BuildTransactionAsync();
}
- else
- {
- await ShowErrorAsync("Transaction Building",
- "There are not enough funds to cover the transaction fee",
- "Wasabi was unable to create your transaction.");
Navigate().BackTo<SendViewModel>();
return null;
}
- }
- var dialog = new InsufficientBalanceDialogViewModel(transactionInfo.IsPrivatePocketUsed ? BalanceType.Private : BalanceType.Pocket);
- var result = await NavigateDialogAsync(dialog, NavigationTarget.DialogScreen);
+ private async Task<BuildTransactionResult?> HandleInsufficientBalanceWhenPayJoinAsync(Wallet wallet, TransactionInfo transactionInfo)
+ {
+ if (wallet.Coins.TotalAmount() > transactionInfo.Amount)
+ {
+ await ShowErrorAsync("Transaction Building",
+ $"There are not enough {(transactionInfo.IsPrivatePocketUsed ? "private funds" : "funds selected")} to cover the transaction fee",
+ "Wasabi was unable to create your transaction.");
- if (result.Result)
+ var feeDialogResult = await NavigateDialogAsync(new SendFeeViewModel(wallet, transactionInfo, false));
+ if (feeDialogResult.Kind == DialogResultKind.Normal)
{
- return await Task.Run(() => TransactionHelpers.BuildTransaction(_wallet, transactionInfo, subtractFee: true));
+ transactionInfo.FeeRate = feeDialogResult.Result;
}
- else
- {
- if (_wallet.Coins.Sum(x => x.Amount) > transactionInfo.Amount)
+
+ if (TransactionHelpers.TryBuildTransaction(wallet, transactionInfo, out var txn))
{
- var privacyControlDialogResult = await NavigateDialogAsync(new PrivacyControlViewModel(_wallet, transactionInfo));
+ return txn;
+ }
+ var privacyControlDialogResult = await NavigateDialogAsync(new PrivacyControlViewModel(wallet, transactionInfo));
if (privacyControlDialogResult.Kind == DialogResultKind.Normal && privacyControlDialogResult.Result is { })
{
transactionInfo.Coins = privacyControlDialogResult.Result;
@@ -226,13 +228,14 @@ namespace WalletWasabi.Fluent.ViewModels.Wallets.Send
return await BuildTransactionAsync();
}
- else
- {
+
+ await ShowErrorAsync("Transaction Building",
+ "There are not enough funds to cover the transaction fee",
+ "Wasabi was unable to create your transaction.");
+
Navigate().BackTo<SendViewModel>();
return null;
}
- }
- }
private void UpdateTransaction(BuildTransactionResult transactionResult)
{

check txn after fee adjustment, simplification