@Override public Value convert(CoinType type, Coin coin) { return convert(Value.valueOf(type, coin)); }
@Test(expected = IllegalArgumentException.class) public void zeroExchangeRate() throws Exception { new ShapeShiftExchangeRate(BTC, NBT, "0", "0.01"); } }
/** * Get the lowest deposit or withdraw for the provided amount type */ private Value getLowestAmount(ValueType type) { Value min = type.getMinNonDust(); if (marketInfo != null) { if (marketInfo.minimum.isOfType(min)) { min = Value.max(marketInfo.minimum, min); } else if (marketInfo.rate.canConvert(type, marketInfo.minimum.type)) { min = Value.max(marketInfo.rate.convert(marketInfo.minimum), min); } } return min; }
@Test public void smallValues() { ShapeShiftExchangeRate rate = new ShapeShiftExchangeRate(DOGE, BTC, "5.1e-7", "0.0001"); assertEquals(BTC.value("0"), rate.convert(DOGE.value("1"))); }
@Override public Value convert(Value convertingValue) { Value converted = convertValue(convertingValue); if (!converted.isZero() && minerFee != null) { Value fee; // Deposit -> withdrawal if (converted.type.equals(minerFee.type)) { fee = minerFee.negate(); // Miner fee is removed from withdraw value } else { // Withdrawal -> deposit fee = convertValue(minerFee); // Miner fee is added to the deposit value } converted = converted.add(fee); // If the miner fee is higher than the value we are converting we get 0 if (converted.isNegative()) converted = converted.multiply(0); } return converted; }
@Test public void zeroValues() { ShapeShiftExchangeRate rate = new ShapeShiftExchangeRate(BTC, NBT, "100", "0.01"); assertEquals(BTC.value("0"), rate.convert(NBT.value("0"))); assertEquals(NBT.value("0"), rate.convert(BTC.value("0"))); }
@Test public void baseFee() { ShapeShiftExchangeRate rate = new ShapeShiftExchangeRate(BTC, NBT, "100", "0.01"); assertEquals(BTC.value("1"), rate.value1); assertEquals(NBT.value("100"), rate.value2); assertEquals(NBT.value("0.01"), rate.minerFee); assertEquals(NBT.value("99.99"), rate.convert(BTC.oneCoin())); assertEquals(BTC.value("1"), rate.convert(NBT.value("99.99"))); rate = new ShapeShiftExchangeRate(BTC.oneCoin(), DOGE.value("1911057.69230769"), DOGE.value("1")); assertEquals(BTC.value("1"), rate.value1); assertEquals(DOGE.value("1911057.69230769"), rate.value2); assertEquals(DOGE.value("1"), rate.minerFee); assertEquals(BTC.value("0.00052379"), rate.convert(DOGE.value("1000"))); rate = new ShapeShiftExchangeRate(BTC.oneCoin(), DOGE.value("1878207.54716981"), DOGE.value("1")); assertEquals(BTC.value("1"), rate.value1); assertEquals(DOGE.value("1878207.54716981"), rate.value2); assertEquals(DOGE.value("1"), rate.minerFee); assertEquals(BTC.value("0.00532476"), rate.convert(DOGE.value("10000"))); }
tradeWithdrawAmount = marketInfo.rate.convert(amountSending); } else {
public ShapeShiftRate(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { try { String[] pairs = pair.split("_"); checkState(pairs.length == 2); CoinType typeFrom = CoinID.typeFromSymbol(pairs[0]); CoinType typeTo = CoinID.typeFromSymbol(pairs[1]); rate = new ShapeShiftExchangeRate(typeFrom, typeTo, data.getString("rate"), data.optString("minerFee", null)); } catch (Exception e) { throw new ShapeShiftException("Could not parse object", e); } } else { rate = null; } } }
"minimum: 0.17391304\n" + "}")); assertEquals(BTC.value("0.00098678").subtract("0.0001"), info.rate.convert(PPC.value("1"))); assertEquals(PPC.value("2162.119259"), info.limit); assertEquals(PPC.value("0.173914"), info.minimum); "minimum: 0.0000198\n" + "}")); assertEquals(PPC.value("866.739130").subtract("0.01"), info.rate.convert(BTC.value("1"))); assertEquals(BTC.value("1.1185671"), info.limit); assertEquals(BTC.value("0.0000198"), info.minimum); "minimum: 0.00008692\n" + "}")); assertEquals(NBT.value("226.3957").subtract("0.01"), info.rate.convert(BTC.value("1"))); assertEquals(BTC.value("3.70968678"), info.limit); assertEquals(BTC.value("0.00008692"), info.minimum); "minimum: 0.04542677\n" + "}")); assertEquals(BTC.value("0.00433171").subtract("0.0001"), info.rate.convert(NBT.value("1"))); assertEquals(NBT.value("1021.5033"), info.limit); assertEquals(NBT.value("0.0455"), info.minimum);
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); }
innerData.getString("withdrawalAmount")); expiration = new Date(innerData.getLong("expiration")); rate = new ShapeShiftExchangeRate(typeDeposit, typeWithdrawal, innerData.getString("quotedRate"), innerData.optString("minerFee", null)); } catch (Exception e) {
@Test public void testGetMarketInfo() throws ShapeShiftException, IOException, InterruptedException, JSONException { // Schedule some responses. server.enqueue(new MockResponse().setBody(MARKET_INFO_BTC_NBT_JSON)); ShapeShiftMarketInfo marketInfoReply = shapeShift.getMarketInfo(BTC, NBT); assertFalse(marketInfoReply.isError); assertEquals("btc_nbt", marketInfoReply.pair); assertNotNull(marketInfoReply.rate); assertNotNull(marketInfoReply.rate); assertNotNull(marketInfoReply.limit); assertNotNull(marketInfoReply.minimum); assertEquals(NBT.value("99.99"), marketInfoReply.rate.convert(BTC.value("1"))); assertEquals(BTC.value("4"), marketInfoReply.limit); assertEquals(BTC.value("0.00000104"), marketInfoReply.minimum); // Optional: confirm that your app made the HTTP requests you were expecting. RecordedRequest request = server.takeRequest(); assertEquals("/marketinfo/btc_nbt", request.getPath()); }