From 5e05592048dcf08a288c35e922672dbe3f34c645 Mon Sep 17 00:00:00 2001 From: mrblue313 <144623880+mrblue313@users.noreply.github.com> Date: Thu, 9 Nov 2023 15:16:33 +0100 Subject: [PATCH] Improve BisqEasy amount validation * Before, When offer had min/max amounts, those were used to find the min/max base amounts. This is wrong. Now, the quote amount decided in the contract is used in order to calculate the base amount. * Remove quote validation. This doesn't apply for this protocol. --- .../BisqEasyTakeOfferRequestHandler.java | 52 ++++++------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/trade/src/main/java/bisq/trade/bisq_easy/protocol/messages/BisqEasyTakeOfferRequestHandler.java b/trade/src/main/java/bisq/trade/bisq_easy/protocol/messages/BisqEasyTakeOfferRequestHandler.java index 3fded52347..3453ed3208 100644 --- a/trade/src/main/java/bisq/trade/bisq_easy/protocol/messages/BisqEasyTakeOfferRequestHandler.java +++ b/trade/src/main/java/bisq/trade/bisq_easy/protocol/messages/BisqEasyTakeOfferRequestHandler.java @@ -25,8 +25,8 @@ import bisq.contract.ContractSignatureData; import bisq.contract.bisq_easy.BisqEasyContract; import bisq.offer.Offer; -import bisq.offer.amount.OfferAmountUtil; import bisq.offer.bisq_easy.BisqEasyOffer; +import bisq.offer.price.PriceUtil; import bisq.trade.ServiceProvider; import bisq.trade.bisq_easy.BisqEasyTrade; import bisq.trade.protocol.events.TradeMessageHandler; @@ -103,8 +103,7 @@ protected void verifyMessage(BisqEasyTakeOfferRequest message) { checkArgument(message.getSender().equals(takersContract.getTaker().getNetworkId())); - validateAmount(takersOffer, takersContract, true); - validateAmount(takersOffer, takersContract, false); + validateAmount(takersOffer, takersContract); checkArgument(takersOffer.getBaseSidePaymentMethodSpecs().contains(takersContract.getBaseSidePaymentMethodSpec())); checkArgument(takersOffer.getQuoteSidePaymentMethodSpecs().contains(takersContract.getQuoteSidePaymentMethodSpec())); @@ -118,42 +117,25 @@ private void commitToModel(ContractSignatureData takersContractSignatureData, Co trade.getMaker().getContractSignatureData().set(makersContractSignatureData); } - private void validateAmount(BisqEasyOffer takersOffer, BisqEasyContract takersContract, boolean isBase) { - double tolerancePercentage = 0.01; - - Optional minAmount = getMinAmount(takersOffer, takersContract, isBase); - Optional maxAmount = getMaxAmount(takersOffer, takersContract, isBase); - - checkArgument(minAmount.isPresent() && maxAmount.isPresent(), "No market price available for validation."); + private void validateAmount(BisqEasyOffer takersOffer, BisqEasyContract takersContract) { + Optional amount = getAmount(takersOffer, takersContract); + checkArgument(amount.isPresent(), "No market price available for validation."); - long toleranceAmountForMin = (long) (minAmount.get().getValue() * tolerancePercentage); - long toleranceAmountForMax = (long) (maxAmount.get().getValue() * tolerancePercentage); - long minAmountWithTolerance = minAmount.get().getValue() - toleranceAmountForMin; - long maxAmountWithTolerance = maxAmount.get().getValue() + toleranceAmountForMax; + double tolerancePercentage = 0.01; + long tolerance = (long) (amount.get().getValue() * tolerancePercentage); + long minAmountWithTolerance = amount.get().getValue() - tolerance; + long maxAmountWithTolerance = amount.get().getValue() + tolerance; - long amount = isBase ? takersContract.getBaseSideAmount() : takersContract.getQuoteSideAmount(); + long takersAmount = takersContract.getBaseSideAmount(); String errorMsg = "Market price deviation is too big."; - checkArgument(amount >= minAmountWithTolerance, errorMsg); - checkArgument(amount <= maxAmountWithTolerance, errorMsg); - } - - private Optional getMinAmount(BisqEasyOffer takersOffer, BisqEasyContract takersContract, boolean isBase) { - return isBase - ? OfferAmountUtil.findBaseSideMinOrFixedAmount( - serviceProvider.getBondedRolesService().getMarketPriceService(), - takersOffer.getAmountSpec(), takersContract.getAgreedPriceSpec(), takersOffer.getMarket()) - : OfferAmountUtil.findQuoteSideMinOrFixedAmount( - serviceProvider.getBondedRolesService().getMarketPriceService(), - takersOffer.getAmountSpec(), takersContract.getAgreedPriceSpec(), takersOffer.getMarket()); + checkArgument(takersAmount >= minAmountWithTolerance, errorMsg); + checkArgument(takersAmount <= maxAmountWithTolerance, errorMsg); } - private Optional getMaxAmount(BisqEasyOffer takersOffer, BisqEasyContract takersContract, boolean isBase) { - return isBase - ? OfferAmountUtil.findBaseSideMaxOrFixedAmount( - serviceProvider.getBondedRolesService().getMarketPriceService(), - takersOffer.getAmountSpec(), takersContract.getAgreedPriceSpec(), takersOffer.getMarket()) - : OfferAmountUtil.findQuoteSideMaxOrFixedAmount( - serviceProvider.getBondedRolesService().getMarketPriceService(), - takersOffer.getAmountSpec(), takersContract.getAgreedPriceSpec(), takersOffer.getMarket()); + private Optional getAmount(BisqEasyOffer takersOffer, BisqEasyContract takersContract) { + return PriceUtil.findQuote(serviceProvider.getBondedRolesService().getMarketPriceService(), + takersContract.getAgreedPriceSpec(), takersOffer.getMarket()) + .map(quote -> quote.toBaseSideMonetary(Monetary.from(takersContract.getQuoteSideAmount(), + takersOffer.getMarket().getQuoteCurrencyCode()))); } }