From af439a86ad17edd9a5ed45c354f130aa7cd12286 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Mon, 7 Jan 2019 01:52:32 +0100 Subject: [PATCH 1/3] Remove market price based fee calculation --- .../main/java/bisq/core/offer/OfferUtil.java | 42 ++++--------------- .../main/offer/MutableOfferDataModel.java | 17 ++++---- .../createoffer/CreateOfferDataModel.java | 3 -- .../editoffer/EditOfferDataModel.java | 3 -- 4 files changed, 16 insertions(+), 49 deletions(-) diff --git a/core/src/main/java/bisq/core/offer/OfferUtil.java b/core/src/main/java/bisq/core/offer/OfferUtil.java index ad91bbc7ddc..f6dbafcaaa9 100644 --- a/core/src/main/java/bisq/core/offer/OfferUtil.java +++ b/core/src/main/java/bisq/core/offer/OfferUtil.java @@ -73,17 +73,12 @@ public static boolean isBuyOffer(OfferPayload.Direction direction) { * @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) { - final boolean isCurrencyForMakerFeeBtc = isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount, marketPriceAvailable, marketPriceMargin); - return getMakerFee(isCurrencyForMakerFeeBtc, - amount, - marketPriceAvailable, - marketPriceMargin); + public static Coin getMakerFee(BsqWalletService bsqWalletService, Preferences preferences, Coin amount) { + boolean isCurrencyForMakerFeeBtc = isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount); + return getMakerFee(isCurrencyForMakerFeeBtc, amount); } /** @@ -91,27 +86,13 @@ public static Coin getMakerFee(BsqWalletService bsqWalletService, Preferences pr * * @param isCurrencyForMakerFeeBtc * @param amount - * @param marketPriceAvailable - * @param marketPriceMargin * @return */ @Nullable - public static Coin getMakerFee(boolean isCurrencyForMakerFeeBtc, @Nullable Coin amount, boolean marketPriceAvailable, double marketPriceMargin) { + public static Coin getMakerFee(boolean isCurrencyForMakerFeeBtc, @Nullable Coin amount) { if (amount != null) { Coin feePerBtc = CoinUtil.getFeePerBtc(FeeService.getMakerFeePerBtc(isCurrencyForMakerFeeBtc), amount); - double makerFeeAsDouble = (double) feePerBtc.value; - if (marketPriceAvailable) { - if (marketPriceMargin > 0) - makerFeeAsDouble = makerFeeAsDouble * Math.sqrt(marketPriceMargin * 100); - else - makerFeeAsDouble = 0; - - // For BTC we round so min value change is 100 satoshi - if (isCurrencyForMakerFeeBtc) - makerFeeAsDouble = MathUtils.roundDouble(makerFeeAsDouble / 100, 0) * 100; - } - - return CoinUtil.maxCoin(Coin.valueOf(MathUtils.doubleToLong(makerFeeAsDouble)), FeeService.getMinMakerFee(isCurrencyForMakerFeeBtc)); + return CoinUtil.maxCoin(feePerBtc, FeeService.getMinMakerFee(isCurrencyForMakerFeeBtc)); } else { return null; } @@ -124,14 +105,11 @@ public static Coin getMakerFee(boolean isCurrencyForMakerFeeBtc, @Nullable Coin * @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) { + public static boolean isCurrencyForMakerFeeBtc(Preferences preferences, BsqWalletService bsqWalletService, Coin amount) { boolean payFeeInBtc = preferences.getPayFeeInBtc(); - boolean bsqForFeeAvailable = isBsqForMakerFeeAvailable(bsqWalletService, amount, marketPriceAvailable, marketPriceMargin); + boolean bsqForFeeAvailable = isBsqForMakerFeeAvailable(bsqWalletService, amount); return payFeeInBtc || !bsqForFeeAvailable; } @@ -140,13 +118,11 @@ public static boolean isCurrencyForMakerFeeBtc(Preferences preferences, BsqWalle * * @param bsqWalletService * @param amount - * @param marketPriceAvailable - * @param marketPriceMargin * @return */ - public static boolean isBsqForMakerFeeAvailable(BsqWalletService bsqWalletService, @Nullable Coin amount, boolean marketPriceAvailable, double marketPriceMargin) { + public static boolean isBsqForMakerFeeAvailable(BsqWalletService bsqWalletService, @Nullable Coin amount) { Coin availableBalance = bsqWalletService.getAvailableBalance(); - Coin makerFee = getMakerFee(false, amount, marketPriceAvailable, marketPriceMargin); + Coin makerFee = getMakerFee(false, amount); // If we don't know yet the maker fee (amount is not set) we return true, otherwise we would disable BSQ // fee each time we open the create offer screen as there the amount is not set. diff --git a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java index 4b7ad4a946a..5155d2a2583 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java @@ -53,7 +53,6 @@ import bisq.core.user.Preferences; import bisq.core.user.User; import bisq.core.util.BSFormatter; -import bisq.core.util.BsqFormatter; import bisq.network.p2p.P2PService; @@ -110,7 +109,6 @@ public abstract class MutableOfferDataModel extends OfferDataModel implements Bs private final TradeWalletService tradeWalletService; private final FeeService feeService; private final ReferralIdService referralIdService; - private final BsqFormatter bsqFormatter; private final BSFormatter btcFormatter; private final String offerId; private final BalanceListener btcBalanceListener; @@ -153,7 +151,7 @@ public MutableOfferDataModel(OpenOfferManager openOfferManager, BtcWalletService Preferences preferences, User user, KeyRing keyRing, P2PService p2PService, PriceFeedService priceFeedService, FilterManager filterManager, AccountAgeWitnessService accountAgeWitnessService, TradeWalletService tradeWalletService, - FeeService feeService, ReferralIdService referralIdService, BsqFormatter bsqFormatter, + FeeService feeService, ReferralIdService referralIdService, BSFormatter btcFormatter) { super(btcWalletService); @@ -169,7 +167,6 @@ public MutableOfferDataModel(OpenOfferManager openOfferManager, BtcWalletService this.tradeWalletService = tradeWalletService; this.feeService = feeService; this.referralIdService = referralIdService; - this.bsqFormatter = bsqFormatter; this.btcFormatter = btcFormatter; offerId = Utilities.getRandomPrefix(5, 8) + "-" + @@ -825,23 +822,23 @@ public void setMarketPriceAvailable(boolean marketPriceAvailable) { } public Coin getMakerFee(boolean isCurrencyForMakerFeeBtc) { - return OfferUtil.getMakerFee(isCurrencyForMakerFeeBtc, amount.get(), marketPriceAvailable, marketPriceMargin); + return OfferUtil.getMakerFee(isCurrencyForMakerFeeBtc, amount.get()); } public Coin getMakerFee() { - return OfferUtil.getMakerFee(bsqWalletService, preferences, amount.get(), marketPriceAvailable, marketPriceMargin); + return OfferUtil.getMakerFee(bsqWalletService, preferences, amount.get()); } public Coin getMakerFeeInBtc() { - return OfferUtil.getMakerFee(true, amount.get(), marketPriceAvailable, marketPriceMargin); + return OfferUtil.getMakerFee(true, amount.get()); } public Coin getMakerFeeInBsq() { - return OfferUtil.getMakerFee(false, amount.get(), marketPriceAvailable, marketPriceMargin); + return OfferUtil.getMakerFee(false, amount.get()); } public boolean isCurrencyForMakerFeeBtc() { - return OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount.get(), marketPriceAvailable, marketPriceMargin); + return OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount.get()); } public boolean isPreferredFeeCurrencyBtc() { @@ -849,7 +846,7 @@ public boolean isPreferredFeeCurrencyBtc() { } public boolean isBsqForFeeAvailable() { - return OfferUtil.isBsqForMakerFeeAvailable(bsqWalletService, amount.get(), marketPriceAvailable, marketPriceMargin); + return OfferUtil.isBsqForMakerFeeAvailable(bsqWalletService, amount.get()); } public boolean isHalCashAccount() { diff --git a/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModel.java b/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModel.java index 630ad4bbbd2..b3f3dd1e2ae 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModel.java @@ -35,7 +35,6 @@ import bisq.core.user.Preferences; import bisq.core.user.User; import bisq.core.util.BSFormatter; -import bisq.core.util.BsqFormatter; import bisq.network.p2p.P2PService; @@ -64,7 +63,6 @@ public CreateOfferDataModel(OpenOfferManager openOfferManager, TradeWalletService tradeWalletService, FeeService feeService, ReferralIdService referralIdService, - BsqFormatter bsqFormatter, BSFormatter btcFormatter) { super(openOfferManager, btcWalletService, @@ -79,7 +77,6 @@ public CreateOfferDataModel(OpenOfferManager openOfferManager, tradeWalletService, feeService, referralIdService, - bsqFormatter, btcFormatter); } } diff --git a/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java b/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java index 253b70de574..4472e574b88 100644 --- a/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java @@ -39,7 +39,6 @@ import bisq.core.user.Preferences; import bisq.core.user.User; import bisq.core.util.BSFormatter; -import bisq.core.util.BsqFormatter; import bisq.network.p2p.P2PService; @@ -69,7 +68,6 @@ class EditOfferDataModel extends MutableOfferDataModel { TradeWalletService tradeWalletService, FeeService feeService, ReferralIdService referralIdService, - BsqFormatter bsqFormatter, BSFormatter btcFormatter, CorePersistenceProtoResolver corePersistenceProtoResolver) { super(openOfferManager, @@ -85,7 +83,6 @@ class EditOfferDataModel extends MutableOfferDataModel { tradeWalletService, feeService, referralIdService, - bsqFormatter, btcFormatter); this.corePersistenceProtoResolver = corePersistenceProtoResolver; } From 5be44132baf64f1916edee5c32ae687d32cfffb0 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Mon, 7 Jan 2019 01:58:09 +0100 Subject: [PATCH 2/3] Apply code inspection - Add null and isPresent checks --- .../main/offer/MutableOfferDataModel.java | 14 ++++++-------- .../editoffer/EditOfferDataModel.java | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java index 5155d2a2583..755f4aa3a8e 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java @@ -135,10 +135,10 @@ public abstract class MutableOfferDataModel extends OfferDataModel implements Bs protected PaymentAccount paymentAccount; protected boolean isTabSelected; protected double marketPriceMargin = 0; - protected Coin txFeeFromFeeService = Coin.ZERO; - protected boolean marketPriceAvailable; - protected int feeTxSize = 260; // size of typical tx with 1 input - protected int feeTxSizeEstimationRecursionCounter; + private Coin txFeeFromFeeService = Coin.ZERO; + private boolean marketPriceAvailable; + private int feeTxSize = 260; // size of typical tx with 1 input + private int feeTxSizeEstimationRecursionCounter; protected boolean allowAmountUpdate = true; @@ -325,11 +325,9 @@ Offer createAndGetOffer() { ArrayList acceptedCountryCodes = null; if (paymentAccount instanceof SepaAccount) { - acceptedCountryCodes = new ArrayList<>(); - acceptedCountryCodes.addAll(((SepaAccount) paymentAccount).getAcceptedCountryCodes()); + acceptedCountryCodes = new ArrayList<>(((SepaAccount) paymentAccount).getAcceptedCountryCodes()); } else if (paymentAccount instanceof SepaInstantAccount) { - acceptedCountryCodes = new ArrayList<>(); - acceptedCountryCodes.addAll(((SepaInstantAccount) paymentAccount).getAcceptedCountryCodes()); + acceptedCountryCodes = new ArrayList<>(((SepaInstantAccount) paymentAccount).getAcceptedCountryCodes()); } else if (paymentAccount instanceof CountryBasedPaymentAccount) { acceptedCountryCodes = new ArrayList<>(); acceptedCountryCodes.add(((CountryBasedPaymentAccount) paymentAccount).getCountry().code); diff --git a/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java b/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java index 4472e574b88..2054869aa06 100644 --- a/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java @@ -48,6 +48,8 @@ import com.google.inject.Inject; +import java.util.Optional; + class EditOfferDataModel extends MutableOfferDataModel { private final CorePersistenceProtoResolver corePersistenceProtoResolver; @@ -114,14 +116,15 @@ public void applyOpenOffer(OpenOffer openOffer) { this.initialState = openOffer.getState(); PaymentAccount tmpPaymentAccount = user.getPaymentAccount(openOffer.getOffer().getMakerPaymentAccountId()); - TradeCurrency selectedTradeCurrency = CurrencyUtil.getTradeCurrency(openOffer.getOffer().getCurrencyCode()).get(); - - this.paymentAccount = PaymentAccount.fromProto(tmpPaymentAccount.toProtoMessage(), corePersistenceProtoResolver); - - if (paymentAccount.getSingleTradeCurrency() != null) - paymentAccount.setSingleTradeCurrency(selectedTradeCurrency); - else - paymentAccount.setSelectedTradeCurrency(selectedTradeCurrency); + Optional optionalTradeCurrency = CurrencyUtil.getTradeCurrency(openOffer.getOffer().getCurrencyCode()); + if (optionalTradeCurrency.isPresent() && tmpPaymentAccount != null) { + TradeCurrency selectedTradeCurrency = optionalTradeCurrency.get(); + this.paymentAccount = PaymentAccount.fromProto(tmpPaymentAccount.toProtoMessage(), corePersistenceProtoResolver); + if (paymentAccount.getSingleTradeCurrency() != null) + paymentAccount.setSingleTradeCurrency(selectedTradeCurrency); + else + paymentAccount.setSelectedTradeCurrency(selectedTradeCurrency); + } allowAmountUpdate = false; } From f0eb12df0631b5e283c5a6cdb2419c1ee6a017dc Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Mon, 7 Jan 2019 12:19:45 +0100 Subject: [PATCH 3/3] Remove param from CreateOfferViewModelTest --- .../main/offer/createoffer/CreateOfferViewModelTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferViewModelTest.java b/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferViewModelTest.java index bdd3ff6f0e6..d38f32ed5cc 100644 --- a/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferViewModelTest.java +++ b/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferViewModelTest.java @@ -112,7 +112,7 @@ public void setUp() { when(bsqFormatter.formatCoin(any())).thenReturn("0"); when(bsqWalletService.getAvailableBalance()).thenReturn(Coin.ZERO); - CreateOfferDataModel dataModel = new CreateOfferDataModel(null, btcWalletService, bsqWalletService, empty, user, null, null, priceFeedService, null, accountAgeWitnessService, null, feeService, null, bsqFormatter, bsFormatter); + CreateOfferDataModel dataModel = new CreateOfferDataModel(null, btcWalletService, bsqWalletService, empty, user, null, null, priceFeedService, null, accountAgeWitnessService, null, feeService, null, bsFormatter); dataModel.initWithData(OfferPayload.Direction.BUY, new CryptoCurrency("BTC", "bitcoin")); dataModel.activate();