From 162f5446bf760c007681dcee6374ae0520004b24 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Thu, 19 Nov 2020 14:31:23 +0200 Subject: [PATCH] Web3 providers fix --- package.json | 2 +- src/defaultOptions.js | 1 + src/web3/web3Reducer.js | 7 +++++-- src/web3/web3Saga.js | 43 +++++++++++++++++++---------------------- yarn.lock | 10 +++++----- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 2f26a24..1da0e22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ezerous/drizzle", - "version": "0.4.0", + "version": "0.4.1", "description": "A reactive data-store for web3 and smart contracts.", "license": "MIT", "author": "Ezerous ", diff --git a/src/defaultOptions.js b/src/defaultOptions.js index e157670..4e76a5e 100644 --- a/src/defaultOptions.js +++ b/src/defaultOptions.js @@ -1,5 +1,6 @@ const defaultOptions = { web3: { + customProvider:{}, fallback: { type: 'ws', url: 'ws://127.0.0.1:8545' diff --git a/src/web3/web3Reducer.js b/src/web3/web3Reducer.js index 9344cfa..b9a9cfa 100644 --- a/src/web3/web3Reducer.js +++ b/src/web3/web3Reducer.js @@ -2,7 +2,8 @@ import * as Action from './web3Actions' import { ACCOUNTS_FAILED } from '../accounts/accountsActions' const initialState = { - status: 'initializing' + status: 'initializing', + isMetaMask: false } const web3Reducer = (state = initialState, action) => { @@ -14,9 +15,11 @@ const web3Reducer = (state = initialState, action) => { } if (action.type === Action.WEB3_INITIALIZED) { + const { web3 } = action; return { ...state, - status: 'initialized' + status: 'initialized', + isMetaMask: !!(web3 && web3.currentProvider && web3.currentProvider.isMetaMask) } } diff --git a/src/web3/web3Saga.js b/src/web3/web3Saga.js index 8f54fb9..77f317f 100644 --- a/src/web3/web3Saga.js +++ b/src/web3/web3Saga.js @@ -33,31 +33,28 @@ export function * initializeWeb3 (options) { yield put({ type: Action.WEB3_FAILED, error }); } - } else if (typeof window.web3 !== 'undefined') { - // Checking if Web3 has been injected by the browser (Mist/MetaMask) - // Use Mist/MetaMask's provider. - web3 = new Web3(window.web3.currentProvider) - yield put({ type: Action.WEB3_INITIALIZED, web3 }) - - return web3 + } else if (options.customProvider) { + web3 = options.customProvider + yield put({ type: Action.WEB3_INITIALIZED, web3 }) + return web3 } else if (options.fallback) { - // Attempt fallback if no web3 injection. - switch (options.fallback.type) { - case 'ws': - const provider = new Web3.providers.WebsocketProvider( - options.fallback.url - ) - web3 = new Web3(provider) - yield put({ type: Action.WEB3_INITIALIZED, web3 }) - return web3 - - default: - // Invalid options; throw. - throw new Error('Invalid web3 fallback provided.') - } + // Attempt fallback if no web3 injection. + switch (options.fallback.type) { + case 'ws': + const provider = new Web3.providers.WebsocketProvider( + options.fallback.url + ) + web3 = new Web3(provider) + yield put({ type: Action.WEB3_INITIALIZED, web3 }) + return web3 + + default: + // Invalid options; throw. + throw new Error('Invalid web3 fallback provided.') + } } else { - // Out of web3 options; throw. - throw new Error('Cannot find injected web3 or valid fallback.') + // Out of web3 options; throw. + throw new Error('Cannot find injected web3 or valid fallback.') } } catch (error) { yield put({ type: Action.WEB3_FAILED, error }) diff --git a/yarn.lock b/yarn.lock index 2520532..01efb6c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -645,7 +645,7 @@ decompress-response@^3.2.0, decompress-response@^3.3.0: dependencies: mimic-response "^1.0.0" -deepmerge@4.2.2: +deepmerge@~4.2.2: version "4.2.2" resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955" integrity sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg== @@ -1226,7 +1226,7 @@ is-plain-obj@^1.1.0: resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e" integrity sha1-caUMhCnfync8kqOQpKA7OfzVHT4= -is-plain-object@5.0.0: +is-plain-object@~5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-5.0.0.tgz#4427f50ab3429e9025ea7d52e9043a9ef4159344" integrity sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q== @@ -1767,14 +1767,14 @@ readable-stream@^3.6.0: string_decoder "^1.1.1" util-deprecate "^1.0.1" -redux-saga@1.1.3: +redux-saga@~1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/redux-saga/-/redux-saga-1.1.3.tgz#9f3e6aebd3c994bbc0f6901a625f9a42b51d1112" integrity sha512-RkSn/z0mwaSa5/xH/hQLo8gNf4tlvT18qXDNvedihLcfzh+jMchDgaariQoehCpgRltEm4zHKJyINEz6aqswTw== dependencies: "@redux-saga/core" "^1.1.3" -redux@4.0.5, redux@^4.0.4: +redux@^4.0.4, redux@~4.0.5: version "4.0.5" resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.5.tgz#4db5de5816e17891de8a80c424232d06f051d93f" integrity sha512-VSz1uMAH24DM6MF72vcojpYPtrTUu3ByVWfPL1nPfVRb5mZVTve5GnNCUV53QM/BZ66xfWrm0CTWoM+Xlz8V1w== @@ -2417,7 +2417,7 @@ web3-utils@1.3.0: underscore "1.9.1" utf8 "3.0.0" -web3@1.3.0: +web3@~1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/web3/-/web3-1.3.0.tgz#8fe4cd6e2a21c91904f343ba75717ee4c76bb349" integrity sha512-4q9dna0RecnrlgD/bD1C5S+81Untbd6Z/TBD7rb+D5Bvvc0Wxjr4OP70x+LlnwuRDjDtzBwJbNUblh2grlVArw==