Search in sources :

Example 1 with OfferPayload

use of io.bisq.core.offer.OfferPayload in project bisq-api by mrosseel.

the class OfferBuilder method build.

public Offer build(String offerId, String accountId, OfferPayload.Direction direction, BigDecimal amount, BigDecimal minAmount, boolean useMarketBasedPrice, Double marketPriceMargin, String marketPair, long fiatPrice, Long buyerSecurityDeposit) throws NoAcceptedArbitratorException, PaymentAccountNotFoundException, IncompatiblePaymentAccountException {
    final List<NodeAddress> acceptedArbitratorAddresses = user.getAcceptedArbitratorAddresses();
    if (null == acceptedArbitratorAddresses || acceptedArbitratorAddresses.size() == 0) {
        throw new NoAcceptedArbitratorException("No arbitrator has been chosen");
    }
    // Checked that if fixed we have a fixed price, if percentage we have a percentage
    if (marketPriceMargin == null && useMarketBasedPrice) {
        throw new ValidationException("When choosing PERCENTAGE price, fill in percentageFromMarketPrice");
    } else if (0 == fiatPrice && !useMarketBasedPrice) {
        throw new ValidationException("When choosing FIXED price, fill in fixed_price with a price > 0");
    }
    // fix marketPair if it's lowercase
    marketPair = marketPair.toUpperCase();
    checkMarketValidity(marketPair);
    Market market = new Market(marketPair);
    // if right side is fiat, then left is base currency.
    // else right side is base currency.
    String baseCurrencyCode = CurrencyUtil.isFiatCurrency(market.getRsymbol()) ? market.getLsymbol() : market.getRsymbol();
    String counterCurrencyCode = CurrencyUtil.isFiatCurrency(market.getRsymbol()) ? market.getRsymbol() : market.getLsymbol();
    Optional<PaymentAccount> optionalAccount = getPaymentAccounts().stream().filter(account1 -> account1.getId().equals(accountId)).findFirst();
    if (!optionalAccount.isPresent()) {
        throw new PaymentAccountNotFoundException("Could not find payment account with id: " + accountId);
    }
    PaymentAccount paymentAccount = optionalAccount.get();
    // COPIED from CreateDataOfferModel: TODO refactor uit of GUI module  /////////////////////////////
    String countryCode = paymentAccount instanceof CountryBasedPaymentAccount ? ((CountryBasedPaymentAccount) paymentAccount).getCountry().code : null;
    ArrayList<String> acceptedCountryCodes = null;
    if (paymentAccount instanceof SepaAccount) {
        acceptedCountryCodes = new ArrayList<>();
        acceptedCountryCodes.addAll(((SepaAccount) paymentAccount).getAcceptedCountryCodes());
    } else if (paymentAccount instanceof CountryBasedPaymentAccount) {
        acceptedCountryCodes = new ArrayList<>();
        acceptedCountryCodes.add(((CountryBasedPaymentAccount) paymentAccount).getCountry().code);
    }
    String bankId = paymentAccount instanceof BankAccount ? ((BankAccount) paymentAccount).getBankId() : null;
    ArrayList<String> acceptedBanks = null;
    if (paymentAccount instanceof SpecificBanksAccount) {
        acceptedBanks = new ArrayList<>(((SpecificBanksAccount) paymentAccount).getAcceptedBanks());
    } else if (paymentAccount instanceof SameBankAccount) {
        acceptedBanks = new ArrayList<>();
        acceptedBanks.add(((SameBankAccount) paymentAccount).getBankId());
    }
    long maxTradeLimit = paymentAccount.getPaymentMethod().getMaxTradeLimitAsCoin(baseCurrencyCode).value;
    long maxTradePeriod = paymentAccount.getPaymentMethod().getMaxTradePeriod();
    boolean isPrivateOffer = false;
    boolean useAutoClose = false;
    boolean useReOpenAfterAutoClose = false;
    long lowerClosePrice = 0;
    long upperClosePrice = 0;
    String hashOfChallenge = null;
    HashMap<String, String> extraDataMap = null;
    // COPIED from CreateDataOfferModel /////////////////////////////
    updateMarketPriceAvailable(baseCurrencyCode);
    // TODO dummy values in this constructor !!!
    Coin coinAmount = Coin.valueOf(amount.longValueExact());
    if (null == buyerSecurityDeposit) {
        buyerSecurityDeposit = preferences.getBuyerSecurityDepositAsCoin().value;
    }
    OfferPayload offerPayload = new OfferPayload(null == offerId ? UUID.randomUUID().toString() : offerId, new Date().getTime(), p2PService.getAddress(), keyRing.getPubKeyRing(), direction, fiatPrice, marketPriceMargin, useMarketBasedPrice, amount.longValueExact(), minAmount.longValueExact(), baseCurrencyCode, counterCurrencyCode, acceptedArbitratorAddresses, user.getAcceptedMediatorAddresses(), paymentAccount.getPaymentMethod().getId(), paymentAccount.getId(), // will be filled in by BroadcastMakerFeeTx class
    null, countryCode, acceptedCountryCodes, bankId, acceptedBanks, Version.VERSION, btcWalletService.getLastBlockSeenHeight(), // default also used in code CreateOfferDataModel
    feeService.getTxFee(600).value, getMakerFee(coinAmount, marketPriceMargin).value, preferences.getPayFeeInBtc() || !isBsqForFeeAvailable(coinAmount, marketPriceMargin), buyerSecurityDeposit, Restrictions.getSellerSecurityDeposit().value, maxTradeLimit, maxTradePeriod, useAutoClose, useReOpenAfterAutoClose, upperClosePrice, lowerClosePrice, isPrivateOffer, hashOfChallenge, extraDataMap, Version.TRADE_PROTOCOL_VERSION);
    Offer offer = new Offer(offerPayload);
    offer.setPriceFeedService(priceFeedService);
    if (!isPaymentAccountValidForOffer(offer, paymentAccount)) {
        final String errorMessage = "PaymentAccount is not valid for offer, needs " + offer.getCurrencyCode();
        throw new IncompatiblePaymentAccountException(errorMessage);
    }
    if (null == getMakerFee(false, Coin.valueOf(amount.longValue()), marketPriceMargin)) {
        throw new ValidationException("makerFee must not be null");
    }
    return offer;
}
Also used : java.util(java.util) Version(io.bisq.common.app.Version) Coin(org.bitcoinj.core.Coin) Inject(com.google.inject.Inject) Preferences(io.bisq.core.user.Preferences) User(io.bisq.core.user.User) StringUtils(org.apache.commons.lang3.StringUtils) PaymentAccountUtil.isPaymentAccountValidForOffer(io.bisq.core.payment.PaymentAccountUtil.isPaymentAccountValidForOffer) BigDecimal(java.math.BigDecimal) CoinUtil(io.bisq.core.util.CoinUtil) Market(io.bisq.api.model.Market) FeeService(io.bisq.core.provider.fee.FeeService) CurrencyUtil(io.bisq.common.locale.CurrencyUtil) Nullable(javax.annotation.Nullable) OfferPayload(io.bisq.core.offer.OfferPayload) BisqEnvironment(io.bisq.core.app.BisqEnvironment) NodeAddress(io.bisq.network.p2p.NodeAddress) BsqWalletService(io.bisq.core.btc.wallet.BsqWalletService) MathUtils(io.bisq.common.util.MathUtils) Offer(io.bisq.core.offer.Offer) io.bisq.core.payment(io.bisq.core.payment) BtcWalletService(io.bisq.core.btc.wallet.BtcWalletService) P2PService(io.bisq.network.p2p.P2PService) ValidationException(javax.validation.ValidationException) KeyRing(io.bisq.common.crypto.KeyRing) PriceFeedService(io.bisq.core.provider.price.PriceFeedService) Restrictions(io.bisq.core.btc.Restrictions) ValidationException(javax.validation.ValidationException) Coin(org.bitcoinj.core.Coin) NodeAddress(io.bisq.network.p2p.NodeAddress) Market(io.bisq.api.model.Market) PaymentAccountUtil.isPaymentAccountValidForOffer(io.bisq.core.payment.PaymentAccountUtil.isPaymentAccountValidForOffer) Offer(io.bisq.core.offer.Offer) OfferPayload(io.bisq.core.offer.OfferPayload)

