From 0d411707e55dd211e142a34c20d80d511e6134b9 Mon Sep 17 00:00:00 2001 From: Mike Rosseel Date: Fri, 13 Oct 2017 14:40:18 +0200 Subject: [PATCH] Documentation and cleanup of Offer related classes Signed-off-by: Mike Rosseel --- .../java/io/bisq/core/offer/OfferUtil.java | 56 ++++++++++++++++++- .../createoffer/CreateOfferDataModel.java | 16 +++--- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/io/bisq/core/offer/OfferUtil.java b/core/src/main/java/io/bisq/core/offer/OfferUtil.java index 42e76df535a..3f28d53014a 100644 --- a/core/src/main/java/io/bisq/core/offer/OfferUtil.java +++ b/core/src/main/java/io/bisq/core/offer/OfferUtil.java @@ -27,16 +27,49 @@ import javax.annotation.Nullable; +/** + * This class holds utility methods for the creation of an Offer. + * Most of these are extracted here because they are used both in the GUI and in the API. + * + * Long-term there could be a GUI-agnostic OfferService which provides these and other functionalities to both the + * GUI and the API. + */ public class OfferUtil { + + /** + * Returns the makerfee as Coin, this can be priced in BTC or BSQ. + * + * @param bsqWalletService + * @param preferences preferences are used to see if the user indicated a preference for paying fees in BTC + * @param amount + * @param marketPriceAvailable + * @param marketPriceMargin + * @return + */ @Nullable public static Coin getMakerFee(BsqWalletService bsqWalletService, Preferences preferences, Coin amount, boolean marketPriceAvailable, double marketPriceMargin) { return getMakerFee(isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount, marketPriceAvailable, marketPriceMargin), amount, marketPriceAvailable, marketPriceMargin); } + /** + * Given the direction, is this a BUY? + * + * @param direction + * @return + */ public static boolean isBuyOffer(OfferPayload.Direction direction) { return direction == OfferPayload.Direction.BUY; } + /** + * Calculates the maker fee for the given amount, marketprice and marketpricemargin. + * + * @param isCurrencyForMakerFeeBtc + * @param amount + * @param marketPriceAvailable + * @param marketPriceMargin + * @return + */ @Nullable public static Coin getMakerFee(boolean isCurrencyForMakerFeeBtc, Coin amount, boolean marketPriceAvailable, double marketPriceMargin) { if (amount != null) { @@ -59,10 +92,30 @@ public static Coin getMakerFee(boolean isCurrencyForMakerFeeBtc, Coin amount, bo } + /** + * Checks if the maker fee should be paid in BTC, this can be the case due to user preference or because the user + * doesn't have enough BSQ. + * + * @param preferences + * @param bsqWalletService + * @param amount + * @param marketPriceAvailable + * @param marketPriceMargin + * @return + */ public static boolean isCurrencyForMakerFeeBtc(Preferences preferences, BsqWalletService bsqWalletService, Coin amount, boolean marketPriceAvailable, double marketPriceMargin) { return preferences.getPayFeeInBtc() || !isBsqForFeeAvailable(bsqWalletService, amount, marketPriceAvailable, marketPriceMargin); } + /** + * Checks if the available BSQ balance is sufficient to pay for the offer's maker fee. + * + * @param bsqWalletService + * @param amount + * @param marketPriceAvailable + * @param marketPriceMargin + * @return + */ public static boolean isBsqForFeeAvailable(BsqWalletService bsqWalletService, Coin amount, boolean marketPriceAvailable, double marketPriceMargin) { return BisqEnvironment.isBaseCurrencySupportingBsq() && getMakerFee(false, amount, marketPriceAvailable, marketPriceMargin) != null && @@ -70,7 +123,4 @@ public static boolean isBsqForFeeAvailable(BsqWalletService bsqWalletService, Co getMakerFee(false , amount, marketPriceAvailable, marketPriceMargin) != null && !bsqWalletService.getAvailableBalance().subtract(getMakerFee(false, amount, marketPriceAvailable, marketPriceMargin)).isNegative(); } - - - } diff --git a/gui/src/main/java/io/bisq/gui/main/offer/createoffer/CreateOfferDataModel.java b/gui/src/main/java/io/bisq/gui/main/offer/createoffer/CreateOfferDataModel.java index 1f251db9c51..d3e86eb7d73 100644 --- a/gui/src/main/java/io/bisq/gui/main/offer/createoffer/CreateOfferDataModel.java +++ b/gui/src/main/java/io/bisq/gui/main/offer/createoffer/CreateOfferDataModel.java @@ -328,7 +328,7 @@ Offer createAndGetOffer() { String countryCode = paymentAccount instanceof CountryBasedPaymentAccount ? ((CountryBasedPaymentAccount) paymentAccount).getCountry().code : null; checkNotNull(p2PService.getAddress(), "Address must not be null"); - checkNotNull(OfferUtil.getMakerFee(bsqWalletService, preferences, Coin.valueOf(amount), marketPriceAvailable, marketPriceMargin), "makerFee must not be null"); + checkNotNull(getMakerFee(), "makerFee must not be null"); long maxTradeLimit = paymentAccount.getPaymentMethod().getMaxTradeLimitAsCoin(currencyCode).value; long maxTradePeriod = paymentAccount.getPaymentMethod().getMaxTradePeriod(); @@ -380,8 +380,8 @@ Offer createAndGetOffer() { Version.VERSION, btcWalletService.getLastBlockSeenHeight(), txFeeFromFeeService.value, - OfferUtil.getMakerFee(bsqWalletService, preferences, Coin.valueOf(amount), marketPriceAvailable, marketPriceMargin).value, - OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, Coin.valueOf(amount), marketPriceAvailable, marketPriceMargin), + getMakerFee().value, + isCurrencyForMakerFeeBtc(), buyerSecurityDepositAsCoin.value, sellerSecurityDeposit.value, maxTradeLimit, @@ -400,7 +400,7 @@ Offer createAndGetOffer() { } void onPlaceOffer(Offer offer, TransactionResultHandler resultHandler) { - checkNotNull(OfferUtil.getMakerFee(bsqWalletService, preferences, this.amount.get(), marketPriceAvailable, marketPriceMargin), "makerFee must not be null"); + checkNotNull(getMakerFee(), "makerFee must not be null"); Coin reservedFundsForOffer = getSecurityDeposit(); if (!isBuyOffer()) @@ -586,10 +586,10 @@ void calculateTotalToPay() { // Maker does not pay the tx fee for the trade txs because the mining fee might be different when maker // created the offer and reserved his funds, so that would not work well with dynamic fees. // The mining fee for the createOfferFee tx is deducted from the createOfferFee and not visible to the trader - final Coin makerFee = OfferUtil.getMakerFee(bsqWalletService, preferences, this.amount.get(), marketPriceAvailable, marketPriceMargin); + final Coin makerFee = getMakerFee(); if (direction != null && amount.get() != null && makerFee != null) { Coin feeAndSecDeposit = getTxFee().add(getSecurityDeposit()); - if (OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount.get(), marketPriceAvailable, marketPriceMargin)) + if (isCurrencyForMakerFeeBtc()) feeAndSecDeposit = feeAndSecDeposit.add(makerFee); Coin total = isBuyOffer() ? feeAndSecDeposit : feeAndSecDeposit.add(amount.get()); totalToPayAsCoin.set(total); @@ -645,10 +645,10 @@ private boolean isBalanceSufficient(Coin balance) { } public Coin getTxFee() { - if (OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount.get(), marketPriceAvailable, marketPriceMargin)) + if (isCurrencyForMakerFeeBtc()) return txFeeFromFeeService; else - return txFeeFromFeeService.subtract(OfferUtil.getMakerFee(bsqWalletService, preferences, this.amount.get(), marketPriceAvailable, marketPriceMargin)); + return txFeeFromFeeService.subtract(getMakerFee()); } public Preferences getPreferences() {