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 all commits
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
21 changes: 20 additions & 1 deletion src/SafeSingletonDeployer.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {VmSafe} from "forge-std/Vm.sol";
import {VmSafe, Vm} from "forge-std/Vm.sol";

/// @notice Library for deploying contracts using Safe's Singleton Factory
/// https://github.com/safe-global/safe-singleton-factory
library SafeSingletonDeployer {
error MissingSafeSingletonFactory();
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 +79,13 @@ 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();

if (SAFE_SINGLETON_FACTORY.code.length == 0) {
revert MissingSafeSingletonFactory();
}

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

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

function prepareAnvil() public {
if (block.chainid == 31337) {
Vm(address(VM)).etch(SafeSingletonDeployer.SAFE_SINGLETON_FACTORY, factoryCode);
}
}
}
18 changes: 10 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 Expand Up @@ -51,4 +43,14 @@ contract SafeSingletonDeployerTest is Test {
salt: bytes32("0x1234")
});
}

function test_deploy_chainWithoutFactory() public {
vm.chainId(1);
vm.expectRevert(SafeSingletonDeployer.MissingSafeSingletonFactory.selector);
SafeSingletonDeployer.deploy({
creationCode: type(Mock).creationCode,
args: abi.encode(1),
salt: bytes32("0x1234")
});
}
}
Loading