-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreate2.sol
70 lines (69 loc) · 2.33 KB
/
create2.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleWallet {
address public owner;
// Only owners can call transactions marked with this modifier
modifier onlyOwner() {
require(owner == msg.sender, "Caller is not the owner");
_;
}
constructor(address _owner) payable {
owner = _owner;
}
// Allows the owner to transfer ownership of the contract
function transferOwnership(address _newOwner) external onlyOwner {
owner = _newOwner;
}
// Returns ETH balance from this contract
function getBalance() public view returns (uint256) {
return address(this).balance;
}
// Allows contract owner to withdraw all funds from the contract
function withdraw() external onlyOwner {
payable(msg.sender).transfer(address(this).balance);
}
// Destroys this contract instance
function destroy(address payable recipient) public onlyOwner {
selfdestruct(recipient);
}
}
contract Factory {
// Returns the address of the newly deployed contract
function deploy(
uint _salt
) public payable returns (address) {
// 不在使用 assembly的新语法调用 create2 , 仅仅传递 salt 就可以
// 参考文档:https://learnblockchain.cn/docs/solidity/control-structures.html#create2
return address(new SimpleWallet{salt: bytes32(_salt)}(msg.sender));
}
// 1. 获取待部署合约字节码
function getBytecode()
public
view
returns (bytes memory)
{
bytes memory bytecode = type(SimpleWallet).creationCode;
return abi.encodePacked(bytecode, abi.encode(msg.sender));
}
/** 2. 计算待部署合约地址
params:
_salt: 随机整数,用于预计算地址
*/
function getAddress(uint256 _salt)
public
view
returns (address)
{
// Get a hash concatenating args passed to encodePacked
bytes32 hash = keccak256(
abi.encodePacked(
bytes1(0xff), // 0
address(this), // address of factory contract
_salt, // a random salt
keccak256(getBytecode()) // the wallet contract bytecode
)
);
// Cast last 20 bytes of hash to address
return address(uint160(uint256(hash)));
}
}