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:
ghubstan 2021-01-20 17:46:35 -03:00
parent 184ffd14db
commit d18b2d5a10
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
3 changed files with 27 additions and 15 deletions

View File

@ -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);
}
///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -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()));
});
}
}

View File

@ -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);
}