1
1
// SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^ 0.8.11 ;
2
+ pragma solidity ^ 0.8.0 ;
3
3
4
4
// ========== External imports ==========
5
5
import "@openzeppelin/contracts/utils/Create2.sol " ;
6
6
import "@openzeppelin/contracts/proxy/Clones.sol " ;
7
7
import "@openzeppelin/contracts/utils/Address.sol " ;
8
+ import "@openzeppelin/contracts/utils/Multicall.sol " ;
8
9
import "@openzeppelin/contracts/access/AccessControlEnumerable.sol " ;
9
10
import "@openzeppelin/contracts/metatx/ERC2771Context.sol " ;
10
11
11
12
// ========== Internal imports ==========
12
- import { IByocFactory } from "./interfaces/IByocFactory .sol " ;
13
+ import { IContractDeployer } from "./interfaces/IContractDeployer .sol " ;
13
14
import { TWRegistry } from "./TWRegistry.sol " ;
14
- import "./ThirdwebContract.sol " ;
15
+ import { IContractMetadataRegistry } from "./interfaces/IContractMetadataRegistry.sol " ;
16
+ import { ThirdwebContract } from "./ThirdwebContract.sol " ;
15
17
16
- contract ByocFactory is IByocFactory , ERC2771Context , AccessControlEnumerable , ThirdwebContract {
18
+ contract ContractDeployer is IContractDeployer , ERC2771Context , Multicall , AccessControlEnumerable {
17
19
/*///////////////////////////////////////////////////////////////
18
20
State variables
19
21
//////////////////////////////////////////////////////////////*/
20
22
21
23
/// @dev The main thirdweb registry.
22
24
TWRegistry private immutable registry;
25
+ /// @dev The contract metadta registry.
26
+ IContractMetadataRegistry private immutable metadataRegistry;
27
+ /// @dev contract address deployed through the factory => deployer
28
+ mapping (address => address ) public getContractDeployer;
23
29
24
30
/// @dev Whether the registry is paused.
25
31
bool public isPaused;
@@ -35,8 +41,13 @@ contract ByocFactory is IByocFactory, ERC2771Context, AccessControlEnumerable, T
35
41
_;
36
42
}
37
43
38
- constructor (address _twRegistry , address _trustedForwarder ) ERC2771Context (_trustedForwarder) {
44
+ constructor (
45
+ address _twRegistry ,
46
+ address _metadataRegistry ,
47
+ address _trustedForwarder
48
+ ) ERC2771Context (_trustedForwarder) {
39
49
registry = TWRegistry (_twRegistry);
50
+ metadataRegistry = IContractMetadataRegistry (_metadataRegistry);
40
51
_setupRole (DEFAULT_ADMIN_ROLE, _msgSender ());
41
52
}
42
53
@@ -51,25 +62,34 @@ contract ByocFactory is IByocFactory, ERC2771Context, AccessControlEnumerable, T
51
62
bytes memory _constructorArgs ,
52
63
bytes32 _salt ,
53
64
uint256 _value ,
54
- ThirdwebContract.ThirdwebInfo memory _thirdwebInfo
65
+ string memory publishMetadataUri
55
66
) external onlyUnpausedOrAdmin returns (address deployedAddress ) {
56
- require (bytes (_thirdwebInfo.publishMetadataUri).length > 0 , "No publish metadata " );
67
+ require (bytes (publishMetadataUri).length > 0 , "No publish metadata " );
68
+
69
+ address caller = _msgSender ();
57
70
58
71
bytes memory contractBytecode = abi.encodePacked (_contractBytecode, _constructorArgs);
59
- bytes32 salt = _salt == "" ? keccak256 (abi.encodePacked (_msgSender (), block .number )) : _salt;
72
+ bytes32 salt = _salt == ""
73
+ ? keccak256 (abi.encodePacked (caller, block .number , keccak256 (contractBytecode)))
74
+ : keccak256 (abi.encodePacked (caller, _salt));
75
+
76
+ // compute the address of the clone and save it
77
+ address computedContractAddress = Create2.computeAddress (salt, keccak256 (contractBytecode), address (this ));
78
+ getContractDeployer[computedContractAddress] = caller;
60
79
80
+ // deploy the contract
61
81
deployedAddress = Create2.deploy (_value, salt, contractBytecode);
62
82
63
- ThirdwebContract (deployedAddress).setThirdwebInfo (_thirdwebInfo);
64
- require (
65
- keccak256 (bytes (ThirdwebContract (deployedAddress).getPublishMetadataUri ())) ==
66
- keccak256 (bytes (_thirdwebInfo.publishMetadataUri)),
67
- "Not a thirdweb contract "
68
- );
83
+ // set the owner
84
+ ThirdwebContract (deployedAddress).tw_initializeOwner (caller);
69
85
70
- registry.add (_publisher, deployedAddress);
86
+ // register to metadata registry
87
+ metadataRegistry.registerMetadata (deployedAddress, publishMetadataUri);
71
88
72
- emit ContractDeployed (_msgSender (), _publisher, deployedAddress);
89
+ // register to TWRegistry
90
+ registry.add (caller, deployedAddress);
91
+
92
+ emit ContractDeployed (caller, _publisher, deployedAddress);
73
93
}
74
94
75
95
/// @notice Deploys a clone pointing to an implementation of a published contract.
@@ -79,26 +99,38 @@ contract ByocFactory is IByocFactory, ERC2771Context, AccessControlEnumerable, T
79
99
bytes memory _initializeData ,
80
100
bytes32 _salt ,
81
101
uint256 _value ,
82
- ThirdwebContract.ThirdwebInfo memory _thirdwebInfo
102
+ string memory publishMetadataUri
83
103
) external onlyUnpausedOrAdmin returns (address deployedAddress ) {
84
- bytes32 salt = _salt == "" ? keccak256 (abi.encodePacked (_msgSender (), block .number )) : _salt;
104
+ require (bytes (publishMetadataUri).length > 0 , "No publish metadata " );
105
+
106
+ address caller = _msgSender ();
107
+
108
+ bytes32 salt = _salt == ""
109
+ ? keccak256 (abi.encodePacked (caller, block .number , _implementation, _initializeData))
110
+ : keccak256 (abi.encodePacked (caller, _salt));
111
+
112
+ // compute the address of the clone and save it
113
+ address computedContractAddress = Clones.predictDeterministicAddress (_implementation, salt, address (this ));
114
+ getContractDeployer[computedContractAddress] = caller;
115
+
116
+ // deploy the clone
85
117
deployedAddress = Clones.cloneDeterministic (_implementation, salt);
86
118
87
- ThirdwebContract (deployedAddress).setThirdwebInfo (_thirdwebInfo);
88
- require (
89
- keccak256 (bytes (ThirdwebContract (deployedAddress).getPublishMetadataUri ())) ==
90
- keccak256 (bytes (_thirdwebInfo.publishMetadataUri)),
91
- "Not a thirdweb contract "
92
- );
119
+ // set the owner
120
+ ThirdwebContract (deployedAddress).tw_initializeOwner (caller);
121
+
122
+ // register to metadata registry
123
+ metadataRegistry.registerMetadata (deployedAddress, publishMetadataUri);
93
124
94
- registry.add (_publisher, deployedAddress);
125
+ // register to TWRegistry
126
+ registry.add (caller, deployedAddress);
95
127
96
128
if (_initializeData.length > 0 ) {
97
129
// slither-disable-next-line unused-return
98
130
Address.functionCallWithValue (deployedAddress, _initializeData, _value);
99
131
}
100
132
101
- emit ContractDeployed (_msgSender () , _publisher, deployedAddress);
133
+ emit ContractDeployed (caller , _publisher, deployedAddress);
102
134
}
103
135
104
136
/*///////////////////////////////////////////////////////////////
0 commit comments