2017-09-26 03:41:11 +00:00
|
|
|
import { handleJSONResponse } from 'api/utils';
|
2018-01-22 04:21:48 +00:00
|
|
|
interface IRateSymbols {
|
|
|
|
symbols: {
|
|
|
|
all: TAllSymbols;
|
|
|
|
fiat: TFiatSymbols;
|
|
|
|
coinAndToken: TCoinAndTokenSymbols;
|
|
|
|
};
|
|
|
|
isFiat: isFiat;
|
|
|
|
}
|
|
|
|
|
|
|
|
type isFiat = (rate: string) => boolean;
|
|
|
|
|
|
|
|
export type TAllSymbols = (keyof ISymbol)[];
|
|
|
|
export type TFiatSymbols = (keyof IFiatSymbols)[];
|
|
|
|
export type TCoinAndTokenSymbols = (keyof ICoinAndTokenSymbols)[];
|
|
|
|
interface ISymbol {
|
|
|
|
USD: number;
|
|
|
|
EUR: number;
|
|
|
|
GBP: number;
|
|
|
|
CHF: number;
|
|
|
|
BTC: number;
|
|
|
|
ETH: number;
|
|
|
|
REP: number;
|
|
|
|
}
|
|
|
|
interface IFiatSymbols {
|
|
|
|
USD: number;
|
|
|
|
EUR: number;
|
|
|
|
GBP: number;
|
|
|
|
CHF: number;
|
|
|
|
}
|
|
|
|
interface ICoinAndTokenSymbols {
|
|
|
|
BTC: number;
|
|
|
|
ETH: number;
|
|
|
|
REP: number;
|
|
|
|
}
|
2017-09-26 03:41:11 +00:00
|
|
|
|
2018-01-22 04:21:48 +00:00
|
|
|
const fiat: TFiatSymbols = ['USD', 'EUR', 'GBP', 'CHF'];
|
|
|
|
const coinAndToken: TCoinAndTokenSymbols = ['BTC', 'ETH', 'REP'];
|
|
|
|
export const rateSymbols: IRateSymbols = {
|
|
|
|
symbols: {
|
|
|
|
all: [...fiat, ...coinAndToken],
|
|
|
|
fiat,
|
|
|
|
coinAndToken
|
|
|
|
},
|
|
|
|
isFiat: (rate: string) => (fiat as string[]).includes(rate)
|
|
|
|
};
|
2017-12-19 22:46:34 +00:00
|
|
|
|
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[]) => {
|
2018-01-22 04:21:48 +00:00
|
|
|
const tsyms = rateSymbols.symbols.all.concat(symbols as any).join(',');
|
2017-11-18 20:15:02 +00:00
|
|
|
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-12-19 22:46:34 +00:00
|
|
|
[symbol: string]: ISymbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IRates extends ISymbol {
|
|
|
|
Response?: 'Error';
|
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-12-19 22:46:34 +00:00
|
|
|
.then((rates: IRates) => {
|
2017-11-29 19:45:38 +00:00
|
|
|
// API errors come as 200s, so check the json for error
|
|
|
|
if (rates.Response && rates.Response === 'Error') {
|
|
|
|
throw new Error('Failed to fetch rates');
|
|
|
|
}
|
|
|
|
|
2017-11-18 20:15:02 +00:00
|
|
|
// 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(
|
2017-12-19 22:46:34 +00:00
|
|
|
(eqRates, sym: keyof ISymbol) => {
|
2017-12-01 20:40:27 +00:00
|
|
|
if (rates[sym]) {
|
2018-01-22 04:21:48 +00:00
|
|
|
eqRates[sym] = rateSymbols.symbols.all.reduce(
|
2017-12-19 22:46:34 +00:00
|
|
|
(symRates, rateSym) => {
|
|
|
|
symRates[rateSym] = 1 / rates[sym] * rates[rateSym];
|
|
|
|
return symRates;
|
|
|
|
},
|
|
|
|
{} as ISymbol
|
|
|
|
);
|
2017-12-01 20:40:27 +00:00
|
|
|
}
|
2017-11-18 20:15:02 +00:00
|
|
|
return eqRates;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ETH: {
|
|
|
|
USD: rates.USD,
|
|
|
|
EUR: rates.EUR,
|
|
|
|
GBP: rates.GBP,
|
|
|
|
CHF: rates.CHF,
|
2018-01-22 04:21:48 +00:00
|
|
|
BTC: rates.BTC,
|
|
|
|
ETH: 1,
|
|
|
|
REP: rates.REP
|
2017-11-18 20:15:02 +00:00
|
|
|
}
|
2017-12-19 22:46:34 +00:00
|
|
|
} as CCResponse
|
2017-11-18 20:15:02 +00:00
|
|
|
);
|
|
|
|
});
|