From eb634ac1bf47b3cbe1df630be6157c443d693650 Mon Sep 17 00:00:00 2001 From: jmacxx <47253594+jmacxx@users.noreply.github.com> Date: Tue, 22 Aug 2023 17:52:47 -0500 Subject: [PATCH 1/5] Adapt to new Poloniex API. Also clean up unused dependencies. Applied code review changes. --- gradle/libs.versions.toml | 19 ++----- .../bisq/price/spot/providers/Poloniex.java | 55 ++++++++++++++++--- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 824b0d0..b2ffe51 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -20,26 +20,18 @@ spring-boot-starter-web-lib = { strictly = '2.5.6' } [libraries] junit-jupiter = { module = 'org.junit.jupiter:junit-jupiter', version.ref = 'junit-jupiter-lib' } knowm-xchange-binance = { module = 'org.knowm.xchange:xchange-binance', version.ref = 'knowm-xchange-lib' } -knowm-xchange-bitbay = { module = 'org.knowm.xchange:xchange-bitbay', version.ref = 'knowm-xchange-lib' } known-xchange-bitfinex = { module = 'org.knowm.xchange:xchange-bitfinex', version.ref = 'knowm-xchange-lib' } known-xchange-bitflyer = { module = 'org.knowm.xchange:xchange-bitflyer', version.ref = 'knowm-xchange-lib' } known-xchange-bitstamp = { module = 'org.knowm.xchange:xchange-bitstamp', version.ref = 'knowm-xchange-lib' } known-xchange-btcmarkets = { module = 'org.knowm.xchange:xchange-btcmarkets', version.ref = 'knowm-xchange-lib' } -known-xchange-cexio = { module = 'org.knowm.xchange:xchange-cexio', version.ref = 'knowm-xchange-lib' } known-xchange-coinbasepro = { module = 'org.knowm.xchange:xchange-coinbasepro', version.ref = 'knowm-xchange-lib' } known-xchange-coinmarketcap = { module = 'org.knowm.xchange:xchange-coinmarketcap', version.ref = 'knowm-xchange-lib' } -known-xchange-coinmate = { module = 'org.knowm.xchange:xchange-coinmate', version.ref = 'knowm-xchange-lib' } known-xchange-coinone = { module = 'org.knowm.xchange:xchange-coinone', version.ref = 'knowm-xchange-lib' } -known-xchange-exmo = { module = 'org.knowm.xchange:xchange-exmo', version.ref = 'knowm-xchange-lib' } -known-xchange-hitbtc = { module = 'org.knowm.xchange:xchange-hitbtc', version.ref = 'knowm-xchange-lib' } -known-xchange-huobi = { module = 'org.knowm.xchange:xchange-huobi', version.ref = 'knowm-xchange-lib' } known-xchange-independentreserve = { module = 'org.knowm.xchange:xchange-independentreserve', version.ref = 'knowm-xchange-lib' } known-xchange-kraken = { module = 'org.knowm.xchange:xchange-kraken', version.ref = 'knowm-xchange-lib' } known-xchange-luno = { module = 'org.knowm.xchange:xchange-luno', version.ref = 'knowm-xchange-lib' } known-xchange-mercadobitcoin = { module = 'org.knowm.xchange:xchange-mercadobitcoin', version.ref = 'knowm-xchange-lib' } known-xchange-paribu = { module = 'org.knowm.xchange:xchange-paribu', version.ref = 'knowm-xchange-lib' } -known-xchange-poloniex = { module = 'org.knowm.xchange:xchange-poloniex', version.ref = 'knowm-xchange-lib' } -known-xchange-quoine = { module = 'org.knowm.xchange:xchange-quoine', version.ref = 'knowm-xchange-lib' } logback-core = { module = 'ch.qos.logback:logback-core', version.ref = 'logback-lib' } logback-classic = { module = 'ch.qos.logback:logback-classic', version.ref = 'logback-lib' } lombok = { module = 'org.projectlombok:lombok', version.ref = 'lombok-lib' } @@ -51,12 +43,11 @@ spring-boot-starter-webflux = { module = 'org.springframework.boot:spring-boot-s [bundles] knowm-xchange-libs = [ - 'knowm-xchange-binance', 'knowm-xchange-bitbay', 'known-xchange-bitfinex', - 'known-xchange-bitflyer', 'known-xchange-bitstamp', 'known-xchange-btcmarkets', 'known-xchange-cexio', - 'known-xchange-coinbasepro', 'known-xchange-coinmarketcap', 'known-xchange-coinmate', 'known-xchange-coinone', - 'known-xchange-exmo', 'known-xchange-hitbtc', 'known-xchange-huobi', 'known-xchange-independentreserve', - 'known-xchange-kraken', 'known-xchange-luno', 'known-xchange-mercadobitcoin', 'known-xchange-paribu', - 'known-xchange-poloniex', 'known-xchange-quoine' + 'knowm-xchange-binance', 'known-xchange-bitfinex', + 'known-xchange-bitflyer', 'known-xchange-bitstamp', 'known-xchange-btcmarkets', + 'known-xchange-coinbasepro', 'known-xchange-coinmarketcap', 'known-xchange-coinone', + 'known-xchange-independentreserve', + 'known-xchange-kraken', 'known-xchange-luno', 'known-xchange-mercadobitcoin', 'known-xchange-paribu' ] [plugins] diff --git a/src/main/java/bisq/price/spot/providers/Poloniex.java b/src/main/java/bisq/price/spot/providers/Poloniex.java index 6664e7e..866485a 100644 --- a/src/main/java/bisq/price/spot/providers/Poloniex.java +++ b/src/main/java/bisq/price/spot/providers/Poloniex.java @@ -20,26 +20,67 @@ import bisq.price.spot.ExchangeRate; import bisq.price.spot.ExchangeRateProvider; -import org.knowm.xchange.poloniex.PoloniexExchange; - +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.springframework.core.env.Environment; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; +import java.math.BigDecimal; import java.time.Duration; -import java.util.Set; +import java.util.*; @Component class Poloniex extends ExchangeRateProvider { - + // Supported fiat: - + // Supported alts: DASH, DCR, DOGE, ETC, ETH, LTC, XMR, ZEC + private static final String CURRENCIES = "DASH,DCR,DOGE,ETC,ETH,LTC,XMR,ZEC"; + private static final String POLONIEX_URL = "https://api.poloniex.com/markets/price"; public Poloniex(Environment env) { super(env, "POLO", "poloniex", Duration.ofMinutes(1)); } @Override public Set doGet() { - // Supported fiat: - - // Supported alts: DASH, DCR, DOGE, ETC, ETH, LTC, XMR, ZEC - return doGet(PoloniexExchange.class); + Set requestedCurrencies = new HashSet<>(Arrays.asList(CURRENCIES.split(","))); + Set exchangeRates = new HashSet<>(); + ResponseEntity response = + new RestTemplate().getForEntity( + UriComponentsBuilder + .fromUriString(POLONIEX_URL).build() + .toUri(), + PoloniexTicker[].class); + PoloniexTicker[] tickers = response.getBody(); + Arrays.stream(Objects.requireNonNull(tickers)) + .filter(ticker -> ticker.isMatch(requestedCurrencies)) + .forEach(ticker -> exchangeRates.add( + new ExchangeRate(ticker.getCurrency(), ticker.getPrice(), new Date(), this.getName()))); + return exchangeRates; + } + + @JsonIgnoreProperties(ignoreUnknown = true) + private static class PoloniexTicker { + public String symbol; + public String price; + + public BigDecimal getPrice() { + return new BigDecimal(price); + } + + public String getCurrency() { + // symbol might not be a BTC pair, in which case return empty string + int splitIndex = symbol.indexOf("_BTC"); + if (splitIndex < 0) { + return ""; + } + return symbol.substring(0, splitIndex); + } + + public boolean isMatch(Set requestedCurrencies) { + return symbol.equalsIgnoreCase(getCurrency() + "_BTC") && + requestedCurrencies.contains(getCurrency()); + } } } From 325bcf76cce59c760e8c2ecdbffb89602a9ff160 Mon Sep 17 00:00:00 2001 From: jmacxx <47253594+jmacxx@users.noreply.github.com> Date: Mon, 4 Sep 2023 16:58:46 -0500 Subject: [PATCH 2/5] Poloniex: Migrate to WebClient. --- .../bisq/price/spot/providers/Poloniex.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/bisq/price/spot/providers/Poloniex.java b/src/main/java/bisq/price/spot/providers/Poloniex.java index 866485a..c82a4bf 100644 --- a/src/main/java/bisq/price/spot/providers/Poloniex.java +++ b/src/main/java/bisq/price/spot/providers/Poloniex.java @@ -22,14 +22,14 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.springframework.core.env.Environment; -import org.springframework.http.ResponseEntity; +import org.springframework.http.MediaType; import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponentsBuilder; +import org.springframework.web.reactive.function.client.WebClient; import java.math.BigDecimal; import java.time.Duration; +import java.time.temporal.ChronoUnit; import java.util.*; @Component @@ -46,13 +46,13 @@ public Poloniex(Environment env) { public Set doGet() { Set requestedCurrencies = new HashSet<>(Arrays.asList(CURRENCIES.split(","))); Set exchangeRates = new HashSet<>(); - ResponseEntity response = - new RestTemplate().getForEntity( - UriComponentsBuilder - .fromUriString(POLONIEX_URL).build() - .toUri(), - PoloniexTicker[].class); - PoloniexTicker[] tickers = response.getBody(); + PoloniexTicker[] tickers = + WebClient.create().get() + .uri(POLONIEX_URL) + .accept(MediaType.APPLICATION_JSON) + .retrieve() + .bodyToMono(PoloniexTicker[].class) + .block(Duration.of(30, ChronoUnit.SECONDS)); Arrays.stream(Objects.requireNonNull(tickers)) .filter(ticker -> ticker.isMatch(requestedCurrencies)) .forEach(ticker -> exchangeRates.add( From c958d250ded1a7ee556a7f39ce8e6b1d74ce60fd Mon Sep 17 00:00:00 2001 From: Boiling Date: Tue, 5 Sep 2023 17:51:38 +0200 Subject: [PATCH 3/5] Code Review Suggestions --- .../bisq/price/spot/providers/Poloniex.java | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/main/java/bisq/price/spot/providers/Poloniex.java b/src/main/java/bisq/price/spot/providers/Poloniex.java index c82a4bf..3626e5f 100644 --- a/src/main/java/bisq/price/spot/providers/Poloniex.java +++ b/src/main/java/bisq/price/spot/providers/Poloniex.java @@ -19,45 +19,43 @@ import bisq.price.spot.ExchangeRate; import bisq.price.spot.ExchangeRateProvider; - import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.springframework.core.env.Environment; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Flux; import java.math.BigDecimal; import java.time.Duration; - -import java.time.temporal.ChronoUnit; -import java.util.*; +import java.util.Date; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; @Component class Poloniex extends ExchangeRateProvider { - // Supported fiat: - - // Supported alts: DASH, DCR, DOGE, ETC, ETH, LTC, XMR, ZEC - private static final String CURRENCIES = "DASH,DCR,DOGE,ETC,ETH,LTC,XMR,ZEC"; + private static final List SUPPORTED_CURRENCIES = + List.of("DASH", "DCR", "DOGE", "ETC", "ETH", "LTC", "XMR", "ZEC"); private static final String POLONIEX_URL = "https://api.poloniex.com/markets/price"; + public Poloniex(Environment env) { super(env, "POLO", "poloniex", Duration.ofMinutes(1)); } @Override public Set doGet() { - Set requestedCurrencies = new HashSet<>(Arrays.asList(CURRENCIES.split(","))); - Set exchangeRates = new HashSet<>(); - PoloniexTicker[] tickers = - WebClient.create().get() - .uri(POLONIEX_URL) - .accept(MediaType.APPLICATION_JSON) - .retrieve() - .bodyToMono(PoloniexTicker[].class) - .block(Duration.of(30, ChronoUnit.SECONDS)); - Arrays.stream(Objects.requireNonNull(tickers)) - .filter(ticker -> ticker.isMatch(requestedCurrencies)) - .forEach(ticker -> exchangeRates.add( - new ExchangeRate(ticker.getCurrency(), ticker.getPrice(), new Date(), this.getName()))); - return exchangeRates; + Flux poloniexTickerFlux = WebClient.create() + .get() + .uri(POLONIEX_URL) + .accept(MediaType.APPLICATION_JSON) + .retrieve() + .bodyToFlux(PoloniexTicker.class); + + return poloniexTickerFlux.filter(PoloniexTicker::isSupportedCurrency) + .map(PoloniexTicker::toExchangeRate) + .collect(Collectors.toSet()) + .block(); } @JsonIgnoreProperties(ignoreUnknown = true) @@ -65,22 +63,25 @@ private static class PoloniexTicker { public String symbol; public String price; + public ExchangeRate toExchangeRate() { + return new ExchangeRate(getCurrency(), getPrice(), new Date(), "Poloniex"); + } + public BigDecimal getPrice() { return new BigDecimal(price); } public String getCurrency() { - // symbol might not be a BTC pair, in which case return empty string - int splitIndex = symbol.indexOf("_BTC"); - if (splitIndex < 0) { - return ""; - } - return symbol.substring(0, splitIndex); + // DASH_BTC, DOGE_BTC, LTC_BTC, ... + return symbol.split("_")[0]; + } + + private boolean isBtcPair() { + return symbol.endsWith("_BTC"); } - public boolean isMatch(Set requestedCurrencies) { - return symbol.equalsIgnoreCase(getCurrency() + "_BTC") && - requestedCurrencies.contains(getCurrency()); + public boolean isSupportedCurrency() { + return isBtcPair() && SUPPORTED_CURRENCIES.contains(getCurrency()); } } } From f2ecc707abc8fa177fc8eb6d403dc04ee71f758e Mon Sep 17 00:00:00 2001 From: jmacxx <47253594+jmacxx@users.noreply.github.com> Date: Tue, 5 Sep 2023 20:41:37 -0500 Subject: [PATCH 4/5] Remove unused dependency coinmarketcap --- gradle/libs.versions.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b2ffe51..def01f5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -25,7 +25,6 @@ known-xchange-bitflyer = { module = 'org.knowm.xchange:xchange-bitflyer', versio known-xchange-bitstamp = { module = 'org.knowm.xchange:xchange-bitstamp', version.ref = 'knowm-xchange-lib' } known-xchange-btcmarkets = { module = 'org.knowm.xchange:xchange-btcmarkets', version.ref = 'knowm-xchange-lib' } known-xchange-coinbasepro = { module = 'org.knowm.xchange:xchange-coinbasepro', version.ref = 'knowm-xchange-lib' } -known-xchange-coinmarketcap = { module = 'org.knowm.xchange:xchange-coinmarketcap', version.ref = 'knowm-xchange-lib' } known-xchange-coinone = { module = 'org.knowm.xchange:xchange-coinone', version.ref = 'knowm-xchange-lib' } known-xchange-independentreserve = { module = 'org.knowm.xchange:xchange-independentreserve', version.ref = 'knowm-xchange-lib' } known-xchange-kraken = { module = 'org.knowm.xchange:xchange-kraken', version.ref = 'knowm-xchange-lib' } @@ -45,7 +44,7 @@ spring-boot-starter-webflux = { module = 'org.springframework.boot:spring-boot-s knowm-xchange-libs = [ 'knowm-xchange-binance', 'known-xchange-bitfinex', 'known-xchange-bitflyer', 'known-xchange-bitstamp', 'known-xchange-btcmarkets', - 'known-xchange-coinbasepro', 'known-xchange-coinmarketcap', 'known-xchange-coinone', + 'known-xchange-coinbasepro', 'known-xchange-coinone', 'known-xchange-independentreserve', 'known-xchange-kraken', 'known-xchange-luno', 'known-xchange-mercadobitcoin', 'known-xchange-paribu' ] From 01c892d6456c206a02c18a613ec395c5782d44fe Mon Sep 17 00:00:00 2001 From: jmacxx <47253594+jmacxx@users.noreply.github.com> Date: Wed, 6 Sep 2023 12:57:27 -0500 Subject: [PATCH 5/5] Correct mismatched provider name. --- src/main/java/bisq/price/spot/providers/Poloniex.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/bisq/price/spot/providers/Poloniex.java b/src/main/java/bisq/price/spot/providers/Poloniex.java index 3626e5f..89b767c 100644 --- a/src/main/java/bisq/price/spot/providers/Poloniex.java +++ b/src/main/java/bisq/price/spot/providers/Poloniex.java @@ -38,9 +38,9 @@ class Poloniex extends ExchangeRateProvider { private static final List SUPPORTED_CURRENCIES = List.of("DASH", "DCR", "DOGE", "ETC", "ETH", "LTC", "XMR", "ZEC"); private static final String POLONIEX_URL = "https://api.poloniex.com/markets/price"; - + private static final String PROVIDER_NAME = "POLO"; public Poloniex(Environment env) { - super(env, "POLO", "poloniex", Duration.ofMinutes(1)); + super(env, PROVIDER_NAME, "poloniex", Duration.ofMinutes(1)); } @Override @@ -64,7 +64,7 @@ private static class PoloniexTicker { public String price; public ExchangeRate toExchangeRate() { - return new ExchangeRate(getCurrency(), getPrice(), new Date(), "Poloniex"); + return new ExchangeRate(getCurrency(), getPrice(), new Date(), PROVIDER_NAME); } public BigDecimal getPrice() {