/** * Makes an exchange and in case of an error returns a null. */ @Nullable private Value convertSafe(Value amount) { try { return exchangeRate.convert(amount); } catch (Exception e) { return null; } }
private void updateLocalRates() { if (localRates != null) { if (txVisualizer != null && localRates.containsKey(sourceType.getSymbol())) { txVisualizer.setExchangeRate(localRates.get(sourceType.getSymbol())); } if (tradeWithdrawAmount != null && localRates.containsKey(tradeWithdrawAmount.type.getSymbol())) { ExchangeRate rate = localRates.get(tradeWithdrawAmount.type.getSymbol()); Value fiatAmount = rate.convert(tradeWithdrawAmount); tradeWithdrawSendOutput.setAmountLocal(GenericUtils.formatFiatValue(fiatAmount)); tradeWithdrawSendOutput.setSymbolLocal(fiatAmount.type.getSymbol()); } } }
public void setExchangeRate(ExchangeRate rate) { if (outputAmount != null) { Value fiatAmount = rate.convert(type, outputAmount.toCoin()); output.setAmountLocal(GenericUtils.formatFiatValue(fiatAmount)); output.setSymbolLocal(fiatAmount.type.getSymbol()); } if (isSending && feeAmount != null) { Value fiatAmount = rate.convert(type, feeAmount.toCoin()); fee.setAmountLocal(GenericUtils.formatFiatValue(fiatAmount)); fee.setSymbolLocal(fiatAmount.type.getSymbol()); } }
/** * Get the lowest deposit or withdraw for the provided amount type */ private Value getLowestAmount(Value amount) { Value min = amount.type.getMinNonDust(); if (minimumDeposit != null) { if (minimumDeposit.isOfType(min)) { min = Value.max(minimumDeposit, min); } else if (lastRate != null && lastRate.canConvert(amount.type, minimumDeposit.type)) { min = Value.max(lastRate.convert(minimumDeposit), min); } } return min; }
/** * 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 testRateWithoutMinerFee2() throws JSONException, ShapeShiftException { JSONObject json = new JSONObject( "{" + "\"pair\" : \"btc_nbt\"," + "\"rate\" : \"123.456789\"" + "}"); ShapeShiftRate rate = new ShapeShiftRate(json); assertNotNull(rate); assertFalse(rate.isError); assertEquals("btc_nbt", rate.pair); assertNotNull(rate.rate); assertEquals(NBT, rate.rate.convert(ONE_BTC).type); assertEquals(BTC, rate.rate.convert(NBT.oneCoin()).type); }
@Test public void testRateWithoutMinerFee() throws JSONException, ShapeShiftException { JSONObject json = new JSONObject( "{" + "\"pair\" : \"btc_ltc\"," + "\"rate\" : \"100\"" + "}"); ShapeShiftRate rate = new ShapeShiftRate(json); assertNotNull(rate); assertFalse(rate.isError); assertEquals("btc_ltc", rate.pair); assertNotNull(rate.rate); assertEquals(LTC.value("100"), rate.rate.convert(BTC.value("1"))); assertEquals(LTC.value("1"), rate.rate.convert(BTC.value("0.01"))); assertEquals(BTC.value("0.01"), rate.rate.convert(LTC.value("1"))); assertEquals(BTC.value("1"), rate.rate.convert(LTC.value("100"))); }
if (lastRate != null && lastRate.canConvert(maximumDeposit.type, destinationType)) { maxDepositString += " (" + lastRate.convert(maximumDeposit) .toFriendlyString() + ")";
@Test public void testRateWithMinerFee() throws JSONException, ShapeShiftException { JSONObject json = new JSONObject( "{" + "\"pair\" : \"btc_nbt\"," + "\"rate\" : \"100\"," + "\"minerFee\" : \"0.01\"" + "}"); ShapeShiftRate rate = new ShapeShiftRate(json); assertNotNull(rate); assertFalse(rate.isError); assertEquals("btc_nbt", rate.pair); assertNotNull(rate.rate); assertEquals(NBT.value("99.99"), rate.rate.convert(BTC.value("1"))); assertEquals(BTC.value("1"), rate.rate.convert(NBT.value("99.99"))); }
@Test public void testGetRate() throws ShapeShiftException, IOException, InterruptedException, JSONException { // Schedule some responses. server.enqueue(new MockResponse().setBody(GET_RATE_BTC_LTC_JSON)); ShapeShiftRate rateReply = shapeShift.getRate(BTC, LTC); assertFalse(rateReply.isError); assertEquals("btc_ltc", rateReply.pair); assertNotNull(rateReply.rate); assertEquals(LTC, rateReply.rate.convert(BTC.oneCoin()).type); assertEquals(BTC, rateReply.rate.convert(LTC.oneCoin()).type); // Optional: confirm that your app made the HTTP requests you were expecting. RecordedRequest request = server.takeRequest(); assertEquals("/rate/btc_ltc", request.getPath()); }
@Test public void testFixedAmountTransaction() throws ShapeShiftException, IOException, InterruptedException, JSONException, AddressMalformedException { // Schedule some responses. server.enqueue(new MockResponse().setBody(FIXED_AMOUNT_TRANSACTION_JSON)); AbstractAddress withdrawal = DOGE.newAddress("DMHLQYG4j96V8cZX9WSuXxLs5RnZn6ibrV"); AbstractAddress refund = BTC.newAddress("1Nz4xHJjNCnZFPjRUq8CN4BZEXTgLZfeUW"); Value amount = DOGE.value("1000"); ShapeShiftAmountTx amountTxReply = shapeShift.exchangeForAmount(amount, withdrawal, refund); assertFalse(amountTxReply.isError); assertEquals("btc_doge", amountTxReply.pair); assertEquals("14gQ3xywKEUA6CfH61F8t2c6oB5nLnUjL5", amountTxReply.deposit.toString()); assertEquals(BTC, amountTxReply.deposit.getType()); assertEquals("0.00052379", amountTxReply.depositAmount.toPlainString()); assertEquals(BTC, amountTxReply.depositAmount.type); assertEquals(withdrawal.toString(), amountTxReply.withdrawal.toString()); assertEquals(DOGE, amountTxReply.withdrawal.getType()); assertEquals(amount.toPlainString(), amountTxReply.withdrawalAmount.toPlainString()); assertEquals(DOGE, amountTxReply.withdrawalAmount.type); assertEquals(1427149038191L, amountTxReply.expiration.getTime()); assertEquals(BTC.value(".00052379"), amountTxReply.rate.convert(Value.parse(DOGE, "1000"))); // Optional: confirm that your app made the HTTP requests you were expecting. RecordedRequest request = server.takeRequest(); assertEquals("/sendamount", request.getPath()); JSONObject reqJson = new JSONObject(request.getBody().readUtf8()); assertEquals(withdrawal.toString(), reqJson.getString("withdrawal")); assertEquals(refund.toString(), reqJson.getString("returnAddress")); assertEquals("btc_doge", reqJson.getString("pair")); assertEquals(amount.toPlainString(), reqJson.getString("amount")); }
@Test public void testAmountTxWithoutMinerFee() throws JSONException, ShapeShiftException { JSONObject json = new JSONObject( "{" + "\"success\":{" + "\"pair\":\"btc_doge\"," + "\"withdrawal\":\"DMHLQYG4j96V8cZX9WSuXxLs5RnZn6ibrV\"," + "\"withdrawalAmount\":\"1000\"," + "\"deposit\":\"14gQ3xywKEUA6CfH61F8t2c6oB5nLnUjL5\"," + "\"depositAmount\":\"0.00052327\"," + "\"expiration\":1427149038191," + "\"quotedRate\":\"1911057.69230769\"" + "}" + "}"); ShapeShiftAmountTx amountTx = new ShapeShiftAmountTx(json); assertNotNull(amountTx); assertFalse(amountTx.isError); assertEquals("btc_doge", amountTx.pair); assertEquals("14gQ3xywKEUA6CfH61F8t2c6oB5nLnUjL5", amountTx.deposit.toString()); assertEquals(BTC, amountTx.deposit.getType()); assertEquals(BTC.value("0.00052327"), amountTx.depositAmount); assertEquals("DMHLQYG4j96V8cZX9WSuXxLs5RnZn6ibrV", amountTx.withdrawal.toString()); assertEquals(DOGE, amountTx.withdrawal.getType()); assertEquals(DOGE.value("1000"), amountTx.withdrawalAmount); assertEquals(1427149038191L, amountTx.expiration.getTime()); assertEquals(BTC.value("0.00052327"), amountTx.rate.convert(Value.parse(DOGE, "1000"))); }
@Test public void testAmountTxWithMinerFee() throws JSONException, ShapeShiftException { JSONObject json = new JSONObject( "{" + "\"success\":{" + "\"pair\":\"btc_doge\"," + "\"withdrawal\":\"DMHLQYG4j96V8cZX9WSuXxLs5RnZn6ibrV\"," + "\"withdrawalAmount\":\"1000\"," + "\"minerFee\":\"1\"," + "\"deposit\":\"14gQ3xywKEUA6CfH61F8t2c6oB5nLnUjL5\"," + "\"depositAmount\":\"0.00052379\"," + "\"expiration\":1427149038191," + "\"quotedRate\":\"1911057.69230769\"" + "}" + "}"); ShapeShiftAmountTx amountTx = new ShapeShiftAmountTx(json); assertNotNull(amountTx); assertFalse(amountTx.isError); assertEquals("btc_doge", amountTx.pair); assertEquals("14gQ3xywKEUA6CfH61F8t2c6oB5nLnUjL5", amountTx.deposit.toString()); assertEquals(BTC, amountTx.deposit.getType()); assertEquals(BTC.value("0.00052379"), amountTx.depositAmount); assertEquals("DMHLQYG4j96V8cZX9WSuXxLs5RnZn6ibrV", amountTx.withdrawal.toString()); assertEquals(DOGE, amountTx.withdrawal.getType()); assertEquals(DOGE.value("1000"), amountTx.withdrawalAmount); assertEquals(1427149038191L, amountTx.expiration.getTime()); assertEquals(BTC.value("0.00052379"), amountTx.rate.convert(Value.parse(DOGE, "1000"))); }