From c4b88ab512e7a7784bd3ab67557057432d6c8d56 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 14 Nov 2019 22:56:42 -0500 Subject: [PATCH] Use createAndGetOffer from createOfferService --- .../bisq/core/offer/CreateOfferService.java | 141 +++++++++++++++++- .../main/offer/MutableOfferDataModel.java | 107 ++----------- 2 files changed, 149 insertions(+), 99 deletions(-) diff --git a/core/src/main/java/bisq/core/offer/CreateOfferService.java b/core/src/main/java/bisq/core/offer/CreateOfferService.java index 9be08b137e..2796861bfe 100644 --- a/core/src/main/java/bisq/core/offer/CreateOfferService.java +++ b/core/src/main/java/bisq/core/offer/CreateOfferService.java @@ -20,16 +20,26 @@ package bisq.core.offer; import bisq.core.account.witness.AccountAgeWitnessService; import bisq.core.btc.TxFeeEstimationService; import bisq.core.btc.wallet.BsqWalletService; +import bisq.core.btc.wallet.BtcWalletService; import bisq.core.btc.wallet.Restrictions; +import bisq.core.filter.FilterManager; +import bisq.core.locale.CurrencyUtil; +import bisq.core.locale.Res; import bisq.core.monetary.Price; import bisq.core.payment.HalCashAccount; import bisq.core.payment.PaymentAccount; +import bisq.core.payment.PaymentAccountUtil; import bisq.core.provider.price.MarketPrice; import bisq.core.provider.price.PriceFeedService; +import bisq.core.trade.statistics.ReferralIdService; import bisq.core.user.Preferences; +import bisq.core.user.User; import bisq.core.util.CoinUtil; +import bisq.network.p2p.P2PService; + import bisq.common.app.Version; +import bisq.common.crypto.PubKeyRing; import bisq.common.util.Tuple2; import bisq.common.util.Utilities; @@ -38,6 +48,11 @@ import org.bitcoinj.core.Coin; import javax.inject.Inject; import javax.inject.Singleton; +import com.google.common.collect.Lists; + +import java.util.Date; +import java.util.List; +import java.util.Map; import java.util.UUID; import lombok.extern.slf4j.Slf4j; @@ -51,6 +66,12 @@ public class CreateOfferService { private final Preferences preferences; private final PriceFeedService priceFeedService; private final AccountAgeWitnessService accountAgeWitnessService; + private final ReferralIdService referralIdService; + private final FilterManager filterManager; + private final P2PService p2PService; + private final PubKeyRing pubKeyRing; + private final User user; + private final BtcWalletService btcWalletService; /////////////////////////////////////////////////////////////////////////////////////////// @@ -63,13 +84,25 @@ public class CreateOfferService { BsqWalletService bsqWalletService, Preferences preferences, PriceFeedService priceFeedService, - AccountAgeWitnessService accountAgeWitnessService) { + AccountAgeWitnessService accountAgeWitnessService, + ReferralIdService referralIdService, + FilterManager filterManager, + P2PService p2PService, + PubKeyRing pubKeyRing, + User user, + BtcWalletService btcWalletService) { this.txFeeEstimationService = txFeeEstimationService; this.makerFeeProvider = makerFeeProvider; this.bsqWalletService = bsqWalletService; this.preferences = preferences; this.priceFeedService = priceFeedService; this.accountAgeWitnessService = accountAgeWitnessService; + this.referralIdService = referralIdService; + this.filterManager = filterManager; + this.p2PService = p2PService; + this.pubKeyRing = pubKeyRing; + this.user = user; + this.btcWalletService = btcWalletService; } @@ -160,6 +193,108 @@ public class CreateOfferService { } } + public Offer createAndGetOffer(String offerId, + OfferPayload.Direction direction, + String currencyCode, + Coin amount, + Coin minAmount, + boolean useMarketBasedPrice, + Price price, + double marketPriceMargin, + double buyerSecurityDeposit, + PaymentAccount paymentAccount) { + boolean useMarketBasedPriceValue = isUseMarketBasedPriceValue(useMarketBasedPrice, currencyCode, paymentAccount); + + long priceAsLong = getPriceAsLong(price, useMarketBasedPriceValue); + + boolean isCryptoCurrency = CurrencyUtil.isCryptoCurrency(currencyCode); + String baseCurrencyCode = isCryptoCurrency ? currencyCode : Res.getBaseCurrencyCode(); + String counterCurrencyCode = isCryptoCurrency ? Res.getBaseCurrencyCode() : currencyCode; + + double marketPriceMarginParam = marketPriceMarginParam(useMarketBasedPriceValue, marketPriceMargin); + + long amountAsLong = amount != null ? amount.getValue() : 0L; + long minAmountAsLong = minAmount != null ? minAmount.getValue() : 0L; + + List acceptedCountryCodes = PaymentAccountUtil.getAcceptedCountryCodes(paymentAccount); + List acceptedBanks = PaymentAccountUtil.getAcceptedBanks(paymentAccount); + String bankId = PaymentAccountUtil.getBankId(paymentAccount); + String countryCode = PaymentAccountUtil.getCountryCode(paymentAccount); + + long maxTradeLimit = getMaxTradeLimit(paymentAccount, currencyCode, direction); + long maxTradePeriod = paymentAccount.getMaxTradePeriod(); + + // reserved for future use cases + // Use null values if not set + boolean isPrivateOffer = false; + boolean useAutoClose = false; + boolean useReOpenAfterAutoClose = false; + long lowerClosePrice = 0; + long upperClosePrice = 0; + String hashOfChallenge = null; + + Coin makerFeeAsCoin = getMakerFee(amount); + + Map extraDataMap = OfferUtil.getExtraDataMap(accountAgeWitnessService, + referralIdService, + paymentAccount, + currencyCode, + preferences); + + OfferUtil.validateOfferData(filterManager, + p2PService, + buyerSecurityDeposit, + paymentAccount, + currencyCode, + makerFeeAsCoin); + + Coin buyerSecurityDepositAsCoin = getBuyerSecurityDepositAsCoin(amount, buyerSecurityDeposit); + double sellerSecurityDeposit = getSellerSecurityDeposit(); + Coin sellerSecurityDepositAsCoin = getSellerSecurityDepositAsCoin(amount, getSellerSecurityDeposit()); + Coin txFeeFromFeeService = getEstimatedFeeAndTxSize(amount, direction, buyerSecurityDeposit, sellerSecurityDeposit).first; + OfferPayload offerPayload = new OfferPayload(offerId, + new Date().getTime(), + p2PService.getAddress(), + pubKeyRing, + OfferPayload.Direction.valueOf(direction.name()), + priceAsLong, + marketPriceMarginParam, + useMarketBasedPriceValue, + amountAsLong, + minAmountAsLong, + baseCurrencyCode, + counterCurrencyCode, + Lists.newArrayList(user.getAcceptedArbitratorAddresses()), + Lists.newArrayList(user.getAcceptedMediatorAddresses()), + paymentAccount.getPaymentMethod().getId(), + paymentAccount.getId(), + null, + countryCode, + acceptedCountryCodes, + bankId, + acceptedBanks, + Version.VERSION, + btcWalletService.getLastBlockSeenHeight(), + txFeeFromFeeService.value, + makerFeeAsCoin.value, + isCurrencyForMakerFeeBtc(amount), + buyerSecurityDepositAsCoin.value, + sellerSecurityDepositAsCoin.value, + maxTradeLimit, + maxTradePeriod, + useAutoClose, + useReOpenAfterAutoClose, + upperClosePrice, + lowerClosePrice, + isPrivateOffer, + hashOfChallenge, + extraDataMap, + Version.TRADE_PROTOCOL_VERSION); + Offer offer = new Offer(offerPayload); + offer.setPriceFeedService(priceFeedService); + return offer; + } + /////////////////////////////////////////////////////////////////////////////////////////// // Private /////////////////////////////////////////////////////////////////////////////////////////// @@ -195,4 +330,8 @@ public class CreateOfferService { return Coin.valueOf(Math.max(Restrictions.getMinSellerSecurityDepositAsCoin().value, value.value)); } + private boolean isCurrencyForMakerFeeBtc(Coin amount) { + return OfferUtil.isCurrencyForMakerFeeBtc(preferences, bsqWalletService, amount); + } + } 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 d1a2c48092..fdf5f7c834 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java @@ -31,7 +31,6 @@ import bisq.core.btc.wallet.BtcWalletService; import bisq.core.btc.wallet.Restrictions; import bisq.core.filter.FilterManager; import bisq.core.locale.CurrencyUtil; -import bisq.core.locale.Res; import bisq.core.locale.TradeCurrency; import bisq.core.monetary.Price; import bisq.core.monetary.Volume; @@ -42,7 +41,6 @@ import bisq.core.offer.OfferUtil; import bisq.core.offer.OpenOfferManager; import bisq.core.payment.HalCashAccount; import bisq.core.payment.PaymentAccount; -import bisq.core.payment.PaymentAccountUtil; import bisq.core.provider.fee.FeeService; import bisq.core.provider.price.PriceFeedService; import bisq.core.trade.handlers.TransactionResultHandler; @@ -54,7 +52,6 @@ import bisq.core.util.CoinUtil; import bisq.network.p2p.P2PService; -import bisq.common.app.Version; import bisq.common.crypto.KeyRing; import bisq.common.util.MathUtils; import bisq.common.util.Tuple2; @@ -65,8 +62,6 @@ import org.bitcoinj.core.Transaction; import com.google.inject.Inject; -import com.google.common.collect.Lists; - import javafx.beans.property.BooleanProperty; import javafx.beans.property.DoubleProperty; import javafx.beans.property.ObjectProperty; @@ -84,10 +79,7 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.collections.SetChangeListener; -import java.util.Date; import java.util.HashSet; -import java.util.List; -import java.util.Map; import java.util.Optional; import javax.annotation.Nullable; @@ -301,98 +293,17 @@ public abstract class MutableOfferDataModel extends OfferDataModel implements Bs // UI actions /////////////////////////////////////////////////////////////////////////////////////////// - @SuppressWarnings("ConstantConditions") Offer createAndGetOffer() { - boolean useMarketBasedPriceValue = createOfferService.isUseMarketBasedPriceValue(useMarketBasedPrice.get(), tradeCurrencyCode.get(), paymentAccount); - - long priceAsLong = createOfferService.getPriceAsLong(price.get(), useMarketBasedPriceValue); - - String currencyCode = tradeCurrencyCode.get(); - boolean isCryptoCurrency = CurrencyUtil.isCryptoCurrency(currencyCode); - String baseCurrencyCode = isCryptoCurrency ? currencyCode : Res.getBaseCurrencyCode(); - String counterCurrencyCode = isCryptoCurrency ? Res.getBaseCurrencyCode() : currencyCode; - - double marketPriceMarginParam = createOfferService.marketPriceMarginParam(useMarketBasedPriceValue, marketPriceMargin); - - long amountAsLong = this.amount.get() != null ? this.amount.get().getValue() : 0L; - long minAmountAsLong = this.minAmount.get() != null ? this.minAmount.get().getValue() : 0L; - - List acceptedCountryCodes = PaymentAccountUtil.getAcceptedCountryCodes(paymentAccount); - List acceptedBanks = PaymentAccountUtil.getAcceptedBanks(paymentAccount); - String bankId = PaymentAccountUtil.getBankId(paymentAccount); - String countryCode = PaymentAccountUtil.getCountryCode(paymentAccount); - - long maxTradeLimit = createOfferService.getMaxTradeLimit(paymentAccount, tradeCurrencyCode.get(), direction); - long maxTradePeriod = paymentAccount.getMaxTradePeriod(); - - // reserved for future use cases - // Use null values if not set - boolean isPrivateOffer = false; - boolean useAutoClose = false; - boolean useReOpenAfterAutoClose = false; - long lowerClosePrice = 0; - long upperClosePrice = 0; - String hashOfChallenge = null; - - Coin makerFeeAsCoin = getMakerFee(); - - Map extraDataMap = OfferUtil.getExtraDataMap(accountAgeWitnessService, - referralIdService, - paymentAccount, - currencyCode, - preferences); - - OfferUtil.validateOfferData(filterManager, - p2PService, + return createOfferService.createAndGetOffer(offerId, + direction, + tradeCurrencyCode.get(), + amount.get(), + minAmount.get(), + useMarketBasedPrice.get(), + price.get(), + marketPriceMargin, buyerSecurityDeposit.get(), - paymentAccount, - currencyCode, - makerFeeAsCoin); - - Coin buyerSecurityDepositAsCoin = createOfferService.getBuyerSecurityDepositAsCoin(amount.get(), buyerSecurityDeposit.get()); - Coin sellerSecurityDepositAsCoin = createOfferService.getSellerSecurityDepositAsCoin(amount.get(), sellerSecurityDeposit.get()); - Coin txFeeFromFeeService = createOfferService.getEstimatedFeeAndTxSize(amount.get(), direction, buyerSecurityDeposit.get(), sellerSecurityDeposit.get()).first; - OfferPayload offerPayload = new OfferPayload(offerId, - new Date().getTime(), - p2PService.getAddress(), - keyRing.getPubKeyRing(), - OfferPayload.Direction.valueOf(direction.name()), - priceAsLong, - marketPriceMarginParam, - useMarketBasedPriceValue, - amountAsLong, - minAmountAsLong, - baseCurrencyCode, - counterCurrencyCode, - Lists.newArrayList(user.getAcceptedArbitratorAddresses()), - Lists.newArrayList(user.getAcceptedMediatorAddresses()), - paymentAccount.getPaymentMethod().getId(), - paymentAccount.getId(), - null, - countryCode, - acceptedCountryCodes, - bankId, - acceptedBanks, - Version.VERSION, - btcWalletService.getLastBlockSeenHeight(), - txFeeFromFeeService.value, - makerFeeAsCoin.value, - isCurrencyForMakerFeeBtc(), - buyerSecurityDepositAsCoin.value, - sellerSecurityDepositAsCoin.value, - maxTradeLimit, - maxTradePeriod, - useAutoClose, - useReOpenAfterAutoClose, - upperClosePrice, - lowerClosePrice, - isPrivateOffer, - hashOfChallenge, - extraDataMap, - Version.TRADE_PROTOCOL_VERSION); - Offer offer = new Offer(offerPayload); - offer.setPriceFeedService(priceFeedService); - return offer; + paymentAccount); } // This works only if we have already funds in the wallet