Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Withdraw all funds fix #326

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Helpers/ValidationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ public static void ValidateChannelCapacity(ValidatorEventArgs obj)
}
}

public static void ValidateWithdrawalAmount(ValidatorEventArgs obj)
public static void ValidateWithdrawalAmount(ValidatorEventArgs obj, decimal? walletBalance)
{
var amount = (decimal)obj.Value;

obj.Status = ValidationStatus.Success;

decimal minimum = Constants.MINIMUM_WITHDRAWAL_BTC_AMOUNT;
decimal maximum = Constants.MAXIMUM_WITHDRAWAL_BTC_AMOUNT;
decimal maximum = walletBalance ?? Constants.MAXIMUM_WITHDRAWAL_BTC_AMOUNT;

if (amount < minimum)
{
Expand Down
9 changes: 7 additions & 2 deletions src/Pages/Wallets.razor
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@
<Fields>
<Validations @ref="_transferFundsValidations">
<Column ColumnSize="ColumnSize.Is6">
<Validation Validator="args => ValidationHelper.ValidateWithdrawalAmount(args)">
<Validation Validator="args => ValidationHelper.ValidateWithdrawalAmount(args, _sourceBalance)">
<Field>
<FieldLabel>Amount</FieldLabel>
<FieldBody>
Expand All @@ -450,7 +450,7 @@
<Column ColumnSize="ColumnSize.Is6">
<Field>
<FieldBody>
<Check TValue="bool" @bind-Checked="@_transferAllFunds">Transfer all funds</Check>
<Check TValue="bool" CheckedChanged="OnTransferAllFundsCheckedChanged" Checked="@_transferAllFunds">Transfer all funds</Check>
markettes marked this conversation as resolved.
Show resolved Hide resolved
</FieldBody>
</Field>
</Column>
Expand Down Expand Up @@ -1402,4 +1402,9 @@
}
}

private void OnTransferAllFundsCheckedChanged(bool value)
{
_transferAllFunds = value;
_amountToTransfer = _sourceBalance;
}
}
57 changes: 31 additions & 26 deletions src/Pages/Withdrawals.razor
Original file line number Diff line number Diff line change
Expand Up @@ -160,38 +160,42 @@
}
</EditTemplate>
</DataGridColumn>
<DataGridColumn TItem="WalletWithdrawalRequest" Filterable="false" PopupFieldColumnSize="ColumnSize.Is12" Editable="true" Field="@nameof(WalletWithdrawalRequest.Amount)" Caption="Amount (BTC)" Sortable="false" Displayable="@IsPendingRequestsColumnVisible(PendingWithdrawalsColumnName.Amount)" >
<DataGridColumn TItem="WalletWithdrawalRequest" Filterable="false" PopupFieldColumnSize="ColumnSize.Is12" Editable="true" Field="@nameof(WalletWithdrawalRequest.Amount)" Caption="Amount" Sortable="false" Displayable="@IsPendingRequestsColumnVisible(PendingWithdrawalsColumnName.Amount)" >
<DisplayTemplate>
@{
@($"{context.Amount:f8} BTC ({Math.Round(PriceConversionService.BtcToUsdConversion(context.Amount, _btcPrice), 2)} USD)")
}
</DisplayTemplate>
<EditTemplate>
<Validation Validator="args => ValidationHelper.ValidateWithdrawalAmount(args)">
<NumericPicker TValue="decimal" @bind-Value="@_amount" Step="0.00001m" CurrencySymbol="₿ " Max="@(_maxWithdrawal)" Min="_minWithdrawal" Decimals="8" Disabled="@(SelectedUTXOs.Count > 0 || IsCheckedAllFunds)" />
<FieldHelp>
<Validation Validator="args => ValidationHelper.ValidateWithdrawalAmount(args, _selectedRequestWalletBalance)">
<NumericPicker TValue="decimal" @bind-Value="@_amount" Step="0.00001m" CurrencySymbol="₿ " Decimals="8" DecimalSeparator="," Disabled="@(SelectedUTXOs.Count > 0 || IsCheckedAllFunds)">
<Feedback>
<ValidationError/>
markettes marked this conversation as resolved.
Show resolved Hide resolved
</Feedback>
</NumericPicker>
<FieldHelp>
@{
decimal amountToShow = _amount < Constants.MAXIMUM_WITHDRAWAL_BTC_AMOUNT
? _amount
: Constants.MAXIMUM_WITHDRAWAL_BTC_AMOUNT;
decimal convertedAmount = Math.Round(PriceConversionService.SatToUsdConversion(new Money(amountToShow, MoneyUnit.BTC).Satoshi, _btcPrice), 2);
}
@($"Minimum {_minimumWithdrawalAmount:f8}. Current amount: {convertedAmount} USD")
</FieldHelp>
</Validation>
<div class="mb-3">
<Button Color="Color.Primary" Disabled="@(!_selectedWalletId.HasValue)" Clicked="@OpenCoinSelectionModal">Select Coins</Button> or use
@($"Minimum {_minimumWithdrawalAmount:f8} BTC. Current amount: {convertedAmount} USD")
</FieldHelp>
</Validation>
<div class="mb-3">
<Button Color="Color.Primary" Disabled="@(!_selectedWalletId.HasValue || IsCheckedAllFunds)" Clicked="@OpenCoinSelectionModal">Select Coins</Button> or use
<Button Color="Color.Primary" Disabled="@(SelectedUTXOs.Count == 0)" Clicked="@ClearSelectedUTXOs">Default Coin Selection</Button>
</div>
@if (_selectedWalletId.HasValue && SelectedUTXOs.Count > 0)
{
<span class="text-danger">Selected @(SelectedUTXOs.Count) UTXOs, this is a changeless operation</span>
}
else
{
<span class="text-danger">Default coin selection strategy selected</span>
}
</EditTemplate>
@if (_selectedWalletId.HasValue && SelectedUTXOs.Count > 0)
{
<span class="text-danger">Selected @(SelectedUTXOs.Count) UTXOs, this is a changeless operation</span>
}
else
{
<span class="text-danger">Default coin selection strategy selected</span>
}
</EditTemplate>
</DataGridColumn>
<DataGridColumn TItem="WalletWithdrawalRequest" Filterable="false" Field="@nameof(WalletWithdrawalRequest.WalletId)" Caption="Signatures Collected" Sortable="false" Displayable="@IsPendingRequestsColumnVisible(PendingWithdrawalsColumnName.SignaturesCollected)">
<DisplayTemplate>
Expand Down Expand Up @@ -383,7 +387,7 @@
ApproveRequestDelegate="async ()=> await ApproveRequestDelegate()"
/>

