mirror of
https://github.com/boldsuck/haveno.git
synced 2024-12-23 04:29:22 +00:00
support price feed for btc and other cryptos
add btc and remove xmr from market price map closes #125
This commit is contained in:
parent
ed4394dc5d
commit
728760bbc2
3 changed files with 12 additions and 13 deletions
|
@ -27,7 +27,6 @@ import java.util.function.Consumer;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import static bisq.common.util.MathUtils.roundDouble;
|
import static bisq.common.util.MathUtils.roundDouble;
|
||||||
import static bisq.core.locale.CurrencyUtil.isFiatCurrency;
|
|
||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
|
@ -44,14 +43,11 @@ class CorePriceService {
|
||||||
public void getMarketPrice(String currencyCode, Consumer<Double> resultHandler) {
|
public void getMarketPrice(String currencyCode, Consumer<Double> resultHandler) {
|
||||||
String upperCaseCurrencyCode = currencyCode.toUpperCase();
|
String upperCaseCurrencyCode = currencyCode.toUpperCase();
|
||||||
|
|
||||||
if (!isFiatCurrency(upperCaseCurrencyCode))
|
|
||||||
throw new IllegalStateException(format("%s is not a valid currency code", upperCaseCurrencyCode));
|
|
||||||
|
|
||||||
if (!priceFeedService.hasPrices())
|
if (!priceFeedService.hasPrices())
|
||||||
throw new IllegalStateException("price feed service has no prices");
|
throw new IllegalStateException("price feed service has no prices");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
priceFeedService.setCurrencyCode(upperCaseCurrencyCode);
|
priceFeedService.setCurrencyCode(upperCaseCurrencyCode, false); // TODO (woodser): skipping applying to consumer to avoid console warning spam when getting market prices over the api
|
||||||
} catch (Throwable throwable) {
|
} catch (Throwable throwable) {
|
||||||
log.warn("Could not set currency code in PriceFeedService", throwable);
|
log.warn("Could not set currency code in PriceFeedService", throwable);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,6 @@ package bisq.core.provider.price;
|
||||||
|
|
||||||
import bisq.core.locale.CurrencyUtil;
|
import bisq.core.locale.CurrencyUtil;
|
||||||
import bisq.core.locale.TradeCurrency;
|
import bisq.core.locale.TradeCurrency;
|
||||||
import bisq.core.monetary.Altcoin;
|
|
||||||
import bisq.core.monetary.Price;
|
import bisq.core.monetary.Price;
|
||||||
import bisq.core.provider.PriceHttpClient;
|
import bisq.core.provider.PriceHttpClient;
|
||||||
import bisq.core.provider.ProvidersRepository;
|
import bisq.core.provider.ProvidersRepository;
|
||||||
|
@ -55,7 +54,6 @@ import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
@ -272,15 +270,19 @@ public class PriceFeedService {
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
public void setCurrencyCode(String currencyCode) {
|
public void setCurrencyCode(String currencyCode) {
|
||||||
|
setCurrencyCode(currencyCode, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO (woodser): necessary to skip applying to consumer to avoid console warning spam when getting market prices over the api
|
||||||
|
public void setCurrencyCode(String currencyCode, boolean applyPriceToConsumer) {
|
||||||
if (this.currencyCode == null || !this.currencyCode.equals(currencyCode)) {
|
if (this.currencyCode == null || !this.currencyCode.equals(currencyCode)) {
|
||||||
this.currencyCode = currencyCode;
|
this.currencyCode = currencyCode;
|
||||||
currencyCodeProperty.set(currencyCode);
|
currencyCodeProperty.set(currencyCode);
|
||||||
if (priceConsumer != null)
|
if (applyPriceToConsumer && priceConsumer != null)
|
||||||
applyPriceToConsumer();
|
applyPriceToConsumer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Getter
|
// Getter
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -62,7 +62,6 @@ public class PriceProvider extends HttpClientProvider {
|
||||||
String json = httpClient.get("getAllMarketPrices", "User-Agent", "bisq/"
|
String json = httpClient.get("getAllMarketPrices", "User-Agent", "bisq/"
|
||||||
+ Version.VERSION + hsVersion);
|
+ Version.VERSION + hsVersion);
|
||||||
|
|
||||||
|
|
||||||
LinkedTreeMap<?, ?> map = new Gson().fromJson(json, LinkedTreeMap.class);
|
LinkedTreeMap<?, ?> map = new Gson().fromJson(json, LinkedTreeMap.class);
|
||||||
Map<String, Long> tsMap = new HashMap<>();
|
Map<String, Long> tsMap = new HashMap<>();
|
||||||
tsMap.put("btcAverageTs", ((Double) map.get("btcAverageTs")).longValue());
|
tsMap.put("btcAverageTs", ((Double) map.get("btcAverageTs")).longValue());
|
||||||
|
@ -96,15 +95,17 @@ public class PriceProvider extends HttpClientProvider {
|
||||||
if (isFiat) price = price * btcPerXmrFinal;
|
if (isFiat) price = price * btcPerXmrFinal;
|
||||||
else price = price / btcPerXmrFinal;
|
else price = price / btcPerXmrFinal;
|
||||||
|
|
||||||
// TODO (woodser): remove xmr from list since base currency and add btc, test by doing btc/xmr trade
|
// add currency price to map
|
||||||
|
|
||||||
marketPriceMap.put(currencyCode, new MarketPrice(currencyCode, price, timestampSec, true));
|
marketPriceMap.put(currencyCode, new MarketPrice(currencyCode, price, timestampSec, true));
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
log.error(t.toString());
|
log.error(t.toString());
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// add btc to price map, remove xmr since base currency
|
||||||
|
marketPriceMap.put("BTC", new MarketPrice("BTC", 1 / btcPerXmrFinal, marketPriceMap.get("XMR").getTimestampSec(), true));
|
||||||
|
marketPriceMap.remove("XMR");
|
||||||
return new Tuple2<>(tsMap, marketPriceMap);
|
return new Tuple2<>(tsMap, marketPriceMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue