2018-01-31 01:04:36 +00:00
|
|
|
import { networkIdToName } from 'libs/values';
|
|
|
|
import { TypeKeys as WalletTypeKeys } from 'actions/wallet/constants';
|
|
|
|
import { Web3Wallet } from 'libs/wallet';
|
|
|
|
import { SagaIterator } from 'redux-saga';
|
|
|
|
import { select, put, takeEvery, call } from 'redux-saga/effects';
|
|
|
|
import { changeNodeIntent, TypeKeys, web3SetNode } from 'actions/config';
|
2018-02-08 01:55:12 +00:00
|
|
|
import { getNodeId, getStaticAltNodeIdToWeb3 } from 'selectors/config';
|
2018-01-31 01:04:36 +00:00
|
|
|
import { setupWeb3Node, Web3Service } from 'libs/nodes/web3';
|
2018-02-02 22:34:38 +00:00
|
|
|
import { Web3NodeConfig } from 'types/node';
|
2018-01-31 01:04:36 +00:00
|
|
|
|
|
|
|
export function* initWeb3Node(): SagaIterator {
|
|
|
|
const { networkId, lib } = yield call(setupWeb3Node);
|
|
|
|
const config: Web3NodeConfig = {
|
|
|
|
isCustom: false,
|
|
|
|
network: networkIdToName(networkId),
|
|
|
|
service: Web3Service,
|
|
|
|
lib,
|
|
|
|
estimateGas: false,
|
|
|
|
hidden: true
|
|
|
|
};
|
|
|
|
|
|
|
|
yield put(web3SetNode({ id: 'web3', config }));
|
|
|
|
}
|
|
|
|
|
|
|
|
// unset web3 as the selected node if a non-web3 wallet has been selected
|
|
|
|
export function* unsetWeb3NodeOnWalletEvent(action): SagaIterator {
|
|
|
|
const node = yield select(getNodeId);
|
|
|
|
const newWallet = action.payload;
|
|
|
|
const isWeb3Wallet = newWallet instanceof Web3Wallet;
|
|
|
|
|
|
|
|
if (node !== 'web3' || isWeb3Wallet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-08 01:55:12 +00:00
|
|
|
const altNode = yield select(getStaticAltNodeIdToWeb3);
|
2018-01-31 01:04:36 +00:00
|
|
|
// switch back to a node with the same network as MetaMask/Mist
|
|
|
|
yield put(changeNodeIntent(altNode));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* unsetWeb3Node(): SagaIterator {
|
|
|
|
const node = yield select(getNodeId);
|
|
|
|
|
|
|
|
if (node !== 'web3') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-08 01:55:12 +00:00
|
|
|
const altNode = yield select(getStaticAltNodeIdToWeb3);
|
2018-01-31 01:04:36 +00:00
|
|
|
// switch back to a node with the same network as MetaMask/Mist
|
|
|
|
yield put(changeNodeIntent(altNode));
|
|
|
|
}
|
|
|
|
|
|
|
|
export const web3 = [
|
|
|
|
takeEvery(TypeKeys.CONFIG_NODE_WEB3_UNSET, unsetWeb3Node),
|
|
|
|
takeEvery(WalletTypeKeys.WALLET_SET, unsetWeb3NodeOnWalletEvent),
|
|
|
|
takeEvery(WalletTypeKeys.WALLET_RESET, unsetWeb3NodeOnWalletEvent)
|
|
|
|
];
|