private BigDecimal getExchangeRateLastSync(String cryptoCurrency, String fiatCurrency) { if (!ICurrencies.NXT.equalsIgnoreCase(cryptoCurrency)) { return null; // unsupported currency } OrderBookResponse orderBookResponse = api.returnOrderBook("returnOrderBook", "BTC_NXT", 10000); if (orderBookResponse != null) { BigDecimal[][] asks = orderBookResponse.getAsks(); BigDecimal asksTotal = BigDecimal.ZERO; BigDecimal targetAmount = new BigDecimal(100000); // calculate price based on this amount of NXT BigDecimal tradableLimit = BigDecimal.ZERO; for (int i = 0; i < asks.length; i++) { BigDecimal[] ask = asks[i]; // log.debug("ask = " + ask); asksTotal = asksTotal.add(ask[1]); if (targetAmount.compareTo(asksTotal) <= 0) { tradableLimit = ask[0]; break; } } // System.out.println("tradableLimit = " + tradableLimit);; if (tradableLimit != null) { BigDecimal btcRate = btcRs.getExchangeRateLast(ICurrencies.BTC, fiatCurrency); if (btcRate != null) { return btcRate.multiply(tradableLimit); } } return null; } return null; }
@Override public BigDecimal getExchangeRateLast(String cryptoCurrency, String fiatCurrency) { if (!ICurrencies.NXT.equalsIgnoreCase(cryptoCurrency)) { return null; } String key = cryptoCurrency + "_" + fiatCurrency; synchronized (rateAmounts) { long now = System.currentTimeMillis(); BigDecimal amount = rateAmounts.get(key); if (amount == null) { BigDecimal result = getExchangeRateLastSync(cryptoCurrency, fiatCurrency); log.debug("Called bitcoinaverage exchange for rate: " + key + " = " + result); rateAmounts.put(key, result); rateTimes.put(key, now + MAXIMUM_ALLOWED_TIME_OFFSET); return result; } else { Long expirationTime = rateTimes.get(key); if (expirationTime > now) { return rateAmounts.get(key); } else { // do the job; BigDecimal result = getExchangeRateLastSync(cryptoCurrency, fiatCurrency); log.debug("Called bitcoinaverage exchange for rate: " + key + " = " + result); rateAmounts.put(key, result); rateTimes.put(key, now + MAXIMUM_ALLOWED_TIME_OFFSET); return result; } } } }