/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.api;

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 bisq.core.locale.CurrencyUtil.isFiatCurrency;
import static java.lang.String.format;

@Singleton
@Slf4j
class CorePriceService {

    private final PriceFeedService priceFeedService;

    @Inject
    public CorePriceService(PriceFeedService priceFeedService) {
        this.priceFeedService = priceFeedService;
    }

    public void getMarketPrice(String currencyCode, Consumer<Double> resultHandler) {
        String upperCaseCurrencyCode = currencyCode.toUpperCase();

        if (!isFiatCurrency(upperCaseCurrencyCode))
            throw new IllegalStateException(format("%s is not a valid currency code", upperCaseCurrencyCode));

        if (!priceFeedService.hasPrices())
            throw new IllegalStateException("price feed service has no prices");

        try {
            priceFeedService.setCurrencyCode(upperCaseCurrencyCode);
        } catch (Throwable throwable) {
            log.warn("Could not set currency code in PriceFeedService", throwable);
        }

        priceFeedService.requestPriceFeed(price -> {
                    if (price > 0) {
                        log.info("{} price feed request returned {}", upperCaseCurrencyCode, price);
                        resultHandler.accept(roundDouble(price, 4));
                    } else {
                        throw new IllegalStateException(format("%s price is not available", upperCaseCurrencyCode));
                    }
                },
                (errorMessage, throwable) -> log.warn(errorMessage, throwable));
    }
}

@@ -845,16 +845,53 @@
 onsumer;
+%0Aimport java.util.function.Predicate;
 %0A%0Aimport
@@ -973,16 +973,78 @@
 Double;%0A
+import static bisq.core.locale.CurrencyUtil.isCryptoCurrency;%0A
 import s
@@ -1179,16 +1179,119 @@
 vice %7B%0A%0A
+    private final Predicate%3CString%3E isCurrencyCode = (c) -%3E isFiatCurrency(c) %7C%7C isCryptoCurrency(c);%0A%0A
     priv
@@ -1639,28 +1639,24 @@
  if (!is
-Fiat
 Currency
 (upperCa
@@ -1647,16 +1647,25 @@
 Currency
+Code.test
 (upperCa
@@ -2355,49 +2355,592 @@
     
-resultHandler.accept(roundDouble(price, 4
+if (isFiatCurrency(upperCaseCurrencyCode))%0A                            resultHandler.accept(roundDouble(price, 4));%0A                        else if (isCryptoCurrency(upperCaseCurrencyCode))%0A                            resultHandler.accept(roundDouble(price, 8));%0A                        else // should not happen, throw error if it does%0A                            throw new IllegalStateException(%0A                                    format(%22%25s price feed request should not return data for unsupported currency code%22,%0A                                            upperCaseCurrencyCode
 ));%0A
@@ -3145,71 +3145,18 @@
     
- (errorMessage, throwable) -%3E
  log
-.
+::
 warn
-(errorMessage, throwable)
 );%0A 

Allow mkt price lookup for any altcoin from API