2018-02-12 20:43:07 +00:00
|
|
|
|
import { delay, SagaIterator } from 'redux-saga';
|
2018-06-11 22:43:39 +00:00
|
|
|
|
import { call, fork, put, take, takeEvery, select, apply } from 'redux-saga/effects';
|
|
|
|
|
import { bindActionCreators } from 'redux';
|
2018-02-12 20:43:07 +00:00
|
|
|
|
import {
|
|
|
|
|
getNodeId,
|
|
|
|
|
getNodeConfig,
|
|
|
|
|
getOffline,
|
|
|
|
|
isStaticNodeId,
|
|
|
|
|
getCustomNodeFromId,
|
|
|
|
|
getStaticNodeFromId,
|
2018-05-29 14:51:42 +00:00
|
|
|
|
getNetworkConfigById,
|
|
|
|
|
getAllNodes
|
2018-02-12 20:43:07 +00:00
|
|
|
|
} from 'selectors/config';
|
|
|
|
|
import { TypeKeys } from 'actions/config/constants';
|
|
|
|
|
import {
|
2018-04-11 02:53:27 +00:00
|
|
|
|
setOnline,
|
|
|
|
|
setOffline,
|
2018-05-29 14:51:42 +00:00
|
|
|
|
changeNodeRequested,
|
|
|
|
|
changeNodeSucceeded,
|
2018-05-30 02:24:38 +00:00
|
|
|
|
changeNodeFailed,
|
2018-05-29 14:51:42 +00:00
|
|
|
|
changeNodeForce,
|
2018-02-12 20:43:07 +00:00
|
|
|
|
setLatestBlock,
|
|
|
|
|
AddCustomNodeAction,
|
2018-03-01 04:31:33 +00:00
|
|
|
|
ChangeNodeForceAction,
|
2018-05-29 14:51:42 +00:00
|
|
|
|
ChangeNodeRequestedAction,
|
|
|
|
|
ChangeNodeRequestedOneTimeAction,
|
|
|
|
|
ChangeNetworkRequestedAction,
|
|
|
|
|
RemoveCustomNodeAction
|
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,
|
2018-05-29 14:51:42 +00:00
|
|
|
|
getShepherdPending,
|
|
|
|
|
makeAutoNodeName
|
2018-04-06 20:52:48 +00:00
|
|
|
|
} from 'libs/nodes';
|
2018-05-29 14:51:42 +00:00
|
|
|
|
import { INITIAL_STATE as selectedNodeInitialState } from 'reducers/config/nodes/selectedNode';
|
2018-06-11 22:43:39 +00:00
|
|
|
|
import { configuredStore as store } from 'store';
|
|
|
|
|
|
|
|
|
|
window.addEventListener('load', () => {
|
|
|
|
|
const getShepherdStatus = () => ({
|
|
|
|
|
pending: getShepherdPending(),
|
|
|
|
|
isOnline: !getShepherdOffline()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { online, offline, lostNetworkNotif, offlineNotif, restoreNotif } = bindActionCreators(
|
|
|
|
|
{
|
|
|
|
|
offline: setOffline,
|
|
|
|
|
online: setOnline,
|
|
|
|
|
restoreNotif: () =>
|
|
|
|
|
showNotification('success', 'Your connection to the network has been restored!', 3000),
|
|
|
|
|
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
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
offlineNotif: () =>
|
|
|
|
|
showNotification(
|
|
|
|
|
'info',
|
|
|
|
|
'You are currently offline. Some features will be unavailable.',
|
|
|
|
|
5000
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
store.dispatch
|
2018-04-11 02:53:27 +00:00
|
|
|
|
);
|
|
|
|
|
|
2018-06-11 22:43:39 +00:00
|
|
|
|
const getAppOnline = () => !getOffline(store.getState());
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description Repeatedly polls itself to check for online state conflict occurs, implemented in recursive style for flexible polling times
|
|
|
|
|
* as network requests take a variable amount of time.
|
|
|
|
|
*
|
|
|
|
|
* Whenever an app online state conflict occurs, it resolves the conflict with the following priority:
|
|
|
|
|
* * If shepherd is online but app is offline -> do a ping request via shepherd provider, with the result of the ping being the set app state
|
|
|
|
|
* * If shepherd is offline but app is online -> set app to offline as it wont be able to make requests anyway
|
|
|
|
|
*/
|
|
|
|
|
async function detectOnlineStateConflict() {
|
|
|
|
|
const shepherdStatus = getShepherdStatus();
|
|
|
|
|
const appOffline = getAppOnline();
|
|
|
|
|
const onlineStateConflict = shepherdStatus.isOnline !== appOffline;
|
|
|
|
|
|
|
|
|
|
if (shepherdStatus.pending || !onlineStateConflict) {
|
|
|
|
|
return setTimeout(detectOnlineStateConflict, 1000);
|
2018-04-16 22:51:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-11 22:43:39 +00:00
|
|
|
|
// if app reports online but shepherd offline, then set app offline
|
|
|
|
|
if (appOffline && !shepherdStatus.isOnline) {
|
|
|
|
|
lostNetworkNotif();
|
|
|
|
|
offline();
|
|
|
|
|
} else if (!appOffline && shepherdStatus.isOnline) {
|
|
|
|
|
// if app reports offline but shepherd reports online
|
|
|
|
|
// send a request to shepherd provider to see if we can still send out requests
|
|
|
|
|
const success = await shepherdProvider.ping().catch(() => false);
|
|
|
|
|
if (success) {
|
|
|
|
|
restoreNotif();
|
|
|
|
|
online();
|
2018-02-12 20:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-11 22:43:39 +00:00
|
|
|
|
detectOnlineStateConflict();
|
2018-02-12 20:43:07 +00:00
|
|
|
|
}
|
2018-06-11 22:43:39 +00:00
|
|
|
|
detectOnlineStateConflict();
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
2018-06-11 22:43:39 +00:00
|
|
|
|
window.addEventListener('offline', () => {
|
|
|
|
|
const previouslyOnline = getAppOnline();
|
|
|
|
|
|
|
|
|
|
// if browser reports as offline and we were previously online
|
|
|
|
|
// then set offline without checking balancer state
|
|
|
|
|
if (!navigator.onLine && previouslyOnline) {
|
|
|
|
|
offlineNotif();
|
|
|
|
|
offline();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-02-12 20:43:07 +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.
|
|
|
|
|
export function* reload(): SagaIterator {
|
|
|
|
|
setTimeout(() => location.reload(), 1150);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 14:51:42 +00:00
|
|
|
|
export function* handleChangeNodeRequestedOneTime(): SagaIterator {
|
|
|
|
|
const action: ChangeNodeRequestedOneTimeAction = yield take(
|
|
|
|
|
TypeKeys.CONFIG_CHANGE_NODE_REQUESTED_ONETIME
|
2018-04-12 23:17:46 +00:00
|
|
|
|
);
|
|
|
|
|
// allow shepherdProvider async init to complete. TODO - don't export shepherdProvider as promise
|
|
|
|
|
yield call(delay, 100);
|
2018-05-29 14:51:42 +00:00
|
|
|
|
yield put(changeNodeRequested(action.payload));
|
2018-04-12 23:17:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 14:51:42 +00:00
|
|
|
|
export function* handleChangeNodeRequested({
|
2018-02-12 20:43:07 +00:00
|
|
|
|
payload: nodeIdToSwitchTo
|
2018-05-29 14:51:42 +00:00
|
|
|
|
}: ChangeNodeRequestedAction): SagaIterator {
|
2018-02-12 20:43:07 +00:00
|
|
|
|
const isStaticNode: boolean = yield select(isStaticNodeId, nodeIdToSwitchTo);
|
|
|
|
|
const currentConfig: NodeConfig = yield select(getNodeConfig);
|
|
|
|
|
|
2018-05-29 14:51:42 +00:00
|
|
|
|
// Bail out if they're switching to the same node
|
|
|
|
|
if (currentConfig.id === nodeIdToSwitchTo) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 20:43:07 +00:00
|
|
|
|
function* bailOut(message: string) {
|
|
|
|
|
yield put(showNotification('danger', message, 5000));
|
2018-05-30 02:24:38 +00:00
|
|
|
|
yield put(changeNodeFailed());
|
2018-02-12 20:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-27 03:55:31 +00:00
|
|
|
|
const isOffline = yield select(getOffline);
|
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 {
|
2018-04-27 03:55:31 +00:00
|
|
|
|
yield apply(shepherd, shepherd.manual, [nodeIdToSwitchTo, isOffline]);
|
2018-04-06 20:52:48 +00:00
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return yield* bailOut(translateRaw('ERROR_32'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 03:55:31 +00:00
|
|
|
|
let currentBlock = '???';
|
2018-04-06 20:52:48 +00:00
|
|
|
|
try {
|
|
|
|
|
currentBlock = yield apply(shepherdProvider, shepherdProvider.getCurrentBlock);
|
|
|
|
|
} catch (err) {
|
2018-04-27 03:55:31 +00:00
|
|
|
|
if (!isOffline) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return yield* bailOut(translateRaw('ERROR_32'));
|
|
|
|
|
}
|
2018-04-06 20:52:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 20:43:07 +00:00
|
|
|
|
yield put(setLatestBlock(currentBlock));
|
2018-05-29 14:51:42 +00:00
|
|
|
|
yield put(changeNodeSucceeded({ networkId: nextNodeConfig.network, nodeId: nodeIdToSwitchTo }));
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
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 {
|
2018-05-29 14:51:42 +00:00
|
|
|
|
const config = action.payload;
|
2018-04-06 20:52:48 +00:00
|
|
|
|
shepherd.useProvider(
|
|
|
|
|
'myccustom',
|
|
|
|
|
config.id,
|
|
|
|
|
makeProviderConfig({ network: config.network }),
|
|
|
|
|
config
|
|
|
|
|
);
|
2018-05-29 14:51:42 +00:00
|
|
|
|
yield put(changeNodeRequested(config.id));
|
2018-02-12 20:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
2018-05-29 14:51:42 +00:00
|
|
|
|
yield put(changeNodeSucceeded({ networkId: nodeConfig.network, nodeId: staticNodeIdToSwitchTo }));
|
2018-03-01 04:31:33 +00:00
|
|
|
|
|
|
|
|
|
// also put the change through as usual so status check and
|
|
|
|
|
// error messages occur if the node is unavailable
|
2018-05-29 14:51:42 +00:00
|
|
|
|
yield put(changeNodeRequested(staticNodeIdToSwitchTo));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* handleChangeNetworkRequested({ payload: network }: ChangeNetworkRequestedAction) {
|
|
|
|
|
let desiredNode = '';
|
|
|
|
|
const autoNodeName = makeAutoNodeName(network);
|
|
|
|
|
const isStaticNode: boolean = yield select(isStaticNodeId, autoNodeName);
|
|
|
|
|
|
|
|
|
|
if (isStaticNode) {
|
|
|
|
|
desiredNode = autoNodeName;
|
|
|
|
|
} else {
|
|
|
|
|
const allNodes: { [id: string]: NodeConfig } = yield select(getAllNodes);
|
|
|
|
|
const networkNode = Object.values(allNodes).find(n => n.network === network);
|
|
|
|
|
if (networkNode) {
|
|
|
|
|
desiredNode = networkNode.id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (desiredNode) {
|
|
|
|
|
yield put(changeNodeRequested(desiredNode));
|
|
|
|
|
} else {
|
|
|
|
|
yield put(
|
|
|
|
|
showNotification(
|
|
|
|
|
'danger',
|
|
|
|
|
translateRaw('NETWORK_UNKNOWN_ERROR', {
|
|
|
|
|
$network: network
|
|
|
|
|
}),
|
|
|
|
|
5000
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* handleRemoveCustomNode({ payload: nodeId }: RemoveCustomNodeAction): SagaIterator {
|
|
|
|
|
// If custom node is currently selected, go back to default node
|
|
|
|
|
const currentNodeId = yield select(getNodeId);
|
|
|
|
|
if (nodeId === currentNodeId) {
|
|
|
|
|
yield put(changeNodeForce(selectedNodeInitialState.nodeId));
|
|
|
|
|
}
|
2018-03-01 04:31:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 20:43:07 +00:00
|
|
|
|
export const node = [
|
2018-05-29 14:51:42 +00:00
|
|
|
|
fork(handleChangeNodeRequestedOneTime),
|
|
|
|
|
takeEvery(TypeKeys.CONFIG_CHANGE_NODE_REQUESTED, handleChangeNodeRequested),
|
|
|
|
|
takeEvery(TypeKeys.CONFIG_CHANGE_NODE_FORCE, handleNodeChangeForce),
|
|
|
|
|
takeEvery(TypeKeys.CONFIG_CHANGE_NETWORK_REQUESTED, handleChangeNetworkRequested),
|
2018-02-12 20:43:07 +00:00
|
|
|
|
takeEvery(TypeKeys.CONFIG_LANGUAGE_CHANGE, reload),
|
2018-05-29 14:51:42 +00:00
|
|
|
|
takeEvery(TypeKeys.CONFIG_ADD_CUSTOM_NODE, handleAddCustomNode),
|
|
|
|
|
takeEvery(TypeKeys.CONFIG_REMOVE_CUSTOM_NODE, handleRemoveCustomNode)
|
2018-02-12 20:43:07 +00:00
|
|
|
|
];
|