Last active
March 5, 2023 07:36
-
-
Save ewrvp7lv7/7b173ec51cdf1c74cbf7cf2fc3417e35 to your computer and use it in GitHub Desktop.
Create2 creates Create SCs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract SC0 { | |
address public creare2owner; | |
uint256 public smth; | |
constructor(address _addr) { | |
creare2owner = _addr; | |
} | |
function suicide1() external { | |
selfdestruct(payable(creare2owner)); | |
} | |
function doSmth(bytes memory _b) public { | |
smth = _b.length; | |
} | |
} | |
contract SC1 { | |
address public creare2owner; | |
uint256 public smth; | |
constructor(address _addr) { | |
creare2owner = _addr; | |
} | |
function suicide1() external { | |
selfdestruct(payable(creare2owner)); | |
} | |
function doSmth5(bytes memory _b) public { | |
smth = _b.length + 5; | |
} | |
function doSmthPure(bytes memory _b) public pure returns (uint256) { | |
return _b.length + 15; | |
} | |
} | |
contract Create2Contract { | |
address public owner; | |
address public newAddress; | |
constructor(address _addr) { | |
owner = _addr; | |
} | |
function deploy(bytes memory bytecode) external { | |
address addr; | |
assembly { | |
addr := create(0, add(bytecode, 0x20), mload(bytecode)) | |
if iszero(extcodesize(addr)) { | |
revert(0, 0) | |
} | |
} | |
newAddress = addr; | |
} | |
function suicide1() external { | |
selfdestruct(payable(owner)); | |
} | |
function getAddressCreate0(uint256 _nonce) public view returns (address) { | |
address _origin = address(this); | |
bytes memory data; | |
if (_nonce == 0x00) | |
data = abi.encodePacked( | |
bytes1(0xd6), | |
bytes1(0x94), | |
_origin, | |
bytes1(0x80) | |
); | |
else if (_nonce <= 0x7f) | |
data = abi.encodePacked( | |
bytes1(0xd6), | |
bytes1(0x94), | |
_origin, | |
uint8(_nonce) | |
); | |
else if (_nonce <= 0xff) | |
data = abi.encodePacked( | |
bytes1(0xd7), | |
bytes1(0x94), | |
_origin, | |
bytes1(0x81), | |
uint8(_nonce) | |
); | |
else if (_nonce <= 0xffff) | |
data = abi.encodePacked( | |
bytes1(0xd8), | |
bytes1(0x94), | |
_origin, | |
bytes1(0x82), | |
uint16(_nonce) | |
); | |
else if (_nonce <= 0xffffff) | |
data = abi.encodePacked( | |
bytes1(0xd9), | |
bytes1(0x94), | |
_origin, | |
bytes1(0x83), | |
uint24(_nonce) | |
); | |
else | |
data = abi.encodePacked( | |
bytes1(0xda), | |
bytes1(0x94), | |
_origin, | |
bytes1(0x84), | |
uint32(_nonce) | |
); | |
return address(uint160(uint256(keccak256(data)))); | |
} | |
} | |
contract ContractFabric { | |
address public create2addr; | |
bytes32 public constant _salt = "MySecretSalt"; | |
//создаетCreate2Contract соответственно через create2 | |
function deploy() public { | |
create2addr = address(new Create2Contract{salt: _salt}(address(this))); | |
} | |
function getAddressCreate2() public view returns (address) { | |
bytes memory bytecode = abi.encodePacked( | |
type(Create2Contract).creationCode, | |
abi.encode(address(this)) | |
); | |
bytes32 hash = keccak256( | |
abi.encodePacked( | |
bytes1(0xff), | |
address(this), | |
_salt, | |
keccak256(bytecode) | |
) | |
); | |
return address(uint160(uint256(hash))); | |
} | |
function getBytecode0() public view returns (bytes memory) { | |
return | |
abi.encodePacked(type(SC0).creationCode, abi.encode(create2addr)); | |
} | |
function getBytecode1() public view returns (bytes memory) { | |
return | |
abi.encodePacked(type(SC1).creationCode, abi.encode(create2addr)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment