Search in sources :

Example 26 with Offer

use of bisq.core.offer.Offer in project bisq-desktop by bisq-network.

the class OfferBookChartViewModel method updateChartData.

private void updateChartData() {
    List<Offer> allBuyOffers = offerBookListItems.stream().map(OfferBookListItem::getOffer).filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && 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 (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode()))
                return a > b ? 1 : -1;
            else
                return a < b ? 1 : -1;
        } else {
            return 0;
        }
    }).collect(Collectors.toList());
    allBuyOffers = filterOffersWithRelevantPrices(allBuyOffers);
    final Optional<Offer> highestBuyPriceOffer = allBuyOffers.stream().filter(o -> o.getPrice() != null).max(Comparator.comparingLong(o -> o.getPrice().getValue()));
    if (highestBuyPriceOffer.isPresent()) {
        final Offer offer = highestBuyPriceOffer.get();
        maxPlacesForBuyPrice.set(formatPrice(offer, false).length());
    }
    final Optional<Offer> highestBuyVolumeOffer = allBuyOffers.stream().filter(o -> o.getVolume() != null).max(Comparator.comparingLong(o -> o.getVolume().getValue()));
    if (highestBuyVolumeOffer.isPresent()) {
        final Offer offer = highestBuyVolumeOffer.get();
        maxPlacesForBuyVolume.set(formatVolume(offer, false).length());
    }
    buildChartAndTableEntries(allBuyOffers, OfferPayload.Direction.BUY, buyData, topBuyOfferList);
    List<Offer> allSellOffers = offerBookListItems.stream().map(OfferBookListItem::getOffer).filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && 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 (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode()))
                return a < b ? 1 : -1;
            else
                return a > b ? 1 : -1;
        } else {
            return 0;
        }
    }).collect(Collectors.toList());
    allSellOffers = filterOffersWithRelevantPrices(allSellOffers);
    final Optional<Offer> highestSellPriceOffer = allSellOffers.stream().filter(o -> o.getPrice() != null).max(Comparator.comparingLong(o -> o.getPrice().getValue()));
    if (highestSellPriceOffer.isPresent()) {
        final Offer offer = highestSellPriceOffer.get();
        maxPlacesForSellPrice.set(formatPrice(offer, false).length());
    }
    final Optional<Offer> highestSellVolumeOffer = allSellOffers.stream().filter(o -> o.getVolume() != null).max(Comparator.comparingLong(o -> o.getVolume().getValue()));
    if (highestSellVolumeOffer.isPresent()) {
        final Offer offer = highestSellVolumeOffer.get();
        maxPlacesForSellVolume.set(formatVolume(offer, false).length());
    }
    buildChartAndTableEntries(allSellOffers, OfferPayload.Direction.SELL, sellData, topSellOfferList);
}
Also used : GUIUtil(bisq.desktop.util.GUIUtil) TradeCurrency(bisq.core.locale.TradeCurrency) CurrencyListItem(bisq.desktop.util.CurrencyListItem) CurrencyList(bisq.desktop.util.CurrencyList) Inject(com.google.inject.Inject) FXCollections(javafx.collections.FXCollections) XYChart(javafx.scene.chart.XYChart) IntegerProperty(javafx.beans.property.IntegerProperty) BSFormatter(bisq.desktop.util.BSFormatter) ArrayList(java.util.ArrayList) OfferPayload(bisq.core.offer.OfferPayload) ListChangeListener(javafx.collections.ListChangeListener) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) CurrencyUtil(bisq.core.locale.CurrencyUtil) Navigation(bisq.desktop.Navigation) Offer(bisq.core.offer.Offer) ObjectProperty(javafx.beans.property.ObjectProperty) OfferBook(bisq.desktop.main.offer.offerbook.OfferBook) GlobalSettings(bisq.core.locale.GlobalSettings) LongMath(com.google.common.math.LongMath) OfferBookListItem(bisq.desktop.main.offer.offerbook.OfferBookListItem) Collectors(java.util.stream.Collectors) ActivatableViewModel(bisq.desktop.common.model.ActivatableViewModel) MainView(bisq.desktop.main.MainView) List(java.util.List) PriceFeedService(bisq.core.provider.price.PriceFeedService) PreferencesView(bisq.desktop.main.settings.preferences.PreferencesView) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) Preferences(bisq.core.user.Preferences) Price(bisq.core.monetary.Price) ObservableValue(javafx.beans.value.ObservableValue) Optional(java.util.Optional) ObservableList(javafx.collections.ObservableList) ChangeListener(javafx.beans.value.ChangeListener) Comparator(java.util.Comparator) SettingsView(bisq.desktop.main.settings.SettingsView) Offer(bisq.core.offer.Offer)

Example 27 with Offer

