Skip to content

Commit

Permalink
Replace Web3 with thin rpc wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Nov 16, 2020
1 parent 2cf6530 commit 7bfaef4
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 4 additions & 6 deletions plugins/buidler.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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', [
Expand Down
15 changes: 7 additions & 8 deletions plugins/hardhat.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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);

Expand All @@ -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)
Expand Down
24 changes: 7 additions & 17 deletions plugins/resources/nomiclabs.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

68 changes: 56 additions & 12 deletions plugins/resources/plugin.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ==========================
Expand All @@ -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
}
10 changes: 5 additions & 5 deletions plugins/resources/truffle.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ function normalizeConfig(config){
}

module.exports = {
getTestFilePaths: getTestFilePaths,
setNetwork: setNetwork,
setNetworkFrom: setNetworkFrom,
loadLibrary: loadLibrary,
normalizeConfig: normalizeConfig,
getTestFilePaths,
setNetwork,
setNetworkFrom,
loadLibrary,
normalizeConfig,
}
12 changes: 4 additions & 8 deletions plugins/truffle.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]`
Expand Down Expand Up @@ -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', [
Expand Down
33 changes: 25 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 7bfaef4

Please sign in to comment.