-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathCreate2Address.t.sol
154 lines (139 loc) · 4.87 KB
/
Create2Address.t.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: WTFPL
pragma solidity ^0.8.28;
import {Test} from "forge-std/Test.sol";
import {VyperDeployer} from "utils/VyperDeployer.sol";
import {Create2Impl} from "./mocks/Create2Impl.sol";
import {ERC20Mock} from "./mocks/ERC20Mock.sol";
import {ICreate2Address} from "./interfaces/ICreate2Address.sol";
contract Create2AddressTest is Test {
VyperDeployer private vyperDeployer = new VyperDeployer();
Create2Impl private create2Impl = new Create2Impl();
ICreate2Address private create2Address;
address private create2AddressAddr;
function setUp() public {
create2Address = ICreate2Address(
vyperDeployer.deployContract(
"src/snekmate/utils/mocks/",
"create2_address_mock"
)
);
create2AddressAddr = address(create2Address);
}
function testComputeAddress() public {
bytes32 salt = keccak256("WAGMI");
string memory arg1 = "MyToken";
string memory arg2 = "MTKN";
address arg3 = makeAddr("initialAccount");
uint256 arg4 = 100;
bytes memory args = abi.encode(arg1, arg2, arg3, arg4);
bytes memory bytecode = abi.encodePacked(
vm.getCode("ERC20Mock.sol:ERC20Mock"),
args
);
bytes32 bytecodeHash = keccak256(bytecode);
address create2AddressComputed = create2Address.compute_address(
salt,
bytecodeHash,
address(this)
);
ERC20Mock create2AddressComputedOnChain = new ERC20Mock{salt: salt}(
arg1,
arg2,
arg3,
arg4
);
assertEq(
create2AddressComputed,
address(create2AddressComputedOnChain)
);
}
function testComputeAddressSelf() public {
bytes32 salt = keccak256("WAGMI");
string memory arg1 = "MyToken";
string memory arg2 = "MTKN";
address arg3 = makeAddr("initialAccount");
uint256 arg4 = 100;
bytes memory args = abi.encode(arg1, arg2, arg3, arg4);
bytes memory bytecode = abi.encodePacked(
vm.getCode("ERC20Mock.sol:ERC20Mock"),
args
);
bytes32 bytecodeHash = keccak256(bytecode);
address create2AddressComputed = create2Address.compute_address_self(
salt,
bytecodeHash
);
address create2AddressOZComputed = create2Impl
.computeAddressWithDeployer(salt, bytecodeHash, create2AddressAddr);
vm.prank(create2AddressAddr);
ERC20Mock create2AddressComputedOnChain = new ERC20Mock{salt: salt}(
arg1,
arg2,
arg3,
arg4
);
assertEq(create2AddressComputed, create2AddressOZComputed);
assertEq(
create2AddressComputed,
address(create2AddressComputedOnChain)
);
}
function testFuzzComputeAddress(bytes32 salt, address deployer) public {
string memory arg1 = "MyToken";
string memory arg2 = "MTKN";
address arg3 = makeAddr("initialAccount");
uint256 arg4 = 100;
bytes memory args = abi.encode(arg1, arg2, arg3, arg4);
bytes memory bytecode = abi.encodePacked(
vm.getCode("ERC20Mock.sol:ERC20Mock"),
args
);
bytes32 bytecodeHash = keccak256(bytecode);
address create2AddressComputed = create2Address.compute_address(
salt,
bytecodeHash,
deployer
);
vm.prank(deployer);
ERC20Mock create2AddressComputedOnChain = new ERC20Mock{salt: salt}(
arg1,
arg2,
arg3,
arg4
);
assertEq(
create2AddressComputed,
address(create2AddressComputedOnChain)
);
}
function testFuzzComputeAddressSelf(bytes32 salt) public {
string memory arg1 = "MyToken";
string memory arg2 = "MTKN";
address arg3 = makeAddr("initialAccount");
uint256 arg4 = 100;
bytes memory args = abi.encode(arg1, arg2, arg3, arg4);
bytes memory bytecode = abi.encodePacked(
vm.getCode("ERC20Mock.sol:ERC20Mock"),
args
);
bytes32 bytecodeHash = keccak256(bytecode);
address create2AddressComputed = create2Address.compute_address_self(
salt,
bytecodeHash
);
address create2AddressOZComputed = create2Impl
.computeAddressWithDeployer(salt, bytecodeHash, create2AddressAddr);
vm.prank(create2AddressAddr);
ERC20Mock create2AddressComputedOnChain = new ERC20Mock{salt: salt}(
arg1,
arg2,
arg3,
arg4
);
assertEq(create2AddressComputed, create2AddressOZComputed);
assertEq(
create2AddressComputed,
address(create2AddressComputedOnChain)
);
}
}