From 2687163dbb623ef03acdb95af2fc2ed149e0e242 Mon Sep 17 00:00:00 2001 From: Erik Marks <25517051+rekmarks@users.noreply.github.com> Date: Tue, 17 Nov 2020 11:07:59 -0800 Subject: [PATCH] Fix minor issues with web3 metrics (#9895) * Fix minor issues with web3 metrics * Log error, use try/catch --- app/scripts/lib/setupWeb3.js | 126 +++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/app/scripts/lib/setupWeb3.js b/app/scripts/lib/setupWeb3.js index 922d29cf5..8f196a94e 100644 --- a/app/scripts/lib/setupWeb3.js +++ b/app/scripts/lib/setupWeb3.js @@ -71,15 +71,19 @@ export default function setupWeb3(log) { for (const [key, path] of applyTrapKeys) { web3[topKey][key] = new Proxy(web3[topKey][key], { apply: (...params) => { - window.ethereum.request({ - method: 'metamask_logInjectedWeb3Usage', - params: [ - { - action: 'apply', - path, - }, - ], - }) + try { + window.ethereum.request({ + method: 'metamask_logInjectedWeb3Usage', + params: [ + { + action: 'apply', + path, + }, + ], + }) + } catch (error) { + log.debug('Failed to log web3 usage.', error) + } // Call function normally return Reflect.apply(...params) @@ -93,15 +97,19 @@ export default function setupWeb3(log) { const name = stringifyKey(key) if (getTrapKeys.has(name)) { - window.ethereum.request({ - method: 'metamask_logInjectedWeb3Usage', - params: [ - { - action: 'get', - path: getTrapKeys.get(name), - }, - ], - }) + try { + window.ethereum.request({ + method: 'metamask_logInjectedWeb3Usage', + params: [ + { + action: 'get', + path: getTrapKeys.get(name), + }, + ], + }) + } catch (error) { + log.debug('Failed to log web3 usage.', error) + } } // return value normally @@ -109,46 +117,50 @@ export default function setupWeb3(log) { }, }) }) - } - const topLevelFunctions = [ - 'isConnected', - 'setProvider', - 'reset', - 'sha3', - 'toHex', - 'toAscii', - 'fromAscii', - 'toDecimal', - 'fromDecimal', - 'fromWei', - 'toWei', - 'toBigNumber', - 'isAddress', - ] - - // apply-trap top-level functions - topLevelFunctions.forEach((key) => { - // This type check is probably redundant, but we've been burned before. - if (typeof web3[key] === 'function') { - web3[key] = new Proxy(web3[key], { - apply: (...params) => { - window.ethereum.request({ - method: 'metamask_logInjectedWeb3Usage', - params: [ - { - action: 'apply', - path: `web3.${key}`, - }, - ], - }) - - // Call function normally - return Reflect.apply(...params) - }, - }) - } - }) + const topLevelFunctions = [ + 'isConnected', + 'setProvider', + 'reset', + 'sha3', + 'toHex', + 'toAscii', + 'fromAscii', + 'toDecimal', + 'fromDecimal', + 'fromWei', + 'toWei', + 'toBigNumber', + 'isAddress', + ] + + // apply-trap top-level functions + topLevelFunctions.forEach((key) => { + // This type check is probably redundant, but we've been burned before. + if (typeof web3[key] === 'function') { + web3[key] = new Proxy(web3[key], { + apply: (...params) => { + try { + window.ethereum.request({ + method: 'metamask_logInjectedWeb3Usage', + params: [ + { + action: 'apply', + path: `web3.${key}`, + }, + ], + }) + } catch (error) { + log.debug('Failed to log web3 usage.', error) + } + + // Call function normally + return Reflect.apply(...params) + }, + }) + } + }) + } const web3Proxy = new Proxy(web3, { get: (...params) => {