Skip to content

Commit

Permalink
Merge pull request bisq-network#5582 from jmacxx/pricenode_config_ccy…
Browse files Browse the repository at this point in the history
…_excl

PriceNode: exclude currencies via config
  • Loading branch information
ripcurlx authored Jul 13, 2021
2 parents eace7c2 + 202b073 commit dddabe3
Show file tree
Hide file tree
Showing 43 changed files with 251 additions and 83 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ configure(project(':pricenode')) {
testRuntime("org.junit.jupiter:junit-jupiter-engine:$jupiterVersion")
testCompileOnly "org.projectlombok:lombok:$lombokVersion"
testAnnotationProcessor "org.projectlombok:lombok:$lombokVersion"
testCompile "org.mockito:mockito-core:$mockitoVersion"
}

test {
Expand Down
59 changes: 48 additions & 11 deletions pricenode/src/main/java/bisq/price/spot/ExchangeRateProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
import org.knowm.xchange.service.marketdata.params.CurrencyPairsParam;
import org.knowm.xchange.service.marketdata.params.Params;

import org.springframework.core.env.Environment;

import java.time.Duration;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
Expand All @@ -59,21 +62,55 @@
*/
public abstract class ExchangeRateProvider extends PriceProvider<Set<ExchangeRate>> {

public static final Set<String> SUPPORTED_CRYPTO_CURRENCIES = CurrencyUtil.getAllSortedCryptoCurrencies().stream()
.map(TradeCurrency::getCode)
.collect(Collectors.toSet());

public static final Set<String> SUPPORTED_FIAT_CURRENCIES = CurrencyUtil.getAllSortedFiatCurrencies().stream()
.map(TradeCurrency::getCode)
.collect(Collectors.toSet());

private static Set<String> SUPPORTED_CRYPTO_CURRENCIES = new HashSet<>();
private static Set<String> SUPPORTED_FIAT_CURRENCIES = new HashSet<>();
private final String name;
private final String prefix;
private final Environment env;

public ExchangeRateProvider(String name, String prefix, Duration refreshInterval) {
public ExchangeRateProvider(Environment env, String name, String prefix, Duration refreshInterval) {
super(refreshInterval);
this.name = name;
this.prefix = prefix;
this.env = env;
}

public Set<String> getSupportedFiatCurrencies() {
if (SUPPORTED_FIAT_CURRENCIES.isEmpty()) { // one-time initialization
List<String> excludedFiatCurrencies =
Arrays.asList(env.getProperty("bisq.price.fiatcurrency.excluded", "")
.toUpperCase().trim().split("\\s*,\\s*"));
String validatedExclusionList = excludedFiatCurrencies.stream()
.filter(ccy -> !ccy.isEmpty())
.filter(CurrencyUtil::isFiatCurrency)
.collect(Collectors.toList()).toString();
SUPPORTED_FIAT_CURRENCIES = CurrencyUtil.getAllSortedFiatCurrencies().stream()
.map(TradeCurrency::getCode)
.filter(ccy -> !validatedExclusionList.contains(ccy.toUpperCase()))
.collect(Collectors.toSet());
log.info("fiat currencies excluded: {}", validatedExclusionList);
log.info("fiat currencies supported: {}", SUPPORTED_FIAT_CURRENCIES.size());
}
return SUPPORTED_FIAT_CURRENCIES;
}

public Set<String> getSupportedCryptoCurrencies() {
if (SUPPORTED_CRYPTO_CURRENCIES.isEmpty()) { // one-time initialization
List<String> excludedCryptoCurrencies =
Arrays.asList(env.getProperty("bisq.price.cryptocurrency.excluded", "")
.toUpperCase().trim().split("\\s*,\\s*"));
String validatedExclusionList = excludedCryptoCurrencies.stream()
.filter(ccy -> !ccy.isEmpty())
.filter(CurrencyUtil::isCryptoCurrency)
.collect(Collectors.toList()).toString();
SUPPORTED_CRYPTO_CURRENCIES = CurrencyUtil.getAllSortedCryptoCurrencies().stream()
.map(TradeCurrency::getCode)
.filter(ccy -> !validatedExclusionList.contains(ccy.toUpperCase()))
.collect(Collectors.toSet());
log.info("crypto currencies excluded: {}", validatedExclusionList);
log.info("crypto currencies supported: {}", SUPPORTED_CRYPTO_CURRENCIES.size());
}
return SUPPORTED_CRYPTO_CURRENCIES;
}

public String getName() {
Expand Down Expand Up @@ -119,13 +156,13 @@ protected Set<ExchangeRate> doGet(Class<? extends Exchange> exchangeClass) {
// Find the desired fiat pairs (pair format is BTC-FIAT)
List<CurrencyPair> desiredFiatPairs = allCurrencyPairsOnExchange.stream()
.filter(cp -> cp.base.equals(Currency.BTC))
.filter(cp -> SUPPORTED_FIAT_CURRENCIES.contains(cp.counter.getCurrencyCode()))
.filter(cp -> getSupportedFiatCurrencies().contains(cp.counter.getCurrencyCode()))
.collect(Collectors.toList());

// Find the desired altcoin pairs (pair format is ALT-BTC)
List<CurrencyPair> desiredCryptoPairs = allCurrencyPairsOnExchange.stream()
.filter(cp -> cp.counter.equals(Currency.BTC))
.filter(cp -> SUPPORTED_CRYPTO_CURRENCIES.contains(cp.base.getCurrencyCode()))
.filter(cp -> getSupportedCryptoCurrencies().contains(cp.base.getCurrencyCode()))
.collect(Collectors.toList());

// Retrieve in bulk all tickers offered by the exchange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.knowm.xchange.btcmarkets.BTCMarketsExchange;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -31,8 +32,8 @@
@Component
class BTCMarkets extends ExchangeRateProvider {

public BTCMarkets() {
super("BTCMARKETS", "btcmarkets", Duration.ofMinutes(1));
public BTCMarkets(Environment env) {
super(env, "BTCMARKETS", "btcmarkets", Duration.ofMinutes(1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.knowm.xchange.binance.BinanceExchange;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -31,8 +32,8 @@
@Component
class Binance extends ExchangeRateProvider {

public Binance() {
super("BINANCE", "binance", Duration.ofMinutes(1));
public Binance(Environment env) {
super(env, "BINANCE", "binance", Duration.ofMinutes(1));
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions pricenode/src/main/java/bisq/price/spot/providers/Bitbay.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.knowm.xchange.bitbay.BitbayExchange;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -31,8 +32,8 @@
@Component
class Bitbay extends ExchangeRateProvider {

public Bitbay() {
super("BITBAY", "bitbay", Duration.ofMinutes(1));
public Bitbay(Environment env) {
super(env, "BITBAY", "bitbay", Duration.ofMinutes(1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bisq.price.spot.ExchangeRate;
import bisq.price.spot.ExchangeRateProvider;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -34,12 +35,12 @@
@Component
class BitcoinAverage extends ExchangeRateProvider {

public BitcoinAverage() {
public BitcoinAverage(Environment env) {
// Simulate a deactivated BitcoinAverage provider
// We still need the class to exist and be registered as a provider though,
// because the returned data structure must contain the "btcAverageTs" key
// for backward compatibility with Bisq clients which hardcode that key
super("BA", "btcAverage", Duration.ofMinutes(100));
super(env, "BA", "btcAverage", Duration.ofMinutes(100));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.knowm.xchange.bitfinex.BitfinexExchange;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -31,8 +32,8 @@
@Component
class Bitfinex extends ExchangeRateProvider {

public Bitfinex() {
super("BITFINEX", "bitfinex", Duration.ofMinutes(1));
public Bitfinex(Environment env) {
super(env, "BITFINEX", "bitfinex", Duration.ofMinutes(1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.knowm.xchange.bitflyer.BitflyerExchange;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -31,8 +32,8 @@
@Component
class Bitflyer extends ExchangeRateProvider {

public Bitflyer() {
super("BITFLYER", "bitflyer", Duration.ofMinutes(1));
public Bitflyer(Environment env) {
super(env, "BITFLYER", "bitflyer", Duration.ofMinutes(1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.knowm.xchange.bitstamp.BitstampExchange;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -31,8 +32,8 @@
@Component
class Bitstamp extends ExchangeRateProvider {

public Bitstamp() {
super("BITSTAMP", "bitstamp", Duration.ofMinutes(1));
public Bitstamp(Environment env) {
super(env, "BITSTAMP", "bitstamp", Duration.ofMinutes(1));
}

@Override
Expand Down
11 changes: 6 additions & 5 deletions pricenode/src/main/java/bisq/price/spot/providers/CoinGecko.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bisq.price.util.coingecko.CoinGeckoMarketData;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.env.Environment;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
Expand All @@ -44,8 +45,8 @@ class CoinGecko extends ExchangeRateProvider {

private final RestTemplate restTemplate = new RestTemplate();

public CoinGecko() {
super("COINGECKO", "coingecko", Duration.ofMinutes(1));
public CoinGecko(Environment env) {
super(env, "COINGECKO", "coingecko", Duration.ofMinutes(1));
}

@Override
Expand All @@ -56,16 +57,16 @@ public Set<ExchangeRate> doGet() {

Set<ExchangeRate> result = new HashSet<ExchangeRate>();

Predicate<Map.Entry> isDesiredFiatPair = t -> SUPPORTED_FIAT_CURRENCIES.contains(t.getKey());
Predicate<Map.Entry> isDesiredCryptoPair = t -> SUPPORTED_CRYPTO_CURRENCIES.contains(t.getKey());
Predicate<Map.Entry> isDesiredFiatPair = t -> getSupportedFiatCurrencies().contains(t.getKey());
Predicate<Map.Entry> isDesiredCryptoPair = t -> getSupportedCryptoCurrencies().contains(t.getKey());

getMarketData().getRates().entrySet().stream()
.filter(isDesiredFiatPair.or(isDesiredCryptoPair))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
.forEach((key, ticker) -> {

boolean useInverseRate = false;
if (SUPPORTED_CRYPTO_CURRENCIES.contains(key)) {
if (getSupportedCryptoCurrencies().contains(key)) {
// Use inverse rate for alts, because the API returns the
// conversion rate in the opposite direction than what we need
// API returns the BTC/Alt rate, we need the Alt/BTC rate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bisq.price.spot.ExchangeRate;
import bisq.price.spot.ExchangeRateProvider;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -33,8 +34,8 @@
@Component
class CoinMarketCap extends ExchangeRateProvider {

public CoinMarketCap() {
super("CMC", "coinmarketcap", Duration.ofMinutes(5)); // large data structure, so don't request it too often
public CoinMarketCap(Environment env) {
super(env, "CMC", "coinmarketcap", Duration.ofMinutes(5)); // large data structure, so don't request it too often
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.knowm.xchange.coinone.CoinoneExchange;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -31,8 +32,8 @@
@Component
class Coinone extends ExchangeRateProvider {

public Coinone() {
super("COINONE", "coinone", Duration.ofMinutes(1));
public Coinone(Environment env) {
super(env, "COINONE", "coinone", Duration.ofMinutes(1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bisq.price.util.coinpaprika.CoinpaprikaMarketData;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.env.Environment;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -55,8 +56,8 @@ class Coinpaprika extends ExchangeRateProvider {
"INR, MYR, NOK, PKR, SEK, TWD, ZAR, VND, BOB, COP, PEN, ARS, ISK")
.replace(" ", ""); // Strip any spaces

public Coinpaprika() {
super("COINPAPRIKA", "coinpaprika", Duration.ofMinutes(1));
public Coinpaprika(Environment env) {
super(env, "COINPAPRIKA", "coinpaprika", Duration.ofMinutes(1));
}

@Override
Expand All @@ -67,7 +68,7 @@ public Set<ExchangeRate> doGet() {

Set<ExchangeRate> result = new HashSet<ExchangeRate>();

Predicate<Map.Entry> isDesiredFiatPair = t -> SUPPORTED_FIAT_CURRENCIES.contains(t.getKey());
Predicate<Map.Entry> isDesiredFiatPair = t -> getSupportedFiatCurrencies().contains(t.getKey());

getMarketData().getQuotes().entrySet().stream()
.filter(isDesiredFiatPair)
Expand Down
5 changes: 3 additions & 2 deletions pricenode/src/main/java/bisq/price/spot/providers/Exmo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.knowm.xchange.exmo.ExmoExchange;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -31,9 +32,9 @@
@Component
class Exmo extends ExchangeRateProvider {

public Exmo() {
public Exmo(Environment env) {
// API rate limit = 10 calls / second from the same IP ( see https://exmo.com/en/api )
super("EXMO", "exmo", Duration.ofMinutes(1));
super(env, "EXMO", "exmo", Duration.ofMinutes(1));
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions pricenode/src/main/java/bisq/price/spot/providers/Huobi.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.knowm.xchange.huobi.HuobiExchange;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.time.Duration;
Expand All @@ -31,8 +32,8 @@
@Component
class Huobi extends ExchangeRateProvider {

public Huobi() {
super("HUOBI", "huobi", Duration.ofMinutes(1));
public Huobi(Environment env) {
super(env, "HUOBI", "huobi", Duration.ofMinutes(1));
}

@Override
Expand Down
Loading

0 comments on commit dddabe3

Please sign in to comment.