-
Notifications
You must be signed in to change notification settings - Fork 6
/
NFTDropMarketFixedPriceSale.sol
314 lines (283 loc) · 12.5 KB
/
NFTDropMarketFixedPriceSale.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.12;
import "@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol";
import "@openzeppelin/contracts/access/IAccessControl.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
import "../../interfaces/INFTDropCollectionMint.sol";
import "../shared/Constants.sol";
import "../shared/MarketFees.sol";
/// @param limitPerAccount The limit of tokens an account can purchase.
error NFTDropMarketFixedPriceSale_Cannot_Buy_More_Than_Limit(uint256 limitPerAccount);
error NFTDropMarketFixedPriceSale_Limit_Per_Account_Must_Be_Set();
error NFTDropMarketFixedPriceSale_Mint_Permission_Required();
error NFTDropMarketFixedPriceSale_Must_Buy_At_Least_One_Token();
error NFTDropMarketFixedPriceSale_Must_Have_Sale_In_Progress();
error NFTDropMarketFixedPriceSale_Must_Not_Be_Sold_Out();
error NFTDropMarketFixedPriceSale_Must_Not_Have_Pending_Sale();
error NFTDropMarketFixedPriceSale_Must_Support_Collection_Mint_Interface();
error NFTDropMarketFixedPriceSale_Only_Callable_By_Collection_Owner();
/// @param mintCost The total cost for this purchase.
error NFTDropMarketFixedPriceSale_Too_Much_Value_Provided(uint256 mintCost);
/**
* @title Allows creators to list a drop collection for sale at a fixed price point.
* @dev Listing a collection for sale in this market requires the collection to implement
* the functions in `INFTDropCollectionMint` and to register that interface with ERC165.
* Additionally the collection must implement access control, or more specifically:
* `hasRole(bytes32(0), msg.sender)` must return true when called from the creator or admin's account
* and `hasRole(keccak256("MINTER_ROLE", address(this)))` must return true for this market's address.
*/
abstract contract NFTDropMarketFixedPriceSale is MarketFees {
using AddressUpgradeable for address;
using AddressUpgradeable for address payable;
using ERC165Checker for address;
/**
* @notice Configuration for the terms of the sale.
* @dev This structure is packed in order to consume just a single slot.
*/
struct FixedPriceSaleConfig {
/**
* @notice The seller for the drop.
*/
address payable seller;
/**
* @notice The fixed price per NFT in the collection.
* @dev The maximum price that can be set on an NFT is ~1.2M (2^80/10^18) ETH.
*/
uint80 price;
/**
* @notice The max number of NFTs an account may have while minting.
*/
uint16 limitPerAccount;
}
/**
* @notice Stores the current sale information for all drop contracts.
*/
mapping(address => FixedPriceSaleConfig) private nftContractToFixedPriceSaleConfig;
/**
* @notice The `role` type used to validate drop collections have granted this market access to mint.
* @return `keccak256("MINTER_ROLE")`
*/
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
/**
* @notice Emitted when a collection is listed for sale.
* @param nftContract The address of the NFT drop collection.
* @param seller The address for the seller which listed this for sale.
* @param price The price per NFT minted.
* @param limitPerAccount The max number of NFTs an account may have while minting.
*/
event CreateFixedPriceSale(
address indexed nftContract,
address indexed seller,
uint256 price,
uint256 limitPerAccount
);
/**
* @notice Emitted when NFTs are minted from the drop.
* @dev The total price paid by the buyer is `totalFees + creatorRev`.
* @param nftContract The address of the NFT drop collection.
* @param buyer The address of the buyer.
* @param firstTokenId The tokenId for the first NFT minted.
* The other minted tokens are assigned sequentially, so `firstTokenId` - `firstTokenId + count - 1` were minted.
* @param count The number of NFTs minted.
* @param totalFees The amount of ETH that was sent to Foundation & referrals for this sale.
* @param creatorRev The amount of ETH that was sent to the creator for this sale.
*/
event MintFromFixedPriceDrop(
address indexed nftContract,
address indexed buyer,
uint256 indexed firstTokenId,
uint256 count,
uint256 totalFees,
uint256 creatorRev
);
/**
* @notice Create a fixed price sale drop.
* @param nftContract The address of the NFT drop collection.
* @param price The price per NFT minted.
* Set price to 0 for a first come first serve airdrop-like drop.
* @param limitPerAccount The max number of NFTs an account may have while minting.
* @dev Notes:
* a) The sale is final and can not be updated or canceled.
* b) The sale is immediately kicked off.
* c) Any collection that abides by `INFTDropCollectionMint` and `IAccessControl` is supported.
*/
/* solhint-disable-next-line code-complexity */
function createFixedPriceSale(
address nftContract,
uint80 price,
uint16 limitPerAccount
) external {
// Confirm the drop collection is supported
if (!nftContract.supportsInterface(type(INFTDropCollectionMint).interfaceId)) {
revert NFTDropMarketFixedPriceSale_Must_Support_Collection_Mint_Interface();
}
if (INFTDropCollectionMint(nftContract).numberOfTokensAvailableToMint() == 0) {
revert NFTDropMarketFixedPriceSale_Must_Not_Be_Sold_Out();
}
// Use the AccessControl interface to confirm the msg.sender has permissions to list.
if (!IAccessControl(nftContract).hasRole(DEFAULT_ADMIN_ROLE, msg.sender)) {
revert NFTDropMarketFixedPriceSale_Only_Callable_By_Collection_Owner();
}
// And that this contract has permission to mint.
if (!IAccessControl(nftContract).hasRole(MINTER_ROLE, address(this))) {
revert NFTDropMarketFixedPriceSale_Mint_Permission_Required();
}
// Validate input params.
if (limitPerAccount == 0) {
revert NFTDropMarketFixedPriceSale_Limit_Per_Account_Must_Be_Set();
}
// Any price is supported, including 0.
// Confirm this collection has not already been listed.
FixedPriceSaleConfig storage saleConfig = nftContractToFixedPriceSaleConfig[nftContract];
if (saleConfig.seller != payable(0)) {
revert NFTDropMarketFixedPriceSale_Must_Not_Have_Pending_Sale();
}
// Save the sale details.
saleConfig.seller = payable(msg.sender);
saleConfig.price = price;
saleConfig.limitPerAccount = limitPerAccount;
emit CreateFixedPriceSale(nftContract, saleConfig.seller, saleConfig.price, saleConfig.limitPerAccount);
}
/**
* @notice Used to mint `count` number of NFTs from the collection.
* @param nftContract The address of the NFT drop collection.
* @param count The number of NFTs to mint.
* @param buyReferrer The address which referred this purchase, or address(0) if n/a.
* @return firstTokenId The tokenId for the first NFT minted.
* The other minted tokens are assigned sequentially, so `firstTokenId` - `firstTokenId + count - 1` were minted.
* @dev This call may revert if the collection has sold out, has an insufficient number of tokens available,
* or if the market's minter permissions were removed.
* If insufficient msg.value is included, the msg.sender's available FETH token balance will be used.
*/
function mintFromFixedPriceSale(
address nftContract,
uint16 count,
address payable buyReferrer
) external payable returns (uint256 firstTokenId) {
// Validate input params.
if (count == 0) {
revert NFTDropMarketFixedPriceSale_Must_Buy_At_Least_One_Token();
}
FixedPriceSaleConfig memory saleConfig = nftContractToFixedPriceSaleConfig[nftContract];
// Confirm that the buyer will not exceed the limit specified after minting.
if (IERC721(nftContract).balanceOf(msg.sender) + count > saleConfig.limitPerAccount) {
if (saleConfig.limitPerAccount == 0) {
// Provide a more targeted error if the collection has not been listed.
revert NFTDropMarketFixedPriceSale_Must_Have_Sale_In_Progress();
}
revert NFTDropMarketFixedPriceSale_Cannot_Buy_More_Than_Limit(saleConfig.limitPerAccount);
}
// Calculate the total cost, considering the `count` requested.
uint256 mintCost;
unchecked {
// Can not overflow as 2^80 * 2^16 == 2^96 max which fits in 256 bits.
mintCost = uint256(saleConfig.price) * count;
}
// The sale price is immutable so the buyer is aware of how much they will be paying when their tx is broadcasted.
if (msg.value > mintCost) {
// Since price is known ahead of time, if too much ETH is sent then something went wrong.
revert NFTDropMarketFixedPriceSale_Too_Much_Value_Provided(mintCost);
}
// Withdraw from the user's available FETH balance if insufficient msg.value was included.
_tryUseFETHBalance(mintCost, false);
// Mint the NFTs.
firstTokenId = INFTDropCollectionMint(nftContract).mintCountTo(count, msg.sender);
// Distribute revenue from this sale.
(uint256 totalFees, uint256 creatorRev, ) = _distributeFunds(
nftContract,
firstTokenId,
saleConfig.seller,
mintCost,
buyReferrer
);
emit MintFromFixedPriceDrop(nftContract, msg.sender, firstTokenId, count, totalFees, creatorRev);
}
/**
* @notice Returns the max number of NFTs a given account may mint.
* @param nftContract The address of the NFT drop collection.
* @param user The address of the user which will be minting.
* @return numberThatCanBeMinted How many NFTs the user can mint.
*/
function getAvailableCountFromFixedPriceSale(address nftContract, address user)
external
view
returns (uint256 numberThatCanBeMinted)
{
(, , uint256 limitPerAccount, uint256 numberOfTokensAvailableToMint, bool marketCanMint) = getFixedPriceSale(
nftContract
);
if (!marketCanMint) {
// No one can mint in the current state.
return 0;
}
uint256 currentBalance = IERC721(nftContract).balanceOf(user);
if (currentBalance >= limitPerAccount) {
// User has exhausted their limit.
return 0;
}
uint256 availableToMint = limitPerAccount - currentBalance;
if (availableToMint > numberOfTokensAvailableToMint) {
// User has more tokens available than the collection has available.
return numberOfTokensAvailableToMint;
}
return availableToMint;
}
/**
* @notice Returns details for a drop collection's fixed price sale.
* @param nftContract The address of the NFT drop collection.
* @return seller The address of the seller which listed this drop for sale.
* This value will be address(0) if the collection is not listed or has sold out.
* @return price The price per NFT minted.
* @return limitPerAccount The max number of NFTs an account may have while minting.
* @return numberOfTokensAvailableToMint The total number of NFTs that may still be minted.
* @return marketCanMint True if this contract has permissions to mint from the given collection.
*/
function getFixedPriceSale(address nftContract)
public
view
returns (
address payable seller,
uint256 price,
uint256 limitPerAccount,
uint256 numberOfTokensAvailableToMint,
bool marketCanMint
)
{
try INFTDropCollectionMint(nftContract).numberOfTokensAvailableToMint() returns (uint256 count) {
if (count != 0) {
try IAccessControl(nftContract).hasRole(MINTER_ROLE, address(this)) returns (bool hasRole) {
marketCanMint = hasRole;
} catch {
// The contract is not supported - return default values.
return (payable(0), 0, 0, 0, false);
}
FixedPriceSaleConfig memory saleConfig = nftContractToFixedPriceSaleConfig[nftContract];
seller = saleConfig.seller;
price = saleConfig.price;
limitPerAccount = saleConfig.limitPerAccount;
numberOfTokensAvailableToMint = count;
}
// Else minted completed -- return default values.
} catch // solhint-disable-next-line no-empty-blocks
{
// Contract not supported or self destructed - return default values
}
}
/**
* @inheritdoc MarketSharedCore
* @dev Returns the seller for a collection if listed and not already sold out.
*/
function _getSellerOf(
address nftContract,
uint256 /* tokenId */
) internal view virtual override returns (address payable seller) {
(seller, , , , ) = getFixedPriceSale(nftContract);
}
/**
* @notice This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[1000] private __gap;
}