Skip to content

Commit

Permalink
Use the deterministic deployer when broadcasting with CREATE2
Browse files Browse the repository at this point in the history
  • Loading branch information
mslipper committed Sep 14, 2024
1 parent 6cf35da commit d3ce45a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
9 changes: 9 additions & 0 deletions op-chain-ops/script/deterministic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package script

import "github.com/ethereum/go-ethereum/common"

var (
// DeterministicDeployerAddress is the address of the deterministic deployer Forge uses
// to provide deterministic contract addresses.
DeterministicDeployerAddress = common.HexToAddress("0x4e59b44847b379578588920ca78fbf26c0b4956c")
)
7 changes: 7 additions & 0 deletions op-chain-ops/script/prank.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func (h *Host) handleCaller(caller vm.ContractRef) vm.ContractRef {
if len(h.callStack) > 0 {
parentCallFrame := h.callStack[len(h.callStack)-1]
if parentCallFrame.Prank != nil && caller.Address() != VMAddr { // pranks do not apply to the cheatcode precompile
if parentCallFrame.Prank.Broadcast && parentCallFrame.LastOp == vm.CREATE2 {
return &prankRef{
prank: DeterministicDeployerAddress,
ref: caller,
}
}

if parentCallFrame.Prank.Sender != nil {
return &prankRef{
prank: *parentCallFrame.Prank.Sender,
Expand Down
8 changes: 5 additions & 3 deletions op-chain-ops/script/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func TestScriptBroadcast(t *testing.T) {
Nonce: 0, // first action of 0x123456
},
{
From: cafeAddr,
To: crypto.CreateAddress2(cafeAddr, salt, crypto.Keccak256(expectedInitCode)),
From: DeterministicDeployerAddress,
To: crypto.CreateAddress2(DeterministicDeployerAddress, salt, crypto.Keccak256(expectedInitCode)),
Input: expectedInitCode,
Value: (*hexutil.U256)(uint256.NewInt(0)),
Type: BroadcastCreate2,
Expand Down Expand Up @@ -164,5 +164,7 @@ func TestScriptBroadcast(t *testing.T) {
require.EqualValues(t, 0, h.GetNonce(senderAddr))
require.EqualValues(t, 3, h.GetNonce(scriptAddr))
require.EqualValues(t, 2, h.GetNonce(coffeeAddr))
require.EqualValues(t, 1, h.GetNonce(cafeAddr))
// This is zero because the deterministic deployer is the
// address that actually deploys the contract using CREATE2.
require.EqualValues(t, 0, h.GetNonce(cafeAddr))
}

0 comments on commit d3ce45a

Please sign in to comment.