Skip to content

Commit

Permalink
updated to better work with forge
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsoncusack committed Apr 22, 2024
1 parent 70ffde7 commit 25f48cc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SafeSingletonDeployerTest:test_deploy_createsAtExpectedAddress() (gas: 113482)
SafeSingletonDeployerTest:test_deploy_createsContractCorrectly() (gas: 106035)
SafeSingletonDeployerTest:test_deploy_reverts() (gas: 43784)
SafeSingletonDeployerTest:test_deploy_createsAtExpectedAddress() (gas: 110747)
SafeSingletonDeployerTest:test_deploy_createsContractCorrectly() (gas: 100784)
SafeSingletonDeployerTest:test_deploy_reverts() (gas: 41137)
18 changes: 18 additions & 0 deletions scripts/ExampleDeploy.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {Script, console2} from "forge-std/Script.sol";

import {SafeSingletonDeployer} from "../src/SafeSingletonDeployer.sol";
import {Mock} from "../test/Mock.sol";

contract ExampleDeployScript is Script {
function run() public {
SafeSingletonDeployer.broadcastDeploy({
deployer: address(1),
creationCode: type(Mock).creationCode,
args: abi.encode(1),
salt: bytes32("0x1234")
});
}
}
41 changes: 40 additions & 1 deletion src/SafeSingletonDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,46 @@ library SafeSingletonDeployer {
});
}

function deploy(bytes memory creationCode, bytes memory args, bytes32 salt) public returns (address) {
function broadcastDeploy(address deployer, bytes memory creationCode, bytes memory args, bytes32 salt)
internal
returns (address)
{
VM.broadcast(deployer);
return _deploy(creationCode, args, salt);
}

function broadcastDeploy(address deployer, bytes memory creationCode, bytes32 salt) internal returns (address) {
VM.broadcast(deployer);
return _deploy(creationCode, "", salt);
}

function broadcastDeploy(uint256 deployerPrivateKey, bytes memory creationCode, bytes memory args, bytes32 salt)
internal
returns (address)
{
VM.broadcast(deployerPrivateKey);
return _deploy(creationCode, args, salt);
}

function broadcastDeploy(uint256 deployerPrivateKey, bytes memory creationCode, bytes32 salt)
internal
returns (address)
{
VM.broadcast(deployerPrivateKey);
return _deploy(creationCode, "", salt);
}

/// @dev Allows calling without Forge broadcast
function deploy(bytes memory creationCode, bytes memory args, bytes32 salt) internal returns (address) {
return _deploy(creationCode, args, salt);
}

/// @dev Allows calling without Forge broadcast
function deploy(bytes memory creationCode, bytes32 salt) internal returns (address) {
return _deploy(creationCode, "", salt);
}

function _deploy(bytes memory creationCode, bytes memory args, bytes32 salt) private returns (address) {
bytes memory callData = abi.encodePacked(salt, creationCode, args);

(bool success, bytes memory result) = SAFE_SINGLETON_FACTORY.call(callData);
Expand Down
2 changes: 1 addition & 1 deletion test/SafeSingletonDeployer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ contract SafeSingletonDeployerTest is Test {
}

function test_deploy_reverts() public {
vm.expectRevert(SafeSingletonDeployer.DeployFailed.selector);
vm.expectRevert();
SafeSingletonDeployer.deploy({
creationCode: type(MockReverting).creationCode,
args: abi.encode(1),
Expand Down

0 comments on commit 25f48cc

Please sign in to comment.