-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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: migrate to CreateX
for CREATE2
factory (or let user define create2 factory)
#2638
Comments
This is a good config option to add, e.g. |
Especially considering how etherscan represents the current factory, teams may prefer to have their own canonical factory (nothing preventing other people from using it ofc) etc etc |
The main reasons to use another create2 deployer are (1) pass init data to be called after deploy, (2) more leading zeros for cheaper deploy, and I think that's it? Given that, an alternative is to mine a cheap address, agree on the deploy contract's code, deploy it to all the chains, and replace the current default. One downside here is this is a breaking change for anyone relying on the current deployer for vanity addresses. @sambacha would that work as an alternative / are there other reasons people might need a custom deployer? |
The factory contract I linked has additional benefits such as:
Yes for leading zeros, I am using this: https://github.com/0age/create2crunch wdyt @mds1 |
I think all of those are good ideas to include in a canonical create2 deployer contract 👌 Perhaps next week I can scaffold out a proposed interface |
The way we verify contracts or create the transaction depends on the factory we use. So just letting anyone use their own factory implementation is not that straightforward imo. So... if we can agree to a certain interface, it should be fine.
True... |
if your gonna break it, break it before v1.0 |
FoundryCreate2FactoryV8 |
Proposed create2 deployer below, feedback welcome. Interfaces (and the code comments) are largely based on @0age's create2 deployer, with a few modifications based on the above. contract Create2Deployer {
// Mapping to track which addresses have already been deployed.
mapping(address => bool) public isDeployed;
/// @dev Create a contract using CREATE2 by submitting a given salt or nonce
/// along with the initialization code for the contract.
/// @return Address of the contract that will be created, or the null address
/// if a contract already exists at that address.
function create2(bytes32 salt, bytes calldata initCode)
external
payable
returns (address deploymentAddr);
/// @dev Create a contract using CREATE2 by submitting a given salt or nonce
/// along with the initialization code for the contract, and call the
/// contract after deployment with the provided data.
/// @return Address of the contract that will be created, or the null address
/// if a contract already exists at that address.
function create2(bytes32 salt, bytes calldata initCode, bytes calldata data)
external
payable
returns (address deploymentAddr);
/// @dev Create a contract using CREATE2 by submitting a given salt or nonce
/// along with the initialization code for the contract.
/// @dev The first 20 bytes of the salt must match those of the calling
/// address, which prevents contract creation events from being submitted by
/// unintended parties.
/// @return Address of the contract that will be created, or the null address
/// if a contract already exists at that address.
function safeCreate2(bytes32 salt, bytes calldata initCode)
external
payable
containsCaller(salt)
returns (address deploymentAddr);
/// @dev Create a contract using CREATE2 by submitting a given salt or nonce
/// along with the initialization code for the contract, and call the
/// contract after deployment with the provided data.
/// @dev The first 20 bytes of the salt must match those of the calling
/// address, which prevents contract creation events from being submitted by
/// unintended parties.
/// @return Address of the contract that will be created, or the null address
/// if a contract already exists at that address.
function safeCreate2(bytes32 salt, bytes calldata initCode, bytes calldata data)
external
payable
containsCaller(salt)
returns (address deploymentAddr);
/// @dev Compute the address of the contract that will be created when
/// submitting a given salt or nonce to the contract along with the contract's
/// initialization code.
/// @return Address of the contract that will be created, or the null address
/// if a contract has already been deployed to that address.
function computeCreate2Address(bytes32 salt, bytes calldata initCode)
external
view
returns (address deploymentAddr);
/// @dev Compute the address of the contract that will be created when
/// submitting a given salt or nonce to the contract along with the keccak256
/// hash of the contract's initialization code.
/// @return Address of the contract that will be created, or the null address
/// if a contract has already been deployed to that address.
function computeCreate2Address(bytes32 salt, bytes32 initCodeHash)
external
view
returns (address deploymentAddress);
/// @dev Modifier to ensure that the first 20 bytes of a submitted salt match
/// those of the calling account. This provides protection against the salt
/// being stolen by frontrunners or other attackers. The protection can also be
/// bypassed if desired by setting each of the first 20 bytes to zero.
/// @param salt bytes32 The salt value to check against the calling address.
modifier containsCaller(bytes32 salt) {
require(
(address(bytes20(salt)) == msg.sender) || (bytes20(salt) == bytes20(0)),
"Invalid salt: first 20 bytes of the salt must match calling address."
);
_;
}
} We can arguably remove either (1) the
cc'ing a few people for feedback who I think are interested: @sambacha @gakonst @joshieDo @pcaversaccio @storming0x |
Should there be any chain id information included ? |
What chain ID information did you have in mind? Another method that hashes your salt with chain ID to prevent redeploying to another chain at the same address might be a good idea |
This is what I had in mind, but then again I am not sure if its even useful. It would be useful in the edge case of contentious forks, so def YMMV /// constructor
deploymentChainId = block.chainid;
_DOMAIN_SEPARATOR = _calculateDomainSeparator(block.chainid);
/// ... Return the DOMAIN_SEPARATOR.
return block.chainid == deploymentChainId ? _DOMAIN_SEPARATOR : _calculateDomainSeparator(block.chainid);
/// ...abi.encode
block.chainid == deploymentChainId ? _DOMAIN_SEPARATOR : _calculateDomainSeparator(block.chainid), |
Thanks for tagging, yeah i think this covers our use case mostly, being able to deploy init type smart contracts with create2 |
@mds1 thanks for tagging me - the suggested interface would not be compatible with my /**
* @dev Deploys a contract using `CREATE2`. The address where the
* contract will be deployed can be known in advance via {computeAddress}.
*
* The bytecode for a contract can be obtained from Solidity with
* `type(contractName).creationCode`.
*
* Requirements:
* - `bytecode` must not be empty.
* - `salt` must have not been used for `bytecode` already.
* - the factory must have a balance of at least `value`.
* - if `value` is non-zero, `bytecode` must have a `payable` constructor.
*/
function deploy(uint256 value, bytes32 salt, bytes memory code) external returns (address addr);
/**
* @dev Deployment of the {ERC1820Implementer}.
* Further information: https://eips.ethereum.org/EIPS/eip-1820
*/
function deployERC1820Implementer(uint256 value, bytes32 salt) external returns (address addr);
/**
* @dev Returns the address where a contract will be stored if deployed via {deploy}.
* Any change in the `bytecodeHash` or `salt` will result in a new destination address.
*/
function computeAddress(bytes32 salt, bytes32 codeHash) external view returns (address addr);
/**
* @dev Returns the address where a contract will be stored if deployed via {deploy} from a
* contract located at `deployer`. If `deployer` is this contract's address, returns the
* same value as {computeAddress}.
*/
function computeAddressWithDeployer(bytes32 salt, bytes32 codeHash, address deployer) external pure returns (address addr); The |
🫂✨⭐️💫 |
any progress on this? is there a way to define the address for a custom create2 factory? would love to work on it and open a PR if it makes sense to do so |
Nobody's on it, feel free to go for it! |
sounds good @gakonst, Ill start working on it. As per my understanding the main goal here is to add a parameter like
|
I think the core issue is not just about a custom factory, but agreeing on a specific interface for a new factory. Sure, having a parameter for a custom factory is nice, but for the reasons given above, it has to share the same interface as the default factory. |
This sounds good. I'll add this feature while making sure the create2 interface is shared by the default one |
Any movement on this for v1.0 |
Hey, any update on this? |
We haven't prioritized it, no. Is it a big deal for you? It's a small enough bite that I'd prefer a third party contributor to take. But if it is really important we can try to prio. |
@gakonst hmm... I have the same issue as @akshatcx and I think it's important. Could you try to prioritize?
|
@apptreeso as an alternative for the moment being, you can use my Hardhat "function deploy(uint256 value, bytes32 salt, bytes code)" as well as the chains you deploy on have your |
The problem is not that there is a default factory: the problem is that I can not create and define my own default factory.
https://github.com/blockscout/blockscout-rs/tree/main/smart-contract-verifier |
Here we goooooo |
The point of having a deployer in Foundry is to make sure the same deployer (both functionality and address) is the same on real networks. A standard (or de-facto) deployer should be minimal: perform the deployment at a deterministic address across chains. If you want a complex deployer with extra functionality - that's great, go and create one. You don't need specific support from Foundry (or from real-life network): Just deploy it using a standard deterministic deployer. |
In case this helps anyone: The only issue we had with foundry was lack of verification support as foundry seems to not properly detect nested create calls, so we ended up using catapulta-verify which parses the foundry broadcast file and then uses debug_traceTransaction to find create calls and verify contracts. |
FWIW; |
Changing the default create2 deployer would be a breaking change, but a potential solution for forge v1 is:
CreateX has enough features that, based on the above discussion, this should eliminate the majority of the use cases for a custom deployer. Sample UX, open to feedback: contract MyScript is Script {
function run() external {
vm.startBroadcast();
// Default behavior, uses `CreateX.deployCreate2(bytes32,bytes)`
new MyContract{salt: salt}();
// Cheatcode names match the CreateX function names. The cheat arguments
// match the CreateX function arguments, and are forwarded by forge to the
// CreateX function. For safety/explicitness, we expect the contract deploy
// call to match the CreateX function arguments as much as possible. Some
// examples. Assume salt=bytes32(1) and values are 0 unless stated otherwise.
vm.deployCreate3AndInit(salt, initCode, data, values, refundAddress);
new MyContract(); // Revert: Salt was not provided.
vm.deployCreate3AndInit(salt, initCode, data, values, refundAddress);
new MyContract{salt: bytes32(0)}(); // Revert: Salts don't match.
vm.deployCreate3AndInit(salt, initCode, data, values, refundAddress);
new MyContract{salt: salt}(); // Success: Salt matches, value matches
// Now assume values.constructorAmount=1 and values.initCallAmount=2,
// so total expected value is 3
vm.deployCreate3AndInit(salt, initCode, data, values, refundAddress);
new MyContract{salt: salt}(); // Revert: Value is 0, expected 3.
vm.deployCreate3AndInit(salt, initCode, data, values, refundAddress);
new MyContract{salt: salt, value: 1}(); // Revert: Value is 1, expected 3.
vm.deployCreate3AndInit(salt, initCode, data, values, refundAddress);
new MyContract{salt: salt, value: 3}(); // Success
vm.stopBroadcast();
}
} You can also have cheats to help users set the correct bytes to leverage the salt features. One thing that's a bit awkward with this UX is you specify the MyContract c = new MyContract();
c.initalize(initData); So an alternative UX might be to instead not map cheats to function signatures 1:1 and have "logical" cheats based on features. But this gets clunky to read/write, and makes the forge integration much more complex as not every combination of features is available. |
CreateX
for CREATE2
factory (or let user define create2 factory)
Forge now has |
Hey guys! Unfortunately, again, not all chains are covered by the tools & workarounds you mentioned above. For example, I'm working right now to deploy the Uniswap V3 Universal Router on our testnet(Taraxa), and I am. running into a deployment error because of the fixed CREATE2 address... which obviously is a different one on our chain as I can't use the presigned tx from Nick. |
Agree here along with @VargaElod23 . There are other chains with EIP-155 implemented which makes it difficult for us to deploy with https://github.com/Arachnid/deterministic-deployment-proxy
What's the solution if a chain supports EIP-155? maybe @pcaversaccio has some input |
There are a couple of solutions:
Btw, that's why we haven't chosen a keyless deployment for |
FWIW, Hardhat Ignition (Hardhat's chainops solution) now uses |
From #7653 inject call to CREATE2 factory through custom revm handler This changes flow for CREATE2 deployments routed through CREATE2 factory. Earlier we would just patch the sender of CREATE2 frame which resulted in data appearing in traces and state diff being different from the actual transaction data. The way this PR addresses this is by implementing a revm handler register which replaces CREATE2 frame with CALL frame to factory when needed. SolutionImplement InspectorExt trait with should_use_create2_factory method returning false by default which allows our inspectors to decide if the CREATE frame should be routed through create2 factory. Implement handler register overriding create and insert_call_outcome hooks: Overriden create hook returns CALL frame instead of CREATE frame when should_use_create2_factory fn returns true for the current frame. overriden CREATE2 invocation must be treated as a CALL by inspectors and EVM itself, however, its output should be written to interpreter as CREATE2 output which is different than CALL output in terms of stack and memory operations. Thus we can't reuse insert_call_outcome hook because it would invoke inspector.insert_call_outcome(). thanks @klkvr for following up on this issue and providing the fixes |
You can try sending a legacy (non-EIP1557 based) transaction. You can send non-EIP155 transactions over RPC, this is supported by certain RPC providers. I can only think of one chain (Harmony I think) that prevents non-EIP155 transactions at the protocol level. |
FWIW, Filecoin is another one not supporting pre-EIP-155 transactions (see here). Also, as a good reminder, that since the Berlin hard fork, Geth rejects all sending of pre-EIP-155 txs by default (see ethereum/go-ethereum#22339), i.e. even if there are networks that would support them at the network layer, the RPCs usually reject them since they use the default Geth config unfortunately. |
Component
Forge
Describe the feature you would like
Define CREATE2 Factory
I do not want to use your factory, I want to use my own factory.
Additional context
https://etherscan.io/address/0x4e59b44847b379578588920ca78fbf26c0b4956c no
https://goerli.etherscan.io/address/0x179c98f5ccdb3bc7d7aefffa06fb7f608a301877#code yes
The text was updated successfully, but these errors were encountered: