Skip to content

Commit

Permalink
Switch to coingecko.com API (#467)
Browse files Browse the repository at this point in the history
Fixes #464
  • Loading branch information
sindresorhus committed Aug 9, 2018
1 parent bce26d8 commit dbecfaf
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"@sindresorhus/slugify": "^0.3.0",
"coinlist": "^2.0.0",
"coinlist": "^3.0.0",
"crypto-random-string": "^1.0.0",
"del": "^3.0.0",
"delay": "^3.0.0",
Expand Down
61 changes: 27 additions & 34 deletions app/renderer/containers/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import coinlist from 'coinlist';
import roundTo from 'round-to';
import {Container} from 'unstated';
import {appViews, alwaysEnabledCurrencies, ignoreExternalPrice, hiddenCurrencies} from '../../constants';
import {getCurrencySymbols, getCurrencyName} from '../../marketmaker/supported-currencies';
import {getCurrencyName} from '../../marketmaker/supported-currencies';
import fireEvery from '../fire-every';
import {formatCurrency, setLoginWindowBounds} from '../util';
import {isDevelopment} from '../../util-common';
Expand All @@ -21,43 +21,36 @@ const excludedTestCurrencies = new Set([
'BEER',
]);

const getTickerData = async symbol => {
const fallback = {
symbol,
price: 0,
};
const getTickerData = async symbols => {
const baseURL = 'https://api.coingecko.com/api/v3';

if (ignoreExternalPrice.has(symbol)) {
return fallback;
}
const filteredSymbols = symbols.filter(symbol => !ignoreExternalPrice.has(symbol));

const id = coinlist.get(symbol, 'id');
if (!id) { // For example, SUPERNET
return fallback;
}
const ids = filteredSymbols
.map(symbol => coinlist.get(symbol, 'id'))
.filter(symbol => symbol !== undefined); // For example, SUPERNET

// Docs: https://coinmarketcap.com/api/
// Example: https://api.coinmarketcap.com/v2/ticker/99/
let response;
// Docs: https://www.coingecko.com/api/docs/v3#/coins/get_coins_markets
let data;
try {
response = await fetch(`https://api.coinmarketcap.com/v2/ticker/${id}/`);
} catch (_) {
return fallback;
const response = await fetch(`${baseURL}/coins/markets?vs_currency=usd&ids=${ids.join(',')}`);
data = await response.json();
} catch (error) {
console.error('Failed to fetch from CoinGecko API:', error);
data = [];
}

const json = await response.json();

if (json.metadata.error) {
return fallback;
}
// The API just ignores IDs it doesn't support, so we need to iterate
// our list of symbols instead of the result so we can add the fallbacks.
const currencies = symbols.map(symbol => {
const currency = data.find(currency => currency.symbol.toUpperCase() === symbol);
return {
symbol,
price: currency ? currency.current_price : 0,
};
});

const {data} = json;
const quotes = data.quotes.USD;
return {
symbol,
price: quotes.price,
percentChange24h: quotes.percent_change_24h,
};
return currencies;
};

class AppContainer extends Container {
Expand Down Expand Up @@ -135,9 +128,9 @@ class AppContainer extends Container {
}));
}

async watchCMC() {
await fireEvery({minutes: 5}, async () => {
this.coinPrices = await Promise.all(getCurrencySymbols().map(getTickerData));
async watchFiatPrice() {
await fireEvery({minutes: 1}, async () => {
this.coinPrices = await getTickerData(this.state.enabledCoins);
});
}

Expand Down
2 changes: 1 addition & 1 deletion app/renderer/containers/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class LoginContainer extends Container {
await api.enableCurrency('ETOMIC');
await Promise.all(appContainer.state.enabledCoins.map(x => api.enableCurrency(x)));

await appContainer.watchCMC();
await appContainer.watchFiatPrice();
await appContainer.watchCurrencies();

// We have to use dynamic import here as Webpack is unable to resolve circular
Expand Down
6 changes: 3 additions & 3 deletions app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ clean-stack@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/clean-stack/-/clean-stack-1.3.0.tgz#9e821501ae979986c46b1d66d2d432db2fd4ae31"

coinlist@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/coinlist/-/coinlist-2.0.0.tgz#6d0e8ed255e06610cb47e5cd5661e804842afef3"
coinlist@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/coinlist/-/coinlist-3.0.0.tgz#cdb6cd3c7d0ce1cf86a48217bd66ac479c75ba6f"

color-convert@^1.9.0, color-convert@^1.9.1:
version "1.9.1"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@sindresorhus/slugify": "^0.3.0",
"@sindresorhus/to-milliseconds": "^1.0.0",
"bip39": "^2.5.0",
"coinlist": "^2.0.0",
"coinlist": "^3.0.0",
"crypto-hash": "^0.1.0",
"crypto-pouch": "^3.1.3",
"csv-string": "^3.1.3",
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2014,9 +2014,9 @@ code-point-at@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"

coinlist@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/coinlist/-/coinlist-2.0.0.tgz#6d0e8ed255e06610cb47e5cd5661e804842afef3"
coinlist@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/coinlist/-/coinlist-3.0.0.tgz#cdb6cd3c7d0ce1cf86a48217bd66ac479c75ba6f"

collapse-white-space@^1.0.2:
version "1.0.4"
Expand Down

0 comments on commit dbecfaf

Please sign in to comment.