// @flow
import { convert } from 'emerald-js';
import BigNumber from 'bignumber.js';
import { intervalRates } from '../../store/config';
import createLogger from '../../utils/logger';
import ActionTypes from './actionTypes';
import history from '../wallet/history';

const log = createLogger('networkActions');

export function switchChain({ chain, chainId }) {
  return (dispatch, getState) => {
    dispatch({
      type: ActionTypes.SWITCH_CHAIN,
      chain,
      chainId,
    });
  };
}

export function loadHeight(watch) {
  return (dispatch, getState, api) => {
    return api.geth.eth.getBlockNumber().then((result) => {
      dispatch({
        type: ActionTypes.BLOCK,
        height: result,
      });
    });
  };
}

export function loadPeerCount() {
  return (dispatch, getState, api) => {
    return api.geth.net.peerCount().then((result) => {
      dispatch({
        type: ActionTypes.PEER_COUNT,
        peerCount: result,
      });
    });
  };
}

export function loadAddressTransactions(...props) {
  return (dispatch, getState, api) => {
    return api.geth.eth.getAddressTransactions(...props).then((results) => {
      if (results.length === 0) { return; }

      const trackedTxs = getState().wallet.history.get('trackedTransactions');
      const isAlreadyTracked = trackedTxs.find((tx) => results.indexOf(tx.get('hash')) !== -1);
      if (isAlreadyTracked) { return null; }

      return api.geth.ext.getTransactions(results).then((txes) => {
        return dispatch(history.actions.trackTxs(txes.map((tx) => tx.result)));
      });
    }).catch((e) => {
      log.error(e);
    });
  };
}

export function loadSyncing() {
  return (dispatch, getState, api) => {
    return api.geth.eth.getSyncing().then((result) => {
      if (typeof result === 'object') {
        return dispatch({
          type: ActionTypes.SYNCING,
          syncing: true,
          status: result,
        });
      }

      dispatch({
        type: ActionTypes.SYNCING,
        syncing: false,
      });
    });
  };
}

export function getGasPrice() {
  return (dispatch, getState, api) => {
    return api.geth.eth.gasPrice().then((result) => {
      dispatch({
        type: ActionTypes.GAS_PRICE,
        value: result,
      });
    });
  };
}

export function estimateGas(from: string, to: string, gas: string, gasPrice: string, value: string, data: string) {
  return (dispatch, getState, api) => {
    return api.geth.eth.estimateGas({
      from,
      to,
      gas,
      gasPrice,
      value,
      data,
    });
  };
}

@@ -305,16 +305,131 @@
 ons');%0A%0A
+const handleFailedToFetch = (err) =%3E %7B%0A  if (err.message.includes('Failed to fetch')) %7B return; %7D%0A  throw err;%0A%7D;%0A%0A
 export f
@@ -820,32 +820,59 @@
       %7D);%0A    %7D)
+.catch(handleFailedToFetch)
 ;%0A  %7D;%0A%7D%0A%0Aexport
@@ -1084,32 +1084,59 @@
       %7D);%0A    %7D)
+.catch(handleFailedToFetch)
 ;%0A  %7D;%0A%7D%0A%0Aexport
@@ -2167,32 +2167,59 @@
       %7D);%0A    %7D)
+.catch(handleFailedToFetch)
 ;%0A  %7D;%0A%7D%0A%0Aexport
@@ -2425,22 +2425,49 @@
     %7D);%0A
-
     %7D)
+.catch(handleFailedToFetch)
 ;%0A  %7D;%0A%7D

fix failed to fetch errors