You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/services/CoinmarketcapService.js

63 lines
1.6 KiB

6 years ago
/* @flow */
import { JSONRequest, httpRequest } from 'utils/networkUtils';
import { resolveAfter } from 'utils/promiseUtils';
import { READY } from 'actions/constants/localStorage';
6 years ago
import type {
Middleware,
MiddlewareAPI,
MiddlewareDispatch,
State,
Dispatch,
Action,
AsyncAction,
GetState,
} from 'flowtype';
import type { Config, FiatValueTicker } from 'reducers/LocalStorageReducer';
export const RATE_UPDATE: 'rate__update' = 'rate__update';
export type FiatRateAction = {
type: typeof RATE_UPDATE;
network: string;
rate: any;
}
6 years ago
const loadRateAction = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const config: ?Config = getState().localStorage.config;
if (!config) return;
try {
config.fiatValueTickers.forEach(async (ticker: FiatValueTicker) => {
// const rate: ?Array<any> = await JSONRequest(`${ticker.url}?convert=USD`, 'json');
const rate: ?Array<any> = await httpRequest(`${ticker.url}?convert=USD`, 'json');
if (rate) {
dispatch({
type: RATE_UPDATE,
network: ticker.network,
rate: rate[0],
});
}
});
} catch (error) {
// ignore error
6 years ago
}
await resolveAfter(50000);
};
6 years ago
/**
* Middleware
6 years ago
*/
const CoinmarketcapService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => {
6 years ago
next(action);
if (action.type === READY) {
api.dispatch(loadRateAction());
6 years ago
}
return action;
6 years ago
};
export default CoinmarketcapService;