Skip to content

Commit

Permalink
👷 Add script for supporting calledOnContract matcher in hardhat (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
yivlad committed Jul 13, 2022
1 parent 09dccac commit 3ac5546
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 118 deletions.
112 changes: 3 additions & 109 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion waffle-hardhat/.mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ process.env.NODE_ENV = 'test'
module.exports = {
require: 'ts-node/register/transpile-only',
timeout: 50000,
spec: 'test/**/*.test.{js,ts}'
spec: 'test/**/*.test.{js,ts}',
file: 'test/test-setup.ts'
}
6 changes: 4 additions & 2 deletions waffle-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
"engines": {
"node": ">=10.0"
},
"dependencies": {
"ethers": "5.6.2",
"hardhat": "^2.9.2"
},
"devDependencies": {
"@ethereum-waffle/chai": "workspace:*",
"@nomiclabs/hardhat-ethers": "^2.0.3",
"@nomiclabs/hardhat-waffle": "^2.0.3",
"@types/node": "^17.0.41",
"eslint": "^7.14.0",
"ethereum-waffle": "workspace:*",
"ethers": "5.6.2",
"hardhat": "^2.9.2",
"mocha": "^8.2.1"
}
}
62 changes: 62 additions & 0 deletions waffle-hardhat/src/inject-call-history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {waffle} from 'hardhat';
import type {RecordedCall} from 'ethereum-waffle';
import {utils} from 'ethers';

class CallHistory {
recordedCalls: RecordedCall[] = [];

addUniqueCall(call: RecordedCall) {
if (!this.recordedCalls.find(c => c.address === call.address && c.data === call.data)) {
this.recordedCalls.push(call);
}
}

clearAll() {
this.recordedCalls = [];
}
}

function toRecordedCall(message: any): RecordedCall {
return {
address: message.to?.buf ? utils.getAddress(utils.hexlify(message.to.buf)) : undefined,
data: message.data ? utils.hexlify(message.data) : '0x'
};
}

const callHistory = new CallHistory();
(waffle.provider as any).clearCallHistory = () => {
callHistory.clearAll();
};

let beforeMessageListener: (message: any) => void | undefined;

const init = (waffle.provider as any)._hardhatNetwork.provider._wrapped._wrapped._wrapped._init;
(waffle.provider as any)._hardhatNetwork.provider._wrapped._wrapped._wrapped._init = async function () {
await init.apply(this);
if (typeof beforeMessageListener === 'function') {
// hast to be here because of weird behaviour of init function
(waffle.provider as any)
._hardhatNetwork
.provider
._wrapped
._wrapped
._wrapped
._node
._vmTracer
._vm
.off('beforeMessage', beforeMessageListener);
}
beforeMessageListener = (message: any) => {
callHistory.addUniqueCall(toRecordedCall(message));
};
const provider: any = waffle.provider;
provider.callHistory = callHistory.recordedCalls;
(waffle.provider as any)
._hardhatNetwork.provider
._wrapped._wrapped
._wrapped
._node
._vmTracer
._vm
.on('beforeMessage', beforeMessageListener);
};
6 changes: 4 additions & 2 deletions waffle-hardhat/test/calledOnContract/calledOnContract.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {waffle} from 'hardhat';
import {MockProvider} from 'ethereum-waffle';
// import {calledOnContractTest} from '@ethereum-waffle/chai/test';
import {calledOnContractTest} from '@ethereum-waffle/chai/test';

describe('INTEGRATION: calledOnContract', () => {
const provider = waffle.provider as MockProvider;
Expand All @@ -9,5 +9,7 @@ describe('INTEGRATION: calledOnContract', () => {
await provider.send('hardhat_reset', []);
});

// calledOnContractTest(provider);
beforeEach(() => provider.clearCallHistory());

calledOnContractTest(provider);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {waffle} from 'hardhat';
import {MockProvider} from 'ethereum-waffle';
// import {calledOnContractValidatorsTest} from '@ethereum-waffle/chai/test';
import {calledOnContractValidatorsTest} from '@ethereum-waffle/chai/test';

describe('INTEGRATION: ethCalledValidators', () => {
const provider = waffle.provider as MockProvider;
Expand All @@ -9,5 +9,7 @@ describe('INTEGRATION: ethCalledValidators', () => {
await provider.send('hardhat_reset', []);
});

// calledOnContractValidatorsTest(provider);
beforeEach(() => provider.clearCallHistory());

calledOnContractValidatorsTest(provider);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {waffle} from 'hardhat';
import {MockProvider} from 'ethereum-waffle';
// import {calledOnContractWithTest} from '@ethereum-waffle/chai/test';
import {calledOnContractWithTest} from '@ethereum-waffle/chai/test';

describe('INTEGRATION: calledOnContractWith', () => {
const provider = waffle.provider as MockProvider;
Expand All @@ -9,5 +9,7 @@ describe('INTEGRATION: calledOnContractWith', () => {
await provider.send('hardhat_reset', []);
});

// calledOnContractWithTest(provider);
beforeEach(() => provider.clearCallHistory());

calledOnContractWithTest(provider);
});
1 change: 1 addition & 0 deletions waffle-hardhat/test/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../src/inject-call-history';

0 comments on commit 3ac5546

Please sign in to comment.