mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 09:52:23 +01:00
Run an async price feed request when CLI needs market price
The CLI was receiving stale, cached market prices from the feed service.
This commit is contained in:
parent
184ffd14db
commit
d18b2d5a10
@ -213,8 +213,8 @@ public class CoreApi {
|
||||
// Prices
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public double getMarketPrice(String currencyCode) {
|
||||
return corePriceService.getMarketPrice(currencyCode);
|
||||
public void getMarketPrice(String currencyCode, Consumer<Double> resultHandler) {
|
||||
corePriceService.getMarketPrice(currencyCode, resultHandler);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -17,17 +17,17 @@
|
||||
|
||||
package bisq.core.api;
|
||||
|
||||
import bisq.core.provider.price.MarketPrice;
|
||||
import bisq.core.provider.price.PriceFeedService;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import static bisq.common.util.MathUtils.roundDouble;
|
||||
import static java.lang.String.format;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@Singleton
|
||||
@Slf4j
|
||||
@ -40,15 +40,25 @@ class CorePriceService {
|
||||
this.priceFeedService = priceFeedService;
|
||||
}
|
||||
|
||||
public double getMarketPrice(String currencyCode) {
|
||||
public void getMarketPrice(String currencyCode, Consumer<Double> resultHandler) {
|
||||
if (!priceFeedService.hasPrices())
|
||||
throw new IllegalStateException("price feed service has no prices");
|
||||
|
||||
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode.toUpperCase());
|
||||
if (requireNonNull(marketPrice).isPriceAvailable()) {
|
||||
return roundDouble(marketPrice.getPrice(), 4);
|
||||
} else {
|
||||
throw new IllegalStateException(format("'%s' price is not available", currencyCode));
|
||||
}
|
||||
priceFeedService.setCurrencyCode(currencyCode.toUpperCase());
|
||||
|
||||
priceFeedService.requestPriceFeed(price -> {
|
||||
if (price > 0) {
|
||||
log.info("{} price feed request returned {}", priceFeedService.getCurrencyCode(), price);
|
||||
resultHandler.accept(roundDouble(price, 4));
|
||||
} else {
|
||||
throw new IllegalStateException(format("%s price is not available",
|
||||
priceFeedService.getCurrencyCode()));
|
||||
}
|
||||
},
|
||||
(errorMessage, throwable) -> {
|
||||
log.error(errorMessage, throwable);
|
||||
throw new IllegalStateException(format("%s price feed request failed",
|
||||
priceFeedService.getCurrencyCode()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -45,10 +45,12 @@ class GrpcPriceService extends PriceGrpc.PriceImplBase {
|
||||
public void getMarketPrice(MarketPriceRequest req,
|
||||
StreamObserver<MarketPriceReply> responseObserver) {
|
||||
try {
|
||||
double price = coreApi.getMarketPrice(req.getCurrencyCode());
|
||||
var reply = MarketPriceReply.newBuilder().setPrice(price).build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
coreApi.getMarketPrice(req.getCurrencyCode(),
|
||||
price -> {
|
||||
var reply = MarketPriceReply.newBuilder().setPrice(price).build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
});
|
||||
} catch (Throwable cause) {
|
||||
exceptionHandler.handleException(cause, responseObserver);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user