-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate3Test.t.sol
71 lines (59 loc) · 2.42 KB
/
create3Test.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import 'forge-std/Test.sol';
import {Create3Factory, ICreate3Factory, Create3} from '../src/contracts/create3/Create3Factory.sol';
import {Ownable} from '../src/contracts/oz-common/Ownable.sol';
contract MockContract is Ownable {
address public immutable SOME_ADDRESS;
address internal _someOtherAddress;
constructor(address someAddress, address owner) {
SOME_ADDRESS = someAddress;
_transferOwnership(owner);
}
function getOtherAddress() external view returns (address) {
return _someOtherAddress;
}
function initialize(address someOtherAddress, address owner) external {
_someOtherAddress = someOtherAddress;
_transferOwnership(owner);
}
}
contract Create3FactoryTest is Test {
bytes32 public constant CREATE3_FACTORY_SALT = keccak256(bytes('Create3 Factory'));
ICreate3Factory public factory;
function setUp() public {
factory = new Create3Factory{salt: CREATE3_FACTORY_SALT}();
}
function testCreate3WithoutValue(address someAddress, address owner, bytes32 salt) public {
bytes memory encodedParams = abi.encode(someAddress, owner);
bytes memory code = type(MockContract).creationCode;
// deploy Voting portal
address votingPortal = factory.create(salt, abi.encodePacked(code, encodedParams));
assertEq(votingPortal, factory.predictAddress(address(this), salt));
assertEq(
votingPortal,
Create3.addressOfWithPreDeployedFactory(
keccak256(abi.encodePacked(address(this), salt)),
address(factory)
)
);
assertEq(MockContract(votingPortal).owner(), owner);
assertEq(MockContract(votingPortal).SOME_ADDRESS(), someAddress);
vm.expectRevert(abi.encodeWithSelector(Create3.TargetAlreadyExists.selector));
factory.create(salt, abi.encodePacked(code, encodedParams));
}
function testCreate3WithValue(address someAddress, address owner, address creator) public {
bytes memory encodedParams = abi.encode(someAddress, owner);
bytes memory code = type(MockContract).creationCode;
bytes32 salt = keccak256(bytes('Voting portal eth-avax-2'));
// deploy Voting portal
hoax(creator, 10 ether);
address votingPortal = factory.create{value: 3 ether}(
salt,
abi.encodePacked(code, encodedParams)
);
assertEq(votingPortal, factory.predictAddress(creator, salt));
assertEq(creator.balance, 7 ether);
assertEq(votingPortal.balance, 3 ether);
}
}