Skip to content

Commit

Permalink
Merge pull request WalletWasabi#11789 from Szpoti/fixChangelessSugges…
Browse files Browse the repository at this point in the history
…tion

Fix change avoidance giving back two Less/More suggestion
  • Loading branch information
RolandUI authored Oct 25, 2023
2 parents a399659 + 09d22ea commit ae98028
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public record FullPrivacySuggestion(BuildTransactionResult Transaction, decimal

public record BetterPrivacySuggestion(BuildTransactionResult Transaction, string DifferenceFiatText, IEnumerable<SmartCoin> Coins, bool IsChangeless) : PrivacySuggestion(Transaction);

public record ChangeAvoidanceSuggestion(BuildTransactionResult Transaction, string DifferenceFiatText, bool IsMore, bool IsLess) : PrivacySuggestion(Transaction)
public record ChangeAvoidanceSuggestion(BuildTransactionResult Transaction, decimal Difference, string DifferenceFiatText, bool IsMore, bool IsLess) : PrivacySuggestion(Transaction)
{
public Money GetAmount(BitcoinAddress destination) => Transaction!.CalculateDestinationAmount(destination);
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,17 @@ private async Task<List<ChangeAvoidanceSuggestion>> CreateChangeAvoidanceSuggest
return result;
}

// If both is Less/More, only return the one with smaller difference.
if (changeAvoidanceSuggestions.FirstOrDefault(x => x.IsLess == suggestion.IsLess && x.IsMore == suggestion.IsMore) is { } existingSuggestion)
{
if (Math.Abs(suggestion.Difference) < Math.Abs(existingSuggestion.Difference))
{
result.Remove(existingSuggestion);
result.Add(suggestion);
}
return result;
}

result.Add(suggestion);
}

Expand Down Expand Up @@ -332,7 +343,7 @@ private async IAsyncEnumerable<ChangeAvoidanceSuggestion> CreateChangeAvoidanceS
if (transaction is not null)
{
var differenceFiat = GetDifferenceFiat(transactionInfo, transaction, usdExchangeRate);
yield return new ChangeAvoidanceSuggestion(transaction, GetDifferenceFiatText(differenceFiat), IsMore: differenceFiat > 0, IsLess: differenceFiat < 0);
yield return new ChangeAvoidanceSuggestion(transaction, differenceFiat, GetDifferenceFiatText(differenceFiat), IsMore: differenceFiat > 0, IsLess: differenceFiat < 0);
}
}
}
Expand Down

0 comments on commit ae98028

Please sign in to comment.