Skip to content

Commit

Permalink
support ethereum
Browse files Browse the repository at this point in the history
  • Loading branch information
hujw77 committed Dec 10, 2023
1 parent 0918905 commit ba7ede3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
25 changes: 11 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
/broadcast

# Docs
docs/

# Dotenv file
.env

debug/
target/
.DS_Store
/target
/docs
/out
/cache
/artifacts
/broadcast
deployments/*/.deploy
/script/output/*/*.json
!/script/output/*/*-latest.json
!/script/output/*/*.v.json
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ CREATE3Factory:
- arbitrum
- crab
- darwinia
- ethereum
- polygon
- mantle
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ evm_version = "london"
optimizer = true
optimizer_runs = 999999
bytecode_hash = "none"
use_literal_content = true
auto_detect_remappings = false

[rpc_endpoints]
Expand Down
46 changes: 46 additions & 0 deletions script/CREATE3Factory.v.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"language": "Solidity",
"sources": {
"src/CREATE3Factory.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\nimport {CREATE3} from \"solmate/utils/CREATE3.sol\";\n\nimport {ICREATE3Factory} from \"./ICREATE3Factory.sol\";\n\n/// @title Factory for deploying contracts to deterministic addresses via CREATE3\n/// @author echo77\n/// @author Inspired from https://github.com/ZeframLou/create3-factory\n/// @notice Enables deploying contracts using CREATE3. Each deployer (msg.sender) has\n/// its own namespace for deployed addresses.\ncontract CREATE3Factory is ICREATE3Factory {\n /// @inheritdoc\tICREATE3Factory\n function deploy(bytes32 salt, bytes memory creationCode) external payable override returns (address deployed) {\n // hash salt with the deployer address to give each deployer its own namespace\n salt = keccak256(abi.encodePacked(msg.sender, salt));\n return CREATE3.deploy(salt, creationCode, msg.value);\n }\n\n /// @inheritdoc\tICREATE3Factory\n function getDeployed(address deployer, bytes32 salt) external view override returns (address deployed) {\n // hash salt with the deployer address to give each deployer its own namespace\n salt = keccak256(abi.encodePacked(deployer, salt));\n return CREATE3.getDeployed(salt);\n }\n}\n"
},
"lib/solmate/src/utils/CREATE3.sol": {
"content": "// SPDX-License-Identifier: AGPL-3.0-only\npragma solidity >=0.8.0;\n\nimport {Bytes32AddressLib} from \"./Bytes32AddressLib.sol\";\n\n/// @notice Deploy to deterministic addresses without an initcode factor.\n/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/CREATE3.sol)\n/// @author Modified from 0xSequence (https://github.com/0xSequence/create3/blob/master/contracts/Create3.sol)\nlibrary CREATE3 {\n using Bytes32AddressLib for bytes32;\n\n //--------------------------------------------------------------------------------//\n // Opcode | Opcode + Arguments | Description | Stack View //\n //--------------------------------------------------------------------------------//\n // 0x36 | 0x36 | CALLDATASIZE | size //\n // 0x3d | 0x3d | RETURNDATASIZE | 0 size //\n // 0x3d | 0x3d | RETURNDATASIZE | 0 0 size //\n // 0x37 | 0x37 | CALLDATACOPY | //\n // 0x36 | 0x36 | CALLDATASIZE | size //\n // 0x3d | 0x3d | RETURNDATASIZE | 0 size //\n // 0x34 | 0x34 | CALLVALUE | value 0 size //\n // 0xf0 | 0xf0 | CREATE | newContract //\n //--------------------------------------------------------------------------------//\n // Opcode | Opcode + Arguments | Description | Stack View //\n //--------------------------------------------------------------------------------//\n // 0x67 | 0x67XXXXXXXXXXXXXXXX | PUSH8 bytecode | bytecode //\n // 0x3d | 0x3d | RETURNDATASIZE | 0 bytecode //\n // 0x52 | 0x52 | MSTORE | //\n // 0x60 | 0x6008 | PUSH1 08 | 8 //\n // 0x60 | 0x6018 | PUSH1 18 | 24 8 //\n // 0xf3 | 0xf3 | RETURN | //\n //--------------------------------------------------------------------------------//\n bytes internal constant PROXY_BYTECODE = hex\"67_36_3d_3d_37_36_3d_34_f0_3d_52_60_08_60_18_f3\";\n\n bytes32 internal constant PROXY_BYTECODE_HASH = keccak256(PROXY_BYTECODE);\n\n function deploy(\n bytes32 salt,\n bytes memory creationCode,\n uint256 value\n ) internal returns (address deployed) {\n bytes memory proxyChildBytecode = PROXY_BYTECODE;\n\n address proxy;\n /// @solidity memory-safe-assembly\n assembly {\n // Deploy a new contract with our pre-made bytecode via CREATE2.\n // We start 32 bytes into the code to avoid copying the byte length.\n proxy := create2(0, add(proxyChildBytecode, 32), mload(proxyChildBytecode), salt)\n }\n require(proxy != address(0), \"DEPLOYMENT_FAILED\");\n\n deployed = getDeployed(salt);\n (bool success, ) = proxy.call{value: value}(creationCode);\n require(success && deployed.code.length != 0, \"INITIALIZATION_FAILED\");\n }\n\n function getDeployed(bytes32 salt) internal view returns (address) {\n address proxy = keccak256(\n abi.encodePacked(\n // Prefix:\n bytes1(0xFF),\n // Creator:\n address(this),\n // Salt:\n salt,\n // Bytecode hash:\n PROXY_BYTECODE_HASH\n )\n ).fromLast20Bytes();\n\n return\n keccak256(\n abi.encodePacked(\n // 0xd6 = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x01)\n // 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex)\n hex\"d6_94\",\n proxy,\n hex\"01\" // Nonce of the proxy contract (1)\n )\n ).fromLast20Bytes();\n }\n}\n"
},
"src/ICREATE3Factory.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.6.0;\n\n/// @title Factory for deploying contracts to deterministic addresses via CREATE3\n/// @author echo77\n/// @author Inspired from https://github.com/ZeframLou/create3-factory\n/// @notice Enables deploying contracts using CREATE3. Each deployer (msg.sender) has\n/// its own namespace for deployed addresses.\ninterface ICREATE3Factory {\n /// @notice Deploys a contract using CREATE3\n /// @dev The provided salt is hashed together with msg.sender to generate the final salt\n /// @param salt The deployer-specific salt for determining the deployed contract's address\n /// @param creationCode The creation code of the contract to deploy\n /// @return deployed The address of the deployed contract\n function deploy(bytes32 salt, bytes memory creationCode) external payable returns (address deployed);\n\n /// @notice Predicts the address of a deployed contract\n /// @dev The provided salt is hashed together with the deployer address to generate the final salt\n /// @param deployer The deployer account that will call deploy()\n /// @param salt The deployer-specific salt for determining the deployed contract's address\n /// @return deployed The address of the contract that will be deployed\n function getDeployed(address deployer, bytes32 salt) external view returns (address deployed);\n}\n"
},
"lib/solmate/src/utils/Bytes32AddressLib.sol": {
"content": "// SPDX-License-Identifier: AGPL-3.0-only\npragma solidity >=0.8.0;\n\n/// @notice Library for converting between addresses and bytes32 values.\n/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/Bytes32AddressLib.sol)\nlibrary Bytes32AddressLib {\n function fromLast20Bytes(bytes32 bytesValue) internal pure returns (address) {\n return address(uint160(uint256(bytesValue)));\n }\n\n function fillLast12Bytes(address addressValue) internal pure returns (bytes32) {\n return bytes32(bytes20(addressValue));\n }\n}\n"
}
},
"settings": {
"remappings": [
"solmate/=lib/solmate/src/"
],
"optimizer": {
"enabled": true,
"runs": 999999
},
"metadata": {
"useLiteralContent": true,
"bytecodeHash": "none"
},
"outputSelection": {
"*": {
"": [
"ast"
],
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers",
"metadata"
]
}
},
"evmVersion": "london",
"libraries": {}
}
}

0 comments on commit ba7ede3

Please sign in to comment.