From 96893ae3a522a7365d5364a2ce9f33aedd1b3b9c Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 5 Sep 2020 18:51:56 +0300 Subject: [PATCH] Reload on network change --- package.json | 2 +- src/Drizzle.js | 6 ++++++ src/drizzle-middleware.js | 9 --------- src/web3/web3Actions.js | 2 -- src/web3/web3Middleware.js | 11 ++++++----- src/web3/web3Saga.js | 2 +- 6 files changed, 14 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 4ababd0..f0d2909 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ezerous/drizzle", - "version": "0.2.0", + "version": "0.2.1", "description": "A reactive data-store for web3 and smart contracts.", "license": "MIT", "author": "Ezerous ", diff --git a/src/Drizzle.js b/src/Drizzle.js index b52eeed..ee40798 100644 --- a/src/Drizzle.js +++ b/src/Drizzle.js @@ -81,6 +81,12 @@ class Drizzle { events ) + if (this.contracts[drizzleContract.contractName]) { + throw new Error( + `Contract already exists: ${drizzleContract.contractName}` + ) + } + this.store.dispatch({ type: ContractActions.CONTRACT_INITIALIZING, contractConfig }) this.contracts[drizzleContract.contractName] = drizzleContract diff --git a/src/drizzle-middleware.js b/src/drizzle-middleware.js index 71d96f4..0718af2 100644 --- a/src/drizzle-middleware.js +++ b/src/drizzle-middleware.js @@ -1,7 +1,6 @@ import * as DrizzleActions from './drizzleStatus/drizzleActions' import * as ContractActions from './contracts/constants' import { ACCOUNTS_FETCHED } from './accounts/accountsActions' -import { NETWORK_ID_CHANGED } from './web3/web3Actions' export const drizzleMiddleware = drizzleInstance => store => next => action => { const { type } = action @@ -10,14 +9,6 @@ export const drizzleMiddleware = drizzleInstance => store => next => action => { drizzleInstance = action.drizzle } - if (type === NETWORK_ID_CHANGED) { - store.dispatch({ - type: DrizzleActions.DRIZZLE_INITIALIZING, - drizzle: drizzleInstance, - options: drizzleInstance.options - }) - } - if ( type === ACCOUNTS_FETCHED && drizzleInstance && diff --git a/src/web3/web3Actions.js b/src/web3/web3Actions.js index 2175bdc..6272715 100644 --- a/src/web3/web3Actions.js +++ b/src/web3/web3Actions.js @@ -3,7 +3,6 @@ export const WEB3_INITIALIZED = 'WEB3_INITIALIZED' export const WEB3_FAILED = 'WEB3_FAILED' 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_CHANGED = 'NETWORK_ID_CHANGED' export const NETWORK_ID_FAILED = 'NETWORK_ID_FAILED' @@ -21,7 +20,6 @@ export const NETWORK_IDS = { export function networkIdChanged (web3, networkId) { return { type: NETWORK_ID_CHANGED, - web3, networkId } } diff --git a/src/web3/web3Middleware.js b/src/web3/web3Middleware.js index 2c4557c..43f1dd4 100644 --- a/src/web3/web3Middleware.js +++ b/src/web3/web3Middleware.js @@ -1,23 +1,24 @@ import { networkIdChanged, WEB3_INITIALIZED } from './web3Actions' -export const web3Middleware = web3 => store => next => action => { +export const web3Middleware = () => store => next => action => { const { type } = action if (type === WEB3_INITIALIZED) { if(!window.ethereum) console.warn('No Metamask detected, not subscribed to network changes!') else { - web3 = action.web3; window.ethereum.on('networkChanged', (networkId) => { // Warning: 'networkChanged' is deprecated (EIP-1193) const storedNetworkId = store.getState().web3.networkId; - if(storedNetworkId && networkId !== storedNetworkId) - store.dispatch(networkIdChanged(web3, networkId)); + if(storedNetworkId && (networkId !== storedNetworkId)){ + store.dispatch(networkIdChanged(networkId)); // Just to be typical + window.location.reload(); + } }); } } return next(action) } -const initializedMiddleware = web3Middleware(undefined) +const initializedMiddleware = web3Middleware() export default initializedMiddleware diff --git a/src/web3/web3Saga.js b/src/web3/web3Saga.js index 106c8a9..0e496b0 100644 --- a/src/web3/web3Saga.js +++ b/src/web3/web3Saga.js @@ -14,7 +14,7 @@ export function * initializeWeb3 (options) { const { ethereum } = window web3 = options.customProvider || new Web3(ethereum); try { - yield call([ethereum, 'request'], { method: 'eth_requestAccounts' }) + yield call([ethereum, 'request'], { method: 'eth_requestAccounts' }); yield put({ type: Action.WEB3_INITIALIZED, web3 })