From 7aa2a8a983b7c99e561cfcc62626ae4e099e6d51 Mon Sep 17 00:00:00 2001 From: amerkadicE <97883527+amerkadicE@users.noreply.github.com> Date: Mon, 12 Sep 2022 22:46:48 +0200 Subject: [PATCH] Replace web3js package with ethersjs package (#15386) Co-authored-by: Alex Donesky --- app/scripts/controllers/detect-tokens.js | 3 - app/scripts/lib/account-tracker.js | 42 ++++----- lavamoat/browserify/beta/policy.json | 109 ++++++++++++----------- lavamoat/browserify/flask/policy.json | 109 ++++++++++++----------- lavamoat/browserify/main/policy.json | 109 ++++++++++++----------- lavamoat/build-system/policy.json | 2 +- package.json | 9 +- 7 files changed, 201 insertions(+), 182 deletions(-) diff --git a/app/scripts/controllers/detect-tokens.js b/app/scripts/controllers/detect-tokens.js index daef5c7ac..9c28e708c 100644 --- a/app/scripts/controllers/detect-tokens.js +++ b/app/scripts/controllers/detect-tokens.js @@ -1,4 +1,3 @@ -import Web3 from 'web3'; import { warn } from 'loglevel'; import { MINUTE } from '../../../shared/constants/time'; import { MAINNET_CHAIN_ID } from '../../../shared/constants/network'; @@ -112,7 +111,6 @@ export default class DetectTokensController { : tokenList; const tokensToDetect = []; - this.web3.setProvider(this._network._provider); for (const tokenAddress in tokenListUsed) { if ( !this.tokenAddresses.find(({ address }) => @@ -220,7 +218,6 @@ export default class DetectTokensController { return; } this._network = network; - this.web3 = new Web3(network._provider); this._network.store.subscribe(() => { if (this.chainId !== this.getChainIdFromNetworkStore(network)) { this.restartTokenDetection(); diff --git a/app/scripts/lib/account-tracker.js b/app/scripts/lib/account-tracker.js index 8dea01fc9..768da310b 100644 --- a/app/scripts/lib/account-tracker.js +++ b/app/scripts/lib/account-tracker.js @@ -12,7 +12,7 @@ import EthQuery from 'eth-query'; import { ObservableStore } from '@metamask/obs-store'; import log from 'loglevel'; import pify from 'pify'; -import Web3 from 'web3'; +import { ethers } from 'ethers'; import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi'; import { MAINNET_CHAIN_ID, @@ -41,7 +41,6 @@ import { SINGLE_CALL_BALANCES_ADDRESS_FANTOM, SINGLE_CALL_BALANCES_ADDRESS_ARBITRUM, } from '../constants/contracts'; -import { bnToHex } from './util'; /** * This module is responsible for tracking any number of accounts and caching their current balances & transaction @@ -85,7 +84,7 @@ export default class AccountTracker { this._updateForBlock = this._updateForBlock.bind(this); this.getCurrentChainId = opts.getCurrentChainId; - this.web3 = new Web3(this._provider); + this.ethersProvider = new ethers.providers.Web3Provider(this._provider); } start() { @@ -336,26 +335,29 @@ export default class AccountTracker { */ async _updateAccountsViaBalanceChecker(addresses, deployedContractAddress) { const { accounts } = this.store.getState(); - this.web3.setProvider(this._provider); - const ethContract = this.web3.eth - .contract(SINGLE_CALL_BALANCES_ABI) - .at(deployedContractAddress); - const ethBalance = ['0x0']; - - ethContract.balances(addresses, ethBalance, (error, result) => { - if (error) { - log.warn( - `MetaMask - Account Tracker single call balance fetch failed`, - error, - ); - Promise.all(addresses.map(this._updateAccount.bind(this))); - return; - } + this.ethersProvider = new ethers.providers.Web3Provider(this._provider); + + const ethContract = await new ethers.Contract( + deployedContractAddress, + SINGLE_CALL_BALANCES_ABI, + this.ethersProvider, + ); + const ethBalance = ['0x0000000000000000000000000000000000000000']; + + try { + const balances = await ethContract.balances(addresses, ethBalance); + addresses.forEach((address, index) => { - const balance = result[index] ? bnToHex(result[index]) : '0x0'; + const balance = balances[index] ? balances[index].toHexString() : '0x0'; accounts[address] = { address, balance }; }); this.store.updateState({ accounts }); - }); + } catch (error) { + log.warn( + `MetaMask - Account Tracker single call balance fetch failed`, + error, + ); + Promise.all(addresses.map(this._updateAccount.bind(this))); + } } } diff --git a/lavamoat/browserify/beta/policy.json b/lavamoat/browserify/beta/policy.json index 3dc28b6cf..effd83c55 100644 --- a/lavamoat/browserify/beta/policy.json +++ b/lavamoat/browserify/beta/policy.json @@ -2641,6 +2641,7 @@ "@metamask/controllers>isomorphic-fetch": true, "@metamask/controllers>multiformats": true, "@metamask/controllers>nanoid": true, + "@metamask/controllers>web3": true, "@metamask/controllers>web3-provider-engine": true, "@metamask/metamask-eth-abis": true, "browserify>buffer": true, @@ -2661,8 +2662,7 @@ "jsonschema": true, "punycode": true, "single-call-balance-checker-abi": true, - "uuid": true, - "web3": true + "uuid": true } }, "@metamask/controllers>abort-controller": { @@ -2805,6 +2805,22 @@ "crypto.getRandomValues": true } }, + "@metamask/controllers>web3": { + "globals": { + "Web3": "write", + "XMLHttpRequest": true, + "clearTimeout": true, + "console.error": true, + "setTimeout": true + }, + "packages": { + "@metamask/controllers>web3>bignumber.js": true, + "@metamask/controllers>web3>crypto-js": true, + "@metamask/controllers>web3>utf8": true, + "@metamask/controllers>web3>xhr2-cookies": true, + "browserify>buffer": true + } + }, "@metamask/controllers>web3-provider-engine": { "globals": { "WebSocket": true, @@ -2943,6 +2959,38 @@ "browserify>process": true } }, + "@metamask/controllers>web3>bignumber.js": { + "globals": { + "define": true + }, + "packages": { + "browserify>crypto-browserify": true + } + }, + "@metamask/controllers>web3>crypto-js": { + "globals": { + "define": true + } + }, + "@metamask/controllers>web3>utf8": { + "globals": { + "define": true + } + }, + "@metamask/controllers>web3>xhr2-cookies": { + "globals": { + "console.warn": true + }, + "packages": { + "browserify>buffer": true, + "browserify>https-browserify": true, + "browserify>os-browserify": true, + "browserify>process": true, + "browserify>stream-http": true, + "browserify>url": true, + "pubnub>superagent>cookiejar": true + } + }, "@metamask/eth-ledger-bridge-keyring": { "globals": { "addEventListener": true, @@ -5754,6 +5802,11 @@ "browserify>buffer": true } }, + "pubnub>superagent>cookiejar": { + "globals": { + "console.warn": true + } + }, "pump": { "packages": { "browserify>browser-resolve": true, @@ -6308,18 +6361,7 @@ }, "web3": { "globals": { - "Web3": "write", - "XMLHttpRequest": true, - "clearTimeout": true, - "console.error": true, - "setTimeout": true - }, - "packages": { - "browserify>buffer": true, - "web3>bignumber.js": true, - "web3>crypto-js": true, - "web3>utf8": true, - "web3>xhr2-cookies": true + "XMLHttpRequest": true } }, "web3-stream-provider": { @@ -6338,43 +6380,6 @@ "msCrypto": true } }, - "web3>bignumber.js": { - "globals": { - "define": true - }, - "packages": { - "browserify>crypto-browserify": true - } - }, - "web3>crypto-js": { - "globals": { - "define": true - } - }, - "web3>utf8": { - "globals": { - "define": true - } - }, - "web3>xhr2-cookies": { - "globals": { - "console.warn": true - }, - "packages": { - "browserify>buffer": true, - "browserify>https-browserify": true, - "browserify>os-browserify": true, - "browserify>process": true, - "browserify>stream-http": true, - "browserify>url": true, - "web3>xhr2-cookies>cookiejar": true - } - }, - "web3>xhr2-cookies>cookiejar": { - "globals": { - "console.warn": true - } - }, "webextension-polyfill": { "globals": { "browser": true, @@ -6390,4 +6395,4 @@ } } } -} \ No newline at end of file +} diff --git a/lavamoat/browserify/flask/policy.json b/lavamoat/browserify/flask/policy.json index 161988fc3..a6211fa8b 100644 --- a/lavamoat/browserify/flask/policy.json +++ b/lavamoat/browserify/flask/policy.json @@ -2786,6 +2786,7 @@ "@metamask/controllers>isomorphic-fetch": true, "@metamask/controllers>multiformats": true, "@metamask/controllers>nanoid": true, + "@metamask/controllers>web3": true, "@metamask/controllers>web3-provider-engine": true, "@metamask/metamask-eth-abis": true, "browserify>buffer": true, @@ -2806,8 +2807,7 @@ "jsonschema": true, "punycode": true, "single-call-balance-checker-abi": true, - "uuid": true, - "web3": true + "uuid": true } }, "@metamask/controllers>abort-controller": { @@ -2950,6 +2950,22 @@ "crypto.getRandomValues": true } }, + "@metamask/controllers>web3": { + "globals": { + "Web3": "write", + "XMLHttpRequest": true, + "clearTimeout": true, + "console.error": true, + "setTimeout": true + }, + "packages": { + "@metamask/controllers>web3>bignumber.js": true, + "@metamask/controllers>web3>crypto-js": true, + "@metamask/controllers>web3>utf8": true, + "@metamask/controllers>web3>xhr2-cookies": true, + "browserify>buffer": true + } + }, "@metamask/controllers>web3-provider-engine": { "globals": { "WebSocket": true, @@ -3088,6 +3104,38 @@ "browserify>process": true } }, + "@metamask/controllers>web3>bignumber.js": { + "globals": { + "define": true + }, + "packages": { + "browserify>crypto-browserify": true + } + }, + "@metamask/controllers>web3>crypto-js": { + "globals": { + "define": true + } + }, + "@metamask/controllers>web3>utf8": { + "globals": { + "define": true + } + }, + "@metamask/controllers>web3>xhr2-cookies": { + "globals": { + "console.warn": true + }, + "packages": { + "browserify>buffer": true, + "browserify>https-browserify": true, + "browserify>os-browserify": true, + "browserify>process": true, + "browserify>stream-http": true, + "browserify>url": true, + "pubnub>superagent>cookiejar": true + } + }, "@metamask/eth-ledger-bridge-keyring": { "globals": { "addEventListener": true, @@ -6328,6 +6376,11 @@ "browserify>buffer": true } }, + "pubnub>superagent>cookiejar": { + "globals": { + "console.warn": true + } + }, "pump": { "packages": { "browserify>browser-resolve": true, @@ -6939,18 +6992,7 @@ }, "web3": { "globals": { - "Web3": "write", - "XMLHttpRequest": true, - "clearTimeout": true, - "console.error": true, - "setTimeout": true - }, - "packages": { - "browserify>buffer": true, - "web3>bignumber.js": true, - "web3>crypto-js": true, - "web3>utf8": true, - "web3>xhr2-cookies": true + "XMLHttpRequest": true } }, "web3-stream-provider": { @@ -6969,43 +7011,6 @@ "msCrypto": true } }, - "web3>bignumber.js": { - "globals": { - "define": true - }, - "packages": { - "browserify>crypto-browserify": true - } - }, - "web3>crypto-js": { - "globals": { - "define": true - } - }, - "web3>utf8": { - "globals": { - "define": true - } - }, - "web3>xhr2-cookies": { - "globals": { - "console.warn": true - }, - "packages": { - "browserify>buffer": true, - "browserify>https-browserify": true, - "browserify>os-browserify": true, - "browserify>process": true, - "browserify>stream-http": true, - "browserify>url": true, - "web3>xhr2-cookies>cookiejar": true - } - }, - "web3>xhr2-cookies>cookiejar": { - "globals": { - "console.warn": true - } - }, "webextension-polyfill": { "globals": { "browser": true, @@ -7021,4 +7026,4 @@ } } } -} \ No newline at end of file +} diff --git a/lavamoat/browserify/main/policy.json b/lavamoat/browserify/main/policy.json index 3dc28b6cf..effd83c55 100644 --- a/lavamoat/browserify/main/policy.json +++ b/lavamoat/browserify/main/policy.json @@ -2641,6 +2641,7 @@ "@metamask/controllers>isomorphic-fetch": true, "@metamask/controllers>multiformats": true, "@metamask/controllers>nanoid": true, + "@metamask/controllers>web3": true, "@metamask/controllers>web3-provider-engine": true, "@metamask/metamask-eth-abis": true, "browserify>buffer": true, @@ -2661,8 +2662,7 @@ "jsonschema": true, "punycode": true, "single-call-balance-checker-abi": true, - "uuid": true, - "web3": true + "uuid": true } }, "@metamask/controllers>abort-controller": { @@ -2805,6 +2805,22 @@ "crypto.getRandomValues": true } }, + "@metamask/controllers>web3": { + "globals": { + "Web3": "write", + "XMLHttpRequest": true, + "clearTimeout": true, + "console.error": true, + "setTimeout": true + }, + "packages": { + "@metamask/controllers>web3>bignumber.js": true, + "@metamask/controllers>web3>crypto-js": true, + "@metamask/controllers>web3>utf8": true, + "@metamask/controllers>web3>xhr2-cookies": true, + "browserify>buffer": true + } + }, "@metamask/controllers>web3-provider-engine": { "globals": { "WebSocket": true, @@ -2943,6 +2959,38 @@ "browserify>process": true } }, + "@metamask/controllers>web3>bignumber.js": { + "globals": { + "define": true + }, + "packages": { + "browserify>crypto-browserify": true + } + }, + "@metamask/controllers>web3>crypto-js": { + "globals": { + "define": true + } + }, + "@metamask/controllers>web3>utf8": { + "globals": { + "define": true + } + }, + "@metamask/controllers>web3>xhr2-cookies": { + "globals": { + "console.warn": true + }, + "packages": { + "browserify>buffer": true, + "browserify>https-browserify": true, + "browserify>os-browserify": true, + "browserify>process": true, + "browserify>stream-http": true, + "browserify>url": true, + "pubnub>superagent>cookiejar": true + } + }, "@metamask/eth-ledger-bridge-keyring": { "globals": { "addEventListener": true, @@ -5754,6 +5802,11 @@ "browserify>buffer": true } }, + "pubnub>superagent>cookiejar": { + "globals": { + "console.warn": true + } + }, "pump": { "packages": { "browserify>browser-resolve": true, @@ -6308,18 +6361,7 @@ }, "web3": { "globals": { - "Web3": "write", - "XMLHttpRequest": true, - "clearTimeout": true, - "console.error": true, - "setTimeout": true - }, - "packages": { - "browserify>buffer": true, - "web3>bignumber.js": true, - "web3>crypto-js": true, - "web3>utf8": true, - "web3>xhr2-cookies": true + "XMLHttpRequest": true } }, "web3-stream-provider": { @@ -6338,43 +6380,6 @@ "msCrypto": true } }, - "web3>bignumber.js": { - "globals": { - "define": true - }, - "packages": { - "browserify>crypto-browserify": true - } - }, - "web3>crypto-js": { - "globals": { - "define": true - } - }, - "web3>utf8": { - "globals": { - "define": true - } - }, - "web3>xhr2-cookies": { - "globals": { - "console.warn": true - }, - "packages": { - "browserify>buffer": true, - "browserify>https-browserify": true, - "browserify>os-browserify": true, - "browserify>process": true, - "browserify>stream-http": true, - "browserify>url": true, - "web3>xhr2-cookies>cookiejar": true - } - }, - "web3>xhr2-cookies>cookiejar": { - "globals": { - "console.warn": true - } - }, "webextension-polyfill": { "globals": { "browser": true, @@ -6390,4 +6395,4 @@ } } } -} \ No newline at end of file +} diff --git a/lavamoat/build-system/policy.json b/lavamoat/build-system/policy.json index 6bee6dcf2..7bf6ebfba 100644 --- a/lavamoat/build-system/policy.json +++ b/lavamoat/build-system/policy.json @@ -8533,4 +8533,4 @@ } } } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 04e492393..fca4f5294 100644 --- a/package.json +++ b/package.json @@ -232,7 +232,6 @@ "unicode-confusables": "^0.1.1", "uuid": "^8.3.2", "valid-url": "^1.0.9", - "web3": "^0.20.7", "web3-stream-provider": "^4.0.0", "zxcvbn": "^4.4.2" }, @@ -473,7 +472,13 @@ "eth-lattice-keyring>gridplus-sdk>secp256k1": false, "eth-lattice-keyring>secp256k1": false, "@storybook/react>@pmmmwh/react-refresh-webpack-plugin>core-js-pure": false, - "@testing-library/jest-dom>aria-query>@babel/runtime-corejs3>core-js-pure": false + "@testing-library/jest-dom>aria-query>@babel/runtime-corejs3>core-js-pure": false, + "web3": false, + "web3>web3-bzz": false, + "web3>web3-core>web3-core-requestmanager>web3-providers-ws>websocket>bufferutil": false, + "web3>web3-core>web3-core-requestmanager>web3-providers-ws>websocket>es5-ext": false, + "web3>web3-core>web3-core-requestmanager>web3-providers-ws>websocket>utf-8-validate": false, + "web3>web3-shh": false } } }