-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👷 Add script for supporting calledOnContract matcher in hardhat (#755)
- Loading branch information
Showing
8 changed files
with
84 additions
and
118 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,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); | ||
}; |
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
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 @@ | ||
import '../src/inject-call-history'; |