-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🦷 Automatically inject call history into hardhat provider (#776)
- Loading branch information
Showing
11 changed files
with
109 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@ethereum-waffle/chai": patch | ||
"@ethereum-waffle/hardhat": patch | ||
"@ethereum-waffle/optimism": patch | ||
--- | ||
|
||
🦷 (Experimental) Automatically inject call history into hardhat provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type {RecordedCall} from '@ethereum-waffle/provider'; | ||
import {utils} from 'ethers'; | ||
|
||
/** | ||
* Injects call history into hardhat provider, | ||
* making it possible to use matchers like calledOnContract | ||
*/ | ||
|
||
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 inject = () => { | ||
let waffle: any; | ||
try { | ||
waffle = require('hardhat')?.waffle; | ||
} catch { return; } | ||
if (!waffle || !waffle.provider) return; | ||
const callHistory = new CallHistory(); | ||
(waffle.provider as any).clearCallHistory = () => { | ||
callHistory.clearAll(); | ||
}; | ||
|
||
let beforeMessageListener: (message: any) => void | undefined; | ||
const init = waffle.provider?._hardhatNetwork?.provider?._wrapped?._wrapped?._wrapped?._init; | ||
if (!init) return; | ||
waffle.provider._hardhatNetwork.provider._wrapped._wrapped._wrapped._init = async function () { | ||
await init.apply(this); | ||
if (typeof beforeMessageListener === 'function') { | ||
// has to be here because of weird behaviour of init function, which requires us to re-register the handler. | ||
waffle.provider | ||
?._hardhatNetwork | ||
?.provider | ||
?._wrapped | ||
?._wrapped | ||
?._wrapped | ||
?._node | ||
?._vmTracer | ||
?._vm | ||
?.off?.('beforeMessage', beforeMessageListener); | ||
} | ||
beforeMessageListener = (message: any) => { | ||
callHistory.addUniqueCall(toRecordedCall(message)); | ||
}; | ||
waffle.provider.callHistory = callHistory.recordedCalls; | ||
waffle.provider | ||
?._hardhatNetwork.provider | ||
?._wrapped._wrapped | ||
?._wrapped | ||
?._node | ||
?._vmTracer | ||
?._vm | ||
?.on?.('beforeMessage', beforeMessageListener); | ||
}; | ||
}; | ||
|
||
let injected = false; | ||
if (!injected && !!process.env.WAFFLE_EXPERIMENTAL_HARDHAT_CALL_HISTORY) { | ||
injected = true; | ||
inject(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import {waffleChai} from '../src'; | ||
|
||
chai.use(chaiAsPromised); | ||
chai.use(waffleChai); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
import '../src/inject-call-history'; | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import {solidity} from 'ethereum-waffle'; | ||
|
||
chai.use(chaiAsPromised); | ||
chai.use(solidity); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import {solidity} from 'ethereum-waffle'; | ||
|
||
chai.use(chaiAsPromised); | ||
chai.use(solidity); |