Search in sources :

Example 1 with Price

use of bisq.core.monetary.Price in project bisq-core by bisq-network.

the class OfferForJson method setDisplayStrings.

private void setDisplayStrings() {
    try {
        final Price price = getPrice();
        if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
            primaryMarketDirection = direction == OfferPayload.Direction.BUY ? OfferPayload.Direction.SELL : OfferPayload.Direction.BUY;
            currencyPair = currencyCode + "/" + Res.getBaseCurrencyCode();
            // int precision = 8;
            // decimalFormat.setMaximumFractionDigits(precision);
            // amount and volume is inverted for json
            priceDisplayString = altcoinFormat.noCode().format(price.getMonetary()).toString();
            primaryMarketMinAmountDisplayString = altcoinFormat.noCode().format(getMinVolume().getMonetary()).toString();
            primaryMarketAmountDisplayString = altcoinFormat.noCode().format(getVolume().getMonetary()).toString();
            primaryMarketMinVolumeDisplayString = coinFormat.noCode().format(getMinAmountAsCoin()).toString();
            primaryMarketVolumeDisplayString = coinFormat.noCode().format(getAmountAsCoin()).toString();
            primaryMarketPrice = price.getValue();
            primaryMarketMinAmount = getMinVolume().getValue();
            primaryMarketAmount = getVolume().getValue();
            primaryMarketMinVolume = getMinAmountAsCoin().getValue();
            primaryMarketVolume = getAmountAsCoin().getValue();
        } else {
            primaryMarketDirection = direction;
            currencyPair = Res.getBaseCurrencyCode() + "/" + currencyCode;
            priceDisplayString = fiatFormat.noCode().format(price.getMonetary()).toString();
            primaryMarketMinAmountDisplayString = coinFormat.noCode().format(getMinAmountAsCoin()).toString();
            primaryMarketAmountDisplayString = coinFormat.noCode().format(getAmountAsCoin()).toString();
            primaryMarketMinVolumeDisplayString = fiatFormat.noCode().format(getMinVolume().getMonetary()).toString();
            primaryMarketVolumeDisplayString = fiatFormat.noCode().format(getVolume().getMonetary()).toString();
            // we use precision 4 for fiat based price but on the markets api we use precision 8 so we scale up by 10000
            primaryMarketPrice = (long) MathUtils.scaleUpByPowerOf10(price.getValue(), 4);
            primaryMarketMinVolume = (long) MathUtils.scaleUpByPowerOf10(getMinVolume().getValue(), 4);
            primaryMarketVolume = (long) MathUtils.scaleUpByPowerOf10(getVolume().getValue(), 4);
            primaryMarketMinAmount = getMinAmountAsCoin().getValue();
            primaryMarketAmount = getAmountAsCoin().getValue();
        }
    } catch (Throwable t) {
        log.error("Error at setDisplayStrings: " + t.getMessage());
    }
}
Also used : Price(bisq.core.monetary.Price)

Example 2 with Price

use of bisq.core.monetary.Price in project bisq-desktop by bisq-network.

the class OfferBookView method initialize.

