Skip to content

Commit

Permalink
prepare anvil before deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Sep 24, 2024
1 parent cf2b89c commit 35499cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
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

0 comments on commit 35499cf

Please sign in to comment.