-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeralfileArtworkV5.sol
713 lines (623 loc) · 22.7 KB
/
FeralfileArtworkV5.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "./Authorizable.sol";
import "./FeralfileSaleDataV2.sol";
import "./ECDSASigner.sol";
import "./FeralfileVaultV2.sol";
contract FeralfileExhibitionV5 is
ERC1155,
Authorizable,
FeralfileSaleDataV2,
ECDSASigner
{
using Strings for uint256;
struct Artwork {
uint256 seriesId;
uint256 tokenId;
uint256 amount;
}
struct MintData {
uint256 seriesId;
uint256 tokenId;
address owner;
uint256 amount;
}
struct BurnData {
address from;
uint256 tokenId;
uint256 amount;
}
// version code of contract
string public constant codeVersion = "FeralfileExhibitionV5";
// contract URI
string public contractURI;
// burnable
bool public burnable;
// selling
bool public selling;
// mintable
bool public mintable = true;
// cost receiver
address public costReceiver;
// vault contract instance
IFeralfileVaultV2 public vault;
// mapping from series ID to max supply of the series
mapping(uint256 => uint256) internal _seriesMaxSupplies;
// mapping from series ID to max supply of artwork belongs to the series
mapping(uint256 => uint256) internal _seriesArtworkMaxSupplies;
// mapping from series ID to total supply of the series
mapping(uint256 => uint256) internal _seriesTotalSupplies;
// mapping from token ID representing the artwork to total supply of the artwork
mapping(uint256 => uint256) internal _artworkTotalSupplies;
// mapping from token ID to Artwork
mapping(uint256 => Artwork) internal _allArtworks;
// mapping from owner to an array of token ID
mapping(address => uint256[]) internal _ownedTokens;
// mapping from token ID to index of the owner token list
mapping(uint256 => uint256) private _ownedTokensIndex;
constructor(
string memory baseTokenURI_,
string memory contractURI_,
address signer_,
address vault_,
address costReceiver_,
bool burnable_,
uint256[] memory seriesIds_,
uint256[] memory seriesMaxSupplies_,
uint256[] memory seriesArtworkMaxSupplies_
) ERC1155(baseTokenURI_) ECDSASigner(signer_) {
require(
bytes(baseTokenURI_).length > 0,
"FeralfileExhibitionV5: baseTokenURI_ is empty"
);
require(
bytes(contractURI_).length > 0,
"FeralfileExhibitionV5: contractURI_ is empty"
);
require(
vault_ != address(0),
"FeralfileExhibitionV5: vault_ is zero address"
);
require(
costReceiver_ != address(0),
"FeralfileExhibitionV5: costReceiver_ is zero address"
);
require(
seriesIds_.length > 0,
"FeralfileExhibitionV5: seriesIds_ is empty"
);
require(
seriesMaxSupplies_.length > 0,
"FeralfileExhibitionV5: _seriesMaxSupplies is empty"
);
require(
seriesArtworkMaxSupplies_.length > 0,
"FeralfileExhibitionV5: seriesArtworkMaxSupplies_ is empty"
);
require(
seriesIds_.length == seriesMaxSupplies_.length &&
seriesIds_.length == seriesArtworkMaxSupplies_.length,
"FeralfileExhibitionV5: seriesMaxSupplies_ and seriesIds_ lengths are not the same"
);
contractURI = contractURI_;
costReceiver = costReceiver_;
vault = IFeralfileVaultV2(payable(vault_));
burnable = burnable_;
// initialize max supply map
for (uint256 i = 0; i < seriesIds_.length; i++) {
// Check invalid series ID
require(seriesIds_[i] > 0, "FeralfileExhibitionV5: zero seriesId");
// Check invalid max supply
require(
seriesMaxSupplies_[i] > 0,
"FeralfileExhibitionV5: zero series max supply"
);
require(
seriesArtworkMaxSupplies_[i] > 0,
"FeralfileExhibitionV5: zero artwork max supply"
);
// Check duplicate with others
require(
_seriesMaxSupplies[seriesIds_[i]] == 0,
"FeralfileExhibitionV5: duplicate seriesId"
);
require(
_seriesArtworkMaxSupplies[seriesIds_[i]] == 0,
"FeralfileExhibitionV5: duplicate seriesId"
);
_seriesMaxSupplies[seriesIds_[i]] = seriesMaxSupplies_[i];
_seriesArtworkMaxSupplies[
seriesIds_[i]
] = seriesArtworkMaxSupplies_[i];
}
}
/// @notice Get all Artworks of an owner
/// @param owner_ an address of the owner
function artworkOf(
address owner_
) external view returns (Artwork[] memory) {
uint256[] memory tokens = _ownedTokens[owner_];
Artwork[] memory artworks = new Artwork[](tokens.length);
for (uint256 i = 0; i < tokens.length; i++) {
artworks[i] = _allArtworks[tokens[i]];
}
return artworks;
}
/// @notice Get all token IDs of an owner
/// @param owner_ an address of the owner
function tokenOf(address owner_) external view returns (uint256[] memory) {
return _ownedTokens[owner_];
}
/// @notice Get series max supply
/// @param seriesId_ a series ID
/// @return uint256 the max supply
function seriesMaxSupply(
uint256 seriesId_
) external view virtual returns (uint256) {
return _seriesMaxSupplies[seriesId_];
}
/// @notice Get series artwork max supply
/// @param seriesId_ a series ID
function seriesArtworkMaxSupply(
uint256 seriesId_
) external view virtual returns (uint256) {
return _seriesArtworkMaxSupplies[seriesId_];
}
/// @notice Get series total supply
/// @param seriesId_ a series ID
/// @return uint256 the total supply
function seriesTotalSupply(
uint256 seriesId_
) external view virtual returns (uint256) {
return _seriesTotalSupplies[seriesId_];
}
/// @notice Get artwork total supply
/// @param tokenId_ a token ID representing the artwork
/// @return uint256 the total supply
function artworkTotalSupply(
uint256 tokenId_
) external view virtual returns (uint256) {
return _artworkTotalSupplies[tokenId_];
}
/// @notice Get artwork data
/// @param tokenId_ a token ID representing the artwork
/// @return Artwork the Artwork object
function getArtwork(
uint256 tokenId_
) external view virtual returns (Artwork memory) {
return _allArtworks[tokenId_];
}
/// @notice Set vault contract
/// @param vault_ - the address of vault contract
/// @dev don't allow to set vault as zero address
function setVault(address vault_) external onlyOwner {
require(
vault_ != address(0),
"FeralfileExhibitionV5: vault_ is zero address"
);
vault = IFeralfileVaultV2(payable(vault_));
}
/// @notice set contract URI
/// @param contractURI_ contract URI
function setContractURI(string memory contractURI_) external onlyOwner {
contractURI = contractURI_;
emit ContractURIUpdated();
}
/// @notice set base token URI
/// @param uri_ token URI
function setBaseTokenURI(string memory uri_) external onlyOwner {
require(bytes(uri_).length > 0, "FeralfileExhibitionV5: uri_ is empty");
_setURI(uri_);
}
/// @notice the cost receiver address
/// @param costReceiver_ - the address of cost receiver
function setCostReceiver(address costReceiver_) external onlyOwner {
require(
costReceiver_ != address(0),
"FeralfileExhibitionV5: costReceiver_ is zero address"
);
costReceiver = costReceiver_;
}
/// @notice Start artwork sale
function startSale() external onlyOwner {
mintable = false;
resumeSale();
}
/// @notice Resume artwork sale
function resumeSale() public onlyOwner {
require(
!mintable,
"FeralfileExhibitionV5: mintable required to be false"
);
require(
!selling,
"FeralfileExhibitionV5: selling required to be false"
);
require(
_ownedTokens[address(this)].length > 0,
"FeralfileExhibitionV5: no more artwork to sell"
);
selling = true;
}
/// @notice Pause artwork sale
function pauseSale() public onlyOwner {
require(
!mintable,
"FeralfileExhibitionV5: mintable required to be false"
);
require(selling, "FeralfileExhibitionV5: selling required to be true");
selling = false;
}
/// @notice burn unsold artworks
/// @param limit_ the maximum number of unsold artworks to burn
/// @dev the limit_ is used to prevent the function from running out of gas
function burnUnsoldArtworks(uint256 limit_) external onlyOwner {
require(limit_ > 0, "FeralfileExhibitionV5: limit_ is zero");
require(
!mintable,
"FeralfileExhibitionV5: mintable required to be false"
);
require(
!selling,
"FeralfileExhibitionV5: selling required to be false"
);
// get all token IDs of the contract
uint256[] memory tokenIds = _ownedTokens[address(this)];
if (tokenIds.length == 0) {
return;
}
if (tokenIds.length < limit_) {
limit_ = tokenIds.length;
}
// burn data
BurnData[] memory data = new BurnData[](limit_);
for (uint256 i = 0; i < limit_; i++) {
data[i] = BurnData({
from: address(this),
tokenId: tokenIds[i],
amount: balanceOf(address(this), tokenIds[i])
});
}
// burn artworks
for (uint256 i = 0; i < data.length; i++) {
_burnArtwork(data[i].from, data[i].tokenId, data[i].amount);
}
}
/// @notice transfer unsold artworks to a destination address
/// @param tokenIds_ an array of token IDs
/// @param to_ the destination address
function transferUnsoldArtworks(
uint256[] calldata tokenIds_,
address to_
) external onlyOwner {
require(
to_ != address(0),
"FeralfileExhibitionV5: to_ is zero address"
);
require(
tokenIds_.length > 0,
"FeralfileExhibitionV5: tokenIds_ is empty"
);
require(
!mintable,
"FeralfileExhibitionV5: mintable required to be false"
);
require(
!selling,
"FeralfileExhibitionV5: selling required to be false"
);
uint256[] memory amounts = new uint256[](tokenIds_.length);
for (uint256 i = 0; i < tokenIds_.length; i++) {
uint256 amount = balanceOf(address(this), tokenIds_[i]);
amounts[i] = amount;
}
_safeBatchTransferFrom(address(this), to_, tokenIds_, amounts, "");
}
/// @notice pay to get artworks to a destination address. The pricing, costs and other details is included in the saleData
/// @param r_ - part of signature for validating parameters integrity
/// @param s_ - part of signature for validating parameters integrity
/// @param v_ - part of signature for validating parameters integrity
/// @param saleData_ - the sale data
function buyArtworks(
bytes32 r_,
bytes32 s_,
uint8 v_,
SaleData calldata saleData_
) external payable {
// validation
require(selling, "FeralfileExhibitionV5: sale is not started");
require(
_ownedTokens[address(this)].length > 0,
"FeralfileExhibitionV5: no artwork to sell"
);
validateSaleData(saleData_);
// payment
saleData_.payByVaultContract
? vault.payForSale(r_, s_, v_, saleData_)
: require(
saleData_.price == msg.value,
"FeralfileExhibitionV5: invalid payment amount"
);
// validate signature
bytes32 message = keccak256(
abi.encode(block.chainid, address(this), saleData_)
);
require(
isValidSignature(message, r_, s_, v_),
"FeralfileExhibitionV5: invalid signature"
);
// item royalty
uint256 itemRevenue;
if (saleData_.price > saleData_.cost) {
itemRevenue =
(saleData_.price - saleData_.cost) /
saleData_.tokenIds.length;
}
uint256 distributedRevenue;
uint256 platformRevenue;
for (uint256 i = 0; i < saleData_.tokenIds.length; i++) {
require(
saleData_.tokenIds[i] != 0,
"FeralfileExhibitionV5: token ID is zero"
);
// send token
_safeTransferFrom(
address(this),
saleData_.destination,
saleData_.tokenIds[i],
1,
""
); // only support 1 token per transaction
if (itemRevenue > 0) {
// distribute royalty
for (
uint256 j = 0;
j < saleData_.revenueShares[i].length;
j++
) {
uint256 rev = (itemRevenue *
saleData_.revenueShares[i][j].bps) / 10000;
if (
saleData_.revenueShares[i][j].recipient == costReceiver
) {
platformRevenue += rev;
continue;
}
distributedRevenue += rev;
payable(saleData_.revenueShares[i][j].recipient).transfer(
rev
);
}
}
emit BuyArtwork(
saleData_.destination,
saleData_.tokenIds[i],
saleData_.biddingUnix
);
}
require(
saleData_.price - saleData_.cost >=
distributedRevenue + platformRevenue,
"FeralfileExhibitionV5: total bps over 10,000"
);
// Transfer cost, platform revenue and remaining funds
uint256 leftOver = saleData_.price - distributedRevenue;
if (leftOver > 0) {
payable(costReceiver).transfer(leftOver);
}
}
function _beforeTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual override {
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
for (uint256 i = 0; i < ids.length; i++) {
uint256 tokenId = ids[i];
if (
from != address(0) &&
from != to &&
balanceOf(from, tokenId) == amounts[i]
) {
// only remove Artwork from Artwork enumeration if the current owned
// token amount is equal to the amount of token to be transferred
// otherwise, just update the amount of the token
_removeTokenFromOwnerEnumeration(from, ids[i]);
}
if (
to != address(0) &&
to != from &&
_ownedTokensIndex[tokenId] == 0
) {
// only add Artwork to Artwork enumeration if the token is not owned by the receiver
// otherwise, just update the amount of the token
_addTokenToOwnerEnumeration(to, tokenId);
}
}
}
/// @dev Modify from {ERC721Enumerable}
function _removeTokenFromOwnerEnumeration(
address from,
uint256 tokenId
) private {
// To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and
// then delete the last slot (swap and pop).
uint256 lastTokenIndex = _ownedTokens[from].length - 1;
uint256 tokenIndex = _ownedTokensIndex[tokenId];
// When the token to delete is the last token, the swap operation is unnecessary
if (tokenIndex != lastTokenIndex) {
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
_ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
_ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
}
delete _ownedTokensIndex[tokenId];
_ownedTokens[from].pop();
}
/// @dev Modify from {ERC721Enumerable}
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
uint256[] storage tokens = _ownedTokens[to];
uint256 length = tokens.length;
tokens.push(tokenId);
_ownedTokensIndex[tokenId] = length;
}
/// @notice Mint new collection of Artwork
/// @dev the function iterates over the array of MintData to call the internal function _mintArtwork
/// @param data_ an array of MintData
function mintArtworks(
MintData[] calldata data_
) external virtual onlyAuthorized {
require(
mintable,
"FeralfileExhibitionV5: contract doesn't allow to mint"
);
for (uint256 i = 0; i < data_.length; i++) {
_mintArtwork(
data_[i].seriesId,
data_[i].tokenId,
data_[i].owner,
data_[i].amount
);
}
}
function _mintArtwork(
uint256 seriesId_,
uint256 tokenId_,
address owner_,
uint256 amount_
) internal {
require(tokenId_ != 0, "FeralfileExhibitionV5: token ID is zero");
require(amount_ != 0, "FeralfileExhibitionV5: amount is zero");
Artwork memory artwork = _allArtworks[tokenId_];
if (artwork.tokenId != 0) {
// reassign seriesId for existing artwork to avoid bypass the max supply checks
seriesId_ = artwork.seriesId;
}
// check series exists
require(
_seriesMaxSupplies[seriesId_] > 0,
string(
abi.encodePacked(
"FeralfileExhibitionV5: seriesId doesn't exist: ",
Strings.toString(seriesId_)
)
)
);
// check artwork total supplies
require(
_artworkTotalSupplies[tokenId_] + amount_ <=
_seriesArtworkMaxSupplies[seriesId_],
"FeralfileExhibitionV5: no more artwork slots available"
);
if (artwork.tokenId != 0) {
// if artwork exists before
// increase artwork amount
_allArtworks[tokenId_].amount += amount_;
} else {
// if artwork doesn't exist before
// 1.check series total supplies
require(
_seriesTotalSupplies[seriesId_] < _seriesMaxSupplies[seriesId_],
"FeralfileExhibitionV5: no more series slots available"
);
// 2. increase series total supplies
_seriesTotalSupplies[seriesId_] += 1;
// 3. add artwork to allArtworks
_allArtworks[tokenId_] = Artwork({
seriesId: seriesId_,
tokenId: tokenId_,
amount: amount_
});
}
// increase artwork total supplies
_artworkTotalSupplies[tokenId_] += amount_;
// mint token
_mint(owner_, tokenId_, amount_, "");
// emit event
emit NewArtwork(owner_, seriesId_, tokenId_, amount_);
}
/// @notice Burn a collection of artworks
/// @dev the function iterates over the array of token ID to call the internal function _burnArtwork
/// @param data_ an array of BurnData
function burnArtworks(BurnData[] calldata data_) external {
require(burnable, "FeralfileExhibitionV5: token is not burnable");
for (uint256 i = 0; i < data_.length; i++) {
_burnArtwork(data_[i].from, data_[i].tokenId, data_[i].amount);
}
}
function _burnArtwork(
address from_,
uint256 tokenId_,
uint256 amount_
) internal {
require(tokenId_ != 0, "FeralfileExhibitionV5: token ID is zero");
require(amount_ != 0, "FeralfileExhibitionV5: amount is zero");
require(
from_ != address(0),
"FeralfileExhibitionV5: from is zero address"
);
Artwork memory artwork = _allArtworks[tokenId_];
require(
artwork.tokenId != 0,
"FeralfileExhibitionV5: artwork doesn't exist"
);
if (artwork.amount <= amount_) {
// if burn whole token of artwork
// 1. decrease series total supplies
// 2. remove artwork from allArtworks
_seriesTotalSupplies[artwork.seriesId] -= 1;
delete _allArtworks[tokenId_];
} else {
// if burn part of token of artwork
// just decrease artwork amount
_allArtworks[tokenId_].amount -= amount_;
}
// decrease artwork total supplies
_artworkTotalSupplies[tokenId_] -= amount_;
// burn artwork
_burn(from_, tokenId_, amount_);
// emit event
emit BurnArtwork(tokenId_, amount_);
}
/// @notice able to receive fund from vault contract
receive() external payable {
require(
msg.sender == address(vault),
"FeralfileExhibitionV5: only accept fund from vault contract."
);
}
function onERC1155Received(
address,
address from_,
uint256,
uint256,
bytes memory
) public pure returns (bytes4) {
require(
from_ == address(0),
"FeralfileExhibitionV5: not allowed to send token back"
);
return this.onERC1155Received.selector;
}
/// @notice Event emitted when new Artwork has been minted
event NewArtwork(
address indexed owner_,
uint256 indexed seriesId_,
uint256 indexed tokenId_,
uint256 amount_
);
/// @notice Event emitted when Artwork has been burned
event BurnArtwork(uint256 indexed tokenId_, uint256 amount_);
/// @notice Event emitted when Artwork has been sold
event BuyArtwork(
address indexed buyer_,
uint256 indexed tokenId_,
uint256 biddingUnix_
);
/// @notice Event emitted when contract URI has been updated
event ContractURIUpdated();
}