@Override
public void initialize() {
    root.setPadding(new Insets(20, 25, 5, 25));
    addTitledGroupBg(root, gridRow, 2, Res.get("offerbook.availableOffers"));
    final Tuple3<HBox, AutoTooltipLabel, ComboBox> filterBoxTuple = addHBoxLabelComboBox(root, gridRow, Res.get("offerbook.filterByCurrency"), Layout.FIRST_ROW_DISTANCE);
    final HBox filterBox = filterBoxTuple.first;
    currencyComboBox = filterBoxTuple.third;
    currencyComboBox.setPromptText(Res.get("list.currency.select"));
    // noinspection unchecked
    paymentMethodComboBox = new ComboBox<>();
    final Label paymentMethodLabel = new AutoTooltipLabel(Res.getWithCol("offerbook.filterByPaymentMethod"));
    paymentMethodLabel.setPadding(new Insets(0, 0, 0, 10));
    filterBox.getChildren().addAll(paymentMethodLabel, paymentMethodComboBox);
    paymentMethodComboBox.setPromptText(Res.get("shared.selectPaymentMethod"));
    paymentMethodComboBox.setVisibleRowCount(20);
    paymentMethodComboBox.setConverter(new StringConverter<PaymentMethod>() {

        @Override
        public String toString(PaymentMethod paymentMethod) {
            String id = paymentMethod.getId();
            if (id.equals(GUIUtil.SHOW_ALL_FLAG))
                return "▶ " + Res.get("list.currency.showAll");
            else if (paymentMethod.equals(PaymentMethod.BLOCK_CHAINS))
                return "✦ " + Res.get(id);
            else
                return "★ " + Res.get(id);
        }

        @Override
        public PaymentMethod fromString(String s) {
            return null;
        }
    });
    tableView = new TableView<>();
    GridPane.setRowIndex(tableView, ++gridRow);
    GridPane.setColumnIndex(tableView, 0);
    GridPane.setColumnSpan(tableView, 2);
    GridPane.setMargin(tableView, new Insets(10, -10, -10, -10));
    GridPane.setVgrow(tableView, Priority.ALWAYS);
    root.getChildren().add(tableView);
    marketColumn = getMarketColumn();
    priceColumn = getPriceColumn();
    tableView.getColumns().add(priceColumn);
    amountColumn = getAmountColumn();
    tableView.getColumns().add(amountColumn);
    volumeColumn = getVolumeColumn();
    tableView.getColumns().add(volumeColumn);
    TableColumn<OfferBookListItem, OfferBookListItem> paymentMethodColumn = getPaymentMethodColumn();
    tableView.getColumns().add(paymentMethodColumn);
    TableColumn<OfferBookListItem, OfferBookListItem> avatarColumn = getAvatarColumn();
    tableView.getColumns().add(avatarColumn);
    tableView.getColumns().add(getActionColumn());
    tableView.getSortOrder().add(priceColumn);
    tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    Label placeholder = new AutoTooltipLabel(Res.get("table.placeholder.noItems", Res.get("shared.multipleOffers")));
    placeholder.setWrapText(true);
    tableView.setPlaceholder(placeholder);
    marketColumn.setComparator((o1, o2) -> {
        String str1 = formatter.getCurrencyPair(o1.getOffer().getCurrencyCode());
        String str2 = formatter.getCurrencyPair(o2.getOffer().getCurrencyCode());
        return str1 != null && str2 != null ? str1.compareTo(str2) : 0;
    });
    priceColumn.setComparator((o1, o2) -> {
        Price price1 = o1.getOffer().getPrice();
        Price price2 = o2.getOffer().getPrice();
        return price1 != null && price2 != null ? price1.compareTo(price2) : 0;
    });
    amountColumn.setComparator(Comparator.comparing(o -> o.getOffer().getAmount()));
    volumeColumn.setComparator((o1, o2) -> {
        Volume offerVolume1 = o1.getOffer().getVolume();
        Volume offerVolume2 = o2.getOffer().getVolume();
        return offerVolume1 != null && offerVolume2 != null ? offerVolume1.compareTo(offerVolume2) : 0;
    });
    paymentMethodColumn.setComparator(Comparator.comparing(o -> o.getOffer().getPaymentMethod()));
    avatarColumn.setComparator(Comparator.comparing(o -> o.getOffer().getOwnerNodeAddress().getFullAddress()));
    nrOfOffersLabel = new AutoTooltipLabel("");
    nrOfOffersLabel.setId("num-offers");
    GridPane.setHalignment(nrOfOffersLabel, HPos.LEFT);
    GridPane.setVgrow(nrOfOffersLabel, Priority.NEVER);
    GridPane.setValignment(nrOfOffersLabel, VPos.TOP);
    GridPane.setRowIndex(nrOfOffersLabel, ++gridRow);
    GridPane.setColumnIndex(nrOfOffersLabel, 0);
    GridPane.setMargin(nrOfOffersLabel, new Insets(10, 0, 0, -5));
    root.getChildren().add(nrOfOffersLabel);
    createOfferButton = addButton(root, gridRow, "");
    createOfferButton.setMinHeight(40);
    createOfferButton.setPadding(new Insets(0, 20, 0, 20));
    createOfferButton.setGraphicTextGap(10);
    GridPane.setMargin(createOfferButton, new Insets(15, 0, 0, 0));
    GridPane.setHalignment(createOfferButton, HPos.RIGHT);
    GridPane.setVgrow(createOfferButton, Priority.NEVER);
    GridPane.setValignment(createOfferButton, VPos.TOP);
    offerListListener = c -> nrOfOffersLabel.setText(Res.get("offerbook.nrOffers", model.getOfferList().size()));
    // Fixes incorrect ordering of Available offers:
    // https://github.com/bisq-network/exchange/issues/588
    priceFeedUpdateCounterListener = (observable, oldValue, newValue) -> tableView.sort();
}
Also used : Button(javafx.scene.control.Button) HPos(javafx.geometry.HPos) HyperlinkWithIcon(bisq.desktop.components.HyperlinkWithIcon) ArbitratorSelectionView(bisq.desktop.main.account.content.arbitratorselection.ArbitratorSelectionView) Coin(org.bitcoinj.core.Coin) Layout(bisq.desktop.util.Layout) FiatAccountsView(bisq.desktop.main.account.content.fiataccounts.FiatAccountsView) BSFormatter(bisq.desktop.util.BSFormatter) FormBuilder.getIcon(bisq.desktop.util.FormBuilder.getIcon) ComboBox(javafx.scene.control.ComboBox) ReadOnlyObjectWrapper(javafx.beans.property.ReadOnlyObjectWrapper) ListChangeListener(javafx.collections.ListChangeListener) Res(bisq.core.locale.Res) MonadicBinding(org.fxmisc.easybind.monadic.MonadicBinding) TableView(javafx.scene.control.TableView) Navigation(bisq.desktop.Navigation) HBox(javafx.scene.layout.HBox) Popup(bisq.desktop.main.overlays.popups.Popup) Offer(bisq.core.offer.Offer) AutoTooltipTableColumn(bisq.desktop.components.AutoTooltipTableColumn) AutoTooltipLabel(bisq.desktop.components.AutoTooltipLabel) FormBuilder.addTitledGroupBg(bisq.desktop.util.FormBuilder.addTitledGroupBg) Canvas(javafx.scene.canvas.Canvas) Subscription(org.fxmisc.easybind.Subscription) PaymentMethod(bisq.core.payment.payload.PaymentMethod) Priority(javafx.scene.layout.Priority) PaymentAccount(bisq.core.payment.PaymentAccount) NodeAddress(bisq.network.p2p.NodeAddress) AutoTooltipButton(bisq.desktop.components.AutoTooltipButton) AppOptionKeys(bisq.core.app.AppOptionKeys) AccountSettingsView(bisq.desktop.main.account.settings.AccountSettingsView) Optional(java.util.Optional) MaterialDesignIcon(de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon) NotNull(org.jetbrains.annotations.NotNull) GUIUtil(bisq.desktop.util.GUIUtil) Scene(javafx.scene.Scene) TradeCurrency(bisq.core.locale.TradeCurrency) FormBuilder.addHBoxLabelComboBox(bisq.desktop.util.FormBuilder.addHBoxLabelComboBox) OfferDetailsWindow(bisq.desktop.main.overlays.windows.OfferDetailsWindow) FiatCurrency(bisq.core.locale.FiatCurrency) Volume(bisq.core.monetary.Volume) FxmlView(bisq.desktop.common.view.FxmlView) TableColumn(javafx.scene.control.TableColumn) InfoAutoTooltipLabel(bisq.desktop.components.InfoAutoTooltipLabel) Inject(javax.inject.Inject) TableCell(javafx.scene.control.TableCell) WithdrawalView(bisq.desktop.main.funds.withdrawal.WithdrawalView) OfferPayload(bisq.core.offer.OfferPayload) Tuple3(bisq.common.util.Tuple3) Insets(javafx.geometry.Insets) VPos(javafx.geometry.VPos) ColoredDecimalPlacesWithZerosText(bisq.desktop.components.ColoredDecimalPlacesWithZerosText) Callback(javafx.util.Callback) FundsView(bisq.desktop.main.funds.FundsView) Tooltip(javafx.scene.control.Tooltip) AccountView(bisq.desktop.main.account.AccountView) PrivateNotificationManager(bisq.core.alert.PrivateNotificationManager) GridPane(javafx.scene.layout.GridPane) Label(javafx.scene.control.Label) TableRow(javafx.scene.control.TableRow) DontShowAgainLookup(bisq.core.user.DontShowAgainLookup) OfferView(bisq.desktop.main.offer.OfferView) StringConverter(javafx.util.StringConverter) PeerInfoIcon(bisq.desktop.components.PeerInfoIcon) FormBuilder.addButton(bisq.desktop.util.FormBuilder.addButton) MainView(bisq.desktop.main.MainView) EasyBind(org.fxmisc.easybind.EasyBind) ImageView(javafx.scene.image.ImageView) Price(bisq.core.monetary.Price) ObservableValue(javafx.beans.value.ObservableValue) Named(com.google.inject.name.Named) ActivatableViewAndModel(bisq.desktop.common.view.ActivatableViewAndModel) ChangeListener(javafx.beans.value.ChangeListener) Comparator(java.util.Comparator) ContentDisplay(javafx.scene.control.ContentDisplay) HBox(javafx.scene.layout.HBox) Insets(javafx.geometry.Insets) ComboBox(javafx.scene.control.ComboBox) FormBuilder.addHBoxLabelComboBox(bisq.desktop.util.FormBuilder.addHBoxLabelComboBox) AutoTooltipLabel(bisq.desktop.components.AutoTooltipLabel) InfoAutoTooltipLabel(bisq.desktop.components.InfoAutoTooltipLabel) Label(javafx.scene.control.Label) Price(bisq.core.monetary.Price) Volume(bisq.core.monetary.Volume) PaymentMethod(bisq.core.payment.payload.PaymentMethod) AutoTooltipLabel(bisq.desktop.components.AutoTooltipLabel) InfoAutoTooltipLabel(bisq.desktop.components.InfoAutoTooltipLabel)

