Browse Source

Reinitialize drizzle on network changes

develop
Ezerous 4 years ago
parent
commit
7a67f9d4a9
  1. 2
      src/contracts/contractsSaga.js
  2. 9
      src/drizzle-middleware.js
  3. 2
      src/drizzleStatus/drizzleStatusSaga.js
  4. 4
      src/rootSaga.js
  5. 6
      src/web3/web3Actions.js
  6. 8
      src/web3/web3Middleware.js
  7. 7
      src/web3/web3Saga.js

2
src/contracts/contractsSaga.js

@ -274,7 +274,7 @@ function isSendOrCallOptions (options) {
} }
export function * isContractDeployed ({ web3, contractConfig }) { export function * isContractDeployed ({ web3, contractConfig }) {
const networkId = yield call(getNetworkId, { web3 }) const networkId = yield call(web3.eth.net.getId);
if(contractConfig.networks[networkId]){ if(contractConfig.networks[networkId]){
const contractAddress = contractConfig.networks[networkId].address; const contractAddress = contractConfig.networks[networkId].address;

9
src/drizzle-middleware.js

@ -1,6 +1,7 @@
import * as DrizzleActions from './drizzleStatus/drizzleActions' import * as DrizzleActions from './drizzleStatus/drizzleActions'
import * as ContractActions from './contracts/constants' import * as ContractActions from './contracts/constants'
import { ACCOUNTS_FETCHED } from './accounts/accountsActions' import { ACCOUNTS_FETCHED } from './accounts/accountsActions'
import { NETWORK_ID_CHANGED } from './web3/web3Actions'
export const drizzleMiddleware = drizzleInstance => store => next => action => { export const drizzleMiddleware = drizzleInstance => store => next => action => {
const { type } = action const { type } = action
@ -9,6 +10,14 @@ export const drizzleMiddleware = drizzleInstance => store => next => action => {
drizzleInstance = action.drizzle drizzleInstance = action.drizzle
} }
if (type === NETWORK_ID_CHANGED) {
store.dispatch({
type: DrizzleActions.DRIZZLE_INITIALIZING,
drizzle: drizzleInstance,
options: drizzleInstance.options
})
}
if ( if (
type === ACCOUNTS_FETCHED && type === ACCOUNTS_FETCHED &&
drizzleInstance && drizzleInstance &&

2
src/drizzleStatus/drizzleStatusSaga.js

@ -7,7 +7,7 @@ import { getAccountBalances } from '../accountBalances/accountBalancesSaga'
import * as DrizzleActions from './drizzleActions' import * as DrizzleActions from './drizzleActions'
import * as BlocksActions from '../blocks/blockActions' import * as BlocksActions from '../blocks/blockActions'
import { NETWORK_IDS, NETWORK_MISMATCH } from '../web3/web3Actions' import { NETWORK_ID_CHANGED, NETWORK_IDS, NETWORK_MISMATCH } from '../web3/web3Actions'
import { CONTRACT_NOT_DEPLOYED } from '../contracts/constants' import { CONTRACT_NOT_DEPLOYED } from '../contracts/constants'
import { isContractDeployed } from '../contracts/contractsSaga' import { isContractDeployed } from '../contracts/contractsSaga'

4
src/rootSaga.js

@ -2,12 +2,10 @@ import accountBalancesSaga from './accountBalances/accountBalancesSaga'
import blocksSaga from './blocks/blocksSaga' import blocksSaga from './blocks/blocksSaga'
import contractsSaga from './contracts/contractsSaga' import contractsSaga from './contracts/contractsSaga'
import drizzleStatusSaga from './drizzleStatus/drizzleStatusSaga' import drizzleStatusSaga from './drizzleStatus/drizzleStatusSaga'
import web3Saga from './web3/web3Saga'
export default [ export default [
accountBalancesSaga, accountBalancesSaga,
blocksSaga, blocksSaga,
contractsSaga, contractsSaga,
drizzleStatusSaga, drizzleStatusSaga
web3Saga
] ]

6
src/web3/web3Actions.js

@ -3,6 +3,7 @@ export const WEB3_INITIALIZED = 'WEB3_INITIALIZED'
export const WEB3_FAILED = 'WEB3_FAILED' export const WEB3_FAILED = 'WEB3_FAILED'
export const WEB3_USER_DENIED = 'WEB3_USER_DENIED' export const WEB3_USER_DENIED = 'WEB3_USER_DENIED'
export const NETWORK_ID_FETCHING = 'NETWORK_ID_FETCHING'
export const NETWORK_ID_FETCHED = 'NETWORK_ID_FETCHED' export const NETWORK_ID_FETCHED = 'NETWORK_ID_FETCHED'
export const NETWORK_ID_CHANGED = 'NETWORK_ID_CHANGED' export const NETWORK_ID_CHANGED = 'NETWORK_ID_CHANGED'
export const NETWORK_ID_FAILED = 'NETWORK_ID_FAILED' export const NETWORK_ID_FAILED = 'NETWORK_ID_FAILED'
@ -17,9 +18,10 @@ export const NETWORK_IDS = {
ganache: 5777 ganache: 5777
} }
export function networkIdChanged (web3) { export function networkIdChanged (web3, networkId) {
return { return {
type: NETWORK_ID_CHANGED, type: NETWORK_ID_CHANGED,
web3 web3,
networkId
} }
} }

8
src/web3/web3Middleware.js

@ -8,9 +8,11 @@ export const web3Middleware = web3 => store => next => action => {
console.warn('No Metamask detected, not subscribed to network changes!') console.warn('No Metamask detected, not subscribed to network changes!')
else { else {
web3 = action.web3; web3 = action.web3;
window.ethereum.on('networkChanged', () => { window.ethereum.on('networkChanged', (networkId) => {
// We could have listened to 'networkChanged' but it is deprecated (EIP-1193) // Warning: 'networkChanged' is deprecated (EIP-1193)
store.dispatch(networkIdChanged(web3)); const storedNetworkId = store.getState().web3.networkId;
if(storedNetworkId && networkId !== storedNetworkId)
store.dispatch(networkIdChanged(web3, networkId));
}); });
} }
} }

7
src/web3/web3Saga.js

@ -85,10 +85,3 @@ export function * getNetworkId ({ web3 }) {
console.error(error); console.error(error);
} }
} }
function * web3Saga () {
yield takeLatest(Action.NETWORK_ID_CHANGED, getNetworkId);
}
export default web3Saga

Loading…
Cancel
Save