<CancelOrRejectPopup

Check warning on line 390 in src/Pages/Withdrawals.razor

View workflow job for this annotation

GitHub Actions / build-and-test

Component 'CancelOrRejectPopup' expects a value for the parameter 'Reason', but a value may not have been provided.
@ref="@_rejectCancelModalRef"
Title='@("Wallet withdrawal:"+_selectedRequest?.Id)'
Validator="@RejectReasonValidator"
Expand Down Expand Up @@ -445,8 +449,6 @@
private bool _isNodeManager = false;
private WalletWithdrawalRequestStatus? _rejectCancelStatus = WalletWithdrawalRequestStatus.Rejected;
private decimal? _selectedRequestWalletBalance;
private decimal _maxWithdrawal = Constants.MAXIMUM_WITHDRAWAL_BTC_AMOUNT;
private decimal _minWithdrawal = Constants.MINIMUM_WITHDRAWAL_BTC_AMOUNT;
private decimal _btcPrice;
private ColumnLayout<AllWithdrawalsColumnName> AllRequestsColumnLayout;
private Dictionary<string, bool> _pendingRequestsColumns = new();
Expand Down Expand Up @@ -543,13 +545,11 @@
private async Task OnRowInserted(SavedRowItem<WalletWithdrawalRequest, Dictionary<string, object>> arg)
{
if (arg.Item == null) return;
//Balance validation
if (await ValidateBalance(arg)) return;
var amount = SelectedUTXOs.Count > 0 ? SelectedUTXOsValue() : _amount;
arg.Item.Wallet = await WalletRepository.GetById(arg.Item.WalletId);
arg.Item.Amount = amount;
arg.Item.Changeless = SelectedUTXOs.Count > 0;
arg.Item.WithdrawAllFunds = arg.Item.WithdrawAllFunds || _amount == _selectedRequestWalletBalance;
arg.Item.WithdrawAllFunds = IsCheckedAllFunds || _amount == _selectedRequestWalletBalance;
if (arg.Item.Wallet.IsHotWallet)
{
await GetData();
Expand Down Expand Up @@ -590,6 +590,7 @@
if (IsCheckedAllFunds)
{
ClearSelectedUTXOs();
_amount = _selectedRequestWalletBalance ?? _minimumWithdrawalAmount;
}
}

Expand Down Expand Up @@ -829,8 +830,12 @@
_selectedWalletId = id == 0 ? null : id;
if (wallet != null)
{
var (balance,_) = await BitcoinService.GetWalletConfirmedBalance(wallet);
_selectedRequestWalletBalance = balance;
var (balance,_) = await BitcoinService.GetWalletConfirmedBalance(wallet);
_selectedRequestWalletBalance = balance;
if (IsCheckedAllFunds)
{
_amount = balance;
}
}
}

Expand Down Expand Up @@ -881,7 +886,7 @@
{
_selectedRequest.Wallet = null;
_selectedRequest.Changeless = SelectedUTXOs.Count > 0;
_selectedRequest.WithdrawAllFunds = _selectedRequest.WithdrawAllFunds || _amount == _selectedRequestWalletBalance;
_selectedRequest.WithdrawAllFunds = IsCheckedAllFunds || _amount == _selectedRequestWalletBalance;
var createWithdrawalResult = await WalletWithdrawalRequestRepository.AddAsync(_selectedRequest);
if (!createWithdrawalResult.Item1)
{
Expand Down
Loading