mirror of https://github.com/trezor/trezor-wallet
parent
e1d2852faa
commit
52505cb31a
@ -0,0 +1,60 @@
|
|||||||
|
/* @flow */
|
||||||
|
|
||||||
|
import { httpRequest } from 'utils/networkUtils';
|
||||||
|
import { resolveAfter } from 'utils/promiseUtils';
|
||||||
|
import { READY } from 'actions/constants/localStorage';
|
||||||
|
|
||||||
|
import type {
|
||||||
|
Middleware,
|
||||||
|
MiddlewareAPI,
|
||||||
|
MiddlewareDispatch,
|
||||||
|
Dispatch,
|
||||||
|
Action,
|
||||||
|
AsyncAction,
|
||||||
|
GetState,
|
||||||
|
} from 'flowtype';
|
||||||
|
|
||||||
|
export const RATE_UPDATE: 'rate__update' = 'rate__update';
|
||||||
|
|
||||||
|
export type FiatRateAction = {
|
||||||
|
type: typeof RATE_UPDATE;
|
||||||
|
network: string;
|
||||||
|
rate: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
const loadRateAction = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||||
|
const { config } = getState().localStorage;
|
||||||
|
if (!config) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
config.fiatValueTickers.forEach(async (ticker) => {
|
||||||
|
const response: ?Array<any> = await httpRequest(`${ticker.url}?tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false`, 'json');
|
||||||
|
if (response) {
|
||||||
|
console.log({
|
||||||
|
type: RATE_UPDATE,
|
||||||
|
network: response.symbol,
|
||||||
|
rate: response.market_data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
|
||||||
|
await resolveAfter(50000);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Middleware
|
||||||
|
*/
|
||||||
|
const TickerService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => {
|
||||||
|
next(action);
|
||||||
|
|
||||||
|
if (action.type === READY) {
|
||||||
|
api.dispatch(loadRateAction());
|
||||||
|
}
|
||||||
|
|
||||||
|
return action;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default TickerService;
|
Loading…
Reference in new issue