Skip to content

Commit

Permalink
Block API attempt swap BSQ if !sufficient funds
Browse files Browse the repository at this point in the history
This API bug was relying on the UI purposed TaskRunner to bubble up an
insufficient funds error to the client, but it cannot do that.
This change blocks the takeoffer attempt if sufficient funds are not
found after the swap model calculates the taker's trade amounts.

Related: bisq-network#6221
Based on bisq-network#6250.
Based on branch `add-api-price-service-getaverageprice`.
  • Loading branch information
ghubstan committed Jun 20, 2022
1 parent 2c043b4 commit 2893ec2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions core/src/main/java/bisq/core/api/CoreTradesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ void takeBsqSwapOffer(Offer offer,
bsqSwapTakeOfferModel.initWithData(offer);
bsqSwapTakeOfferModel.applyAmount(offer.getAmount());

// Block attempt to take swap offer if there are insufficient funds for the trade.
var missingCoin = bsqSwapTakeOfferModel.getMissingFundsAsCoin();
if (missingCoin.value > 0)
throw new NotAvailableException(
format("wallet has insufficient funds to take offer with id '%s'", offer.getId()));

log.info("Initiating take {} offer, {}",
offer.isBuyOffer() ? "buy" : "sell",
bsqSwapTakeOfferModel);
Expand Down Expand Up @@ -155,6 +161,7 @@ void takeOffer(Offer offer,
offer.isBuyOffer() ? "buy" : "sell",
takeOfferModel);

// Block attempt to take swap offer if there are insufficient funds for the trade.
if (!takeOfferModel.isBtcWalletFunded())
throw new NotAvailableException(
format("wallet has insufficient btc to take offer with id '%s'", offer.getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

import javax.annotation.Nullable;

import static org.bitcoinj.core.Coin.ZERO;

@Slf4j
public class BsqSwapOfferModel {
public final static String BSQ = "BSQ";
Expand Down Expand Up @@ -90,7 +92,7 @@ public class BsqSwapOfferModel {
@Getter
private final ObjectProperty<Coin> payoutAmountAsCoin = new SimpleObjectProperty<>();
@Getter
private final ObjectProperty<Coin> missingFunds = new SimpleObjectProperty<>(Coin.ZERO);
private final ObjectProperty<Coin> missingFunds = new SimpleObjectProperty<>(ZERO);
@Nullable
private Coin txFee;
@Getter
Expand Down Expand Up @@ -380,6 +382,10 @@ public long getMaxTradeLimit() {
return PaymentMethod.BSQ_SWAP.getMaxTradeLimitAsCoin(BSQ).getValue();
}

public Coin getMissingFundsAsCoin() {
// This extra getter is needed for API CoreTradesService, which avoids importing JFX dependencies.
return missingFunds.get().isPositive() ? missingFunds.get() : ZERO;
}

///////////////////////////////////////////////////////////////////////////////////////////
// Utils
Expand All @@ -398,6 +404,6 @@ private void applyTxFeePerVbyte() {

private void resetTxFeeAndMissingFunds() {
txFee = null;
missingFunds.set(Coin.ZERO);
missingFunds.set(ZERO);
}
}

0 comments on commit 2893ec2

Please sign in to comment.