Example 3 with Price

use of bisq.core.monetary.Price in project bisq-desktop by bisq-network.

the class OfferBookViewModel method getPrice.

String getPrice(OfferBookListItem item) {
    if ((item == null))
        return "";
    final Offer offer = item.getOffer();
    final Price price = offer.getPrice();
    if (price != null) {
        return formatPrice(offer, true) + formatMarketPriceMargin(offer, true);
    } else {
        return Res.get("shared.na");
    }
}
Also used : Offer(bisq.core.offer.Offer) Price(bisq.core.monetary.Price)

Example 4 with Price

use of bisq.core.monetary.Price in project bisq-desktop by bisq-network.

the class OfferBookChartViewModel method buildChartAndTableEntries.

private void buildChartAndTableEntries(List<Offer> sortedList, OfferPayload.Direction direction, List<XYChart.Data> data, ObservableList<OfferListItem> offerTableList) {
    data.clear();
    double accumulatedAmount = 0;
    List<OfferListItem> offerTableListTemp = new ArrayList<>();
    for (Offer offer : sortedList) {
        Price price = offer.getPrice();
        if (price != null) {
            double amount = (double) offer.getAmount().value / LongMath.pow(10, offer.getAmount().smallestUnitExponent());
            accumulatedAmount += amount;
            offerTableListTemp.add(new OfferListItem(offer, accumulatedAmount));
            double priceAsDouble = (double) price.getValue() / LongMath.pow(10, price.smallestUnitExponent());
            if (CurrencyUtil.isCryptoCurrency(getCurrencyCode())) {
                if (direction.equals(OfferPayload.Direction.SELL))
                    data.add(0, new XYChart.Data<>(priceAsDouble, accumulatedAmount));
                else
                    data.add(new XYChart.Data<>(priceAsDouble, accumulatedAmount));
            } else {
                if (direction.equals(OfferPayload.Direction.BUY))
                    data.add(0, new XYChart.Data<>(priceAsDouble, accumulatedAmount));
                else
                    data.add(new XYChart.Data<>(priceAsDouble, accumulatedAmount));
            }
        }
    }
    offerTableList.setAll(offerTableListTemp);
}
Also used : Offer(bisq.core.offer.Offer) Price(bisq.core.monetary.Price) ArrayList(java.util.ArrayList)

