use of bisq.core.offer.OfferPayload in project bisq-desktop by bisq-network.
the class BSFormatterTest method testFormatAmountWithAlignmenWithDecimals.
@Test
public void testFormatAmountWithAlignmenWithDecimals() {
OfferPayload offerPayload = mock(OfferPayload.class);
Offer offer = new Offer(offerPayload);
when(offerPayload.getMinAmount()).thenReturn(10000000L);
when(offerPayload.getAmount()).thenReturn(20000000L);
assertEquals("0.1000 - 0.2000", formatter.formatAmount(offer, 4, true, 15));
}
use of bisq.core.offer.OfferPayload in project bisq-desktop by bisq-network.
the class CreateOfferDataModel method createAndGetOffer.
// /////////////////////////////////////////////////////////////////////////////////////////
// UI actions
// /////////////////////////////////////////////////////////////////////////////////////////
@SuppressWarnings("ConstantConditions")
Offer createAndGetOffer() {
final boolean useMarketBasedPriceValue = marketPriceAvailable && useMarketBasedPrice.get();
long priceAsLong = price.get() != null && !useMarketBasedPriceValue ? price.get().getValue() : 0L;
String currencyCode = tradeCurrencyCode.get();
boolean isCryptoCurrency = CurrencyUtil.isCryptoCurrency(currencyCode);
String baseCurrencyCode = isCryptoCurrency ? currencyCode : Res.getBaseCurrencyCode();
String counterCurrencyCode = isCryptoCurrency ? Res.getBaseCurrencyCode() : currencyCode;
double marketPriceMarginParam = useMarketBasedPriceValue ? marketPriceMargin : 0;
long amount = this.amount.get() != null ? this.amount.get().getValue() : 0L;
long minAmount = this.minAmount.get() != null ? this.minAmount.get().getValue() : 0L;
ArrayList<String> acceptedCountryCodes = null;
if (paymentAccount instanceof SepaAccount) {
acceptedCountryCodes = new ArrayList<>();
acceptedCountryCodes.addAll(((SepaAccount) paymentAccount).getAcceptedCountryCodes());
} else if (paymentAccount instanceof SepaInstantAccount) {
acceptedCountryCodes = new ArrayList<>();
acceptedCountryCodes.addAll(((SepaInstantAccount) paymentAccount).getAcceptedCountryCodes());
} else if (paymentAccount instanceof CountryBasedPaymentAccount) {
acceptedCountryCodes = new ArrayList<>();
acceptedCountryCodes.add(((CountryBasedPaymentAccount) paymentAccount).getCountry().code);
}
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());
}
String bankId = paymentAccount instanceof BankAccount ? ((BankAccount) paymentAccount).getBankId() : null;
// That is optional and set to null if not supported (AltCoins, OKPay,...)
String countryCode = paymentAccount instanceof CountryBasedPaymentAccount ? ((CountryBasedPaymentAccount) paymentAccount).getCountry().code : null;
checkNotNull(p2PService.getAddress(), "Address must not be null");
checkNotNull(getMakerFee(), "makerFee must not be null");
long maxTradeLimit = getMaxTradeLimit();
long maxTradePeriod = paymentAccount.getPaymentMethod().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;
HashMap<String, String> extraDataMap = null;
if (CurrencyUtil.isFiatCurrency(currencyCode)) {
extraDataMap = new HashMap<>();
final String myWitnessHashAsHex = accountAgeWitnessService.getMyWitnessHashAsHex(paymentAccount.getPaymentAccountPayload());
extraDataMap.put(OfferPayload.ACCOUNT_AGE_WITNESS_HASH, myWitnessHashAsHex);
}
Coin buyerSecurityDepositAsCoin = buyerSecurityDeposit.get();
checkArgument(buyerSecurityDepositAsCoin.compareTo(Restrictions.getMaxBuyerSecurityDeposit()) <= 0, "securityDeposit must be not exceed " + Restrictions.getMaxBuyerSecurityDeposit().toFriendlyString());
checkArgument(buyerSecurityDepositAsCoin.compareTo(Restrictions.getMinBuyerSecurityDeposit()) >= 0, "securityDeposit must be not be less than " + Restrictions.getMinBuyerSecurityDeposit().toFriendlyString());
checkArgument(!filterManager.isCurrencyBanned(currencyCode), Res.get("offerbook.warning.currencyBanned"));
checkArgument(!filterManager.isPaymentMethodBanned(paymentAccount.getPaymentMethod()), Res.get("offerbook.warning.paymentMethodBanned"));
OfferPayload offerPayload = new OfferPayload(offerId, new Date().getTime(), p2PService.getAddress(), keyRing.getPubKeyRing(), OfferPayload.Direction.valueOf(direction.name()), priceAsLong, marketPriceMarginParam, useMarketBasedPriceValue, amount, minAmount, 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, getMakerFee().value, isCurrencyForMakerFeeBtc(), buyerSecurityDepositAsCoin.value, sellerSecurityDeposit.value, maxTradeLimit, maxTradePeriod, useAutoClose, useReOpenAfterAutoClose, upperClosePrice, lowerClosePrice, isPrivateOffer, hashOfChallenge, extraDataMap, Version.TRADE_PROTOCOL_VERSION);
Offer offer = new Offer(offerPayload);
offer.setPriceFeedService(priceFeedService);
return offer;
}
use of bisq.core.offer.OfferPayload in project bisq-desktop by bisq-network.
the class BSFormatterTest method testFormatAmountWithAlignmenWithDecimalsNoRange.
@Test
public void testFormatAmountWithAlignmenWithDecimalsNoRange() {
OfferPayload offerPayload = mock(OfferPayload.class);
Offer offer = new Offer(offerPayload);
when(offerPayload.getMinAmount()).thenReturn(10000000L);
when(offerPayload.getAmount()).thenReturn(10000000L);
assertEquals(" 0.1000", formatter.formatAmount(offer, 4, true, 15));
}
use of bisq.core.offer.OfferPayload in project bisq-desktop by bisq-network.
the class BSFormatterTest method testFormatDifferentAmount.
@Test
public void testFormatDifferentAmount() {
OfferPayload offerPayload = mock(OfferPayload.class);
Offer offer = new Offer(offerPayload);
when(offerPayload.getMinAmount()).thenReturn(10000000L);
when(offerPayload.getAmount()).thenReturn(20000000L);
assertEquals("0.10 - 0.20", formatter.formatAmount(offer));
}
use of 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, long amount, long 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 fixedPrice with a price > 0");
}
if (null == marketPriceMargin)
marketPriceMargin = 0d;
// 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.
final String currencyCode = market.getRsymbol();
final boolean isFiatCurrency = CurrencyUtil.isFiatCurrency(currencyCode);
String baseCurrencyCode = !isFiatCurrency ? currencyCode : market.getLsymbol();
String counterCurrencyCode = !isFiatCurrency ? market.getLsymbol() : currencyCode;
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;
if (isFiatCurrency) {
extraDataMap = new HashMap<>();
final String myWitnessHashAsHex = accountAgeWitnessService.getMyWitnessHashAsHex(paymentAccount.getPaymentAccountPayload());
extraDataMap.put(OfferPayload.ACCOUNT_AGE_WITNESS_HASH, myWitnessHashAsHex);
}
// COPIED from CreateDataOfferModel /////////////////////////////
updateMarketPriceAvailable(baseCurrencyCode);
// TODO dummy values in this constructor !!!
Coin coinAmount = Coin.valueOf(amount);
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, minAmount, 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), marketPriceMargin)) {
throw new ValidationException("makerFee must not be null");
}
return offer;
}
Aggregations