Replace web3js package with ethersjs package (#15386)

Co-authored-by: Alex Donesky <adonesky@gmail.com>
feature/default_network_editable
amerkadicE 2 years ago committed by GitHub
parent 398b93cf51
commit 7aa2a8a983
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/scripts/controllers/detect-tokens.js
  2. 42
      app/scripts/lib/account-tracker.js
  3. 109
      lavamoat/browserify/beta/policy.json
  4. 109
      lavamoat/browserify/flask/policy.json
  5. 109
      lavamoat/browserify/main/policy.json
  6. 2
      lavamoat/build-system/policy.json
  7. 9
      package.json

@ -1,4 +1,3 @@
import Web3 from 'web3';
import { warn } from 'loglevel'; import { warn } from 'loglevel';
import { MINUTE } from '../../../shared/constants/time'; import { MINUTE } from '../../../shared/constants/time';
import { MAINNET_CHAIN_ID } from '../../../shared/constants/network'; import { MAINNET_CHAIN_ID } from '../../../shared/constants/network';
@ -112,7 +111,6 @@ export default class DetectTokensController {
: tokenList; : tokenList;
const tokensToDetect = []; const tokensToDetect = [];
this.web3.setProvider(this._network._provider);
for (const tokenAddress in tokenListUsed) { for (const tokenAddress in tokenListUsed) {
if ( if (
!this.tokenAddresses.find(({ address }) => !this.tokenAddresses.find(({ address }) =>
@ -220,7 +218,6 @@ export default class DetectTokensController {
return; return;
} }
this._network = network; this._network = network;
this.web3 = new Web3(network._provider);
this._network.store.subscribe(() => { this._network.store.subscribe(() => {
if (this.chainId !== this.getChainIdFromNetworkStore(network)) { if (this.chainId !== this.getChainIdFromNetworkStore(network)) {
this.restartTokenDetection(); this.restartTokenDetection();

@ -12,7 +12,7 @@ import EthQuery from 'eth-query';
import { ObservableStore } from '@metamask/obs-store'; import { ObservableStore } from '@metamask/obs-store';
import log from 'loglevel'; import log from 'loglevel';
import pify from 'pify'; import pify from 'pify';
import Web3 from 'web3'; import { ethers } from 'ethers';
import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi'; import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi';
import { import {
MAINNET_CHAIN_ID, MAINNET_CHAIN_ID,
@ -41,7 +41,6 @@ import {
SINGLE_CALL_BALANCES_ADDRESS_FANTOM, SINGLE_CALL_BALANCES_ADDRESS_FANTOM,
SINGLE_CALL_BALANCES_ADDRESS_ARBITRUM, SINGLE_CALL_BALANCES_ADDRESS_ARBITRUM,
} from '../constants/contracts'; } from '../constants/contracts';
import { bnToHex } from './util';
/** /**
* This module is responsible for tracking any number of accounts and caching their current balances & transaction * 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._updateForBlock = this._updateForBlock.bind(this);
this.getCurrentChainId = opts.getCurrentChainId; this.getCurrentChainId = opts.getCurrentChainId;
this.web3 = new Web3(this._provider); this.ethersProvider = new ethers.providers.Web3Provider(this._provider);
} }
start() { start() {
@ -336,26 +335,29 @@ export default class AccountTracker {
*/ */
async _updateAccountsViaBalanceChecker(addresses, deployedContractAddress) { async _updateAccountsViaBalanceChecker(addresses, deployedContractAddress) {
const { accounts } = this.store.getState(); const { accounts } = this.store.getState();
this.web3.setProvider(this._provider); this.ethersProvider = new ethers.providers.Web3Provider(this._provider);
const ethContract = this.web3.eth
.contract(SINGLE_CALL_BALANCES_ABI) const ethContract = await new ethers.Contract(
.at(deployedContractAddress); deployedContractAddress,
const ethBalance = ['0x0']; SINGLE_CALL_BALANCES_ABI,
this.ethersProvider,
ethContract.balances(addresses, ethBalance, (error, result) => { );
if (error) { const ethBalance = ['0x0000000000000000000000000000000000000000'];
log.warn(
`MetaMask - Account Tracker single call balance fetch failed`, try {
error, const balances = await ethContract.balances(addresses, ethBalance);
);
Promise.all(addresses.map(this._updateAccount.bind(this)));
return;
}
addresses.forEach((address, index) => { addresses.forEach((address, index) => {
const balance = result[index] ? bnToHex(result[index]) : '0x0'; const balance = balances[index] ? balances[index].toHexString() : '0x0';
accounts[address] = { address, balance }; accounts[address] = { address, balance };
}); });
this.store.updateState({ accounts }); 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)));
}
} }
} }

@ -2641,6 +2641,7 @@
"@metamask/controllers>isomorphic-fetch": true, "@metamask/controllers>isomorphic-fetch": true,
"@metamask/controllers>multiformats": true, "@metamask/controllers>multiformats": true,
"@metamask/controllers>nanoid": true, "@metamask/controllers>nanoid": true,
"@metamask/controllers>web3": true,
"@metamask/controllers>web3-provider-engine": true, "@metamask/controllers>web3-provider-engine": true,
"@metamask/metamask-eth-abis": true, "@metamask/metamask-eth-abis": true,
"browserify>buffer": true, "browserify>buffer": true,
@ -2661,8 +2662,7 @@
"jsonschema": true, "jsonschema": true,
"punycode": true, "punycode": true,
"single-call-balance-checker-abi": true, "single-call-balance-checker-abi": true,
"uuid": true, "uuid": true
"web3": true
} }
}, },
"@metamask/controllers>abort-controller": { "@metamask/controllers>abort-controller": {
@ -2805,6 +2805,22 @@
"crypto.getRandomValues": true "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": { "@metamask/controllers>web3-provider-engine": {
"globals": { "globals": {
"WebSocket": true, "WebSocket": true,
@ -2943,6 +2959,38 @@
"browserify>process": true "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": { "@metamask/eth-ledger-bridge-keyring": {
"globals": { "globals": {
"addEventListener": true, "addEventListener": true,
@ -5754,6 +5802,11 @@
"browserify>buffer": true "browserify>buffer": true
} }
}, },
"pubnub>superagent>cookiejar": {
"globals": {
"console.warn": true
}
},
"pump": { "pump": {
"packages": { "packages": {
"browserify>browser-resolve": true, "browserify>browser-resolve": true,
@ -6308,18 +6361,7 @@
}, },
"web3": { "web3": {
"globals": { "globals": {
"Web3": "write", "XMLHttpRequest": true
"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
} }
}, },
"web3-stream-provider": { "web3-stream-provider": {
@ -6338,43 +6380,6 @@
"msCrypto": true "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": { "webextension-polyfill": {
"globals": { "globals": {
"browser": true, "browser": true,
@ -6390,4 +6395,4 @@
} }
} }
} }
} }

@ -2786,6 +2786,7 @@
"@metamask/controllers>isomorphic-fetch": true, "@metamask/controllers>isomorphic-fetch": true,
"@metamask/controllers>multiformats": true, "@metamask/controllers>multiformats": true,
"@metamask/controllers>nanoid": true, "@metamask/controllers>nanoid": true,
"@metamask/controllers>web3": true,
"@metamask/controllers>web3-provider-engine": true, "@metamask/controllers>web3-provider-engine": true,
"@metamask/metamask-eth-abis": true, "@metamask/metamask-eth-abis": true,
"browserify>buffer": true, "browserify>buffer": true,
@ -2806,8 +2807,7 @@
"jsonschema": true, "jsonschema": true,
"punycode": true, "punycode": true,
"single-call-balance-checker-abi": true, "single-call-balance-checker-abi": true,
"uuid": true, "uuid": true
"web3": true
} }
}, },
"@metamask/controllers>abort-controller": { "@metamask/controllers>abort-controller": {
@ -2950,6 +2950,22 @@
"crypto.getRandomValues": true "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": { "@metamask/controllers>web3-provider-engine": {
"globals": { "globals": {
"WebSocket": true, "WebSocket": true,
@ -3088,6 +3104,38 @@
"browserify>process": true "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": { "@metamask/eth-ledger-bridge-keyring": {
"globals": { "globals": {
"addEventListener": true, "addEventListener": true,
@ -6328,6 +6376,11 @@
"browserify>buffer": true "browserify>buffer": true
} }
}, },
"pubnub>superagent>cookiejar": {
"globals": {
"console.warn": true
}
},
"pump": { "pump": {
"packages": { "packages": {
"browserify>browser-resolve": true, "browserify>browser-resolve": true,
@ -6939,18 +6992,7 @@
}, },
"web3": { "web3": {
"globals": { "globals": {
"Web3": "write", "XMLHttpRequest": true
"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
} }
}, },
"web3-stream-provider": { "web3-stream-provider": {
@ -6969,43 +7011,6 @@
"msCrypto": true "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": { "webextension-polyfill": {
"globals": { "globals": {
"browser": true, "browser": true,
@ -7021,4 +7026,4 @@
} }
} }
} }
} }

@ -2641,6 +2641,7 @@
"@metamask/controllers>isomorphic-fetch": true, "@metamask/controllers>isomorphic-fetch": true,
"@metamask/controllers>multiformats": true, "@metamask/controllers>multiformats": true,
"@metamask/controllers>nanoid": true, "@metamask/controllers>nanoid": true,
"@metamask/controllers>web3": true,
"@metamask/controllers>web3-provider-engine": true, "@metamask/controllers>web3-provider-engine": true,
"@metamask/metamask-eth-abis": true, "@metamask/metamask-eth-abis": true,
"browserify>buffer": true, "browserify>buffer": true,
@ -2661,8 +2662,7 @@
"jsonschema": true, "jsonschema": true,
"punycode": true, "punycode": true,
"single-call-balance-checker-abi": true, "single-call-balance-checker-abi": true,
"uuid": true, "uuid": true
"web3": true
} }
}, },
"@metamask/controllers>abort-controller": { "@metamask/controllers>abort-controller": {
@ -2805,6 +2805,22 @@
"crypto.getRandomValues": true "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": { "@metamask/controllers>web3-provider-engine": {
"globals": { "globals": {
"WebSocket": true, "WebSocket": true,
@ -2943,6 +2959,38 @@
"browserify>process": true "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": { "@metamask/eth-ledger-bridge-keyring": {
"globals": { "globals": {
"addEventListener": true, "addEventListener": true,
@ -5754,6 +5802,11 @@
"browserify>buffer": true "browserify>buffer": true
} }
}, },
"pubnub>superagent>cookiejar": {
"globals": {
"console.warn": true
}
},
"pump": { "pump": {
"packages": { "packages": {
"browserify>browser-resolve": true, "browserify>browser-resolve": true,
@ -6308,18 +6361,7 @@
}, },
"web3": { "web3": {
"globals": { "globals": {
"Web3": "write", "XMLHttpRequest": true
"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
} }
}, },
"web3-stream-provider": { "web3-stream-provider": {
@ -6338,43 +6380,6 @@
"msCrypto": true "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": { "webextension-polyfill": {
"globals": { "globals": {
"browser": true, "browser": true,
@ -6390,4 +6395,4 @@
} }
} }
} }
} }

@ -8533,4 +8533,4 @@
} }
} }
} }
} }

@ -232,7 +232,6 @@
"unicode-confusables": "^0.1.1", "unicode-confusables": "^0.1.1",
"uuid": "^8.3.2", "uuid": "^8.3.2",
"valid-url": "^1.0.9", "valid-url": "^1.0.9",
"web3": "^0.20.7",
"web3-stream-provider": "^4.0.0", "web3-stream-provider": "^4.0.0",
"zxcvbn": "^4.4.2" "zxcvbn": "^4.4.2"
}, },
@ -473,7 +472,13 @@
"eth-lattice-keyring>gridplus-sdk>secp256k1": false, "eth-lattice-keyring>gridplus-sdk>secp256k1": false,
"eth-lattice-keyring>secp256k1": false, "eth-lattice-keyring>secp256k1": false,
"@storybook/react>@pmmmwh/react-refresh-webpack-plugin>core-js-pure": 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
} }
} }
} }

Loading…
Cancel
Save