diff --git a/package.json b/package.json index db76f4b2..bf03ce9b 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "recursive-readdir": "^2.2.2", "sc-istanbul": "^0.4.5", "shelljs": "^0.8.3", - "web3": "1.2.9" + "web3-utils": "^1.3.0" }, "devDependencies": { "@nomiclabs/buidler": "^1.3.6", diff --git a/plugins/buidler.plugin.js b/plugins/buidler.plugin.js index d7a8f481..8449d1a5 100644 --- a/plugins/buidler.plugin.js +++ b/plugins/buidler.plugin.js @@ -6,7 +6,6 @@ const PluginUI = require('./resources/nomiclabs.ui'); const pkg = require('./../package.json'); const death = require('death'); const path = require('path'); -const Web3 = require('web3'); const { task, types } = require("@nomiclabs/buidler/config"); const { ensurePluginLoadedWithUsePlugin } = require("@nomiclabs/buidler/plugins"); @@ -57,13 +56,12 @@ function plugin() { const client = api.client || require('ganache-cli'); const address = await api.ganache(client); - const web3 = new Web3(address); - const accounts = await web3.eth.getAccounts(); - const nodeInfo = await web3.eth.getNodeInfo(); - const ganacheVersion = nodeInfo.split('/')[1]; + const accountsRequest = await utils.getAccountsGanache(api.server.provider); + const nodeInfoRequest = await utils.getNodeInfoGanache(api.server.provider); + const ganacheVersion = nodeInfoRequest.result.split('/')[1]; // Set default account - network.from = accounts[0]; + network.from = accountsRequest.result[0]; // Version Info ui.report('versions', [ diff --git a/plugins/hardhat.plugin.js b/plugins/hardhat.plugin.js index 08228c9b..99c8fbaf 100644 --- a/plugins/hardhat.plugin.js +++ b/plugins/hardhat.plugin.js @@ -79,7 +79,6 @@ task("coverage", "Generates a code coverage report for tests") let config; let client; let address; - let web3; let failedTests = 0; instrumentedSources = {}; @@ -150,8 +149,8 @@ task("coverage", "Generates a code coverage report for tests") const network = nomiclabsUtils.setupHardhatNetwork(env, api, ui); if (network.isHardhatEVM){ - accounts = await nomiclabsUtils.getAccounts(network.provider); - nodeInfo = await nomiclabsUtils.getNodeInfo(network.provider); + accounts = await utils.getAccountsHardhat(network.provider); + nodeInfo = await utils.getNodeInfoHardhat(network.provider); api.attachToHardhatVM(network.provider); @@ -160,18 +159,18 @@ task("coverage", "Generates a code coverage report for tests") env.network.name, ]); } else { - const Web3 = require('web3'); client = api.client || require('ganache-cli'); address = await api.ganache(client); - web3 = new Web3(address); - accounts = await web3.eth.getAccounts(); - nodeInfo = await web3.eth.getNodeInfo(); + const accountsRequest = await utils.getAccountsGanache(api.server.provider); + const nodeInfoRequest = await utils.getNodeInfoGanache(api.server.provider); ui.report('ganache-network', [ - nodeInfo.split('/')[1], + nodeInfoRequest.result.split('/')[1], env.network.name, api.port ]); + + accounts = accountsRequest.result; } // Set default account (if not already configured) diff --git a/plugins/resources/nomiclabs.utils.js b/plugins/resources/nomiclabs.utils.js index 3dda458b..1100edd1 100644 --- a/plugins/resources/nomiclabs.utils.js +++ b/plugins/resources/nomiclabs.utils.js @@ -156,14 +156,6 @@ function configureHttpProvider(networkConfig, api, ui){ networkConfig.url = `http://${api.host}:${api.port}`; } -async function getAccounts(provider){ - return provider.send("eth_accounts", []) -} - -async function getNodeInfo(provider){ - return provider.send("web3_clientVersion", []) -} - /** * Sets the default `from` account field in the network that will be used. * This needs to be done after accounts are fetched from the launched client. @@ -208,14 +200,12 @@ async function finish(config, api){ } module.exports = { - normalizeConfig: normalizeConfig, - finish: finish, - tempCacheDir: tempCacheDir, - setupBuidlerNetwork: setupBuidlerNetwork, - setupHardhatNetwork: setupHardhatNetwork, - getTestFilePaths: getTestFilePaths, - getAccounts: getAccounts, - getNodeInfo: getNodeInfo, - setNetworkFrom: setNetworkFrom + normalizeConfig, + finish, + tempCacheDir, + setupBuidlerNetwork, + setupHardhatNetwork, + getTestFilePaths, + setNetworkFrom } diff --git a/plugins/resources/plugin.utils.js b/plugins/resources/plugin.utils.js index 933cfdab..487a8c52 100644 --- a/plugins/resources/plugin.utils.js +++ b/plugins/resources/plugin.utils.js @@ -233,6 +233,46 @@ function loadSolcoverJS(config={}){ return coverageConfig; } +// ========================== +// Setup RPC Calls +// ========================== +async function getAccountsHardhat(provider){ + return provider.send("eth_accounts", []) +} + +async function getNodeInfoHardhat(provider){ + return provider.send("web3_clientVersion", []) +} + +async function getAccountsGanache(provider){ + const payload = { + jsonrpc: "2.0", + method: "eth_accounts", + params: [], + id: 1 + }; + return ganacheRequest(provider, payload) +} + +async function getNodeInfoGanache(provider){ + const payload = { + jsonrpc: "2.0", + method: "web3_clientVersion", + params: [], + id: 1 + }; + return ganacheRequest(provider, payload) +} + +async function ganacheRequest(provider, payload){ + return new Promise((resolve, reject) => { + provider.sendAsync(payload, function(err, res){ + if (err) return reject(err) + resolve(res); + }) + }); +} + // ========================== // Finishing / Cleanup // ========================== @@ -258,16 +298,20 @@ async function finish(config, api){ } module.exports = { - assembleFiles: assembleFiles, - assembleSkipped: assembleSkipped, - assembleTargets: assembleTargets, - checkContext: checkContext, - finish: finish, - getTempLocations: getTempLocations, - loadSource: loadSource, - loadSolcoverJS: loadSolcoverJS, - reportSkipped: reportSkipped, - save: save, - toRelativePath: toRelativePath, - setupTempFolders: setupTempFolders + assembleFiles, + assembleSkipped, + assembleTargets, + checkContext, + finish, + getTempLocations, + loadSource, + loadSolcoverJS, + reportSkipped, + save, + toRelativePath, + setupTempFolders, + getAccountsHardhat, + getNodeInfoHardhat, + getAccountsGanache, + getNodeInfoGanache } diff --git a/plugins/resources/truffle.utils.js b/plugins/resources/truffle.utils.js index f33f109f..d729ab4b 100644 --- a/plugins/resources/truffle.utils.js +++ b/plugins/resources/truffle.utils.js @@ -209,9 +209,9 @@ function normalizeConfig(config){ } module.exports = { - getTestFilePaths: getTestFilePaths, - setNetwork: setNetwork, - setNetworkFrom: setNetworkFrom, - loadLibrary: loadLibrary, - normalizeConfig: normalizeConfig, + getTestFilePaths, + setNetwork, + setNetworkFrom, + loadLibrary, + normalizeConfig, } diff --git a/plugins/truffle.plugin.js b/plugins/truffle.plugin.js index 8e337c6e..6888d67d 100644 --- a/plugins/truffle.plugin.js +++ b/plugins/truffle.plugin.js @@ -5,8 +5,6 @@ const PluginUI = require('./resources/truffle.ui'); const pkg = require('./../package.json'); const death = require('death'); const path = require('path'); -const Web3 = require('web3'); - /** * Truffle Plugin: `truffle run coverage [options]` @@ -37,13 +35,11 @@ async function plugin(config){ // Server launch const client = api.client || truffle.ganache; const address = await api.ganache(client); + const accountsRequest = await utils.getAccountsGanache(api.server.provider); + const nodeInfoRequest = await utils.getNodeInfoGanache(api.server.provider); + const ganacheVersion = nodeInfoRequest.result.split('/')[1]; - const web3 = new Web3(address); - const accounts = await web3.eth.getAccounts(); - const nodeInfo = await web3.eth.getNodeInfo(); - const ganacheVersion = nodeInfo.split('/')[1]; - - truffleUtils.setNetworkFrom(config, accounts); + truffleUtils.setNetworkFrom(config, accountsRequest.result); // Version Info ui.report('versions', [ diff --git a/yarn.lock b/yarn.lock index 3eb1eda6..76158999 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1057,6 +1057,10 @@ bn.js@4.11.8, bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.10.0, bn.js@^4. version "4.11.8" resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f" +bn.js@^4.11.9: + version "4.11.9" + resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.9.tgz#26d556829458f9d1e81fc48952493d0ba3507828" + body-parser@1.19.0, body-parser@^1.16.0: version "1.19.0" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a" @@ -2224,6 +2228,14 @@ eth-lib@0.2.7: elliptic "^6.4.0" xhr-request-promise "^0.1.2" +eth-lib@0.2.8, eth-lib@^0.2.8: + version "0.2.8" + resolved "https://registry.yarnpkg.com/eth-lib/-/eth-lib-0.2.8.tgz#b194058bef4b220ad12ea497431d6cb6aa0623c8" + dependencies: + bn.js "^4.11.6" + elliptic "^6.4.0" + xhr-request-promise "^0.1.2" + eth-lib@^0.1.26: version "0.1.27" resolved "https://registry.yarnpkg.com/eth-lib/-/eth-lib-0.1.27.tgz#f0b0fd144f865d2d6bf8257a40004f2e75ca1dd6" @@ -2236,14 +2248,6 @@ eth-lib@^0.1.26: ws "^3.0.0" xhr-request-promise "^0.1.2" -eth-lib@^0.2.8: - version "0.2.8" - resolved "https://registry.yarnpkg.com/eth-lib/-/eth-lib-0.2.8.tgz#b194058bef4b220ad12ea497431d6cb6aa0623c8" - dependencies: - bn.js "^4.11.6" - elliptic "^6.4.0" - xhr-request-promise "^0.1.2" - eth-sig-util@^2.5.2: version "2.5.2" resolved "https://registry.yarnpkg.com/eth-sig-util/-/eth-sig-util-2.5.2.tgz#f30b94509786fa4fbf71adb3164b1701e15724a8" @@ -6718,6 +6722,19 @@ web3-utils@1.2.9: underscore "1.9.1" utf8 "3.0.0" +web3-utils@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/web3-utils/-/web3-utils-1.3.0.tgz#5bac16e5e0ec9fe7bdcfadb621655e8aa3cf14e1" + dependencies: + bn.js "^4.11.9" + eth-lib "0.2.8" + ethereum-bloom-filters "^1.0.6" + ethjs-unit "0.1.6" + number-to-bn "1.7.0" + randombytes "^2.1.0" + underscore "1.9.1" + utf8 "3.0.0" + web3@1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/web3/-/web3-1.2.1.tgz#5d8158bcca47838ab8c2b784a2dee4c3ceb4179b"