-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Add SimulateCall library #6290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ernestognw
merged 18 commits into
OpenZeppelin:master
from
ernestognw:feat/add-reverting-relayer-to-relayed-call
Jan 27, 2026
Merged
Add SimulateCall library #6290
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6b6f66f
Add reverting relayer to RelayedCall
ernestognw 7fb28f1
Apply suggestion from @Amxx
Amxx 621e25c
reorder for consistency
Amxx f68e576
remove LowLevelCall dependency, handling the call nativelly
Amxx 9294a05
update tests
Amxx 0025c38
Split into SimulateCall library
ernestognw 600add4
up
ernestognw 1ea0072
up
ernestognw eee8a28
Refactor SimulateCall to delegatecall into a single relayer (no more …
Amxx 63deb8f
Put initcode at FMP and compute create2 in scratch space
Amxx b0b5795
add docs
Amxx abdc231
Update contracts/utils/SimulateCall.sol
ernestognw 63c3cb4
Update .changeset/flat-flies-hear.md
ernestognw 42011b8
Lint
ernestognw 3c4529f
up
ernestognw 8cd6778
add tests value+revert
Amxx ee703d7
Review comment
ernestognw ed49f87
Explain inverted revert mechanism
ernestognw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| 'openzeppelin-solidity': minor | ||
| --- | ||
|
|
||
| `SimulateCall`: Add a new call simulation utilities that allow inspecting return data from contract calls by executing them in a non-mutating, revert-based context. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,105 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| /** | ||
| * @dev Library for simulating external calls and inspecting the result of the call while reverting any state changes | ||
| * of events the call may have produced. | ||
| * | ||
| * This pattern is useful when you need to simulate the result of a call without actually executing it on-chain. Since | ||
| * the address of the sender is preserved, this supports simulating calls that perform token swap that use the caller's | ||
| * balance, or any operation that is restricted to the caller. | ||
| */ | ||
| library SimulateCall { | ||
| /// @dev Simulates a call to the target contract through a dynamically deployed simulator. | ||
| function simulateCall(address target, bytes memory data) internal returns (bool success, bytes memory retData) { | ||
| return simulateCall(target, 0, data); | ||
| } | ||
|
|
||
| /// @dev Same as {simulateCall-address-bytes} but with a value. | ||
| function simulateCall( | ||
| address target, | ||
| uint256 value, | ||
| bytes memory data | ||
| ) internal returns (bool success, bytes memory retData) { | ||
| (success, retData) = getSimulator().delegatecall(abi.encodePacked(target, value, data)); | ||
| success = !success; | ||
| } | ||
|
|
||
| /// @dev Returns the simulator address. | ||
| function getSimulator() internal returns (address instance) { | ||
| // [Simulator details] | ||
| // deployment prefix: 60315f8160095f39f3 | ||
| // deployed bytecode: 60333611600a575f5ffd5b6034360360345f375f5f603436035f6014355f3560601c5af13d5f5f3e5f3d91602f57f35bfd | ||
| // | ||
| // offset | bytecode | opcode | stack | ||
| // -------|-------------|----------------|-------- | ||
| // 0x0000 | 6033 | push1 0x33 | 0x33 | ||
| // 0x0002 | 36 | calldatasize | cds 0x33 | ||
| // 0x0003 | 11 | gt | (cds>0x33) | ||
| // 0x0004 | 600a | push1 0x0a | 0x0a (cds>0x33) | ||
| // 0x0006 | 57 | jumpi | | ||
| // 0x0007 | 5f | push0 | 0 | ||
| // 0x0008 | 5f | push0 | 0 0 | ||
| // 0x0009 | fd | revert | | ||
| // 0x000a | 5b | jumpdest | | ||
| // 0x000b | 6034 | push1 0x34 | 0x34 | ||
| // 0x000d | 36 | calldatasize | cds 0x34 | ||
| // 0x000e | 03 | sub | (cds-0x34) | ||
| // 0x000f | 6034 | push1 0x34 | 0x34 (cds-0x34) | ||
| // 0x0011 | 5f | push0 | 0 0x34 (cds-0x34) | ||
| // 0x0012 | 37 | calldatacopy | | ||
| // 0x0013 | 5f | push0 | 0 | ||
| // 0x0014 | 5f | push0 | 0 0 | ||
| // 0x0015 | 6034 | push1 0x34 | 0x34 0 0 | ||
| // 0x0017 | 36 | calldatasize | cds 0x34 0 0 | ||
| // 0x0018 | 03 | sub | (cds-0x34) 0 0 | ||
| // 0x0019 | 5f | push0 | 0 (cds-0x34) 0 0 | ||
| // 0x001a | 6014 | push1 0x14 | 0x14 0 (cds-0x34) 0 0 | ||
| // 0x001c | 35 | calldataload | cd[0x14] 0 (cds-0x34) 0 0 | ||
| // 0x001d | 5f | push0 | 0 cd[0x14] 0 (cds-0x34) 0 0 | ||
| // 0x001e | 35 | calldataload | cd[0] cd[0x14] 0 (cds-0x34) 0 0 | ||
| // 0x001f | 6060 | push1 0x60 | 0x60 cd[0] cd[0x14] 0 (cds-0x34) 0 0 | ||
| // 0x0021 | 1c | shr | target cd[0x14] 0 (cds-0x34) 0 0 | ||
| // 0x0022 | 5a | gas | gas target cd[0x14] 0 (cds-0x34) 0 0 | ||
| // 0x0023 | f1 | call | suc | ||
| // 0x0024 | 3d | returndatasize | rds suc | ||
| // 0x0025 | 5f | push0 | 0 rds suc | ||
| // 0x0026 | 5f | push0 | 0 0 rds suc | ||
| // 0x0027 | 3e | returndatacopy | suc | ||
| // 0x0028 | 5f | push0 | 0 suc | ||
| // 0x0029 | 3d | returndatasize | rds 0 suc | ||
| // 0x002a | 91 | swap2 | suc 0 rds | ||
| // 0x002b | 602f | push1 0x2f | 0x2f suc 0 rds | ||
| // 0x002d | 57 | jumpi | 0 rds | ||
| // 0x002e | f3 | return | | ||
| // 0x002f | 5b | jumpdest | 0 rds | ||
| // 0x0030 | fd | revert | | ||
| assembly ("memory-safe") { | ||
| let fmp := mload(0x40) | ||
|
|
||
| // build initcode at FMP | ||
| mstore(add(fmp, 0x20), 0x5f375f5f603436035f6014355f3560601c5af13d5f5f3e5f3d91602f57f35bfd) | ||
| mstore(fmp, 0x60315f8160095f39f360333611600a575f5ffd5b603436036034) | ||
| let initcodehash := keccak256(add(fmp, 0x06), 0x3a) | ||
|
|
||
| // compute create2 address | ||
| mstore(0x40, initcodehash) | ||
| mstore(0x20, 0) | ||
| mstore(0x00, address()) | ||
| mstore8(0x0b, 0xff) | ||
| instance := and(keccak256(0x0b, 0x55), shr(96, not(0))) | ||
|
|
||
| // if simulator not yet deployed, deploy it | ||
| if iszero(extcodesize(instance)) { | ||
| if iszero(create2(0, add(fmp, 0x06), 0x3a, 0)) { | ||
| returndatacopy(fmp, 0x00, returndatasize()) | ||
| revert(fmp, returndatasize()) | ||
| } | ||
| } | ||
|
|
||
| // cleanup fmp space used as scratch | ||
| mstore(0x40, fmp) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,101 @@ | ||
| const { ethers } = require('hardhat'); | ||
| const { expect } = require('chai'); | ||
| const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
|
|
||
| const value = 42n; | ||
|
|
||
| async function fixture() { | ||
| const [receiver, other] = await ethers.getSigners(); | ||
|
|
||
| const mock = await ethers.deployContract('$SimulateCall'); | ||
| const simulator = ethers.getCreate2Address( | ||
| mock.target, | ||
| ethers.ZeroHash, | ||
| ethers.keccak256( | ||
| ethers.concat([ | ||
| '0x60315f8160095f39f3', | ||
| '0x60333611600a575f5ffd5b6034360360345f375f5f603436035f6014355f3560601c5af13d5f5f3e5f3d91602f57f35bfd', | ||
| ]), | ||
| ), | ||
| ); | ||
|
|
||
| const target = await ethers.deployContract('$CallReceiverMock'); | ||
|
|
||
| // fund the mock contract (for tests that use value) | ||
| await other.sendTransaction({ to: mock, value }); | ||
|
|
||
| return { mock, target, receiver, other, simulator }; | ||
| } | ||
|
|
||
| describe('SimulateCall', function () { | ||
| beforeEach(async function () { | ||
| Object.assign(this, await loadFixture(fixture)); | ||
| }); | ||
|
|
||
| it('automatic simulator deployment', async function () { | ||
| await expect(ethers.provider.getCode(this.simulator)).to.eventually.equal('0x'); | ||
|
|
||
| // First call performs deployment | ||
| await expect(this.mock.$getSimulator()).to.emit(this.mock, 'return$getSimulator').withArgs(this.simulator); | ||
|
|
||
| await expect(ethers.provider.getCode(this.simulator)).to.eventually.not.equal('0x'); | ||
|
|
||
| // Following calls use the same simulator | ||
| await expect(this.mock.$getSimulator()).to.emit(this.mock, 'return$getSimulator').withArgs(this.simulator); | ||
| }); | ||
|
|
||
| describe('simulated call', function () { | ||
| it('target success', async function () { | ||
| const txPromise = this.mock.$simulateCall( | ||
| ethers.Typed.address(this.target), | ||
| ethers.Typed.bytes(this.target.interface.encodeFunctionData('mockFunctionWithArgsReturn', [10, 20])), | ||
| ); | ||
|
|
||
| await expect(txPromise).to.changeEtherBalances([this.mock, this.simulator, this.target], [0n, 0n, 0n]); | ||
| await expect(txPromise) | ||
| .to.emit(this.mock, 'return$simulateCall_address_bytes') | ||
| .withArgs(true, ethers.AbiCoder.defaultAbiCoder().encode(['uint256', 'uint256'], [10, 20])) | ||
| .to.not.emit(this.target, 'MockFunctionCalledWithArgs'); | ||
| }); | ||
|
|
||
| it('target success (with value)', async function () { | ||
| // perform simulated call | ||
| const txPromise = this.mock.$simulateCall( | ||
| ethers.Typed.address(this.target), | ||
| ethers.Typed.uint256(value), | ||
| ethers.Typed.bytes(this.target.interface.encodeFunctionData('mockFunctionExtra')), | ||
| ); | ||
|
|
||
| await expect(txPromise).to.changeEtherBalances([this.mock, this.simulator, this.target], [0n, 0n, 0n]); | ||
| await expect(txPromise) | ||
| .to.emit(this.mock, 'return$simulateCall_address_uint256_bytes') | ||
| .withArgs(true, ethers.AbiCoder.defaultAbiCoder().encode(['address', 'uint256'], [this.mock.target, value])) | ||
| .to.not.emit(this.target, 'MockFunctionCalledExtra'); | ||
| }); | ||
|
|
||
| it('target revert', async function () { | ||
| const txPromise = this.mock.$simulateCall( | ||
| ethers.Typed.address(this.target), | ||
| ethers.Typed.bytes(this.target.interface.encodeFunctionData('mockFunctionRevertsReason')), | ||
| ); | ||
|
|
||
| await expect(txPromise).to.changeEtherBalances([this.mock, this.simulator, this.target], [0n, 0n, 0n]); | ||
| await expect(txPromise) | ||
| .to.emit(this.mock, 'return$simulateCall_address_bytes') | ||
| .withArgs(false, this.target.interface.encodeErrorResult('Error', ['CallReceiverMock: reverting'])); | ||
| }); | ||
|
|
||
| it('target revert (with value)', async function () { | ||
| const txPromise = this.mock.$simulateCall( | ||
| ethers.Typed.address(this.target), | ||
| ethers.Typed.uint256(value), | ||
| ethers.Typed.bytes(this.target.interface.encodeFunctionData('mockFunctionRevertsReason')), | ||
| ); | ||
|
|
||
| await expect(txPromise).to.changeEtherBalances([this.mock, this.simulator, this.target], [0n, 0n, 0n]); | ||
| await expect(txPromise) | ||
| .to.emit(this.mock, 'return$simulateCall_address_uint256_bytes') | ||
| .withArgs(false, this.target.interface.encodeErrorResult('Error', ['CallReceiverMock: reverting'])); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.