2018-02-12 20:43:07 +00:00
|
|
|
|
import { delay, SagaIterator } from 'redux-saga';
|
|
|
|
|
import {
|
|
|
|
|
call,
|
|
|
|
|
cancel,
|
|
|
|
|
fork,
|
|
|
|
|
put,
|
|
|
|
|
take,
|
|
|
|
|
takeEvery,
|
|
|
|
|
select,
|
|
|
|
|
apply,
|
|
|
|
|
takeLatest
|
|
|
|
|
} from 'redux-saga/effects';
|
|
|
|
|
import {
|
|
|
|
|
getNodeId,
|
|
|
|
|
getNodeConfig,
|
|
|
|
|
getOffline,
|
|
|
|
|
isStaticNodeId,
|
|
|
|
|
getCustomNodeFromId,
|
|
|
|
|
getStaticNodeFromId,
|
|
|
|
|
getNetworkConfigById
|
|
|
|
|
} from 'selectors/config';
|
|
|
|
|
import { TypeKeys } from 'actions/config/constants';
|
|
|
|
|
import {
|
2018-04-11 02:53:27 +00:00
|
|
|
|
setOnline,
|
|
|
|
|
setOffline,
|
2018-02-12 20:43:07 +00:00
|
|
|
|
changeNode,
|
|
|
|
|
changeNodeIntent,
|
|
|
|
|
setLatestBlock,
|
|
|
|
|
AddCustomNodeAction,
|
2018-03-01 04:31:33 +00:00
|
|
|
|
ChangeNodeForceAction,
|
2018-04-12 23:17:46 +00:00
|
|
|
|
ChangeNodeIntentAction,
|
|
|
|
|
ChangeNodeIntentOneTimeAction
|
2018-02-12 20:43:07 +00:00
|
|
|
|
} from 'actions/config';
|
|
|
|
|
import { showNotification } from 'actions/notifications';
|
2018-02-25 01:32:34 +00:00
|
|
|
|
import { resetWallet } from 'actions/wallet';
|
2018-02-12 20:43:07 +00:00
|
|
|
|
import { translateRaw } from 'translations';
|
|
|
|
|
import { StaticNodeConfig, CustomNodeConfig, NodeConfig } from 'types/node';
|
|
|
|
|
import { CustomNetworkConfig, StaticNetworkConfig } from 'types/network';
|
2018-04-06 20:52:48 +00:00
|
|
|
|
import {
|
|
|
|
|
getShepherdOffline,
|
|
|
|
|
isAutoNode,
|
|
|
|
|
shepherd,
|
|
|
|
|
shepherdProvider,
|
|
|
|
|
stripWeb3Network,
|
2018-04-06 22:23:25 +00:00
|
|
|
|
makeProviderConfig,
|
2018-04-16 22:51:15 +00:00
|
|
|
|
getShepherdNetwork,
|
|
|
|
|
getShepherdPending
|
2018-04-06 20:52:48 +00:00
|
|
|
|
} from 'libs/nodes';
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
|
|
|
|
export function* pollOfflineStatus(): SagaIterator {
|
2018-04-06 20:52:48 +00:00
|
|
|
|
let hasCheckedOnline = false;
|
2018-04-11 02:53:27 +00:00
|
|
|
|
|
|
|
|
|
const restoreNotif = showNotification(
|
|
|
|
|
'success',
|
|
|
|
|
'Your connection to the network has been restored!',
|
|
|
|
|
3000
|
|
|
|
|
);
|
|
|
|
|
const lostNetworkNotif = 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
|
|
|
|
|
);
|
|
|
|
|
const offlineNotif = showNotification(
|
|
|
|
|
'info',
|
|
|
|
|
'You are currently offline. Some features will be unavailable.',
|
|
|
|
|
5000
|
|
|
|
|
);
|
|
|
|
|
|
2018-02-12 20:43:07 +00:00
|
|
|
|
while (true) {
|
2018-04-11 02:53:27 +00:00
|
|
|
|
yield call(delay, 2500);
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
2018-04-16 22:51:15 +00:00
|
|
|
|
const pending: ReturnType<typeof getShepherdPending> = yield call(getShepherdPending);
|
|
|
|
|
if (pending) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 02:53:27 +00:00
|
|
|
|
const isOffline: boolean = yield select(getOffline);
|
|
|
|
|
const balancerOffline = yield call(getShepherdOffline);
|
2018-04-16 22:51:15 +00:00
|
|
|
|
|
2018-04-11 02:53:27 +00:00
|
|
|
|
if (!balancerOffline && isOffline) {
|
|
|
|
|
// If we were able to ping but redux says we're offline, mark online
|
|
|
|
|
yield put(restoreNotif);
|
|
|
|
|
yield put(setOnline());
|
|
|
|
|
} else if (balancerOffline && !isOffline) {
|
|
|
|
|
// If we were unable to ping but redux says we're online, mark offline
|
|
|
|
|
// If they had been online, show an error.
|
|
|
|
|
// If they hadn't been online, just inform them with a warning.
|
|
|
|
|
yield put(setOffline());
|
|
|
|
|
if (hasCheckedOnline) {
|
|
|
|
|
yield put(lostNetworkNotif);
|
2018-02-12 20:43:07 +00:00
|
|
|
|
} else {
|
2018-04-11 02:53:27 +00:00
|
|
|
|
yield put(offlineNotif);
|
2018-02-12 20:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-11 02:53:27 +00:00
|
|
|
|
hasCheckedOnline = true;
|
2018-02-12 20:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fork our recurring API call, watch for the need to cancel.
|
|
|
|
|
export function* handlePollOfflineStatus(): SagaIterator {
|
|
|
|
|
const pollOfflineStatusTask = yield fork(pollOfflineStatus);
|
|
|
|
|
yield take('CONFIG_STOP_POLL_OFFLINE_STATE');
|
|
|
|
|
yield cancel(pollOfflineStatusTask);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @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.
|
|
|
|
|
export function* reload(): SagaIterator {
|
|
|
|
|
setTimeout(() => location.reload(), 1150);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-12 23:17:46 +00:00
|
|
|
|
export function* handleNodeChangeIntentOneTime(): SagaIterator {
|
|
|
|
|
const action: ChangeNodeIntentOneTimeAction = yield take(
|
|
|
|
|
TypeKeys.CONFIG_NODE_CHANGE_INTENT_ONETIME
|
|
|
|
|
);
|
|
|
|
|
// allow shepherdProvider async init to complete. TODO - don't export shepherdProvider as promise
|
|
|
|
|
yield call(delay, 100);
|
|
|
|
|
yield put(changeNodeIntent(action.payload));
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 20:43:07 +00:00
|
|
|
|
export function* handleNodeChangeIntent({
|
|
|
|
|
payload: nodeIdToSwitchTo
|
|
|
|
|
}: ChangeNodeIntentAction): SagaIterator {
|
|
|
|
|
const isStaticNode: boolean = yield select(isStaticNodeId, nodeIdToSwitchTo);
|
|
|
|
|
const currentConfig: NodeConfig = yield select(getNodeConfig);
|
|
|
|
|
|
|
|
|
|
function* bailOut(message: string) {
|
|
|
|
|
const currentNodeId: string = yield select(getNodeId);
|
|
|
|
|
yield put(showNotification('danger', message, 5000));
|
|
|
|
|
yield put(changeNode({ networkId: currentConfig.network, nodeId: currentNodeId }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let nextNodeConfig: CustomNodeConfig | StaticNodeConfig;
|
|
|
|
|
|
|
|
|
|
if (!isStaticNode) {
|
|
|
|
|
const config: CustomNodeConfig | undefined = yield select(
|
|
|
|
|
getCustomNodeFromId,
|
|
|
|
|
nodeIdToSwitchTo
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (config) {
|
|
|
|
|
nextNodeConfig = config;
|
|
|
|
|
} else {
|
|
|
|
|
return yield* bailOut(`Attempted to switch to unknown node '${nodeIdToSwitchTo}'`);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
nextNodeConfig = yield select(getStaticNodeFromId, nodeIdToSwitchTo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextNetwork: StaticNetworkConfig | CustomNetworkConfig = yield select(
|
|
|
|
|
getNetworkConfigById,
|
2018-04-06 20:52:48 +00:00
|
|
|
|
stripWeb3Network(nextNodeConfig.network)
|
2018-02-12 20:43:07 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!nextNetwork) {
|
|
|
|
|
return yield* bailOut(
|
|
|
|
|
`Unknown custom network for your node '${nodeIdToSwitchTo}', try re-adding it`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-06 20:52:48 +00:00
|
|
|
|
if (isAutoNode(nodeIdToSwitchTo)) {
|
|
|
|
|
shepherd.auto();
|
2018-04-06 22:23:25 +00:00
|
|
|
|
if (getShepherdNetwork() !== nextNodeConfig.network) {
|
2018-04-06 20:52:48 +00:00
|
|
|
|
yield apply(shepherd, shepherd.switchNetworks, [nextNodeConfig.network]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
yield apply(shepherd, shepherd.manual, [nodeIdToSwitchTo, false]);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return yield* bailOut(translateRaw('ERROR_32'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let currentBlock;
|
|
|
|
|
try {
|
|
|
|
|
currentBlock = yield apply(shepherdProvider, shepherdProvider.getCurrentBlock);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return yield* bailOut(translateRaw('ERROR_32'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 20:43:07 +00:00
|
|
|
|
yield put(setLatestBlock(currentBlock));
|
|
|
|
|
yield put(changeNode({ networkId: nextNodeConfig.network, nodeId: nodeIdToSwitchTo }));
|
|
|
|
|
|
2018-02-25 01:32:34 +00:00
|
|
|
|
if (currentConfig.network !== nextNodeConfig.network) {
|
|
|
|
|
yield fork(handleNewNetwork);
|
2018-02-12 20:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-06 20:52:48 +00:00
|
|
|
|
export function* handleAddCustomNode(action: AddCustomNodeAction): SagaIterator {
|
|
|
|
|
const { payload: { config } } = action;
|
|
|
|
|
shepherd.useProvider(
|
|
|
|
|
'myccustom',
|
|
|
|
|
config.id,
|
|
|
|
|
makeProviderConfig({ network: config.network }),
|
|
|
|
|
config
|
|
|
|
|
);
|
2018-02-12 20:43:07 +00:00
|
|
|
|
yield put(changeNodeIntent(action.payload.id));
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-25 01:32:34 +00:00
|
|
|
|
export function* handleNewNetwork() {
|
|
|
|
|
yield put(resetWallet());
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-01 04:31:33 +00:00
|
|
|
|
export function* handleNodeChangeForce({ payload: staticNodeIdToSwitchTo }: ChangeNodeForceAction) {
|
|
|
|
|
// does not perform node online check before changing nodes
|
|
|
|
|
// necessary when switching back from Web3 provider so node
|
|
|
|
|
// dropdown does not get stuck if node is offline
|
|
|
|
|
|
|
|
|
|
const isStaticNode: boolean = yield select(isStaticNodeId, staticNodeIdToSwitchTo);
|
|
|
|
|
|
|
|
|
|
if (!isStaticNode) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nodeConfig = yield select(getStaticNodeFromId, staticNodeIdToSwitchTo);
|
|
|
|
|
|
|
|
|
|
// force the node change
|
|
|
|
|
yield put(changeNode({ networkId: nodeConfig.network, nodeId: staticNodeIdToSwitchTo }));
|
|
|
|
|
|
|
|
|
|
// also put the change through as usual so status check and
|
|
|
|
|
// error messages occur if the node is unavailable
|
|
|
|
|
yield put(changeNodeIntent(staticNodeIdToSwitchTo));
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 20:43:07 +00:00
|
|
|
|
export const node = [
|
2018-04-12 23:17:46 +00:00
|
|
|
|
fork(handleNodeChangeIntentOneTime),
|
2018-02-12 20:43:07 +00:00
|
|
|
|
takeEvery(TypeKeys.CONFIG_NODE_CHANGE_INTENT, handleNodeChangeIntent),
|
2018-03-01 04:31:33 +00:00
|
|
|
|
takeEvery(TypeKeys.CONFIG_NODE_CHANGE_FORCE, handleNodeChangeForce),
|
2018-02-12 20:43:07 +00:00
|
|
|
|
takeLatest(TypeKeys.CONFIG_POLL_OFFLINE_STATUS, handlePollOfflineStatus),
|
|
|
|
|
takeEvery(TypeKeys.CONFIG_LANGUAGE_CHANGE, reload),
|
2018-04-06 20:52:48 +00:00
|
|
|
|
takeEvery(TypeKeys.CONFIG_ADD_CUSTOM_NODE, handleAddCustomNode)
|
2018-02-12 20:43:07 +00:00
|
|
|
|
];
|