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

Fix withdrawal fee calculation & the min relay fee problem. #6544

Merged
merged 1 commit into from Jan 28, 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
1 change: 1 addition & 0 deletions core/src/main/java/bisq/core/api/CoreWalletsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ void sendBtc(String address,
// See WithdrawalView # onWithdraw (and refactor).
Transaction feeEstimationTransaction =
btcWalletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses,
address,
receiverAmount,
txFeePerVbyte);
if (feeEstimationTransaction == null)
Expand Down
24 changes: 7 additions & 17 deletions core/src/main/java/bisq/core/btc/wallet/BtcWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1056,13 +1056,7 @@ public Transaction getFeeEstimationTransaction(String fromAddress,
}

public Transaction getFeeEstimationTransactionForMultipleAddresses(Set<String> fromAddresses,
Coin amount)
throws AddressFormatException, AddressEntryException, InsufficientFundsException {
Coin txFeeForWithdrawalPerVbyte = getTxFeeForWithdrawalPerVbyte();
return getFeeEstimationTransactionForMultipleAddresses(fromAddresses, amount, txFeeForWithdrawalPerVbyte);
}

public Transaction getFeeEstimationTransactionForMultipleAddresses(Set<String> fromAddresses,
String toAddress,
Coin amount,
Coin txFeeForWithdrawalPerVbyte)
throws AddressFormatException, AddressEntryException, InsufficientFundsException {
Expand Down Expand Up @@ -1090,11 +1084,7 @@ public Transaction getFeeEstimationTransactionForMultipleAddresses(Set<String> f
do {
counter++;
fee = txFeeForWithdrawalPerVbyte.multiply(txVsize);
// We use a dummy address for the output
// We don't know here whether the output is segwit or not but we don't care too much because the size of
// a segwit ouput is just 3 byte smaller than the size of a legacy ouput.
final String dummyReceiver = SegwitAddress.fromKey(params, new ECKey()).toString();
SendRequest sendRequest = getSendRequestForMultipleAddresses(fromAddresses, dummyReceiver, amount, fee, null, aesKey);
SendRequest sendRequest = getSendRequestForMultipleAddresses(fromAddresses, toAddress, amount, fee, null, aesKey);
wallet.completeTx(sendRequest);
tx = sendRequest.tx;
txVsize = tx.getVsize();
Expand All @@ -1113,14 +1103,14 @@ public Transaction getFeeEstimationTransactionForMultipleAddresses(Set<String> f
}

private boolean feeEstimationNotSatisfied(int counter, Transaction tx) {
return feeEstimationNotSatisfied(counter, tx, getTxFeeForWithdrawalPerVbyte());
return feeEstimationNotSatisfied(counter, tx.getFee().value, tx.getVsize(), getTxFeeForWithdrawalPerVbyte());
}

private boolean feeEstimationNotSatisfied(int counter, Transaction tx, Coin txFeeForWithdrawalPerVbyte) {
long targetFee = txFeeForWithdrawalPerVbyte.multiply(tx.getVsize()).value;
private boolean feeEstimationNotSatisfied(int counter, long txFee, long txVsize, Coin txFeeForWithdrawalPerVbyte) {
long targetFee = txFeeForWithdrawalPerVbyte.multiply(txVsize).value;
long higherThanTargetFee = txFee - targetFee;
return counter < 10 &&
(tx.getFee().value < targetFee ||
tx.getFee().value - targetFee > 1000);
(txFee < targetFee || higherThanTargetFee > 1000);
}

public int getEstimatedFeeTxVsize(List<Coin> outputValues, Coin txFee)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ private void onWithdraw() {
final Coin sendersAmount;

// We do not know sendersAmount if senderPaysFee is true. We repeat fee calculation after first attempt if senderPaysFee is true.
Transaction feeEstimationTransaction = btcWalletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses, amountAsCoin, feeRate);
Transaction feeEstimationTransaction = btcWalletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses, withdrawToAddress, amountAsCoin, feeRate);
if (feeExcluded && feeEstimationTransaction != null) {
feeEstimationTransaction = btcWalletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses, amountAsCoin.add(feeEstimationTransaction.getFee()), feeRate);
feeEstimationTransaction = btcWalletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses, withdrawToAddress, amountAsCoin.add(feeEstimationTransaction.getFee()), feeRate);
}
checkNotNull(feeEstimationTransaction, "feeEstimationTransaction must not be null");

Expand Down