/** * Get Market Info * * This is a combined call for {@link #getRate(CoinType, CoinType) getRate()} and * {@link #getLimit(CoinType, CoinType) getLimit()} API calls. */ public ShapeShiftMarketInfo getMarketInfo(String pair) throws ShapeShiftException, IOException { log.info("Market info for pair {}", pair); String apiUrl = getApiUrl(String.format(MARKET_INFO_API, pair)); Request request = new Request.Builder().url(apiUrl).build(); ShapeShiftMarketInfo reply = new ShapeShiftMarketInfo(getMakeApiCall(request)); if (!reply.isError) checkPair(pair, reply.pair); return reply; }
/** * Updates the exchange rate and limits for the specific market. * Note: if the current pair is different that the marketInfo pair, do nothing */ private void onMarketUpdate(ShapeShiftMarketInfo marketInfo) { if (address != null && marketInfo.isPair(account.getCoinType(), address.getType())) { this.marketInfo = marketInfo; } }
public ShapeShiftMarketInfo(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { // In market info minerFee is mandatory if (!data.has("minerFee")) { throw new ShapeShiftException("Missing value minerfee"); } try { CoinType[] types = ShapeShift.parsePair(pair); rate = new ShapeShiftExchangeRate(types[0], types[1], data.getString("rate"), data.getString("minerFee")); limit = parseValue(types[0], data.getString("limit"), RoundingMode.DOWN); minimum = parseValue(types[0], data.getString("minimum"), RoundingMode.UP); } catch (JSONException e) { throw new ShapeShiftException("Could not parse object", e); } } else { rate = null; limit = null; minimum = null; } } }
@Test public void testMarketInfo() throws JSONException, ShapeShiftException { JSONObject json = new JSONObject( "{" + "\"pair\" : \"btc_nbt\"," + "\"rate\" : \"100\"," + "\"minerFee\" : \"0.01\"," + "\"limit\" : \"4\"," + "\"minimum\" : 0.00000104" + "}"); ShapeShiftMarketInfo marketInfo = new ShapeShiftMarketInfo(json); assertNotNull(marketInfo); assertFalse(marketInfo.isError); assertEquals("btc_nbt", marketInfo.pair); assertTrue(marketInfo.isPair("BTC_NBT")); assertTrue(marketInfo.isPair("btc_nbt")); assertTrue(marketInfo.isPair(BTC, NBT)); assertFalse(marketInfo.isPair("doge_ltc")); assertFalse(marketInfo.isPair(DOGE, LTC)); assertNotNull(marketInfo.rate); assertNotNull(marketInfo.limit); assertNotNull(marketInfo.minimum); assertEquals(NBT.value("99.99"), marketInfo.rate.convert(BTC.value("1"))); assertEquals(BTC.value("4"), marketInfo.limit); assertEquals(BTC.value("0.00000104"), marketInfo.minimum); }
/** * Updates the exchange rate and limits for the specific market. * Note: if the current pair is different that the marketInfo pair, do nothing */ private void onMarketUpdate(ShapeShiftMarketInfo marketInfo) { // If not current pair, do nothing if (!marketInfo.isPair(sourceAccount.getCoinType(), destinationType)) return; maximumDeposit = marketInfo.limit; minimumDeposit = marketInfo.minimum; lastRate = marketInfo.rate; amountCalculatorLink.setExchangeRate(lastRate); if (amountCalculatorLink.isEmpty() && lastRate != null) { Value hintValue = sourceAccount.getCoinType().oneCoin(); Value exchangedValue = lastRate.convert(hintValue); Value minerFee100 = marketInfo.rate.minerFee.multiply(100); // If hint value is too small, make it higher to get a no zero exchanged value and // at least 10 times higher than the miner fee for (int tries = 8; tries > 0 && (exchangedValue.isZero() || exchangedValue.compareTo(minerFee100) < 0); tries--) { hintValue = hintValue.multiply(10); exchangedValue = lastRate.convert(hintValue); } amountCalculatorLink.setExchangeRateHints(hintValue); } }
@Test public void testMarketInfo2() throws JSONException, ShapeShiftException { ShapeShiftMarketInfo info = new ShapeShiftMarketInfo(new JSONObject( "{\n" + "pair: \"ppc_btc\",\n" + assertEquals(PPC.value("0.173914"), info.minimum); info = new ShapeShiftMarketInfo(new JSONObject( "{\n" + "pair: \"btc_ppc\",\n" + assertEquals(BTC.value("0.0000198"), info.minimum); info = new ShapeShiftMarketInfo(new JSONObject( "{\n" + "pair: \"btc_nbt\",\n" + assertEquals(BTC.value("0.00008692"), info.minimum); info = new ShapeShiftMarketInfo(new JSONObject( "{\n" + "pair: \"nbt_btc\",\n" +