From ba7ede3a823175714af20b64e918e05aeb9f0a70 Mon Sep 17 00:00:00 2001 From: echo Date: Sun, 10 Dec 2023 10:26:20 +0800 Subject: [PATCH] support ethereum --- .gitignore | 25 +++++++++----------- README.md | 1 + foundry.toml | 1 + script/CREATE3Factory.v.json | 46 ++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 script/CREATE3Factory.v.json diff --git a/.gitignore b/.gitignore index d6e5201..41a5edd 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index 09b5f2e..fe8bbbf 100644 --- a/README.md +++ b/README.md @@ -16,5 +16,6 @@ CREATE3Factory: - arbitrum - crab - darwinia +- ethereum - polygon - mantle diff --git a/foundry.toml b/foundry.toml index 295b67f..2151abf 100644 --- a/foundry.toml +++ b/foundry.toml @@ -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] diff --git a/script/CREATE3Factory.v.json b/script/CREATE3Factory.v.json new file mode 100644 index 0000000..c630c16 --- /dev/null +++ b/script/CREATE3Factory.v.json @@ -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": {} + } +}