2017-10-11 05:04:49 +00:00
|
|
|
|
import { delay, SagaIterator } from 'redux-saga';
|
|
|
|
|
import {
|
|
|
|
|
call,
|
|
|
|
|
cancel,
|
|
|
|
|
fork,
|
|
|
|
|
put,
|
|
|
|
|
take,
|
|
|
|
|
takeLatest,
|
|
|
|
|
takeEvery,
|
2017-11-18 20:33:53 +00:00
|
|
|
|
select,
|
|
|
|
|
race
|
2017-10-11 05:04:49 +00:00
|
|
|
|
} from 'redux-saga/effects';
|
2017-10-04 04:37:06 +00:00
|
|
|
|
import { NODES } from 'config/data';
|
2017-11-18 20:33:53 +00:00
|
|
|
|
import {
|
|
|
|
|
makeCustomNodeId,
|
|
|
|
|
getCustomNodeConfigFromId,
|
|
|
|
|
makeNodeConfigFromCustomConfig
|
|
|
|
|
} from 'utils/node';
|
2017-12-01 16:09:51 +00:00
|
|
|
|
import { makeCustomNetworkId } from 'utils/network';
|
2017-11-29 00:17:26 +00:00
|
|
|
|
import {
|
|
|
|
|
getNode,
|
|
|
|
|
getNodeConfig,
|
|
|
|
|
getCustomNodeConfigs,
|
2017-12-01 16:09:51 +00:00
|
|
|
|
getCustomNetworkConfigs,
|
2017-11-29 00:17:26 +00:00
|
|
|
|
getOffline,
|
|
|
|
|
getForceOffline
|
|
|
|
|
} from 'selectors/config';
|
2017-10-11 05:04:49 +00:00
|
|
|
|
import { AppState } from 'reducers';
|
|
|
|
|
import { TypeKeys } from 'actions/config/constants';
|
2017-11-10 03:30:20 +00:00
|
|
|
|
import {
|
|
|
|
|
toggleOfflineConfig,
|
|
|
|
|
changeNode,
|
2017-11-18 20:33:53 +00:00
|
|
|
|
changeNodeIntent,
|
|
|
|
|
setLatestBlock,
|
2017-12-01 16:09:51 +00:00
|
|
|
|
removeCustomNetwork,
|
2017-11-30 04:07:16 +00:00
|
|
|
|
AddCustomNodeAction,
|
|
|
|
|
ChangeNodeIntentAction
|
2017-11-10 03:30:20 +00:00
|
|
|
|
} from 'actions/config';
|
2017-11-18 20:33:53 +00:00
|
|
|
|
import { showNotification } from 'actions/notifications';
|
|
|
|
|
import translate from 'translations';
|
|
|
|
|
import { Web3Wallet } from 'libs/wallet';
|
|
|
|
|
import { getWalletInst } from 'selectors/wallet';
|
|
|
|
|
import { TypeKeys as WalletTypeKeys } from 'actions/wallet/constants';
|
2017-11-12 19:45:52 +00:00
|
|
|
|
import {
|
|
|
|
|
State as ConfigState,
|
|
|
|
|
INITIAL_STATE as configInitialState
|
|
|
|
|
} from 'reducers/config';
|
2017-10-11 05:04:49 +00:00
|
|
|
|
|
|
|
|
|
export const getConfig = (state: AppState): ConfigState => state.config;
|
|
|
|
|
|
2017-11-29 00:17:26 +00:00
|
|
|
|
let hasCheckedOnline = false;
|
2017-10-11 05:04:49 +00:00
|
|
|
|
export function* pollOfflineStatus(): SagaIterator {
|
|
|
|
|
while (true) {
|
2017-11-29 00:17:26 +00:00
|
|
|
|
const node = yield select(getNodeConfig);
|
|
|
|
|
const isOffline = yield select(getOffline);
|
|
|
|
|
const isForcedOffline = yield select(getForceOffline);
|
|
|
|
|
|
|
|
|
|
// If they're forcing themselves offline, exit the loop. It will be
|
|
|
|
|
// kicked off again if they toggle it in handleTogglePollOfflineStatus.
|
|
|
|
|
if (isForcedOffline) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If our offline state disagrees with the browser, run a check
|
|
|
|
|
// Don't check if the user is in another tab or window
|
|
|
|
|
const shouldPing = !hasCheckedOnline || navigator.onLine === isOffline;
|
|
|
|
|
if (shouldPing && !document.hidden) {
|
|
|
|
|
hasCheckedOnline = true;
|
|
|
|
|
const { pingSucceeded } = yield race({
|
|
|
|
|
pingSucceeded: call(node.lib.ping.bind(node.lib)),
|
|
|
|
|
timeout: call(delay, 5000)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (pingSucceeded && isOffline) {
|
|
|
|
|
// If we were able to ping but redux says we're offline, mark online
|
|
|
|
|
yield put(
|
|
|
|
|
showNotification(
|
|
|
|
|
'success',
|
|
|
|
|
'Your connection to the network has been restored!',
|
|
|
|
|
3000
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
yield put(toggleOfflineConfig());
|
|
|
|
|
} else if (!pingSucceeded && !isOffline) {
|
|
|
|
|
// If we were unable to ping but redux says we're online, mark offline
|
|
|
|
|
yield put(
|
|
|
|
|
showNotification(
|
|
|
|
|
'danger',
|
|
|
|
|
`You’ve lost your connection to the network, check your internet
|
|
|
|
|
connection or try changing networks from the dropdown at the
|
|
|
|
|
top right of the page.`,
|
|
|
|
|
Infinity
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
yield put(toggleOfflineConfig());
|
|
|
|
|
} else {
|
|
|
|
|
// If neither case was true, try again in 5s
|
|
|
|
|
yield call(delay, 5000);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
yield call(delay, 1000);
|
2017-10-11 05:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fork our recurring API call, watch for the need to cancel.
|
2017-11-30 04:07:16 +00:00
|
|
|
|
export function* handlePollOfflineStatus(): SagaIterator {
|
2017-10-11 05:04:49 +00:00
|
|
|
|
const pollOfflineStatusTask = yield fork(pollOfflineStatus);
|
|
|
|
|
yield take('CONFIG_STOP_POLL_OFFLINE_STATE');
|
|
|
|
|
yield cancel(pollOfflineStatusTask);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
|
export function* handleTogglePollOfflineStatus(): SagaIterator {
|
2017-11-29 00:17:26 +00:00
|
|
|
|
const isForcedOffline = yield select(getForceOffline);
|
|
|
|
|
if (isForcedOffline) {
|
|
|
|
|
yield fork(handlePollOfflineStatus);
|
|
|
|
|
} else {
|
|
|
|
|
yield call(handlePollOfflineStatus);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 18:05:38 +00:00
|
|
|
|
// @HACK For now we reload the app when doing a language swap to force non-connected
|
|
|
|
|
// data to reload. Also the use of timeout to avoid using additional actions for now.
|
2017-11-30 04:07:16 +00:00
|
|
|
|
export function* reload(): SagaIterator {
|
2017-09-25 02:06:28 +00:00
|
|
|
|
setTimeout(() => location.reload(), 250);
|
2017-08-28 18:05:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
|
export function* handleNodeChangeIntent(
|
|
|
|
|
action: ChangeNodeIntentAction
|
|
|
|
|
): SagaIterator {
|
2017-11-18 20:33:53 +00:00
|
|
|
|
const currentNode = yield select(getNode);
|
|
|
|
|
const currentConfig = yield select(getNodeConfig);
|
|
|
|
|
const currentNetwork = currentConfig.network;
|
2017-11-10 03:30:20 +00:00
|
|
|
|
|
2017-11-18 20:33:53 +00:00
|
|
|
|
let actionConfig = NODES[action.payload];
|
|
|
|
|
if (!actionConfig) {
|
|
|
|
|
const customConfigs = yield select(getCustomNodeConfigs);
|
|
|
|
|
const config = getCustomNodeConfigFromId(action.payload, customConfigs);
|
|
|
|
|
if (config) {
|
|
|
|
|
actionConfig = makeNodeConfigFromCustomConfig(config);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!actionConfig) {
|
|
|
|
|
yield put(
|
|
|
|
|
showNotification(
|
|
|
|
|
'danger',
|
|
|
|
|
`Attempted to switch to unknown node '${action.payload}'`,
|
|
|
|
|
5000
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
yield put(changeNode(currentNode, currentConfig));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Grab latest block from the node, before switching, to confirm it's online
|
|
|
|
|
// Give it 5 seconds before we call it offline
|
|
|
|
|
let latestBlock;
|
|
|
|
|
let timeout;
|
|
|
|
|
try {
|
|
|
|
|
const { lb, to } = yield race({
|
|
|
|
|
lb: call(actionConfig.lib.getCurrentBlock.bind(actionConfig.lib)),
|
|
|
|
|
to: call(delay, 5000)
|
|
|
|
|
});
|
|
|
|
|
latestBlock = lb;
|
|
|
|
|
timeout = to;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Whether it times out or errors, same message
|
|
|
|
|
timeout = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timeout) {
|
|
|
|
|
yield put(showNotification('danger', translate('ERROR_32'), 5000));
|
|
|
|
|
yield put(changeNode(currentNode, currentConfig));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield put(setLatestBlock(latestBlock));
|
|
|
|
|
yield put(changeNode(action.payload, actionConfig));
|
2017-11-10 03:30:20 +00:00
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
|
const currentWallet = yield select(getWalletInst);
|
|
|
|
|
|
2017-11-10 03:30:20 +00:00
|
|
|
|
// if there's no wallet, do not reload as there's no component state to resync
|
2017-11-18 20:33:53 +00:00
|
|
|
|
if (currentWallet && currentNetwork !== actionConfig.network) {
|
2017-10-04 04:37:06 +00:00
|
|
|
|
yield call(reload);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-18 20:33:53 +00:00
|
|
|
|
export function* switchToNewNode(action: AddCustomNodeAction): SagaIterator {
|
|
|
|
|
const nodeId = makeCustomNodeId(action.payload);
|
|
|
|
|
yield put(changeNodeIntent(nodeId));
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 16:09:51 +00:00
|
|
|
|
// If there are any orphaned custom networks, purge them
|
|
|
|
|
export function* cleanCustomNetworks(): SagaIterator {
|
|
|
|
|
const customNodes = yield select(getCustomNodeConfigs);
|
|
|
|
|
const customNetworks = yield select(getCustomNetworkConfigs);
|
|
|
|
|
const networksInUse = customNodes.reduce((prev, conf) => {
|
|
|
|
|
prev[conf.network] = true;
|
|
|
|
|
return prev;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
for (const net of customNetworks) {
|
|
|
|
|
if (!networksInUse[makeCustomNetworkId(net)]) {
|
|
|
|
|
yield put(removeCustomNetwork(net));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 03:30:20 +00:00
|
|
|
|
// unset web3 as the selected node if a non-web3 wallet has been selected
|
2017-12-01 16:32:29 +00:00
|
|
|
|
export function* unsetWeb3NodeOnWalletEvent(action): SagaIterator {
|
2017-11-10 03:30:20 +00:00
|
|
|
|
const node = yield select(getNode);
|
|
|
|
|
const nodeConfig = yield select(getNodeConfig);
|
|
|
|
|
const newWallet = action.payload;
|
|
|
|
|
const isWeb3Wallet = newWallet instanceof Web3Wallet;
|
|
|
|
|
|
|
|
|
|
if (node !== 'web3' || isWeb3Wallet) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// switch back to a node with the same network as MetaMask/Mist
|
2017-11-30 04:07:16 +00:00
|
|
|
|
yield put(changeNodeIntent(equivalentNodeOrDefault(nodeConfig)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
|
export function* unsetWeb3Node(): SagaIterator {
|
|
|
|
|
const node = yield select(getNode);
|
|
|
|
|
|
|
|
|
|
if (node !== 'web3') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nodeConfig = yield select(getNodeConfig);
|
|
|
|
|
const newNode = equivalentNodeOrDefault(nodeConfig);
|
|
|
|
|
|
|
|
|
|
yield put(changeNodeIntent(newNode));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
|
export const equivalentNodeOrDefault = nodeConfig => {
|
|
|
|
|
const node = Object.keys(NODES)
|
2017-11-10 03:30:20 +00:00
|
|
|
|
.filter(key => key !== 'web3')
|
|
|
|
|
.reduce((found, key) => {
|
|
|
|
|
const config = NODES[key];
|
|
|
|
|
if (found.length) {
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
if (nodeConfig.network === config.network) {
|
|
|
|
|
return (found = key);
|
|
|
|
|
}
|
|
|
|
|
return found;
|
|
|
|
|
}, '');
|
|
|
|
|
|
|
|
|
|
// if no equivalent node was found, use the app default
|
2017-11-30 04:07:16 +00:00
|
|
|
|
return node.length ? node : configInitialState.nodeSelection;
|
|
|
|
|
};
|
2017-11-10 03:30:20 +00:00
|
|
|
|
|
2017-10-11 05:04:49 +00:00
|
|
|
|
export default function* configSaga(): SagaIterator {
|
|
|
|
|
yield takeLatest(
|
|
|
|
|
TypeKeys.CONFIG_POLL_OFFLINE_STATUS,
|
|
|
|
|
handlePollOfflineStatus
|
|
|
|
|
);
|
2017-11-29 00:17:26 +00:00
|
|
|
|
yield takeEvery(TypeKeys.CONFIG_FORCE_OFFLINE, handleTogglePollOfflineStatus);
|
2017-10-11 05:04:49 +00:00
|
|
|
|
yield takeEvery(TypeKeys.CONFIG_NODE_CHANGE_INTENT, handleNodeChangeIntent);
|
|
|
|
|
yield takeEvery(TypeKeys.CONFIG_LANGUAGE_CHANGE, reload);
|
2017-11-18 20:33:53 +00:00
|
|
|
|
yield takeEvery(TypeKeys.CONFIG_ADD_CUSTOM_NODE, switchToNewNode);
|
2017-12-01 16:09:51 +00:00
|
|
|
|
yield takeEvery(TypeKeys.CONFIG_REMOVE_CUSTOM_NODE, cleanCustomNetworks);
|
2017-12-01 16:32:29 +00:00
|
|
|
|
yield takeEvery(TypeKeys.CONFIG_NODE_WEB3_UNSET, unsetWeb3Node);
|
|
|
|
|
yield takeEvery(WalletTypeKeys.WALLET_SET, unsetWeb3NodeOnWalletEvent);
|
|
|
|
|
yield takeEvery(WalletTypeKeys.WALLET_RESET, unsetWeb3NodeOnWalletEvent);
|
2017-08-28 18:05:38 +00:00
|
|
|
|
}
|