Skip to content

Commit

Permalink
CoinPaprika API
Browse files Browse the repository at this point in the history
  • Loading branch information
koh-gt committed Sep 26, 2024
1 parent 551edc3 commit 0171d77
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
8 changes: 4 additions & 4 deletions wallet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ android {
}
}
defaultConfig {
versionCode 30107
versionName "3.1.7"
versionCode 30108
versionName "3.1.8"
vectorDrawables.useSupportLibrary = true

// Enabling multidex support.
Expand All @@ -109,11 +109,11 @@ android {
productFlavors {
_testNet {
dimension "version"
applicationId = "de.schildbach.wallet.ferrite_test"
applicationId = "wallet.fec_test"
}
prod {
dimension "version"
applicationId = "de.schildbach.wallet.ferrite"
applicationId = "wallet.fec"
}
}

Expand Down
6 changes: 3 additions & 3 deletions wallet/prod/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"type": "APK",
"kind": "Directory"
},
"applicationId": "de.schildbach.wallet.ferrite",
"applicationId": "wallet.fec",
"variantName": "prodRelease",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 30107,
"versionName": "3.1.7",
"versionCode": 30108,
"versionName": "3.1.8",
"outputFile": "wallet-prod-release.apk"
}
],
Expand Down
62 changes: 27 additions & 35 deletions wallet/src/de/schildbach/wallet/data/ExchangeRatesProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,56 +237,49 @@ public String getType(final Uri uri) {

private Map<String, ExchangeRate> requestExchangeRates() {
final Stopwatch watch = Stopwatch.createStarted();

final Request.Builder request = new Request.Builder();
request.url(COINGECKO_URL);
request.url(COINPAPRIKA_URL);
request.header("User-Agent", userAgent);

final Call call = Constants.HTTP_CLIENT.newCall(request.build());
try {
final Response response = call.execute();
if (response.isSuccessful()) {
final String content = response.body().string();
final JSONObject head = new JSONObject(content);
final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();

//check for correct information
if(head.getString("id").equals("ferrite")) {
JSONObject marketData = head.getJSONObject("market_data");
JSONObject currentPrice = marketData.getJSONObject("current_price");

for (final Iterator<String> i = currentPrice.keys(); i.hasNext(); ) {
final String currencyCode = i.next();
final String fiatCurrencyCode = currencyCode.toUpperCase();
if (!fiatCurrencyCode.equals(MonetaryFormat.CODE_BTC)
&& !fiatCurrencyCode.equals(MonetaryFormat.CODE_MBTC)
&& !fiatCurrencyCode.equals(MonetaryFormat.CODE_UBTC)) {
final String exchangeRate = currentPrice.getString(currencyCode);//head.getJSONObject(currencyCode);
try {
final Fiat rate = parseFiatInexact(fiatCurrencyCode, exchangeRate);
if (rate.signum() > 0)
rates.put(fiatCurrencyCode, new ExchangeRate(
new org.bitcoinj.utils.ExchangeRate(rate), COINGECKO_SOURCE));
} catch (final IllegalArgumentException x) {
log.warn("problem fetching {} exchange rate from {}: {}", currencyCode,
COINGECKO_URL, x.getMessage());
}
final Map<String, ExchangeRate> rates = new TreeMap<>();

// Check if the API response is for Ferrite
if (head.getString("id").equals("fec-ferrite")) {
JSONObject quotes = head.getJSONObject("quotes");
JSONObject usdQuote = quotes.getJSONObject("USD");

// Extract the USD price
final String exchangeRate = usdQuote.getString("price");
try {
final Fiat rate = parseFiatInexact("USD", exchangeRate);
if (rate.signum() > 0) {
rates.put("USD", new ExchangeRate(
new org.bitcoinj.utils.ExchangeRate(rate), COINPAPRIKA_SOURCE));
}
} catch (final IllegalArgumentException x) {
log.warn("problem fetching USD exchange rate from {}: {}", COINPAPRIKA_URL, x.getMessage());
}

watch.stop();
log.info("fetched exchange rates from {}, {} chars, took {}", COINGECKO_URL, content.length(),
log.info("fetched exchange rates from {}, {} chars, took {}", COINPAPRIKA_URL, content.length(),
watch);

return rates;
}
} else {
log.warn("http status {} when fetching exchange rates from {}", response.code(), COINGECKO_URL);
log.warn("http status {} when fetching exchange rates from {}", response.code(), COINPAPRIKA_URL);
}
} catch (final Exception x) {
log.warn("problem fetching exchange rates from " + COINGECKO_URL, x);
log.warn("problem fetching exchange rates from " + COINPAPRIKA_URL, x);
}

return null;
}

Expand All @@ -296,8 +289,7 @@ private static Fiat parseFiatInexact(final String currencyCode, final String str
return Fiat.valueOf(currencyCode, val);
}

private static String COINGECKO_URL = "https://api.coingecko.com/api/v3/coins/ferrite?localization=false&community_data=false&developer_data=false&sparkline=false";
private static final String COINGECKO_SOURCE = "CoinGecko.com";

private static final String COINPAPRIKA_URL = "https://api.coinpaprika.com/v1/tickers/fec-ferrite";
private static final String COINPAPRIKA_SOURCE = "CoinPaprika.com";

}

0 comments on commit 0171d77

Please sign in to comment.