use of bisq.core.offer.Offer 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 28 with Offer

use of bisq.core.offer.Offer 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)

Example 29 with Offer

use of bisq.core.offer.Offer in project bisq-desktop by bisq-network.

the class OfferBookView method getAvatarColumn.

private TableColumn<OfferBookListItem, OfferBookListItem> getAvatarColumn() {
    TableColumn<OfferBookListItem, OfferBookListItem> column = new AutoTooltipTableColumn<OfferBookListItem, OfferBookListItem>(Res.get("offerbook.trader")) {

        {
            setMinWidth(80);
            setMaxWidth(80);
            setSortable(true);
        }
    };
    column.setCellValueFactory((offer) -> new ReadOnlyObjectWrapper<>(offer.getValue()));
    column.setCellFactory(new Callback<TableColumn<OfferBookListItem, OfferBookListItem>, TableCell<OfferBookListItem, OfferBookListItem>>() {

        @Override
        public TableCell<OfferBookListItem, OfferBookListItem> call(TableColumn<OfferBookListItem, OfferBookListItem> column) {
            return new TableCell<OfferBookListItem, OfferBookListItem>() {

                @Override
                public void updateItem(final OfferBookListItem newItem, boolean empty) {
                    super.updateItem(newItem, empty);
                    if (newItem != null && !empty) {
                        final Offer offer = newItem.getOffer();
                        final NodeAddress makersNodeAddress = offer.getOwnerNodeAddress();
                        String role = Res.get("peerInfoIcon.tooltip.maker");
                        int numTrades = model.getNumTrades(offer);
                        PeerInfoIcon peerInfoIcon = new PeerInfoIcon(makersNodeAddress, role, numTrades, privateNotificationManager, offer, model.preferences, model.accountAgeWitnessService, formatter, useDevPrivilegeKeys);
                        setGraphic(peerInfoIcon);
                    } else {
                        setGraphic(null);
                    }
                }
            };
        }
    });
    return column;
}
Also used : PeerInfoIcon(bisq.desktop.components.PeerInfoIcon) AutoTooltipTableColumn(bisq.desktop.components.AutoTooltipTableColumn) TableColumn(javafx.scene.control.TableColumn) TableCell(javafx.scene.control.TableCell) Offer(bisq.core.offer.Offer) AutoTooltipTableColumn(bisq.desktop.components.AutoTooltipTableColumn) NodeAddress(bisq.network.p2p.NodeAddress)

Example 30 with Offer

use of bisq.core.offer.Offer in project bisq-desktop by bisq-network.

the class OfferBookViewModel method applyFilterPredicate.

// /////////////////////////////////////////////////////////////////////////////////////////
// Filters
// /////////////////////////////////////////////////////////////////////////////////////////
private void applyFilterPredicate() {
    filteredItems.setPredicate(offerBookListItem -> {
        Offer offer = offerBookListItem.getOffer();
        boolean directionResult = offer.getDirection() != direction;
        boolean currencyResult = (showAllTradeCurrenciesProperty.get()) || offer.getCurrencyCode().equals(selectedTradeCurrency.getCode());
        boolean paymentMethodResult = showAllPaymentMethods || offer.getPaymentMethod().equals(selectedPaymentMethod);
        boolean notMyOfferOrShowMyOffersActivated = !isMyOffer(offerBookListItem.getOffer()) || preferences.isShowOwnOffersInOfferBook();
        return directionResult && currencyResult && paymentMethodResult && notMyOfferOrShowMyOffersActivated;
    });
}
Also used : Offer(bisq.core.offer.Offer)

Aggregations

Offer (bisq.core.offer.Offer)49 Coin (org.bitcoinj.core.Coin)15 Test (org.junit.Test)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 OfferPayload (bisq.core.offer.OfferPayload)10 Price (bisq.core.monetary.Price)9 OfferMaker.btcUsdOffer (bisq.core.offer.OfferMaker.btcUsdOffer)8 PaymentMethod (bisq.core.payment.payload.PaymentMethod)7 Contract (bisq.core.trade.Contract)7 NodeAddress (bisq.network.p2p.NodeAddress)7 Volume (bisq.core.monetary.Volume)6 PeerInfoIcon (bisq.desktop.components.PeerInfoIcon)6 Popup (bisq.desktop.main.overlays.popups.Popup)6 MainView (bisq.desktop.main.MainView)5 Button (javafx.scene.control.Button)5 TableCell (javafx.scene.control.TableCell)5 TableColumn (javafx.scene.control.TableColumn)5 TradeCurrency (bisq.core.locale.TradeCurrency)4 AutoTooltipButton (bisq.desktop.components.AutoTooltipButton)4 AutoTooltipLabel (bisq.desktop.components.AutoTooltipLabel)4