Browse Source

MetaMask app permission fix

develop
Ezerous 4 years ago
parent
commit
722235d3ed
  1. 3
      src/defaultOptions.js
  2. 29
      src/web3/web3Saga.js

3
src/defaultOptions.js

@ -3,7 +3,8 @@ const defaultOptions = {
fallback: { fallback: {
type: 'ws', type: 'ws',
url: 'ws://127.0.0.1:8545' url: 'ws://127.0.0.1:8545'
} },
retryPermissionDialog: true
}, },
contracts: [], contracts: [],
events: {}, events: {},

29
src/web3/web3Saga.js

@ -10,30 +10,27 @@ export function * initializeWeb3 (options) {
try { try {
let web3 = {} let web3 = {}
if (options.customProvider) {
yield put({ type: Action.WEB3_INITIALIZED, web3: options.customProvider })
return options.customProvider
}
if (window.ethereum) { if (window.ethereum) {
const { ethereum } = window const { ethereum } = window
web3 = new Web3(ethereum) web3 = options.customProvider || new Web3(ethereum);
try { try {
// ethereum.enable() will return the selected account yield call([ethereum, 'request'], { method: 'eth_requestAccounts' })
// unless user opts out and then it will return undefined
const selectedAccount = yield call([ethereum, 'enable'])
yield put({ type: Action.WEB3_INITIALIZED, web3 }) yield put({ type: Action.WEB3_INITIALIZED, web3 })
if (!selectedAccount) {
yield put({ type: Action.WEB3_USER_DENIED })
return
}
return web3 return web3
} catch (error) { } catch (error) {
console.error(error) console.error(error);
yield put({ type: Action.WEB3_FAILED }) 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') { } else if (typeof window.web3 !== 'undefined') {
// Checking if Web3 has been injected by the browser (Mist/MetaMask) // Checking if Web3 has been injected by the browser (Mist/MetaMask)

Loading…
Cancel
Save