Java Code Examples for org.bitcoinj.utils.Fiat#SMALLEST_UNIT_EXPONENT

The following examples show how to use org.bitcoinj.utils.Fiat#SMALLEST_UNIT_EXPONENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PriceAlert.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
private void update() {
    if (user.getPriceAlertFilter() != null) {
        PriceAlertFilter filter = user.getPriceAlertFilter();
        String currencyCode = filter.getCurrencyCode();
        MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
        if (marketPrice != null) {
            int exp = CurrencyUtil.isCryptoCurrency(currencyCode) ? Altcoin.SMALLEST_UNIT_EXPONENT : Fiat.SMALLEST_UNIT_EXPONENT;
            double priceAsDouble = marketPrice.getPrice();
            long priceAsLong = MathUtils.roundDoubleToLong(MathUtils.scaleUpByPowerOf10(priceAsDouble, exp));
            String currencyName = CurrencyUtil.getNameByCode(currencyCode);
            if (priceAsLong > filter.getHigh() || priceAsLong < filter.getLow()) {
                String msg = Res.get("account.notifications.priceAlert.message.msg",
                        currencyName,
                        formatter.formatMarketPrice(priceAsDouble, currencyCode),
                        formatter.getCurrencyPair(currencyCode));
                MobileMessage message = new MobileMessage(Res.get("account.notifications.priceAlert.message.title", currencyName),
                        msg,
                        MobileMessageType.PRICE);
                log.error(msg);
                try {
                    mobileNotificationService.sendMessage(message);

                    // If we got triggered an alert we remove the filter.
                    user.removePriceAlertFilter();
                } catch (Exception e) {
                    log.error(e.toString());
                    e.printStackTrace();
                }
            }
        }
    }
}
 
Example 2
Source File: Offer.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
@Nullable
public Price getPrice() {
    String currencyCode = getCurrencyCode();
    if (offerPayload.isUseMarketBasedPrice()) {
        checkNotNull(priceFeedService, "priceFeed must not be null");
        MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
        if (marketPrice != null && marketPrice.isRecentExternalPriceAvailable()) {
            double factor;
            double marketPriceMargin = offerPayload.getMarketPriceMargin();
            if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
                factor = getDirection() == OfferPayload.Direction.SELL ?
                        1 - marketPriceMargin : 1 + marketPriceMargin;
            } else {
                factor = getDirection() == OfferPayload.Direction.BUY ?
                        1 - marketPriceMargin : 1 + marketPriceMargin;
            }
            double marketPriceAsDouble = marketPrice.getPrice();
            double targetPriceAsDouble = marketPriceAsDouble * factor;
            try {
                int precision = CurrencyUtil.isCryptoCurrency(currencyCode) ?
                        Altcoin.SMALLEST_UNIT_EXPONENT :
                        Fiat.SMALLEST_UNIT_EXPONENT;
                double scaled = MathUtils.scaleUpByPowerOf10(targetPriceAsDouble, precision);
                final long roundedToLong = MathUtils.roundDoubleToLong(scaled);
                return Price.valueOf(currencyCode, roundedToLong);
            } catch (Exception e) {
                log.error("Exception at getPrice / parseToFiat: " + e.toString() + "\n" +
                        "That case should never happen.");
                return null;
            }
        } else {
            log.debug("We don't have a market price.\n" +
                    "That case could only happen if you don't have a price feed.");
            return null;
        }
    } else {
        return Price.valueOf(currencyCode, offerPayload.getPrice());
    }
}
 
