From e97e869cf349471a3916758e732d24c7a21e790a Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Wed, 2 Dec 2020 17:04:02 -0800 Subject: [PATCH 1/2] Log web3 shim usage --- src/shimWeb3.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/shimWeb3.js b/src/shimWeb3.js index 7a0c9568..3d31871c 100644 --- a/src/shimWeb3.js +++ b/src/shimWeb3.js @@ -28,6 +28,8 @@ module.exports = function shimWeb3 (provider) { console.error( `MetaMask no longer injects web3. For details, see: https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3`, ) + provider.request({ method: 'metamask_logWeb3ShimUsage' }) + .catch(() => undefined) } return Reflect.get(target, property, ...args) }, From 67a8cf63ed1eadb74936ec212206ccb878538c24 Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Wed, 2 Dec 2020 17:39:29 -0800 Subject: [PATCH 2/2] Update shim logging --- src/initializeProvider.js | 5 +++-- src/shimWeb3.js | 13 ++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/initializeProvider.js b/src/initializeProvider.js index 67b9a8a8..8611bf60 100644 --- a/src/initializeProvider.js +++ b/src/initializeProvider.js @@ -14,6 +14,7 @@ const shimWeb3 = require('./shimWeb3') */ function initializeProvider ({ connectionStream, + logger = console, maxEventListeners = 100, shouldSendMetadata = true, shouldSetOnWindow = true, @@ -21,7 +22,7 @@ function initializeProvider ({ } = {}) { let provider = new MetaMaskInpageProvider( - connectionStream, { shouldSendMetadata, maxEventListeners }, + connectionStream, { logger, maxEventListeners, shouldSendMetadata }, ) provider = new Proxy(provider, { @@ -34,7 +35,7 @@ function initializeProvider ({ } if (shouldShimWeb3) { - shimWeb3(provider) + shimWeb3(provider, logger) } return provider diff --git a/src/shimWeb3.js b/src/shimWeb3.js index 3d31871c..4577c94e 100644 --- a/src/shimWeb3.js +++ b/src/shimWeb3.js @@ -3,8 +3,9 @@ * not break dapps that rely on window.web3.currentProvider. * * @param {import('./MetaMaskInpageProvider')} provider - The provider to set as window.web3.currentProvider. + * @param {typeof console} log - The logging API to use. */ -module.exports = function shimWeb3 (provider) { +module.exports = function shimWeb3 (provider, log = console) { if (!window.web3) { const SHIM_IDENTIFIER = '__isMetaMaskShim__' @@ -21,20 +22,22 @@ module.exports = function shimWeb3 (provider) { { get: (target, property, ...args) => { if (property === 'currentProvider') { - console.warn( + log.warn( 'You are accessing the MetaMask window.web3.currentProvider shim. This property is deprecated; use window.ethereum instead. For details, see: https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3', ) } else if (property !== SHIM_IDENTIFIER) { - console.error( + log.error( `MetaMask no longer injects web3. For details, see: https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3`, ) provider.request({ method: 'metamask_logWeb3ShimUsage' }) - .catch(() => undefined) + .catch((error) => { + log.debug('MetaMask: Failed to log web3 shim usage.', error) + }) } return Reflect.get(target, property, ...args) }, set: (...args) => { - console.warn( + log.warn( 'You are accessing the MetaMask window.web3 shim. This object is deprecated; use window.ethereum instead. For details, see: https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3', ) return Reflect.set(...args)