Fix withdrawal fee calculation & the min relay fee problem.

This commit is contained in:
jmacxx 2023-01-27 12:02:02 -06:00
parent 74e1078318
commit efac12e8ce
No known key found for this signature in database
GPG key ID: 155297BABFE94A1B
3 changed files with 10 additions and 19 deletions

View file

@ -289,6 +289,7 @@ class CoreWalletsService {
// See WithdrawalView # onWithdraw (and refactor).
Transaction feeEstimationTransaction =
btcWalletService.getFeeEstimationTransactionForMultipleAddresses(fromAddresses,
address,
receiverAmount,
txFeePerVbyte);
if (feeEstimationTransaction == null)

View file

@ -1056,13 +1056,7 @@ public class BtcWalletService extends WalletService {
}
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 {
@ -1090,11 +1084,7 @@ public class BtcWalletService extends WalletService {
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();
@ -1113,14 +1103,14 @@ public class BtcWalletService extends WalletService {
}
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)

View file

@ -406,9 +406,9 @@ public class WithdrawalView extends ActivatableView<VBox, Void> {
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");