Get web3 working + cleanup sagas
This commit is contained in:
parent
35646af573
commit
f999272769
|
@ -2,7 +2,6 @@ import React from 'react';
|
||||||
import classnames from 'classnames';
|
import classnames from 'classnames';
|
||||||
import Modal, { IButton } from 'components/ui/Modal';
|
import Modal, { IButton } from 'components/ui/Modal';
|
||||||
import translate from 'translations';
|
import translate from 'translations';
|
||||||
import { makeCustomNetworkId } from 'utils/network';
|
|
||||||
import { CustomNetworkConfig } from 'types/network';
|
import { CustomNetworkConfig } from 'types/network';
|
||||||
import { CustomNodeConfig } from 'types/node';
|
import { CustomNodeConfig } from 'types/node';
|
||||||
import { TAddCustomNetwork, addCustomNetwork, AddCustomNodeAction } from 'actions/config';
|
import { TAddCustomNetwork, addCustomNetwork, AddCustomNodeAction } from 'actions/config';
|
||||||
|
@ -131,14 +130,11 @@ class CustomNodeModal extends React.Component<Props, State> {
|
||||||
{net}
|
{net}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
{Object.values(customNetworks).map(net => {
|
{Object.entries(customNetworks).map(([id, net]) => (
|
||||||
const id = makeCustomNetworkId(net);
|
<option key={id} value={id}>
|
||||||
return (
|
{net.name} (Custom)
|
||||||
<option key={id} value={id}>
|
</option>
|
||||||
{net.name} (Custom)
|
))}
|
||||||
</option>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
<option value={CUSTOM}>Custom...</option>
|
<option value={CUSTOM}>Custom...</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
@ -336,8 +332,11 @@ class CustomNodeModal extends React.Component<Props, State> {
|
||||||
|
|
||||||
private makeCustomNodeConfigFromState(): CustomNodeConfig {
|
private makeCustomNodeConfigFromState(): CustomNodeConfig {
|
||||||
const { network } = this.state;
|
const { network } = this.state;
|
||||||
|
|
||||||
const networkId =
|
const networkId =
|
||||||
network === CUSTOM ? makeCustomNetworkId(this.makeCustomNetworkConfigFromState()) : network;
|
network === CUSTOM
|
||||||
|
? this.makeCustomNetworkId(this.makeCustomNetworkConfigFromState())
|
||||||
|
: network;
|
||||||
|
|
||||||
const port = parseInt(this.state.port, 10);
|
const port = parseInt(this.state.port, 10);
|
||||||
const url = this.state.url.trim();
|
const url = this.state.url.trim();
|
||||||
|
@ -392,6 +391,10 @@ class CustomNodeModal extends React.Component<Props, State> {
|
||||||
|
|
||||||
this.props.addCustomNode({ config: node, id: node.id });
|
this.props.addCustomNode({ config: node, id: node.id });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private makeCustomNetworkId(config: CustomNetworkConfig): string {
|
||||||
|
return config.chainId ? `${config.chainId}` : `${config.name}:${config.unit}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const mapStateToProps = (state: AppState): StateProps => ({
|
const mapStateToProps = (state: AppState): StateProps => ({
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
import { network } from './network';
|
||||||
|
import { node } from './node';
|
||||||
|
import { web3 } from './web3';
|
||||||
|
import { all } from 'redux-saga/effects';
|
||||||
|
import { SagaIterator } from 'redux-saga';
|
||||||
|
|
||||||
|
export default function*(): SagaIterator {
|
||||||
|
yield all([...network, ...node, ...web3]);
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { select, takeEvery, put } from 'redux-saga/effects';
|
||||||
|
import { getCustomNodeConfigs, getCustomNetworkConfigs } from 'selectors/config';
|
||||||
|
import { removeCustomNetwork, TypeKeys } from 'actions/config';
|
||||||
|
import { SagaIterator } from 'redux-saga';
|
||||||
|
import { AppState } from 'reducers';
|
||||||
|
|
||||||
|
// If there are any orphaned custom networks, purge them
|
||||||
|
export function* cleanCustomNetworks(): SagaIterator {
|
||||||
|
const customNodes: AppState['config']['nodes']['customNodes'] = yield select(
|
||||||
|
getCustomNodeConfigs
|
||||||
|
);
|
||||||
|
const customNetworks: AppState['config']['networks']['customNetworks'] = yield select(
|
||||||
|
getCustomNetworkConfigs
|
||||||
|
);
|
||||||
|
|
||||||
|
Object.values(customNodes).forEach(function*(n) {
|
||||||
|
if (!customNetworks[n.network]) {
|
||||||
|
yield put(removeCustomNetwork({ id: n.network }));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const network = [takeEvery(TypeKeys.CONFIG_REMOVE_CUSTOM_NODE, cleanCustomNetworks)];
|
|
@ -5,18 +5,15 @@ import {
|
||||||
fork,
|
fork,
|
||||||
put,
|
put,
|
||||||
take,
|
take,
|
||||||
takeLatest,
|
|
||||||
takeEvery,
|
takeEvery,
|
||||||
select,
|
select,
|
||||||
race,
|
race,
|
||||||
apply
|
apply,
|
||||||
|
takeLatest
|
||||||
} from 'redux-saga/effects';
|
} from 'redux-saga/effects';
|
||||||
import { makeCustomNetworkId } from 'utils/network';
|
|
||||||
import {
|
import {
|
||||||
getNodeId,
|
getNodeId,
|
||||||
getNodeConfig,
|
getNodeConfig,
|
||||||
getCustomNodeConfigs,
|
|
||||||
getCustomNetworkConfigs,
|
|
||||||
getOffline,
|
getOffline,
|
||||||
isStaticNodeId,
|
isStaticNodeId,
|
||||||
getCustomNodeFromId,
|
getCustomNodeFromId,
|
||||||
|
@ -29,7 +26,6 @@ import {
|
||||||
changeNode,
|
changeNode,
|
||||||
changeNodeIntent,
|
changeNodeIntent,
|
||||||
setLatestBlock,
|
setLatestBlock,
|
||||||
removeCustomNetwork,
|
|
||||||
AddCustomNodeAction,
|
AddCustomNodeAction,
|
||||||
ChangeNodeIntentAction
|
ChangeNodeIntentAction
|
||||||
} from 'actions/config';
|
} from 'actions/config';
|
||||||
|
@ -42,7 +38,7 @@ import { Web3Service } from 'libs/nodes/web3';
|
||||||
let hasCheckedOnline = false;
|
let hasCheckedOnline = false;
|
||||||
export function* pollOfflineStatus(): SagaIterator {
|
export function* pollOfflineStatus(): SagaIterator {
|
||||||
while (true) {
|
while (true) {
|
||||||
const node: StaticNodeConfig = yield select(getNodeConfig);
|
const nodeConfig: StaticNodeConfig = yield select(getNodeConfig);
|
||||||
const isOffline: boolean = yield select(getOffline);
|
const isOffline: boolean = yield select(getOffline);
|
||||||
|
|
||||||
// If our offline state disagrees with the browser, run a check
|
// If our offline state disagrees with the browser, run a check
|
||||||
|
@ -50,7 +46,7 @@ export function* pollOfflineStatus(): SagaIterator {
|
||||||
const shouldPing = !hasCheckedOnline || navigator.onLine === isOffline;
|
const shouldPing = !hasCheckedOnline || navigator.onLine === isOffline;
|
||||||
if (shouldPing && !document.hidden) {
|
if (shouldPing && !document.hidden) {
|
||||||
const { pingSucceeded } = yield race({
|
const { pingSucceeded } = yield race({
|
||||||
pingSucceeded: call(node.lib.ping.bind(node.lib)),
|
pingSucceeded: call(nodeConfig.lib.ping.bind(nodeConfig.lib)),
|
||||||
timeout: call(delay, 5000)
|
timeout: call(delay, 5000)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -190,26 +186,9 @@ export function* switchToNewNode(action: AddCustomNodeAction): SagaIterator {
|
||||||
yield put(changeNodeIntent(action.payload.id));
|
yield put(changeNodeIntent(action.payload.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there are any orphaned custom networks, purge them
|
export const node = [
|
||||||
export function* cleanCustomNetworks(): SagaIterator {
|
takeEvery(TypeKeys.CONFIG_NODE_CHANGE_INTENT, handleNodeChangeIntent),
|
||||||
const customNodes = yield select(getCustomNodeConfigs);
|
takeLatest(TypeKeys.CONFIG_POLL_OFFLINE_STATUS, handlePollOfflineStatus),
|
||||||
const customNetworks = yield select(getCustomNetworkConfigs);
|
takeEvery(TypeKeys.CONFIG_LANGUAGE_CHANGE, reload),
|
||||||
const networksInUse = customNodes.reduce((prev, conf) => {
|
takeEvery(TypeKeys.CONFIG_ADD_CUSTOM_NODE, switchToNewNode)
|
||||||
prev[conf.network] = true;
|
];
|
||||||
return prev;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
for (const net of customNetworks) {
|
|
||||||
if (!networksInUse[makeCustomNetworkId(net)]) {
|
|
||||||
yield put(removeCustomNetwork(net));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function* configSaga(): SagaIterator {
|
|
||||||
yield takeLatest(TypeKeys.CONFIG_POLL_OFFLINE_STATUS, handlePollOfflineStatus);
|
|
||||||
yield takeEvery(TypeKeys.CONFIG_NODE_CHANGE_INTENT, handleNodeChangeIntent);
|
|
||||||
yield takeEvery(TypeKeys.CONFIG_LANGUAGE_CHANGE, reload);
|
|
||||||
yield takeEvery(TypeKeys.CONFIG_ADD_CUSTOM_NODE, switchToNewNode);
|
|
||||||
yield takeEvery(TypeKeys.CONFIG_REMOVE_CUSTOM_NODE, cleanCustomNetworks);
|
|
||||||
}
|
|
|
@ -6,7 +6,7 @@ import { select, put, takeEvery, call } from 'redux-saga/effects';
|
||||||
import { changeNodeIntent, TypeKeys, web3SetNode } from 'actions/config';
|
import { changeNodeIntent, TypeKeys, web3SetNode } from 'actions/config';
|
||||||
import { getNodeId, getStaticAltNodeToWeb3 } from 'selectors/config';
|
import { getNodeId, getStaticAltNodeToWeb3 } from 'selectors/config';
|
||||||
import { setupWeb3Node, Web3Service } from 'libs/nodes/web3';
|
import { setupWeb3Node, Web3Service } from 'libs/nodes/web3';
|
||||||
import { Web3NodeConfig } from '../../shared/types/node';
|
import { Web3NodeConfig } from 'types/node';
|
||||||
|
|
||||||
export function* initWeb3Node(): SagaIterator {
|
export function* initWeb3Node(): SagaIterator {
|
||||||
const { networkId, lib } = yield call(setupWeb3Node);
|
const { networkId, lib } = yield call(setupWeb3Node);
|
||||||
|
@ -50,7 +50,6 @@ export function* unsetWeb3Node(): SagaIterator {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const web3 = [
|
export const web3 = [
|
||||||
takeEvery(TypeKeys.CONFIG_NODE_WEB3_SET, initWeb3Node),
|
|
||||||
takeEvery(TypeKeys.CONFIG_NODE_WEB3_UNSET, unsetWeb3Node),
|
takeEvery(TypeKeys.CONFIG_NODE_WEB3_UNSET, unsetWeb3Node),
|
||||||
takeEvery(WalletTypeKeys.WALLET_SET, unsetWeb3NodeOnWalletEvent),
|
takeEvery(WalletTypeKeys.WALLET_SET, unsetWeb3NodeOnWalletEvent),
|
||||||
takeEvery(WalletTypeKeys.WALLET_RESET, unsetWeb3NodeOnWalletEvent)
|
takeEvery(WalletTypeKeys.WALLET_RESET, unsetWeb3NodeOnWalletEvent)
|
|
@ -52,7 +52,7 @@ import { loadWalletConfig, saveWalletConfig } from 'utils/localStorage';
|
||||||
import { getTokenBalances, filterScannedTokenBalances } from './helpers';
|
import { getTokenBalances, filterScannedTokenBalances } from './helpers';
|
||||||
import { Token } from 'types/network';
|
import { Token } from 'types/network';
|
||||||
import { Web3NodeConfig } from '../../../shared/types/node';
|
import { Web3NodeConfig } from '../../../shared/types/node';
|
||||||
import { initWeb3Node } from 'sagas/web3';
|
import { initWeb3Node } from 'sagas/config/web3';
|
||||||
|
|
||||||
export interface TokenBalanceLookup {
|
export interface TokenBalanceLookup {
|
||||||
[symbol: string]: TokenBalance;
|
[symbol: string]: TokenBalance;
|
||||||
|
@ -264,7 +264,7 @@ export function* unlockWeb3(): SagaIterator {
|
||||||
yield put(changeNodeIntent('web3'));
|
yield put(changeNodeIntent('web3'));
|
||||||
yield take(
|
yield take(
|
||||||
action =>
|
action =>
|
||||||
action.type === ConfigTypeKeys.CONFIG_NODE_CHANGE && action.payload.nodeSelection === 'web3'
|
action.type === ConfigTypeKeys.CONFIG_NODE_CHANGE && action.payload.nodeId === 'web3'
|
||||||
);
|
);
|
||||||
|
|
||||||
const web3Node: Web3NodeConfig | null = yield select(getWeb3Node);
|
const web3Node: Web3NodeConfig | null = yield select(getWeb3Node);
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
import { CustomNetworkConfig } from 'types/network';
|
|
||||||
|
|
||||||
export function makeCustomNetworkId(config: CustomNetworkConfig): string {
|
|
||||||
return config.chainId ? `${config.chainId}` : `${config.name}:${config.unit}`;
|
|
||||||
}
|
|
Loading…
Reference in New Issue