Example 3
Source File: MutableOfferViewModel.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private void updateMarketPriceToManual() {
    final String currencyCode = dataModel.getTradeCurrencyCode().get();
    MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
    if (marketPrice != null && marketPrice.isRecentExternalPriceAvailable()) {
        double marketPriceAsDouble = marketPrice.getPrice();
        double amountAsDouble = ParsingUtils.parseNumberStringToDouble(amount.get());
        double volumeAsDouble = ParsingUtils.parseNumberStringToDouble(volume.get());
        double manualPriceAsDouble = dataModel.calculateMarketPriceManual(marketPriceAsDouble, volumeAsDouble, amountAsDouble);

        final boolean isCryptoCurrency = CurrencyUtil.isCryptoCurrency(currencyCode);
        int precision = isCryptoCurrency ?
                Altcoin.SMALLEST_UNIT_EXPONENT : Fiat.SMALLEST_UNIT_EXPONENT;
        price.set(FormattingUtils.formatRoundedDoubleWithPrecision(manualPriceAsDouble, precision));
        setPriceToModel();
        dataModel.calculateTotalToPay();
        updateButtonDisableState();
        applyMakerFee();
    } else {
        marketPriceMargin.set("");
        String id = "showNoPriceFeedAvailablePopup";
        if (preferences.showAgain(id)) {
            new Popup().warning(Res.get("popup.warning.noPriceFeedAvailable"))
                    .dontShowAgainId(id)
                    .show();
        }
    }
}
 
Example 4
Source File: PriceAlert.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private void update() {
    if (user.getPriceAlertFilter() != null) {
        PriceAlertFilter filter = user.getPriceAlertFilter();
        String currencyCode = filter.getCurrencyCode();
        MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
        if (marketPrice != null) {
            int exp = CurrencyUtil.isCryptoCurrency(currencyCode) ? Altcoin.SMALLEST_UNIT_EXPONENT : Fiat.SMALLEST_UNIT_EXPONENT;
            double priceAsDouble = marketPrice.getPrice();
            long priceAsLong = MathUtils.roundDoubleToLong(MathUtils.scaleUpByPowerOf10(priceAsDouble, exp));
            String currencyName = CurrencyUtil.getNameByCode(currencyCode);
            if (priceAsLong > filter.getHigh() || priceAsLong < filter.getLow()) {
                String msg = Res.get("account.notifications.priceAlert.message.msg",
                        currencyName,
                        FormattingUtils.formatMarketPrice(priceAsDouble, currencyCode),
                        CurrencyUtil.getCurrencyPair(currencyCode));
                MobileMessage message = new MobileMessage(Res.get("account.notifications.priceAlert.message.title", currencyName),
                        msg,
                        MobileMessageType.PRICE);
                log.error(msg);
                try {
                    mobileNotificationService.sendMessage(message);

                    // If an alert got triggered we remove the filter.
                    user.removePriceAlertFilter();
                } catch (Exception e) {
                    log.error(e.toString());
                    e.printStackTrace();
                }
            }
        }
    }
}
 
Example 5
Source File: Offer.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Nullable
public Price getPrice() {
    String currencyCode = getCurrencyCode();
    if (offerPayload.isUseMarketBasedPrice()) {
        checkNotNull(priceFeedService, "priceFeed must not be null");
        MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
        if (marketPrice != null && marketPrice.isRecentExternalPriceAvailable()) {
            double factor;
            double marketPriceMargin = offerPayload.getMarketPriceMargin();
            if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
                factor = getDirection() == OfferPayload.Direction.SELL ?
                        1 - marketPriceMargin : 1 + marketPriceMargin;
            } else {
                factor = getDirection() == OfferPayload.Direction.BUY ?
                        1 - marketPriceMargin : 1 + marketPriceMargin;
            }
            double marketPriceAsDouble = marketPrice.getPrice();
            double targetPriceAsDouble = marketPriceAsDouble * factor;
            try {
                int precision = CurrencyUtil.isCryptoCurrency(currencyCode) ?
                        Altcoin.SMALLEST_UNIT_EXPONENT :
                        Fiat.SMALLEST_UNIT_EXPONENT;
                double scaled = MathUtils.scaleUpByPowerOf10(targetPriceAsDouble, precision);
                final long roundedToLong = MathUtils.roundDoubleToLong(scaled);
                return Price.valueOf(currencyCode, roundedToLong);
            } catch (Exception e) {
                log.error("Exception at getPrice / parseToFiat: " + e.toString() + "\n" +
                        "That case should never happen.");
                return null;
            }
        } else {
            log.trace("We don't have a market price. " +
                    "That case could only happen if you don't have a price feed.");
            return null;
        }
    } else {
        return Price.valueOf(currencyCode, offerPayload.getPrice());
    }
}