Skip to content

Commit

Permalink
Add fees
Browse files Browse the repository at this point in the history
  • Loading branch information
nopara73 committed Dec 20, 2018
1 parent e56baed commit 922be4c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
4 changes: 4 additions & 0 deletions WalletWasabi.Gui/Controls/WalletExplorer/CoinListViewModel.cs
Expand Up @@ -51,6 +51,8 @@ private SortExpressionComparer<CoinViewModel> MyComparer

public event Action DequeueCoinsPressed;

public event EventHandler<CoinViewModel> SelectionChanged;

public CoinViewModel SelectedCoin
{
get => _selectedCoin;
Expand Down Expand Up @@ -393,6 +395,8 @@ private void Coin_PropertyChanged(object sender, PropertyChangedEventArgs e)
if (e.PropertyName == nameof(CoinViewModel.IsSelected))
{
SetSelections();
var cvm = sender as CoinViewModel;
SelectionChanged?.Invoke(this, cvm);
}
if (e.PropertyName == nameof(CoinViewModel.Status))
{
Expand Down
93 changes: 86 additions & 7 deletions WalletWasabi.Gui/Controls/WalletExplorer/SendTabViewModel.cs
Expand Up @@ -28,12 +28,15 @@ public class SendTabViewModel : WalletActionViewModel
private bool _isMax;
private string _maxClear;
private string _amount;
private bool IgnoreAmountChanges { get; set; }
private int _feeTarget;
private int _minimumFeeTarget;
private int _maximumFeeTarget;
private string _confirmationExpectedText;
private string _feeText;
private decimal _usdFee;
private Money _btcFee;
private Money _satoshiPerByteFeeRate;
private decimal _feePercentage;
private string _password;
private string _address;
private string _label;
Expand All @@ -45,6 +48,7 @@ public class SendTabViewModel : WalletActionViewModel
private const string BuildingTransactionButtonTextString = "Sending Transaction...";
private int _caretIndex;
private ObservableCollection<SuggestionViewModel> _suggestions;
private bool IgnoreAmountChanges { get; set; }

public SendTabViewModel(WalletViewModel walletViewModel)
: base("Send", walletViewModel)
Expand All @@ -58,7 +62,7 @@ public SendTabViewModel(WalletViewModel walletViewModel)
ResetMax();
SetFeeTargetLimits();
FeeTarget = MinimumFeeTarget;
SetFeeTexts();
SetFeesAndTexts();

Global.Synchronizer.PropertyChanged += Synchronizer_PropertyChanged;

Expand Down Expand Up @@ -95,6 +99,7 @@ public SendTabViewModel(WalletViewModel walletViewModel)
});
}
}
SetFeesAndTexts();
});

BuildTransactionCommand = ReactiveCommand.Create(async () =>
Expand Down Expand Up @@ -133,7 +138,6 @@ public SendTabViewModel(WalletViewModel walletViewModel)
var amount = Money.Zero;
if (!IsMax)
{
amount = Money.Parse(Amount);
if (amount == Money.Zero)
{
SetWarningMessage($"Invalid amount.");
Expand Down Expand Up @@ -226,13 +230,20 @@ public SendTabViewModel(WalletViewModel walletViewModel)

this.WhenAnyValue(x => x.FeeTarget).Subscribe(_ =>
{
SetFeeTexts();
SetFeesAndTexts();
});

CoinList.SelectionChanged += CoinList_SelectionChanged;

_suggestions = new ObservableCollection<SuggestionViewModel>();
}

private void SetFeeTexts()
private void CoinList_SelectionChanged(object sender, CoinViewModel e)
{
SetFeesAndTexts();
}

private void SetFeesAndTexts()
{
AllFeeEstimate allFeeEstimate = Global.Synchronizer?.AllFeeEstimate;

Expand Down Expand Up @@ -276,10 +287,50 @@ private void SetFeeTexts()
ConfirmationExpectedText = $"two weeks™";
}

SetFees(allFeeEstimate, feeTarget);

if (allFeeEstimate != null)
{
FeeText = $"(~ {allFeeEstimate.GetFeeRate(feeTarget).Satoshi} sat/byte)";
//FeeText = $"(~ {SatoshiPerByteFeeRate.Satoshi} sat/byte)";
FeeText = $"(~ {BtcFee.ToString(false, false)} BTC)";
}
}

private void SetFees(AllFeeEstimate allFeeEstimate, int feeTarget)
{
SatoshiPerByteFeeRate = allFeeEstimate.GetFeeRate(feeTarget);

IEnumerable<SmartCoin> selectedCoins = CoinList.Coins.Where(cvm => cvm.IsSelected).Select(x => x.Model);

int vsize = 150;
if (selectedCoins.Any())
{
if (IsMax)
{
vsize = NBitcoinHelpers.CalculateVsizeAssumeSegwit(selectedCoins.Count(), 1);
}
else
{
if (Money.TryParse(Amount, out Money amount))
{
var inNum = 0;
var amountSoFar = Money.Zero;
foreach (SmartCoin coin in selectedCoins.OrderByDescending(x => x.Amount))
{
amountSoFar += coin.Amount;
inNum++;
if (amountSoFar > amount)
{
break;
}
}
vsize = NBitcoinHelpers.CalculateVsizeAssumeSegwit(inNum, 2);
}
// Else whatever, don't change.
}
}

BtcFee = Money.Satoshis(vsize * SatoshiPerByteFeeRate);
}

private void Synchronizer_PropertyChanged(object sender, PropertyChangedEventArgs e)
Expand All @@ -295,7 +346,11 @@ private void Synchronizer_PropertyChanged(object sender, PropertyChangedEventArg
{
FeeTarget = MaximumFeeTarget;
}
SetFeeTexts();
SetFeesAndTexts();
}
if (e.PropertyName == nameof(Global.Synchronizer.UsdExchangeRate))
{
SetFeesAndTexts();
}
}

Expand Down Expand Up @@ -441,6 +496,30 @@ public string FeeText
set => this.RaiseAndSetIfChanged(ref _feeText, value);
}

public decimal UsdFee
{
get => _usdFee;
set => this.RaiseAndSetIfChanged(ref _usdFee, value);
}

public Money BtcFee
{
get => _btcFee;
set => this.RaiseAndSetIfChanged(ref _btcFee, value);
}

public Money SatoshiPerByteFeeRate
{
get => _satoshiPerByteFeeRate;
set => this.RaiseAndSetIfChanged(ref _satoshiPerByteFeeRate, value);
}

public decimal FeePercentage
{
get => _feePercentage;
set => this.RaiseAndSetIfChanged(ref _feePercentage, value);
}

public string Password
{
get => _password;
Expand Down

0 comments on commit 922be4c

Please sign in to comment.