2017-09-26 03:41:11 +00:00
|
|
|
import { handleJSONResponse } from 'api/utils';
|
|
|
|
|
2017-11-15 05:51:09 +00:00
|
|
|
export const rateSymbols = ['USD', 'EUR', 'GBP', 'BTC', 'CHF', 'REP', 'ETH'];
|
2017-09-26 03:41:11 +00:00
|
|
|
// TODO - internationalize
|
|
|
|
const ERROR_MESSAGE = 'Could not fetch rate data.';
|
|
|
|
const CCApi = 'https://min-api.cryptocompare.com';
|
|
|
|
|
2017-11-18 20:15:02 +00:00
|
|
|
const CCRates = (symbols: string[]) => {
|
|
|
|
const tsyms = rateSymbols.concat(symbols).join(',');
|
|
|
|
return `${CCApi}/data/price?fsym=ETH&tsyms=${tsyms}`;
|
2017-11-15 05:51:09 +00:00
|
|
|
};
|
2017-09-26 03:41:11 +00:00
|
|
|
|
|
|
|
export interface CCResponse {
|
2017-11-18 20:15:02 +00:00
|
|
|
[symbol: string]: {
|
|
|
|
USD: number;
|
2017-11-15 05:51:09 +00:00
|
|
|
EUR: number;
|
|
|
|
GBP: number;
|
2017-11-18 20:15:02 +00:00
|
|
|
BTC: number;
|
2017-11-15 05:51:09 +00:00
|
|
|
CHF: number;
|
|
|
|
REP: number;
|
|
|
|
ETH: number;
|
|
|
|
};
|
2017-09-26 03:41:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 20:15:02 +00:00
|
|
|
export const fetchRates = (symbols: string[] = []): Promise<CCResponse> =>
|
|
|
|
fetch(CCRates(symbols))
|
2017-11-15 05:51:09 +00:00
|
|
|
.then(response => handleJSONResponse(response, ERROR_MESSAGE))
|
2017-11-18 20:15:02 +00:00
|
|
|
.then(rates => {
|
|
|
|
// All currencies are in ETH right now. We'll do token -> eth -> value to
|
|
|
|
// do it all in one request
|
|
|
|
// to their respective rates via ETH.
|
|
|
|
return symbols.reduce(
|
|
|
|
(eqRates, sym) => {
|
|
|
|
eqRates[sym] = rateSymbols.reduce((symRates, rateSym) => {
|
|
|
|
symRates[rateSym] = 1 / rates[sym] * rates[rateSym];
|
|
|
|
return symRates;
|
|
|
|
}, {});
|
|
|
|
return eqRates;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ETH: {
|
|
|
|
USD: rates.USD,
|
|
|
|
EUR: rates.EUR,
|
|
|
|
GBP: rates.GBP,
|
|
|
|
BTC: rates.BTC,
|
|
|
|
CHF: rates.CHF,
|
|
|
|
REP: rates.REP,
|
|
|
|
ETH: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|