Support TakeOfferRequest's intended trade amt in daemon

This commit is contained in:
ghubstan 2022-08-06 15:08:30 -03:00
parent 39c42ddb07
commit 7d4e7dd696
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
4 changed files with 32 additions and 8 deletions

View File

@ -293,10 +293,12 @@ public class CoreApi {
///////////////////////////////////////////////////////////////////////////////////////////
public void takeBsqSwapOffer(String offerId,
long intendedTradeAmount,
TradeResultHandler<BsqSwapTrade> tradeResultHandler,
ErrorMessageHandler errorMessageHandler) {
Offer bsqSwapOffer = coreOffersService.getBsqSwapOffer(offerId);
coreTradesService.takeBsqSwapOffer(bsqSwapOffer,
intendedTradeAmount,
tradeResultHandler,
errorMessageHandler);
}
@ -304,12 +306,14 @@ public class CoreApi {
public void takeOffer(String offerId,
String paymentAccountId,
String takerFeeCurrencyCode,
long intendedTradeAmount,
Consumer<Trade> resultHandler,
ErrorMessageHandler errorMessageHandler) {
Offer offer = coreOffersService.getOffer(offerId);
coreTradesService.takeOffer(offer,
paymentAccountId,
takerFeeCurrencyCode,
intendedTradeAmount,
resultHandler,
errorMessageHandler);
}

View File

@ -114,15 +114,17 @@ class CoreTradesService {
this.user = user;
}
// TODO We need to pass the intended trade amount, not default to the maximum.
void takeBsqSwapOffer(Offer offer,
long intendedTradeAmount,
TradeResultHandler<BsqSwapTrade> tradeResultHandler,
ErrorMessageHandler errorMessageHandler) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
verifyIntendedTradeAmountIsInRange(intendedTradeAmount, offer);
bsqSwapTakeOfferModel.initWithData(offer);
bsqSwapTakeOfferModel.applyAmount(offer.getAmount());
bsqSwapTakeOfferModel.applyAmount(Coin.valueOf(intendedTradeAmount));
// Block attempt to take swap offer if there are insufficient funds for the trade.
var missingCoin = bsqSwapTakeOfferModel.getMissingFundsAsCoin();
@ -139,10 +141,10 @@ class CoreTradesService {
coreContext.isApiUser());
}
// TODO We need to pass the intended trade amount, not default to the maximum.
void takeOffer(Offer offer,
String paymentAccountId,
String takerFeeCurrencyCode,
long intendedTradeAmount,
Consumer<Trade> resultHandler,
ErrorMessageHandler errorMessageHandler) {
coreWalletsService.verifyWalletsAreAvailable();
@ -154,9 +156,11 @@ class CoreTradesService {
if (paymentAccount == null)
throw new IllegalArgumentException(format("payment account with id '%s' not found", paymentAccountId));
verifyIntendedTradeAmountIsInRange(intendedTradeAmount, offer);
var useSavingsWallet = true;
takeOfferModel.initModel(offer, paymentAccount, useSavingsWallet);
takeOfferModel.initModel(offer, paymentAccount, intendedTradeAmount, useSavingsWallet);
log.info("Initiating take {} offer, {}",
offer.isBuyOffer() ? "buy" : "sell",
takeOfferModel);
@ -167,7 +171,7 @@ class CoreTradesService {
format("wallet has insufficient btc to take offer with id '%s'", offer.getId()));
//noinspection ConstantConditions
tradeManager.onTakeOffer(offer.getAmount(),
tradeManager.onTakeOffer(Coin.valueOf(intendedTradeAmount),
takeOfferModel.getTxFeeFromFeeService(),
takeOfferModel.getTakerFee(),
takeOfferModel.isCurrencyForTakerFeeBtc(),
@ -457,4 +461,14 @@ class CoreTradesService {
String.join(", ", tradeIds)));
});
}
// Throws a RuntimeException if the takeoffer's amount parameter is out of range.
void verifyIntendedTradeAmountIsInRange(long intendedTradeAmount, Offer offer) {
if (intendedTradeAmount < offer.getMinAmount().value || intendedTradeAmount > offer.getAmount().value)
throw new IllegalArgumentException(
format("intended trade amount %s is outside offer's min - max amount range of %s - %s",
Coin.valueOf(intendedTradeAmount).toPlainString().toLowerCase(),
offer.getMinAmount().toPlainString().toLowerCase(),
offer.getAmount().toPlainString().toLowerCase()));
}
}

View File

@ -111,6 +111,7 @@ public class TakeOfferModel implements Model {
public void initModel(Offer offer,
PaymentAccount paymentAccount,
long intendedTradeAmount,
boolean useSavingsWallet) {
this.clearModel();
this.offer = offer;
@ -119,12 +120,12 @@ public class TakeOfferModel implements Model {
validateModelInputs();
this.useSavingsWallet = useSavingsWallet;
this.amount = valueOf(Math.min(offer.getAmount().value, getMaxTradeLimit()));
this.amount = valueOf(Math.min(intendedTradeAmount, getMaxTradeLimit()));
this.securityDeposit = offer.getDirection() == SELL
? offer.getBuyerSecurityDeposit()
: offer.getSellerSecurityDeposit();
this.isCurrencyForTakerFeeBtc = offerUtil.isCurrencyForTakerFeeBtc(amount);
this.takerFee = offerUtil.getTakerFee(isCurrencyForTakerFeeBtc, amount);
this.isCurrencyForTakerFeeBtc = offerUtil.isCurrencyForTakerFeeBtc(this.amount);
this.takerFee = offerUtil.getTakerFee(isCurrencyForTakerFeeBtc, this.amount);
calculateTxFees();
calculateVolume();

View File

@ -97,9 +97,13 @@ class GrpcTradesService extends TradesImplBase {
try {
// Make sure the offer exists before trying to take it.
Offer offer = coreApi.getOffer(req.getOfferId());
var intendedTradeAmount = req.getAmount() == 0
? offer.getAmount().value
: req.getAmount();
if (offer.isBsqSwapOffer()) {
coreApi.takeBsqSwapOffer(offer.getId(),
intendedTradeAmount,
bsqSwapTrade -> {
var reply = buildTakeOfferReply(bsqSwapTrade);
responseObserver.onNext(reply);
@ -113,6 +117,7 @@ class GrpcTradesService extends TradesImplBase {
coreApi.takeOffer(offer.getId(),
req.getPaymentAccountId(),
req.getTakerFeeCurrencyCode(),
intendedTradeAmount,
trade -> {
var reply = buildTakeOfferReply(trade);
responseObserver.onNext(reply);