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

feat(create3): allow deployed solely based on salt #437

Merged
merged 4 commits into from
Dec 14, 2024
Merged
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
28 changes: 25 additions & 3 deletions contracts/src/deploy/Create3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { CREATE3 } from "solmate/src/utils/CREATE3.sol";
/**
* @title Create3
* @notice Factory for deploying contracts to deterministic addresses via CREATE3 Enables deploying
* contracts using CREATE3. Each deployer (msg.sender) has its own namespace for deployed
* addresses.
* @author zefram.eth
* contracts using CREATE3.
* It is based on Zefram’s implementation and includes support for deployment using only a salt value.
* It support two deployment styles:
* 1. Each deployer (msg.sender) has its own namespace for deployed addresses.
* 2. Deterministic deployment using a single salt.
* @custom:attribution zefram.eth (https://github.com/ZeframLou/create3-factory/blob/main/src/CREATE3Factory.sol)
*/
contract Create3 {
Expand Down Expand Up @@ -37,4 +39,24 @@ contract Create3 {
salt = keccak256(abi.encodePacked(deployer, salt));
return CREATE3.getDeployed(salt);
}

/**
* @notice Deploys a contract using CREATE3
* @param salt The deployer-specific salt for determining the deployed contract's address
* @param creationCode The creation code of the contract to deploy
* @return deployed The address of the deployed contract
*/
function deployDeterministic(bytes memory creationCode, bytes32 salt) external payable returns (address deployed) {
return CREATE3.deploy(salt, creationCode, msg.value);
}

/**
* @notice Predicts the address of a deployed contract
* @param salt The deployer-specific salt for determining the deployed contract's address
* @return deployed The address of the contract that will be deployed
*/
function predictDeterministicAddress(bytes32 salt) external view returns (address deployed) {
// hash salt with the deployer address to give each deployer its own namespace
return CREATE3.getDeployed(salt);
}
}
25 changes: 25 additions & 0 deletions contracts/test/deploy/Create3.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,29 @@ contract Create3Test is Test {
expected = create3.getDeployed(address(this), otherSalt);
assertEq(deployed, expected);
}

function testCreate3_deploy_onlySalt() public {
// deployDeterministic and predictDeterministicAddress return same address when deployed by the same salt.
bytes32 salt = 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef;
bytes memory creationCode = type(Create3).creationCode;
address deployed = create3.deployDeterministic(creationCode, salt);
address expected = create3.predictDeterministicAddress(salt);
assertEq(deployed, expected);

// Network shall generate the same address for the same salt.
vm.expectRevert("DEPLOYMENT_FAILED");
deployed = create3.deployDeterministic(creationCode, salt);

// Network shall generate same addresses for different deployers.
address otherAddr = address(0xf398C12A45Bc409b6C652E25bb0a3e702492A4ab);
vm.prank(otherAddr);
vm.expectRevert("DEPLOYMENT_FAILED");
deployed = create3.deployDeterministic(creationCode, salt);

// Network shall generate different addresses for different salts.
bytes32 otherSalt = 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890fedcba;
deployed = create3.deployDeterministic(creationCode, otherSalt);
expected = create3.predictDeterministicAddress(otherSalt);
assertEq(deployed, expected);
}
}
Loading