Aggregations

Inject (com.google.inject.Inject)1 Market (io.bisq.api.model.Market)1 Version (io.bisq.common.app.Version)1 KeyRing (io.bisq.common.crypto.KeyRing)1 CurrencyUtil (io.bisq.common.locale.CurrencyUtil)1 MathUtils (io.bisq.common.util.MathUtils)1 BisqEnvironment (io.bisq.core.app.BisqEnvironment)1 Restrictions (io.bisq.core.btc.Restrictions)1 BsqWalletService (io.bisq.core.btc.wallet.BsqWalletService)1 BtcWalletService (io.bisq.core.btc.wallet.BtcWalletService)1 Offer (io.bisq.core.offer.Offer)1 OfferPayload (io.bisq.core.offer.OfferPayload)1 io.bisq.core.payment (io.bisq.core.payment)1 PaymentAccountUtil.isPaymentAccountValidForOffer (io.bisq.core.payment.PaymentAccountUtil.isPaymentAccountValidForOffer)1 FeeService (io.bisq.core.provider.fee.FeeService)1 PriceFeedService (io.bisq.core.provider.price.PriceFeedService)1 Preferences (io.bisq.core.user.Preferences)1 User (io.bisq.core.user.User)1 CoinUtil (io.bisq.core.util.CoinUtil)1 NodeAddress (io.bisq.network.p2p.NodeAddress)1