Added coingecko api

pull/287/head
Vladimir Volek 6 years ago committed by Szymon Lesisz
parent e1d2852faa
commit 52505cb31a

@ -59,11 +59,11 @@
"fiatValueTickers": [ "fiatValueTickers": [
{ {
"network": "eth", "network": "eth",
"url": "https://api.coinmarketcap.com/v1/ticker/ethereum/" "url": "https://api.coingecko.com/api/v3/coins/ethereum"
}, },
{ {
"network": "etc", "network": "etc",
"url": "https://api.coinmarketcap.com/v1/ticker/ethereum-classic/" "url": "https://api.coingecko.com/api/v3/coins/ethereum-classic"
} }
], ],

@ -31,7 +31,7 @@ import type { TokenAction } from 'actions/TokenActions';
import type { TrezorConnectAction } from 'actions/TrezorConnectActions'; import type { TrezorConnectAction } from 'actions/TrezorConnectActions';
import type { WalletAction } from 'actions/WalletActions'; import type { WalletAction } from 'actions/WalletActions';
import type { Web3Action } from 'actions/Web3Actions'; import type { Web3Action } from 'actions/Web3Actions';
import type { FiatRateAction } from 'services/CoinmarketcapService'; // this service has no action file, all is written inside one file import type { FiatRateAction } from 'services/TickerService'; // this service has no action file, all is written inside one file
import type { import type {
Device, Device,

@ -1,9 +1,9 @@
/* @flow */ /* @flow */
import { RATE_UPDATE } from 'services/CoinmarketcapService'; import { RATE_UPDATE } from 'services/TickerService';
import type { Action } from 'flowtype'; import type { Action } from 'flowtype';
import type { FiatRateAction } from 'services/CoinmarketcapService'; import type { FiatRateAction } from 'services/TickerService';
export type Fiat = { export type Fiat = {
+network: string; +network: string;
@ -14,15 +14,11 @@ export const initialState: Array<Fiat> = [];
const update = (state: Array<Fiat>, action: FiatRateAction): Array<Fiat> => { const update = (state: Array<Fiat>, action: FiatRateAction): Array<Fiat> => {
const newState: Array<Fiat> = [...state]; const newState: Array<Fiat> = [...state];
const exists: ?Fiat = newState.find(f => f.network === action.network); newState.push({
if (exists) { network: action.network,
exists.value = action.rate.price_usd; value: action.rate.current_price.usd,
} else { });
newState.push({ console.log('newState', newState);
network: action.network,
value: action.rate.price_usd,
});
}
return newState; return newState;
}; };

@ -30,6 +30,7 @@ const loadRateAction = (): AsyncAction => async (dispatch: Dispatch, getState: G
config.fiatValueTickers.forEach(async (ticker) => { config.fiatValueTickers.forEach(async (ticker) => {
// const rate: ?Array<any> = await JSONRequest(`${ticker.url}?convert=USD`, 'json'); // const rate: ?Array<any> = await JSONRequest(`${ticker.url}?convert=USD`, 'json');
const rate: ?Array<any> = await httpRequest(`${ticker.url}?convert=USD`, 'json'); const rate: ?Array<any> = await httpRequest(`${ticker.url}?convert=USD`, 'json');
console.log('rate');
if (rate) { if (rate) {
dispatch({ dispatch({
type: RATE_UPDATE, type: RATE_UPDATE,

@ -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;

@ -2,7 +2,7 @@ import WalletService from './WalletService';
import LogService from './LogService'; import LogService from './LogService';
import RouterService from './RouterService'; import RouterService from './RouterService';
import LocalStorageService from './LocalStorageService'; import LocalStorageService from './LocalStorageService';
import CoinmarketcapService from './CoinmarketcapService'; import TickerService from './TickerService';
import TrezorConnectService from './TrezorConnectService'; import TrezorConnectService from './TrezorConnectService';
export default [ export default [
@ -11,5 +11,5 @@ export default [
RouterService, RouterService,
LocalStorageService, LocalStorageService,
TrezorConnectService, TrezorConnectService,
CoinmarketcapService, TickerService,
]; ];
Loading…
Cancel
Save