Skip to content
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

prepare anvil before deploy #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/SafeSingletonDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ library SafeSingletonDeployer {
error DeployFailed();

address constant SAFE_SINGLETON_FACTORY = 0x914d7Fec6aaC8cd542e72Bca78B30650d45643d7;

// cast code 0x914d7Fec6aaC8cd542e72Bca78B30650d45643d7 --rpc-url https://mainnet.base.org
bytes constant factoryCode =
hex"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3";

VmSafe private constant VM = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));

function computeAddress(bytes memory creationCode, bytes32 salt) public pure returns (address) {
Expand Down Expand Up @@ -73,6 +78,9 @@ library SafeSingletonDeployer {
}

function _deploy(bytes memory creationCode, bytes memory args, bytes32 salt) private returns (address) {
// ensure Safe Singleton Factory exists if we're deploying to anvil
prepareAnvil();

bytes memory callData = abi.encodePacked(salt, creationCode, args);

(bool success, bytes memory result) = SAFE_SINGLETON_FACTORY.call(callData);
Expand All @@ -89,4 +97,10 @@ library SafeSingletonDeployer {
function _hashInitCode(bytes memory creationCode, bytes memory args) private pure returns (bytes32) {
return keccak256(abi.encodePacked(creationCode, args));
}

function prepareAnvil() {
if (block.chainid == 31337) {
vm.etch(SafeSingletonDeployer.SAFE_SINGLETON_FACTORY, factoryCode);
}
}
}
8 changes: 0 additions & 8 deletions test/SafeSingletonDeployer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import {Mock} from "./Mock.sol";
import {MockReverting} from "./MockReverting.sol";

contract SafeSingletonDeployerTest is Test {
// cast code 0x914d7Fec6aaC8cd542e72Bca78B30650d45643d7 --rpc-url https://mainnet.base.org
bytes factoryCode =
hex"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3";

function setUp() public {
vm.etch(SafeSingletonDeployer.SAFE_SINGLETON_FACTORY, factoryCode);
}

function test_deploy_createsAtExpectedAddress() public {
address expectedAddress =
SafeSingletonDeployer.computeAddress(type(Mock).creationCode, abi.encode(1), bytes32("0x1234"));
Expand Down