Example 5 with Price

use of bisq.core.monetary.Price in project bisq-desktop by bisq-network.

the class SpreadViewModel method update.

private void update(ObservableList<OfferBookListItem> offerBookListItems) {
    Map<String, List<Offer>> offersByCurrencyMap = new HashMap<>();
    for (OfferBookListItem offerBookListItem : offerBookListItems) {
        Offer offer = offerBookListItem.getOffer();
        String currencyCode = offer.getCurrencyCode();
        if (!offersByCurrencyMap.containsKey(currencyCode))
            offersByCurrencyMap.put(currencyCode, new ArrayList<>());
        offersByCurrencyMap.get(currencyCode).add(offer);
    }
    spreadItems.clear();
    Coin totalAmount = null;
    for (String currencyCode : offersByCurrencyMap.keySet()) {
        List<Offer> offers = offersByCurrencyMap.get(currencyCode);
        final boolean isFiatCurrency = CurrencyUtil.isFiatCurrency(currencyCode);
        List<Offer> buyOffers = offers.stream().filter(e -> e.getDirection().equals(OfferPayload.Direction.BUY)).sorted((o1, o2) -> {
            long a = o1.getPrice() != null ? o1.getPrice().getValue() : 0;
            long b = o2.getPrice() != null ? o2.getPrice().getValue() : 0;
            if (a != b) {
                if (isFiatCurrency) {
                    return a < b ? 1 : -1;
                } else {
                    return a < b ? -1 : 1;
                }
            }
            return 0;
        }).collect(Collectors.toList());
        List<Offer> sellOffers = offers.stream().filter(e -> e.getDirection().equals(OfferPayload.Direction.SELL)).sorted((o1, o2) -> {
            long a = o1.getPrice() != null ? o1.getPrice().getValue() : 0;
            long b = o2.getPrice() != null ? o2.getPrice().getValue() : 0;
            if (a != b) {
                if (isFiatCurrency) {
                    return a > b ? 1 : -1;
                } else {
                    return a > b ? -1 : 1;
                }
            }
            return 0;
        }).collect(Collectors.toList());
        Price spread = null;
        String percentage = "";
        Price bestSellOfferPrice = sellOffers.isEmpty() ? null : sellOffers.get(0).getPrice();
        Price bestBuyOfferPrice = buyOffers.isEmpty() ? null : buyOffers.get(0).getPrice();
        if (bestBuyOfferPrice != null && bestSellOfferPrice != null) {
            MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
            // happens again
            try {
                if (isFiatCurrency)
                    spread = bestSellOfferPrice.subtract(bestBuyOfferPrice);
                else
                    spread = bestBuyOfferPrice.subtract(bestSellOfferPrice);
                if (spread != null && marketPrice != null && marketPrice.isPriceAvailable()) {
                    double marketPriceAsDouble = marketPrice.getPrice();
                    final double precision = isFiatCurrency ? Math.pow(10, Fiat.SMALLEST_UNIT_EXPONENT) : Math.pow(10, Altcoin.SMALLEST_UNIT_EXPONENT);
                    BigDecimal marketPriceAsBigDecimal = BigDecimal.valueOf(marketPriceAsDouble).multiply(BigDecimal.valueOf(precision));
                    // We multiply with 10000 because we use precision of 2 at % (100.00%)
                    double result = BigDecimal.valueOf(spread.getValue()).multiply(BigDecimal.valueOf(10000)).divide(marketPriceAsBigDecimal, RoundingMode.HALF_UP).doubleValue() / 10000;
                    percentage = formatter.formatPercentagePrice(result);
                }
            } catch (Throwable t) {
                try {
                    // Don't translate msg. It is just for rare error cases and can be removed probably later if
                    // that error never gets reported again.
                    String msg = "An error occurred at the spread calculation.\n" + "Error msg: " + t.toString() + "\n" + "Details of offer data: \n" + "bestSellOfferPrice: " + bestSellOfferPrice.getValue() + "\n" + "bestBuyOfferPrice: " + bestBuyOfferPrice.getValue() + "\n" + "sellOffer getCurrencyCode: " + sellOffers.get(0).getCurrencyCode() + "\n" + "buyOffer getCurrencyCode: " + buyOffers.get(0).getCurrencyCode() + "\n\n" + "Please copy and paste this data and send it to the developers so they can investigate the issue.";
                    new Popup<>().error(msg).show();
                    log.error(t.toString());
                    t.printStackTrace();
                } catch (Throwable t2) {
                    log.error(t2.toString());
                    t2.printStackTrace();
                }
            }
        }
        totalAmount = Coin.valueOf(offers.stream().mapToLong(offer -> offer.getAmount().getValue()).sum());
        spreadItems.add(new SpreadItem(currencyCode, buyOffers.size(), sellOffers.size(), offers.size(), spread, percentage, totalAmount));
    }
    maxPlacesForAmount.set(formatAmount(totalAmount, false).length());
}
Also used : Altcoin(bisq.core.monetary.Altcoin) GUIUtil(bisq.desktop.util.GUIUtil) Coin(org.bitcoinj.core.Coin) Inject(com.google.inject.Inject) FXCollections(javafx.collections.FXCollections) HashMap(java.util.HashMap) IntegerProperty(javafx.beans.property.IntegerProperty) BSFormatter(bisq.desktop.util.BSFormatter) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) OfferPayload(bisq.core.offer.OfferPayload) ListChangeListener(javafx.collections.ListChangeListener) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) Map(java.util.Map) CurrencyUtil(bisq.core.locale.CurrencyUtil) RoundingMode(java.math.RoundingMode) Popup(bisq.desktop.main.overlays.popups.Popup) Offer(bisq.core.offer.Offer) OfferBook(bisq.desktop.main.offer.offerbook.OfferBook) Fiat(org.bitcoinj.utils.Fiat) OfferBookListItem(bisq.desktop.main.offer.offerbook.OfferBookListItem) Collectors(java.util.stream.Collectors) ActivatableViewModel(bisq.desktop.common.model.ActivatableViewModel) List(java.util.List) PriceFeedService(bisq.core.provider.price.PriceFeedService) Price(bisq.core.monetary.Price) ObservableList(javafx.collections.ObservableList) MarketPrice(bisq.core.provider.price.MarketPrice) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Coin(org.bitcoinj.core.Coin) OfferBookListItem(bisq.desktop.main.offer.offerbook.OfferBookListItem) MarketPrice(bisq.core.provider.price.MarketPrice) Offer(bisq.core.offer.Offer) Price(bisq.core.monetary.Price) MarketPrice(bisq.core.provider.price.MarketPrice) ArrayList(java.util.ArrayList) List(java.util.List) ObservableList(javafx.collections.ObservableList)

