From 722235d3ede4b770b39ffdfebefdd8ed807d93fc Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 5 Sep 2020 18:32:42 +0300 Subject: [PATCH] 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)