Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ class API {
}

hardhatTraceHandler(trace, isTraceFromCall){
if (trace.bytecode && trace.bytecode.instructions){
for (const instruction of trace.bytecode.instructions){
for (const step of trace.steps){
if (trace.bytecode && trace.bytecode._pcToInstruction){
const instruction = trace.bytecode._pcToInstruction.get(step.pc)
this.collector.trackHardhatEVMInstruction(instruction)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DataCollector {
* @param {HardhatEVMTraceInstruction} instruction
*/
trackHardhatEVMInstruction(instruction){
if (instruction.pushData){
if (instruction && instruction.pushData){
let hash = `0x` + instruction.pushData.toString('hex');
this._registerHash(hash)
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/hardhat.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ async function plugin(args, env) {
]);
}

// Set default account
network.from = accounts[0];
// Set default account (if not already configured)
nomiclabsUtils.setNetworkFrom(network.config, accounts);

// Run post-launch server hook;
await api.onServerReady(config);
Expand Down
15 changes: 14 additions & 1 deletion plugins/resources/nomiclabs.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ 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.
* @param {env} config
* @param {Array} accounts
*/
function setNetworkFrom(networkConfig, accounts){
if (!networkConfig.from){
networkConfig.from = accounts[0];
}
}

// TODO: Hardhat cacheing??
/**
* Generates a path to a temporary compilation cache directory
Expand Down Expand Up @@ -205,6 +217,7 @@ module.exports = {
setupHardhatNetwork: setupHardhatNetwork,
getTestFilePaths: getTestFilePaths,
getAccounts: getAccounts,
getNodeInfo: getNodeInfo
getNodeInfo: getNodeInfo,
setNetworkFrom: setNetworkFrom
}

9 changes: 9 additions & 0 deletions test/sources/js/account-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Simple = artifacts.require('Simple');

contract('Simple', () => {
it('should use 0xe7a46b209a65baadc11bf973c0f4d5f19465ae83', async function(){
const simple = await Simple.new()
const result = await simple.test(5);
assert.equal(result.receipt.from, "0xe7a46b209a65baadc11bf973c0f4d5f19465ae83")
});
});
9 changes: 9 additions & 0 deletions test/sources/js/account-zero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Simple = artifacts.require('Simple');

contract('Simple', () => {
it('should use 0x42ecc9ab31d7c0240532992682ee3533421dd7f5', async function(){
const simple = await Simple.new()
const result = await simple.test(5);
assert.equal(result.receipt.from, "0x42ecc9ab31d7c0240532992682ee3533421dd7f5")
});
});
4 changes: 2 additions & 2 deletions test/sources/solidity/contracts/app/Simple.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ pragma solidity ^0.5.3;

contract Simple {
uint x = 0;

function test(uint val) public {
x = x + val;
x = x + val;
}

function getX() public view returns (uint){
Expand Down
91 changes: 88 additions & 3 deletions test/units/hardhat/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ describe('Hardhat Plugin: standard use cases', function() {
mock.clean();

mock.loggerOutput.val = '';
solcoverConfig = { skipFiles: ['Migrations.sol']};
solcoverConfig = {
skipFiles: ['Migrations.sol'],
istanbulReporter: [ "json-summary", "text"]
};
hardhatConfig = mock.getDefaultHardhatConfig();
verify.cleanInitialState();
})
Expand Down Expand Up @@ -287,15 +290,15 @@ describe('Hardhat Plugin: standard use cases', function() {
const expected = [
{
file: mock.pathToContract(hardhatConfig, 'ContractA.sol'),
pct: 92.31
pct: 61.54
},
{
file: mock.pathToContract(hardhatConfig, 'ContractB.sol'),
pct: 0,
},
{
file: mock.pathToContract(hardhatConfig, 'B_Wallet.sol'),
pct: 100,
pct: 80,
},

];
Expand All @@ -310,4 +313,86 @@ describe('Hardhat Plugin: standard use cases', function() {

await this.env.run("coverage");
});

it('uses account[0] as default "from" (ganache)', async function(){
const mnemonic = "purity blame spice arm main narrow olive roof science verb parrot flash";
const account0 = "0x42ecc9ab31d7c0240532992682ee3533421dd7f5"
const taskArgs = {
network: "development"
}

solcoverConfig.providerOptions = {
mnemonic: mnemonic
};

mock.install('Simple', 'account-zero.js', solcoverConfig);
mock.hardhatSetupEnv(this);

await this.env.run("coverage", taskArgs);

const expected = [
{
file: mock.pathToContract(hardhatConfig, 'Simple.sol'),
pct: 50
}
];

verify.lineCoverage(expected);
})

it('inherits network defined "from" (ganache)', async function(){
const mnemonic = "purity blame spice arm main narrow olive roof science verb parrot flash";
const account1 = "0xe7a46b209a65baadc11bf973c0f4d5f19465ae83"
const taskArgs = {
network: "development"
}

solcoverConfig.providerOptions = {
mnemonic: mnemonic
};

const hardhatConfig = mock.getDefaultHardhatConfig()
hardhatConfig.networks.development.from = account1;

mock.install('Simple', 'account-one.js', solcoverConfig, hardhatConfig);
mock.hardhatSetupEnv(this);

await this.env.run("coverage", taskArgs);

const expected = [
{
file: mock.pathToContract(hardhatConfig, 'Simple.sol'),
pct: 50
}
];

verify.lineCoverage(expected);
})

it('inherits network defined "from" (hardhat)', async function(){
const mnemonic = "purity blame spice arm main narrow olive roof science verb parrot flash";
const account1 = "0xe7a46b209a65baadc11bf973c0f4d5f19465ae83"

const hardhatConfig = mock.getDefaultHardhatConfig()
hardhatConfig.networks.hardhat = {
from: account1,
accounts: {
mnemonic: mnemonic
}
}

mock.install('Simple', 'account-one.js', solcoverConfig, hardhatConfig);
mock.hardhatSetupEnv(this);

await this.env.run("coverage");

const expected = [
{
file: mock.pathToContract(hardhatConfig, 'Simple.sol'),
pct: 50
}
];

verify.lineCoverage(expected);
})
})
3 changes: 2 additions & 1 deletion test/util/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function decacheConfigs(){
`${process.cwd()}/${temp}/${buidlerConfigName}`,
`${process.cwd()}/${temp}/${hardhatConfigName}`,
`${process.cwd()}/${temp}/contracts/Simple.sol`,
`${process.cwd()}/${temp}/test/simple.js`
`${process.cwd()}/${temp}/test/simple.js`,
`${process.cwd()}/${temp}/test/account-one.js`
];

paths.forEach(pth => {
Expand Down