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.';
|
2018-05-02 02:13:38 +00:00
|
|
|
const CCApi = 'https://proxy.mycryptoapi.com/cc';
|
2017-09-26 03:41:11 +00:00
|
|
|
|
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(',');
|
2018-05-02 02:13:38 +00:00
|
|
|
return `${CCApi}/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;
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:19:14 +00:00
|
|
|
interface IRatesResponse {
|
|
|
|
[key: string]: number;
|
|
|
|
}
|
|
|
|
interface IRatesError {
|
|
|
|
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))
|
2018-04-05 21:19:14 +00:00
|
|
|
.then((rates: IRatesResponse | IRatesError) => {
|
2017-11-29 19:45:38 +00:00
|
|
|
// API errors come as 200s, so check the json for error
|
2018-04-05 21:19:14 +00:00
|
|
|
if ((rates as IRatesError).Response === 'Error') {
|
2017-11-29 19:45:38 +00:00
|
|
|
throw new Error('Failed to fetch rates');
|
|
|
|
}
|
2018-04-05 21:19:14 +00:00
|
|
|
return rates;
|
|
|
|
})
|
|
|
|
.then((rates: IRatesResponse) => {
|
|
|
|
// Sometimes the API erroneously gives tokens an extremely high value,
|
|
|
|
// like 10000000 ETH to 1 token. Filter those out. If that ever turns
|
|
|
|
// out to be true, we should all go home.
|
|
|
|
return Object.keys(rates).reduce((filteredRates: IRatesResponse, key) => {
|
|
|
|
if (rates[key] > 0.000001) {
|
|
|
|
filteredRates[key] = rates[key];
|
|
|
|
}
|
|
|
|
return filteredRates;
|
|
|
|
}, {});
|
|
|
|
})
|
|
|
|
.then((rates: IRatesResponse) => {
|
2017-11-18 20:15:02 +00:00
|
|
|
// All currencies are in ETH right now. We'll do token -> eth -> value to
|
2018-04-05 21:19:14 +00:00
|
|
|
// do it all in one request to their respective rates via ETH.
|
2017-11-18 20:15:02 +00:00
|
|
|
return symbols.reduce(
|
2018-04-05 21:19:14 +00:00
|
|
|
(eqRates, sym) => {
|
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
|
|
|
);
|
|
|
|
});
|