From 722235d3ede4b770b39ffdfebefdd8ed807d93fc Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 5 Sep 2020 18:32:42 +0300 Subject: [PATCH 1/3] MetaMask app permission fix --- src/defaultOptions.js | 3 ++- src/web3/web3Saga.js | 29 +++++++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/defaultOptions.js b/src/defaultOptions.js index b089253..ed10f3e 100644 --- a/src/defaultOptions.js +++ b/src/defaultOptions.js @@ -3,7 +3,8 @@ const defaultOptions = { fallback: { type: 'ws', url: 'ws://127.0.0.1:8545' - } + }, + retryPermissionDialog: true }, contracts: [], events: {}, diff --git a/src/web3/web3Saga.js b/src/web3/web3Saga.js index d3fc3ee..106c8a9 100644 --- a/src/web3/web3Saga.js +++ b/src/web3/web3Saga.js @@ -10,30 +10,27 @@ export function * initializeWeb3 (options) { try { let web3 = {} - if (options.customProvider) { - yield put({ type: Action.WEB3_INITIALIZED, web3: options.customProvider }) - - return options.customProvider - } - if (window.ethereum) { const { ethereum } = window - web3 = new Web3(ethereum) + web3 = options.customProvider || new Web3(ethereum); try { - // ethereum.enable() will return the selected account - // unless user opts out and then it will return undefined - const selectedAccount = yield call([ethereum, 'enable']) + yield call([ethereum, 'request'], { method: 'eth_requestAccounts' }) yield put({ type: Action.WEB3_INITIALIZED, web3 }) - if (!selectedAccount) { - yield put({ type: Action.WEB3_USER_DENIED }) - return - } return web3 } catch (error) { - console.error(error) - yield put({ type: Action.WEB3_FAILED }) + console.error(error); + if(error.code === 4001){ + console.warn("User rejected MetaMask permission request"); + yield put({ type: Action.WEB3_USER_DENIED }); + if(options.retryPermissionDialog) + return yield call(initializeWeb3, options); // User rejected permission dialog, let's retry + } + else if (error.code === -32002) + console.warn('Please accept the pending MetaMask permission request'); + + yield put({ type: Action.WEB3_FAILED, error }); } } else if (typeof window.web3 !== 'undefined') { // Checking if Web3 has been injected by the browser (Mist/MetaMask) From 96893ae3a522a7365d5364a2ce9f33aedd1b3b9c Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 5 Sep 2020 18:51:56 +0300 Subject: [PATCH 2/3] 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 }) From 84bc52a92a450b4664441cf930bb0e408a3f7402 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 5 Sep 2020 18:57:46 +0300 Subject: [PATCH 3/3] Tiny README fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1b7f14d..9973147 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # @ezerous/drizzle [![Version](https://img.shields.io/npm/v/@ezerous/drizzle.svg)](https://www.npmjs.com/package/@ezerous/drizzle) -[![Dependencies](https://img.shields.io/david/Ezerous/@ezerous/drizzle.svg)](https://david-dm.org/Ezerous/drizzle) +[![Dependencies](https://img.shields.io/david/Ezerous/drizzle.svg)](https://david-dm.org/Ezerous/drizzle) [![License](https://img.shields.io/npm/l/@ezerous/drizzle.svg)](https://www.npmjs.com/package/@ezerous/drizzle) A reactive data-store for web3 and smart contracts. A modified version of [@drizzle/store](https://github.com/trufflesuite/drizzle/tree/develop/packages/store).