public ShapeShiftPairBase(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { try { pair = data.getString("pair").toLowerCase(); } catch (JSONException e) { throw new ShapeShiftException("Could not parse object", e); } } else { pair = null; } }
private void checkPair(String expectedPair, String pair) throws ShapeShiftException { if (!expectedPair.equals(pair)) { String errorMsg = String.format("Pair mismatch, expected %s but got %s.", expectedPair, pair); throw new ShapeShiftException(errorMsg); } }
private void checkValue(Value expected, Value value) throws ShapeShiftException { if (!expected.equals(value)) { String errorMsg = String.format("Value mismatch, expected %s but got %s.", expected, value); throw new ShapeShiftException(errorMsg); } }
public ShapeShiftTime(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { try { secondsRemaining = data.getInt("seconds_remaining"); String statusStr = data.getString("status"); switch (statusStr) { case "pending": status = Status.PENDING; break; case "expired": status = Status.EXPIRED; break; default: status = Status.UNKNOWN; } } catch (JSONException e) { throw new ShapeShiftException("Could not parse object", e); } } else { status = null; secondsRemaining = -1; } } }
public ShapeShiftEmail(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { try { JSONObject innerData = data.getJSONObject("email"); message = innerData.getString("message"); String statusStr = innerData.getString("status"); switch (statusStr) { case "success": status = Status.SUCCESS; break; default: status = Status.UNKNOWN; } } catch (JSONException e) { throw new ShapeShiftException("Could not parse object", e); } } else { status = null; message = null; } } }
public ShapeShiftCoin(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { try { name = data.getString("name"); symbol = data.getString("symbol"); image = new URL(data.getString("image")); isAvailable = data.getString("status").equals("available"); } catch (Exception e) { throw new ShapeShiftException("Could not parse object", e); } } else { name = null; symbol = null; image = null; isAvailable = false; } } }
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; } } }
public ShapeShiftCoins(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { try { ImmutableList.Builder<ShapeShiftCoin> listBuilder = ImmutableList.builder(); Iterator iter = data.keys(); while (iter.hasNext()) { String k = (String) iter.next(); listBuilder.add(new ShapeShiftCoin(data.getJSONObject(k))); } coins = listBuilder.build(); ImmutableList.Builder<CoinType> typesBuilder = ImmutableList.builder(); for (ShapeShiftCoin coin : coins) { if (coin.isAvailable && CoinID.isSymbolSupported(coin.symbol)) { typesBuilder.add(CoinID.typeFromSymbol(coin.symbol)); } } availableCoinTypes = typesBuilder.build(); } catch (Exception e) { throw new ShapeShiftException("Could not parse object", e); } } else { coins = null; availableCoinTypes = null; } } }
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; } } }
public ShapeShiftLimit(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { try { String[] pairs = pair.split("_"); checkState(pairs.length == 2); CoinType typeFrom = CoinID.typeFromSymbol(pairs[0]); limit = parseValue(typeFrom, data.getString("limit"), RoundingMode.DOWN); minimum = parseValue(typeFrom, data.getString("min"), RoundingMode.UP); } catch (Exception e) { throw new ShapeShiftException("Could not parse object", e); } } else { limit = null; minimum = null; } } }
private void checkAddress(AbstractAddress expected, AbstractAddress address) throws ShapeShiftException { if (!expected.getType().equals(address.getType()) || !expected.toString().equals(address.toString())) { String errorMsg = String.format("Address mismatch, expected %s but got %s.", expected, address); throw new ShapeShiftException(errorMsg); } }
innerData.getString("quotedRate"), innerData.optString("minerFee", null)); } catch (Exception e) { throw new ShapeShiftException("Could not parse object", e);
@Override protected Void doInBackground(Void... params) { if (application.isConnected()) { try { shapeShiftCoins = application.getShapeShift().getCoins(); } catch (Exception e) { error = e; } } else { error = new ShapeShiftException("No connection"); } return null; }
private JSONObject getMakeApiCall(Request request) throws ShapeShiftException, IOException { try { Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { JSONObject reply = parseReply(response); String genericMessage = "Error code " + response.code(); throw new IOException( reply != null ? reply.optString("error", genericMessage) : genericMessage); } return parseReply(response); } catch (JSONException e) { throw new ShapeShiftException("Could not parse JSON", e); } }
throw new ShapeShiftException("Could not parse object", e); } catch (AddressMalformedException e) { throw new ShapeShiftException("Could not parse address", e); if (!isError) throw new ShapeShiftException("Unexpected state: no status and no error"); status = null; address = null;
public ShapeShiftNormalTx(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { try { deposit = CoinID.typeFromSymbol(data.getString("depositType")). newAddress(data.getString("deposit")); withdrawal = CoinID.typeFromSymbol(data.getString("withdrawalType")). newAddress(data.getString("withdrawal")); pair = ShapeShift.getPair(deposit.getType(), withdrawal.getType()); } catch (Exception e) { throw new ShapeShiftException("Could not parse object", e); } } else { deposit = null; withdrawal = null; pair = null; } } }
/** * Normal Transaction * * Make a normal exchange and receive with {@code withdrawal} address. The exchange pair is * determined from the {@link CoinType}s of {@code refund} and {@code withdrawal}. */ public ShapeShiftNormalTx exchange(AbstractAddress withdrawal, AbstractAddress refund) throws ShapeShiftException, IOException { JSONObject requestJson = new JSONObject(); try { requestJson.put("withdrawal", withdrawal.toString()); requestJson.put("pair", getPair(refund.getType(), withdrawal.getType())); requestJson.put("returnAddress", refund.toString()); if (apiPublicKey != null) requestJson.put("apiKey", apiPublicKey); } catch (JSONException e) { throw new ShapeShiftException("Could not create a JSON request", e); } String apiUrl = getApiUrl(NORMAL_TX_API); RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, requestJson.toString()); Request request = new Request.Builder().url(apiUrl).post(body).build(); ShapeShiftNormalTx reply = new ShapeShiftNormalTx(getMakeApiCall(request)); if (!reply.isError) checkAddress(withdrawal, reply.withdrawal); return reply; }
/** * Request email receipt * * This call allows you to request a fixed amount to be sent to the {@code withdrawal} address. * You provide a withdrawal address and the amount you want sent to it. We return the amount * to deposit and the address to deposit to. This allows you to use shapeshift as a payment * mechanism. * * The exchange pair is determined from the {@link CoinType}s of {@code refund} and * {@code withdrawal}. */ public ShapeShiftEmail requestEmailReceipt(String email, ShapeShiftTxStatus txStatus) throws ShapeShiftException, IOException { JSONObject requestJson = new JSONObject(); try { requestJson.put("email", email); checkState(txStatus.status == ShapeShiftTxStatus.Status.COMPLETE, "Transaction not complete"); requestJson.put("txid", checkNotNull(txStatus.transactionId, "Null transaction id")); } catch (Exception e) { throw new ShapeShiftException("Could not create a JSON request", e); } String apiUrl = getApiUrl(EMAIL_RECEIPT_API); RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, requestJson.toString()); Request request = new Request.Builder().url(apiUrl).post(body).build(); return new ShapeShiftEmail(getMakeApiCall(request)); }
if (apiPublicKey != null) requestJson.put("apiKey", apiPublicKey); } catch (JSONException e) { throw new ShapeShiftException("Could not create a JSON request", e);