solidity0.8.0 create2

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract D {
    uint256 myVariable;
    address public owner;

    constructor(uint256 initialValue) {
        myVariable = initialValue;
        owner = msg.sender;
    }

    function getMyVariable() public view returns (uint) {
        return myVariable;
    }
}

contract Create2 {
    function getBytes32(uint salt) external pure returns (bytes32) {
        return bytes32(salt);
    }

    function getAddress(
        bytes32 salt,
        uint arg
    ) external view returns (address) {
        address addr = address(
            uint160(
                uint(
                    keccak256(
                        abi.encodePacked(
                            bytes1(0xff),
                            address(this),
                            salt,
                            keccak256(
                                abi.encodePacked(type(D).creationCode, arg)
                            )
                        )
                    )
                )
            )
        );

        return addr;
    }

    address public deployedAddr;

    function createDSalted(bytes32 salt, uint arg) public {
        D d = new D{salt: salt}(arg);
        deployedAddr = address(d);
    }
}

posted @ 2023-08-03 22:35  三省吾身~  阅读(17)  评论(0)    收藏  举报