Aggregations

Price (bisq.core.monetary.Price)10 Offer (bisq.core.offer.Offer)7 BSFormatter (bisq.desktop.util.BSFormatter)4 Res (bisq.core.locale.Res)3 Volume (bisq.core.monetary.Volume)3 OfferPayload (bisq.core.offer.OfferPayload)3 AutoTooltipLabel (bisq.desktop.components.AutoTooltipLabel)3 Popup (bisq.desktop.main.overlays.popups.Popup)3 GUIUtil (bisq.desktop.util.GUIUtil)3 Coin (org.bitcoinj.core.Coin)3 Tuple3 (bisq.common.util.Tuple3)2 PrivateNotificationManager (bisq.core.alert.PrivateNotificationManager)2 AppOptionKeys (bisq.core.app.AppOptionKeys)2 OpenOffer (bisq.core.offer.OpenOffer)2 PaymentAccount (bisq.core.payment.PaymentAccount)2 PaymentMethod (bisq.core.payment.payload.PaymentMethod)2 MarketPrice (bisq.core.provider.price.MarketPrice)2 Navigation (bisq.desktop.Navigation)2 ActivatableViewAndModel (bisq.desktop.common.view.ActivatableViewAndModel)2 FxmlView (bisq.desktop.common.view.FxmlView)2