-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add web3 usage metrics, prepare for web3 removal #9144
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Returns a middleware that implements the following RPC methods: | ||
* - metamask_logInjectedWeb3Usage | ||
* | ||
* @param {Object} opts - The middleware options | ||
* @param {string} opts.origin - The origin for the middleware stack | ||
* @param {Function} opts.sendMetrics - A function for sending a metrics event | ||
* @returns {(req: any, res: any, next: Function, end: Function) => void} | ||
*/ | ||
export default function createMethodMiddleware ({ origin, sendMetrics }) { | ||
return function methodMiddleware (req, res, next, end) { | ||
switch (req.method) { | ||
|
||
case 'metamask_logInjectedWeb3Usage': | ||
|
||
const { action, name } = req.params[0] | ||
|
||
sendMetrics({ | ||
action, | ||
name, | ||
customVariables: { origin }, | ||
}) | ||
|
||
res.result = true | ||
break | ||
|
||
default: | ||
return next() | ||
} | ||
return end() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,67 @@ | ||
/*global Web3*/ | ||
|
||
// TODO:deprecate:2020 | ||
// Delete this file | ||
|
||
export default function setupDappAutoReload (web3, observable) { | ||
import 'web3/dist/web3.min.js' | ||
|
||
const shouldLogUsage = !([ | ||
'docs.metamask.io', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting - are these sites using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right, the test dapp definitely is. For the others, it might be useful to know if they're using the injected There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I swear to verify this manually 😄 |
||
'metamask.github.io', | ||
'metamask.io', | ||
].includes(window.location.hostname)) | ||
|
||
export default function setupWeb3 (log) { | ||
// export web3 as a global, checking for usage | ||
let reloadInProgress = false | ||
let lastTimeUsed | ||
let lastSeenNetwork | ||
let hasBeenWarned = false | ||
|
||
const web3 = new Web3(window.ethereum) | ||
web3.setProvider = function () { | ||
log.debug('MetaMask - overrode web3.setProvider') | ||
} | ||
log.debug('MetaMask - injected web3') | ||
|
||
Object.defineProperty(window.ethereum, '_web3Ref', { | ||
enumerable: false, | ||
writable: true, | ||
configurable: true, | ||
value: web3.eth, | ||
}) | ||
|
||
const web3Proxy = new Proxy(web3, { | ||
get: (_web3, key) => { | ||
|
||
// get the time of use | ||
lastTimeUsed = Date.now() | ||
|
||
// show warning once on web3 access | ||
if (!hasBeenWarned && key !== 'currentProvider') { | ||
if (!hasBeenWarned) { | ||
console.warn(`MetaMask: We will stop injecting web3 in Q4 2020.\nPlease see this article for more information: https://medium.com/metamask/no-longer-injecting-web3-js-4a899ad6e59e`) | ||
hasBeenWarned = true | ||
} | ||
|
||
if (shouldLogUsage) { | ||
window.ethereum.request({ | ||
method: 'metamask_logInjectedWeb3Usage', | ||
params: [{ action: 'window.web3 get', name: key }], | ||
}) | ||
} | ||
|
||
// return value normally | ||
return _web3[key] | ||
}, | ||
set: (_web3, key, value) => { | ||
|
||
if (shouldLogUsage) { | ||
window.ethereum.request({ | ||
method: 'metamask_logInjectedWeb3Usage', | ||
params: [{ action: 'window.web3 set', name: key }], | ||
}) | ||
} | ||
|
||
// set value normally | ||
_web3[key] = value | ||
}, | ||
|
@@ -33,7 +74,7 @@ export default function setupDappAutoReload (web3, observable) { | |
value: web3Proxy, | ||
}) | ||
|
||
observable.subscribe(function (state) { | ||
window.ethereum._publicConfigStore.subscribe((state) => { | ||
// if the auto refresh on network change is false do not | ||
// do anything | ||
if (!window.ethereum.autoRefreshOnNetworkChange) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file and all references to its export were renamed to disambiguate it from the new top-level
methodMiddleware
.