use of bisq.desktop.main.offer.offerbook.OfferBookListItem in project bisq-desktop by bisq-network.
the class SpreadViewModelTest method testMaxCharactersForAmount.
@Test
public void testMaxCharactersForAmount() {
OfferBook offerBook = mock(OfferBook.class);
final ObservableList<OfferBookListItem> offerBookListItems = FXCollections.observableArrayList();
offerBookListItems.addAll(make(OfferBookListItemMaker.btcItem));
when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems);
final SpreadViewModel model = new SpreadViewModel(offerBook, null, new BSFormatter());
model.activate();
// 0.001
assertEquals(6, model.maxPlacesForAmount.intValue());
offerBookListItems.addAll(make(btcItem.but(with(OfferBookListItemMaker.amount, 1403000000L))));
// 14.0300
assertEquals(7, model.maxPlacesForAmount.intValue());
}
use of bisq.desktop.main.offer.offerbook.OfferBookListItem 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);
}
use of bisq.desktop.main.offer.offerbook.OfferBookListItem 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());
}
use of bisq.desktop.main.offer.offerbook.OfferBookListItem in project bisq-desktop by bisq-network.
the class OfferBookChartViewModelTest method testMaxCharactersForBuyVolumeWithNoOffers.
@Test
public void testMaxCharactersForBuyVolumeWithNoOffers() {
OfferBook offerBook = mock(OfferBook.class);
final ObservableList<OfferBookListItem> offerBookListItems = FXCollections.observableArrayList();
when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems);
final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, new BSFormatter());
assertEquals(0, model.maxPlacesForBuyVolume.intValue());
}
use of bisq.desktop.main.offer.offerbook.OfferBookListItem in project bisq-desktop by bisq-network.
the class OfferBookChartViewModelTest method testMaxCharactersForSellPriceWithOfflinePriceFeedService.
@Test
public void testMaxCharactersForSellPriceWithOfflinePriceFeedService() {
OfferBook offerBook = mock(OfferBook.class);
PriceFeedService priceFeedService = mock(PriceFeedService.class);
final ObservableList<OfferBookListItem> offerBookListItems = FXCollections.observableArrayList();
final OfferBookListItem item = make(OfferBookListItemMaker.btcSellItem.but(with(OfferBookListItemMaker.useMarketBasedPrice, true)));
item.getOffer().setPriceFeedService(priceFeedService);
offerBookListItems.addAll(item);
when(priceFeedService.getMarketPrice(anyString())).thenReturn(null);
when(priceFeedService.updateCounterProperty()).thenReturn(new SimpleIntegerProperty());
when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems);
final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, priceFeedService, null, new BSFormatter());
model.activate();
assertEquals(0, model.maxPlacesForSellPrice.intValue());
}
Aggregations