extract duplicade code to func in ChangeAvoidViewModel
MODIFY
WalletWasabi.Fluent/ViewModels/Wallets/Send/ChangeAvoidanceSuggestionViewModel.cs
WalletWasabi.Fluent/ViewModels/Wallets/Send/ChangeAvoidanceSuggestionViewModel.cs
@@ -49,31 +49,28 @@ public partial class ChangeAvoidanceSuggestionViewModel : SuggestionViewModel
public static async IAsyncEnumerable<ChangeAvoidanceSuggestionViewModel> GenerateSuggestionsAsync(
TransactionInfo transactionInfo, BitcoinAddress destination, Wallet wallet, [EnumeratorCancellation] CancellationToken cancellationToken)
{
- Task<ChangeAvoidanceSuggestionViewModel?> moreSuggestionTask = Task.Run(() =>
- {
- if (ChangelessTransactionCoinSelector.TryGetCoins(SuggestionType.More, transactionInfo.Coins, transactionInfo.FeeRate, new TxOut(transactionInfo.Amount, destination), out IEnumerable<SmartCoin>? selection, cancellationToken))
- {
- BuildTransactionResult transaction = TransactionHelpers.BuildChangelessTransaction(
- wallet,
- destination,
- transactionInfo.UserLabels,
- transactionInfo.FeeRate,
- selection,
- tryToSign: false);
+ Task<ChangeAvoidanceSuggestionViewModel?> moreSuggestionTask = Task.Run(() => GetSuggestion(SuggestionType.More, transactionInfo, destination, wallet, cancellationToken));
- return new ChangeAvoidanceSuggestionViewModel(
- transactionInfo.Amount.ToDecimal(MoneyUnit.BTC),
- transaction,
- wallet.Synchronizer.UsdExchangeRate,
- isOriginal: false);
+ Task<ChangeAvoidanceSuggestionViewModel?> lesserSuggestionTask = Task.Run(() => GetSuggestion(SuggestionType.Less, transactionInfo, destination, wallet, cancellationToken));
+
+ ChangeAvoidanceSuggestionViewModel? moreSuggestion = await moreSuggestionTask;
+
+ ChangeAvoidanceSuggestionViewModel? lesserSuggestion = await lesserSuggestionTask;
+
+ if (moreSuggestion is not null)
+ {
+ yield return moreSuggestion;
}
- return null;
- });
+ if (lesserSuggestion is not null)
+ {
+ yield return lesserSuggestion;
+ }
+ }
- Task<ChangeAvoidanceSuggestionViewModel?> lesserSuggestionTask = Task.Run(() =>
+ private static ChangeAvoidanceSuggestionViewModel? GetSuggestion(SuggestionType suggestionType, TransactionInfo transactionInfo, BitcoinAddress destination, Wallet wallet, CancellationToken cancellationToken)
{
- if (ChangelessTransactionCoinSelector.TryGetCoins(SuggestionType.Less, transactionInfo.Coins, transactionInfo.FeeRate, new TxOut(transactionInfo.Amount, destination), out IEnumerable<SmartCoin>? selection, cancellationToken))
+ if (ChangelessTransactionCoinSelector.TryGetCoins(suggestionType, transactionInfo.Coins, transactionInfo.FeeRate, new TxOut(transactionInfo.Amount, destination), out IEnumerable<SmartCoin>? selection, cancellationToken))
{
BuildTransactionResult transaction = TransactionHelpers.BuildChangelessTransaction(
wallet,
@@ -91,20 +88,5 @@ public partial class ChangeAvoidanceSuggestionViewModel : SuggestionViewModel
}
return null;
- });
-
- ChangeAvoidanceSuggestionViewModel? moreSuggestion = await moreSuggestionTask;
-
- ChangeAvoidanceSuggestionViewModel? lesserSuggestion = await lesserSuggestionTask;
-
- if (moreSuggestion is not null)
- {
- yield return moreSuggestion;
- }
-
- if (lesserSuggestion is not null)
- {
- yield return lesserSuggestion;
- }
}
}

extract duplicade code to func in ChangeAvoidViewModel