/
CoreFactory.sol
167 lines (141 loc) · 4.82 KB
/
CoreFactory.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
155
156
157
158
159
160
161
162
163
164
165
166
167
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import {CoreProxy} from './CoreProxy.sol';
import './utils/structs/Collection.sol';
import {CoreCollection} from './CoreCollection.sol';
import {ICoreCollection} from '../interfaces/ICoreCollection.sol';
contract CoreFactory {
struct Project {
string id;
address creator;
}
event NewProject(string id, address creator);
event NewCollection(
string collectionId,
address collection,
string projectId
);
address public immutable collection;
address public immutable splitFactory;
mapping(string => Project) public projects;
mapping(string => address) public collections;
constructor(address _collection, address _splitFactory) {
collection = _collection;
splitFactory = _splitFactory;
}
// ---------------- MODIFIER ----------------
modifier onlyAvailableProject(string memory _projectId) {
require(
projects[_projectId].creator == address(0),
'CoreFactory: Unavailable project id'
);
_;
}
modifier onlyProjectOwner(string memory _projectId) {
require(
projects[_projectId].creator == msg.sender,
'CoreFactory: Not an owner of the project'
);
_;
}
modifier onlyAvailableCollection(string memory _collectionId) {
require(
collections[_collectionId] == address(0),
'CoreFactory: Unavailable collection id'
);
_;
}
// ---------------- EXTERNAL ----------------
/**
* @notice Allows to create a project as well as deploy its collection(s)
* For adding a collection to a project, use the addCollection() method.
* @dev Projects have unique identifiers.
* Collections are deployed using a proxy pattern. This is mainly for gas optimization purposes
* and to support future contract upgrades.
* Collections ownership are transferred to the caller.
* @param _projectId Project id which is a unique identifier
* @param _collections An array of Collection that needs to be deployed
*/
function createProject(
string memory _projectId,
Collection[] memory _collections
) external onlyAvailableProject(_projectId) {
require(
_collections.length > 0,
'CoreFactory: should have more at least one collection'
);
for (uint256 i; i < _collections.length; i++) {
Collection memory _collection = _collections[i];
address coreCollection = _createCollection(_collection);
if (_collection.claimsMerkleRoot != bytes32(0)) {
ICoreCollection(coreCollection).initializeClaims(
_collection.claimsMerkleRoot
);
}
emit NewCollection(_collection.id, coreCollection, _projectId);
ICoreCollection(coreCollection).transferOwnership(msg.sender);
}
Project memory project;
project.id = _projectId;
project.creator = msg.sender;
projects[_projectId] = project;
emit NewProject(_projectId, msg.sender);
}
/**
* @notice Allows to add a collection to a project
* @dev Can only be called by project creator
* Collection's ownership is transferred to the caller
* @param _projectId Project id which is a unique identifier
* @param _collection Collection that needs to be deployed
*/
function addCollection(
string memory _projectId,
Collection memory _collection
) external onlyProjectOwner(_projectId) returns (address) {
address coreCollection = _createCollection(_collection);
if (_collection.claimsMerkleRoot != bytes32(0)) {
ICoreCollection(coreCollection).initializeClaims(
_collection.claimsMerkleRoot
);
}
emit NewCollection(_collection.id, coreCollection, _projectId);
ICoreCollection(coreCollection).transferOwnership(msg.sender);
return coreCollection;
}
// ---------------- VIEW ----------------
function getProject(string memory _projectId)
external
view
returns (Project memory)
{
return projects[_projectId];
}
// ---------------- PRIVATE ----------------
/**
* @notice Instanciates/Deploys a collection
* @param _collection Collection that needs to be deployed
*/
function _createCollection(Collection memory _collection)
private
onlyAvailableCollection(_collection.id)
returns (address)
{
address coreCollection = address(
new CoreProxy{salt: keccak256(abi.encodePacked(_collection.id))}(
collection
)
);
ICoreCollection(coreCollection).initialize(
_collection.name,
_collection.symbol,
_collection.baseURI,
_collection.maxSupply,
_collection.mintFee,
_collection.payableToken,
_collection.isForSale,
splitFactory
);
collections[_collection.id] = coreCollection;
return coreCollection;
}
}