From 9fda20f88b0cb4aedfeeb44114083fdf05c64714 Mon Sep 17 00:00:00 2001 From: woodser Date: Mon, 12 Dec 2022 14:18:08 +0000 Subject: [PATCH] update to new xmr price server api remove timestamp data structure --- .../java/bisq/core/locale/CurrencyUtil.java | 1 - .../core/provider/ProvidersRepository.java | 2 +- .../core/provider/price/PriceFeedService.java | 9 ++-- .../core/provider/price/PriceProvider.java | 54 ++++--------------- .../core/provider/price/PriceRequest.java | 8 +-- .../i18n/displayStrings_de.properties | 2 +- .../main/java/bisq/desktop/main/MainView.java | 4 +- 7 files changed, 21 insertions(+), 59 deletions(-) diff --git a/core/src/main/java/bisq/core/locale/CurrencyUtil.java b/core/src/main/java/bisq/core/locale/CurrencyUtil.java index c7e47bf58c..625c849299 100644 --- a/core/src/main/java/bisq/core/locale/CurrencyUtil.java +++ b/core/src/main/java/bisq/core/locale/CurrencyUtil.java @@ -24,7 +24,6 @@ import bisq.asset.AssetRegistry; import bisq.asset.Coin; import bisq.asset.Token; -import bisq.common.app.DevEnv; import bisq.common.config.BaseCurrencyNetwork; import bisq.common.config.Config; diff --git a/core/src/main/java/bisq/core/provider/ProvidersRepository.java b/core/src/main/java/bisq/core/provider/ProvidersRepository.java index 7d2bcb49eb..4359fe55b4 100644 --- a/core/src/main/java/bisq/core/provider/ProvidersRepository.java +++ b/core/src/main/java/bisq/core/provider/ProvidersRepository.java @@ -104,7 +104,7 @@ public class ProvidersRepository { if (useLocalhostForP2P) { // If we run in localhost mode we don't have the tor node running, so we need a clearnet host // Use localhost for using a locally running provider - // providerAsString = Collections.singletonList("http://localhost:8080/"); + // providers = Collections.singletonList("http://localhost:8078"); providers = Collections.singletonList("https://price.haveno.network/"); // Haveno } else { providers = DEFAULT_NODES; diff --git a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java index 0f006458a0..88fb1674cc 100644 --- a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java +++ b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java @@ -338,8 +338,7 @@ public class PriceFeedService { */ public Map requestAllPrices() throws ExecutionException, InterruptedException, TimeoutException, CancellationException { return new PriceRequest().requestAllPrices(priceProvider) - .get(20, TimeUnit.SECONDS) - .second; + .get(20, TimeUnit.SECONDS); } /////////////////////////////////////////////////////////////////////////////////////////// @@ -406,10 +405,10 @@ public class PriceFeedService { } priceRequest = new PriceRequest(); - SettableFuture, Map>> future = priceRequest.requestAllPrices(provider); + SettableFuture> future = priceRequest.requestAllPrices(provider); Futures.addCallback(future, new FutureCallback<>() { @Override - public void onSuccess(@Nullable Tuple2, Map> result) { + public void onSuccess(@Nullable Map result) { UserThread.execute(() -> { checkNotNull(result, "Result must not be null at requestAllPrices"); // Each currency rate has a different timestamp, depending on when @@ -417,7 +416,7 @@ public class PriceFeedService { // However, the request timestamp is when the pricenode was queried epochInMillisAtLastRequest = System.currentTimeMillis(); - Map priceMap = result.second; + Map priceMap = result; cache.putAll(priceMap); diff --git a/core/src/main/java/bisq/core/provider/price/PriceProvider.java b/core/src/main/java/bisq/core/provider/price/PriceProvider.java index 552b5daa45..5a05767319 100644 --- a/core/src/main/java/bisq/core/provider/price/PriceProvider.java +++ b/core/src/main/java/bisq/core/provider/price/PriceProvider.java @@ -49,9 +49,9 @@ public class PriceProvider extends HttpClientProvider { super(httpClient, baseUrl, false); } - public synchronized Tuple2, Map> getAll() throws IOException { + public synchronized Map getAll() throws IOException { if (shutDownRequested) { - return new Tuple2<>(new HashMap<>(), new HashMap<>()); + return new HashMap<>(); } Map marketPriceMap = new HashMap<>(); @@ -59,64 +59,28 @@ public class PriceProvider extends HttpClientProvider { if (P2PService.getMyNodeAddress() != null) hsVersion = P2PService.getMyNodeAddress().getHostName().length() > 22 ? ", HSv3" : ", HSv2"; - String json = httpClient.get("getAllMarketPrices", "User-Agent", "bisq/" + String json = httpClient.get("getAllMarketPrices", "User-Agent", "haveno/" + Version.VERSION + hsVersion); - LinkedTreeMap map = new Gson().fromJson(json, LinkedTreeMap.class); - Map tsMap = new HashMap<>(); - transfer("btcAverageTs", map, tsMap); - transfer("poloniexTs", map, tsMap); - transfer("coinmarketcapTs", map, tsMap); - // get btc per xmr price to convert all prices to xmr - // TODO (woodser): currently using bisq price feed, switch? List list = (ArrayList) map.get("data"); - double btcPerXmr = findBtcPerXmr(list); - for (Object obj : list) { + list.forEach(obj -> { try { LinkedTreeMap treeMap = (LinkedTreeMap) obj; - String currencyCode = (String) treeMap.get("currencyCode"); + String baseCurrencyCode = (String) treeMap.get("baseCurrencyCode"); + String counterCurrencyCode = (String) treeMap.get("counterCurrencyCode"); + String currencyCode = baseCurrencyCode.equals("XMR") ? counterCurrencyCode : baseCurrencyCode; double price = (Double) treeMap.get("price"); // json uses double for our timestampSec long value... long timestampSec = MathUtils.doubleToLong((Double) treeMap.get("timestampSec")); - - // convert price from btc to xmr - boolean isFiat = CurrencyUtil.isFiatCurrency(currencyCode); - if (isFiat) price = price * btcPerXmr; - else price = price / btcPerXmr; - - // add currency price to map marketPriceMap.put(currencyCode, new MarketPrice(currencyCode, price, timestampSec, true)); } catch (Throwable t) { log.error(t.toString()); t.printStackTrace(); } - } - // add btc to price map, remove xmr since base currency - marketPriceMap.put("BTC", new MarketPrice("BTC", 1 / btcPerXmr, marketPriceMap.get("XMR").getTimestampSec(), true)); - marketPriceMap.remove("XMR"); - return new Tuple2<>(tsMap, marketPriceMap); - } - - private void transfer(String key, LinkedTreeMap map, Map tsMap) { - if (map.containsKey(key)) tsMap.put(key, ((Double) map.get(key)).longValue()); - else log.warn("No prices returned from provider " + key); - } - - - /** - * @return price of 1 XMR in BTC - */ - private static double findBtcPerXmr(List list) { - for (Object obj : list) { - LinkedTreeMap treeMap = (LinkedTreeMap) obj; - String currencyCode = (String) treeMap.get("currencyCode"); - if ("XMR".equalsIgnoreCase(currencyCode)) { - return (double) treeMap.get("price"); - } - } - throw new IllegalStateException("BTC per XMR price not found"); + }); + return marketPriceMap; } public String getBaseUrl() { diff --git a/core/src/main/java/bisq/core/provider/price/PriceRequest.java b/core/src/main/java/bisq/core/provider/price/PriceRequest.java index 3144ec74f5..a4f236d6bd 100644 --- a/core/src/main/java/bisq/core/provider/price/PriceRequest.java +++ b/core/src/main/java/bisq/core/provider/price/PriceRequest.java @@ -45,17 +45,17 @@ public class PriceRequest { public PriceRequest() { } - public SettableFuture, Map>> requestAllPrices(PriceProvider provider) { + public SettableFuture> requestAllPrices(PriceProvider provider) { this.provider = provider; String baseUrl = provider.getBaseUrl(); - SettableFuture, Map>> resultFuture = SettableFuture.create(); - ListenableFuture, Map>> future = executorService.submit(() -> { + SettableFuture> resultFuture = SettableFuture.create(); + ListenableFuture> future = executorService.submit(() -> { Thread.currentThread().setName("PriceRequest @ " + baseUrl); return provider.getAll(); }); Futures.addCallback(future, new FutureCallback<>() { - public void onSuccess(Tuple2, Map> marketPriceTuple) { + public void onSuccess(Map marketPriceTuple) { log.trace("Received marketPriceTuple of {}\nfrom provider {}", marketPriceTuple, provider); if (!shutDownRequested) { resultFuture.set(marketPriceTuple); diff --git a/core/src/main/resources/i18n/displayStrings_de.properties b/core/src/main/resources/i18n/displayStrings_de.properties index dc7b9a521e..cd7c832308 100644 --- a/core/src/main/resources/i18n/displayStrings_de.properties +++ b/core/src/main/resources/i18n/displayStrings_de.properties @@ -1082,7 +1082,7 @@ setting.about.def=Bisq ist keine Firma, sondern ein Gemeinschaftsprojekt, das of setting.about.contribute=Mitwirken setting.about.providers=Datenanbieter setting.about.apisWithFee=Bisq verwendet die Bisq Preis Indizes für Fiat und Altcoin Preise und die Bisq Mempool Nodes für die Abschätzung der Mining-Gebühr. -setting.about.apis=Bisq verwendet den Bisq Price Index für Fiat und Altcoin Preise. +setting.about.apis=Bisq verwendet den Haveno Price Index für Fiat und Altcoin Preise. setting.about.pricesProvided=Marktpreise zur Verfügung gestellt von setting.about.feeEstimation.label=Geschätzte Mining-Gebühr bereitgestellt von setting.about.versionDetails=Versionsdetails diff --git a/desktop/src/main/java/bisq/desktop/main/MainView.java b/desktop/src/main/java/bisq/desktop/main/MainView.java index 0ae4f458a2..05e74ab0e7 100644 --- a/desktop/src/main/java/bisq/desktop/main/MainView.java +++ b/desktop/src/main/java/bisq/desktop/main/MainView.java @@ -489,7 +489,7 @@ public class MainView extends InitializableView { private void updateMarketPriceLabel(Label label) { if (model.getIsPriceAvailable().get()) { if (model.getIsExternallyProvidedPrice().get()) { - label.setText(Res.get("mainView.marketPriceWithProvider.label", "Bisq Price Index")); + label.setText(Res.get("mainView.marketPriceWithProvider.label", "Haveno Price Index")); label.setTooltip(new Tooltip(getPriceProviderTooltipString())); } else { label.setText(Res.get("mainView.marketPrice.bisqInternalPrice")); @@ -509,7 +509,7 @@ public class MainView extends InitializableView { MarketPrice selectedMarketPrice = model.getPriceFeedService().getMarketPrice(selectedCurrencyCode); return Res.get("mainView.marketPrice.tooltip", - "Bisq Price Index for " + selectedCurrencyCode, + "Haveno Price Index for " + selectedCurrencyCode, "", selectedMarketPrice != null ? DisplayUtils.formatTime(new Date(selectedMarketPrice.getTimestampSec())) : Res.get("shared.na"), model.getPriceFeedService().getProviderNodeAddress());