-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartContract
1305 lines (1187 loc) · 39.6 KB
/
SmartContract
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/interfaces/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/interfaces/IERC2981.sol";
import "@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract MetastateNFTMarketPlace is
AccessControlEnumerable,
IERC721Receiver,
ReentrancyGuard,
IERC1155Receiver
{
using SafeMath for uint256;
using Address for address;
struct Sale {
uint256 tokenId;
uint256 price;
uint256 quantity;
address erc20Token;
address seller;
}
struct Auction {
uint256 tokenId;
uint256 basePrice;
uint256 salePrice;
address erc20Token;
uint256 quantity;
address auctioner;
address currentBidder;
uint256 bidAmount;
}
struct Fee {
address receiver;
uint256 percentageValue;
}
event NftListed(
uint256 indexed tokenId,
address indexed nftAddress,
address indexed seller,
uint256 price,
address erc20Token,
uint256 quantity
);
event NftSold(
uint256 indexed tokenId,
address indexed nftAddress,
address indexed seller,
uint256 price,
address erc20Token,
address buyer,
uint256 quantity
);
event SaleCanceled(uint256 tokenId, address sellerOrAdmin);
event AuctionCreated(
uint256 indexed tokenId,
address indexed nftAddress,
address indexed auctioner,
uint256 basePrice,
uint256 salePrice,
address erc20Token,
uint256 quantity
);
event BidPlaced(
uint256 indexed tokenId,
address indexed tokenContract,
address indexed auctioner,
address bidder,
address erc20Token,
uint256 quantity,
uint256 price
);
event AuctionSettled(
uint256 indexed tokenId,
address indexed tokenContract,
address indexed auctioner,
address heighestBidder,
address erc20Token,
uint256 quantity,
uint256 heighestBid
);
event AuctionCancelled(
uint256 indexed tokenId,
address indexed tokenContract,
address indexed auctioner,
uint256 quantity,
address erc20Token,
uint256 heighestBid,
address heighestBidder
);
event FundTransfer(Fee royalty, Fee sellerProfit, Fee platformFee);
event FundReceived(address indexed from, uint256 amount);
// Define the admin role
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
// Define sale type, based this it is determined to start a fixed price sale or auction
uint256 private constant TYPE_SALE = 1;
uint256 private constant TYPE_AUCTION = 2;
/*
@notice only admin and nft owner can cancel the sale
*/
mapping(address => mapping(uint256 => mapping(address => Sale)))
private mapSale;
mapping(address => mapping(uint256 => mapping(address => Auction)))
private mapAuction;
mapping(address => Fee) creatorRoyalties;
Fee private platformFee;
bytes4 private ERC721InterfaceId = 0x80ac58cd; // Interface Id of ERC721
bytes4 private ERC1155InterfaceId = 0xd9b67a26; // Interface Id of ERC1155
bytes4 private royaltyInterfaceId = 0x2a55205a; // interface Id of Royalty
using SafeERC20 for IERC20;
/*
* @notice Constructor
* @param _erc721Token accept collection (nft contract) address
*/
constructor(Fee memory _platformFee, address _rootAdmin) {
_setPlatformFee(_platformFee);
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
_setupRole(ADMIN_ROLE, _rootAdmin);
}
function PlatformFee(Fee memory _platformFee) external onlyOwner {
_setPlatformFee(_platformFee);
}
/**
* @dev modifier to check admin rights.
* contract owner and root admin have admin rights
*/
modifier onlyOwner() {
require(_isAdmin(), "Restricted to admin");
_;
}
modifier callerNotAContract() {
require(msg.sender == tx.origin, "Caller cannot be a contract");
_;
}
/**
* @dev This function is to add creator royalties for third party nfts.
*/
function addCreatorRoyaltyInfo(
address _nftAddress,
address _receiver,
uint256 _percentageValue
) public onlyOwner {
creatorRoyalties[_nftAddress].receiver = payable(_receiver);
creatorRoyalties[_nftAddress].percentageValue = _percentageValue;
}
/**
* @dev This function is to get creator royalties informations using nft contract address.
*/
function getCreatorRoyaltyInfo(address tokenerc721)
public
view
returns (address, uint256)
{
return (
creatorRoyalties[tokenerc721].receiver,
creatorRoyalties[tokenerc721].percentageValue
);
}
/*w
* @notice listNft add nfts for the sale
* @param tokenid accept nft token id
* @param price accept nft price
* @param erc20Token accept erc20 token address for accepting multiple erc20 token for but nfts
*/
function _listNft(
uint256 _tokenId,
uint256 _price,
uint256 _quantity,
address _erc20Token,
address _seller,
address _nftAddress
) internal {
require(_price > 0, "Sell Nfts: Wont Accept Zero Price");
require(_quantity > 0, "Sell Nfts: Wont Accept Zero Value");
require(_seller != address(0), "Sell Nfts: Zero Address");
require(
_nftAddress != address(0),
"Sell Nfts: Nft Address Can Not Be Zero"
);
require(
_isNFT(_nftAddress),
"Sell Nfts: Not confirming to an NFT contract"
);
setSaleDetails(
_tokenId,
_price,
_quantity,
_erc20Token,
_seller,
_nftAddress
);
emit NftListed(
_tokenId,
_nftAddress,
_seller,
_price,
_erc20Token,
_quantity
);
}
/*
* @notice SellNfts add nfts on the sale
* @param starttime accept start time of sale
* @param endtime accept end time of sale
* @param tokenid accept nft token id
* @param price accept nft price
* @param erc20Token accept erc20 token address for accepting multiple erc20 token for but nfts
*/
function setSaleDetails(
uint256 _tokenId,
uint256 _price,
uint256 _quantity,
address _erc20Token,
address _sellerAddress,
address _nftAddress
) internal {
Sale storage NftForSale = mapSale[_nftAddress][_tokenId][
_sellerAddress
];
// Giving the ability to increase the quantity if the item is already listed
// Otherwise create a new listing
if (NftForSale.quantity > 0) {
NftForSale.quantity += _quantity;
} else {
NftForSale.tokenId = _tokenId;
NftForSale.price = _price;
NftForSale.quantity = _quantity;
NftForSale.erc20Token = _erc20Token;
NftForSale.seller = _sellerAddress;
}
}
/*
* @notice get details of sell nfts using token id
* @param starttime selling start time
* @param endTime selling end time
* @param tokenid accept nft token id
* @param price accept nft price
* @param erc20Token accept erc20 token address for accepting multiple erc20 token for but nfts
* @param seller address give nfts seller address
* @param onsale nfts is on sale or not
* @param sold nfts is sold or not
* @param cancel nfts is cancel or not
*/
function getSale(
uint256 tokenId,
address _nftAddress,
address _sellerAddress
) public view returns (Sale memory) {
Sale storage NftForSale = mapSale[_nftAddress][tokenId][_sellerAddress];
return (NftForSale);
}
/*
* @notice this function is used to buy nfts using native crypto currency and multiple erc20 token
* @param _tokenId use to buy nfts on sell
* @param _quantity Token Quantity
* @param _nftAddress Take erc721 and erc1155 address
*/
function buyNft(
uint256 _tokenId,
uint256 _quantity,
address _nftAddress,
address _sellerAddress,
uint256 _saleType
) public payable nonReentrant {
require(msg.sender != address(0), "BuyNfts: Zero Address");
require(_nftAddress != address(0), "BuyNfts: Zero Address");
if (_saleType == TYPE_SALE) {
require(_quantity > 0, "BuyNfts: Zero Quantitiy");
_NftSaleFixedPrice(
_tokenId,
_quantity,
_nftAddress,
_sellerAddress
);
} else if (_saleType == TYPE_AUCTION) {
_NftAuctionInstantBuy(_tokenId, _nftAddress, _sellerAddress);
} else {
revert("Sale type is invalid");
}
}
/*
* @notice This function can only be called by the admin account
* The fiat payment will be converted into to crypto via on-ramp and transferred to the contract for
* administering the payment split and token transfer on-chain
* IMPORTANT: It should only be called after the right amount of crypto/token should received in the contract
* The transfer should be confirmed off chain before calling this function
* @param _tokenId use to buy nfts on sell
* @param _quantity Token Quantity
* @param _nftAddress Take erc721 and erc1155 address
*/
function fiatPurchase(
uint256 _tokenId,
uint256 _quantity,
address _nftAddress,
address _sellerAddress,
address _buyer,
uint256 _saleType
) public payable onlyOwner nonReentrant {
require(_nftAddress != address(0), "BuyNfts: Zero Address");
if (_saleType == TYPE_SALE) {
require(_quantity > 0, "BuyNfts: Zero Quantitiy");
_NftSaleFixedPriceFiat(
_tokenId,
_quantity,
_nftAddress,
_sellerAddress,
_buyer
);
} else if (_saleType == TYPE_AUCTION) {
_NftAuctionInstantBuyFiat(
_tokenId,
_nftAddress,
_sellerAddress,
_buyer
);
} else {
revert("Sale type is invalid");
}
}
/*
* @notice this function is used to cancel the sell
* @param _tokenId : Is the token ID
* @param _nftAddress : NFT contract address
* @param _sellerAddress : The address of the seller
*/
function cancelListing(
uint256 _tokenId,
address _nftAddress,
address _sellerAddress
) public {
Sale storage NftForSale = mapSale[_nftAddress][_tokenId][
_sellerAddress
];
require(
msg.sender == NftForSale.seller || _isAdmin(),
"Cancel Listing : Only Seller or admin can cancel the Listing"
);
require(NftForSale.quantity > 0, "Buy Nfts :No active sale");
//for erc721
if (IERC721(_nftAddress).supportsInterface(ERC721InterfaceId)) {
IERC721(_nftAddress).transferFrom(
address(this),
NftForSale.seller,
_tokenId
);
}
//for erc1155
else if (IERC1155(_nftAddress).supportsInterface(ERC1155InterfaceId)) {
IERC1155(_nftAddress).safeTransferFrom(
address(this),
NftForSale.seller,
_tokenId,
NftForSale.quantity,
""
);
}
delete mapSale[_nftAddress][_tokenId][_sellerAddress];
emit SaleCanceled(_tokenId, msg.sender);
}
/*
* @notice SellNfts add nfts on the sale
* @param starttime accept start time of sale
* @param endtime accept end time of sale
* @param tokenid accept nft token id
* @param price accept nft price
* @param erc20Token accept erc20 token address for accepting multiple erc20 token for but nfts
*/
function _createAuction(Auction memory auctionDetails, address _nftAddress)
internal
{
require(
auctionDetails.basePrice > 0,
"CreateAuction : Not Accept Zero BasePrice"
);
require(auctionDetails.quantity != 0, "CreateAuction: Zero Quantity");
require(
auctionDetails.auctioner != address(0),
"CreateAuction: Zero Address"
);
require(_nftAddress != address(0), "CreateAuction: Zero Address");
require(
_isNFT(_nftAddress),
"CreateAuction: Not confirming to an NFT contract"
);
setAuctionDetails(
auctionDetails.tokenId,
auctionDetails.basePrice,
auctionDetails.salePrice,
auctionDetails.quantity,
auctionDetails.erc20Token,
auctionDetails.auctioner,
_nftAddress
);
emit AuctionCreated(
auctionDetails.tokenId,
_nftAddress,
auctionDetails.auctioner,
auctionDetails.basePrice,
auctionDetails.salePrice,
auctionDetails.erc20Token,
auctionDetails.quantity
);
}
/*
@notice This SetAuctionDetails is used to set the auction details
*/
function setAuctionDetails(
uint256 _tokenId,
uint256 _basePrice,
uint256 _salePrice,
uint256 _quantity,
address _erc20Token,
address _auctioner,
address _nftAddress
) internal {
Auction storage NftOnAuction = mapAuction[_nftAddress][_tokenId][
_auctioner
];
NftOnAuction.tokenId = _tokenId;
NftOnAuction.basePrice = _basePrice;
NftOnAuction.salePrice = _salePrice;
NftOnAuction.erc20Token = _erc20Token;
NftOnAuction.quantity = _quantity;
NftOnAuction.auctioner = _auctioner;
}
/*
* @notice this function is used to buy nfts using native crypto currency and multiple erc20 token
* @param _tokenId use to buy nfts on sell
*/
function getAuction(
uint256 tokenId,
address _nftAddress,
address _auctioner
) external view returns (Auction memory) {
Auction storage nftOnAuction = mapAuction[_nftAddress][tokenId][
_auctioner
];
return (nftOnAuction);
}
/*
* @notice this function is used to place the bid on the nfts using native cryptocurrency and multiple erc20 token
* @param _tokenId use to bid on nfts
* @param _price is used to bid on nfts
*/
function placeBid(
uint256 _tokenId,
uint256 _price,
address _nftAddress,
address _auctioner
) public payable nonReentrant callerNotAContract {
Auction storage NftOnAuction = mapAuction[_nftAddress][_tokenId][
_auctioner
];
require(
NftOnAuction.quantity > 0,
"Place Bid: Bid for non existing auction"
);
require(!_isAdmin(), "PlceBid: Admin Can Not Place Bid");
require(
msg.sender != NftOnAuction.auctioner,
"Place Bid : Seller not allowed to place bid"
);
require(
_price >= NftOnAuction.basePrice,
"Place Bid : Price Less Than the base price"
);
require(
_price > NftOnAuction.bidAmount,
"The price is less then the previous bid amount"
);
if (NftOnAuction.erc20Token == address(0)) {
require(
msg.value == _price,
"Amount received and price should be same"
);
require(
msg.value > NftOnAuction.bidAmount,
"Amount received should be grather than the current bid"
);
if (NftOnAuction.currentBidder != address(0)) {
payable(NftOnAuction.currentBidder).transfer(
NftOnAuction.bidAmount
);
}
} else {
uint256 checkAllowance = IERC20(NftOnAuction.erc20Token).allowance(
msg.sender,
address(this)
);
require(
checkAllowance >= _price,
"Place Bid : Allowance is Less then Price"
);
IERC20(NftOnAuction.erc20Token).safeTransferFrom(
msg.sender,
address(this),
_price
);
if (NftOnAuction.currentBidder != address(0)) {
IERC20(NftOnAuction.erc20Token).safeTransfer(
NftOnAuction.currentBidder,
NftOnAuction.bidAmount
);
}
}
NftOnAuction.bidAmount = _price;
NftOnAuction.currentBidder = msg.sender;
emit BidPlaced(
_tokenId,
_nftAddress,
_auctioner,
msg.sender,
NftOnAuction.erc20Token,
NftOnAuction.quantity,
_price
);
}
//this function is used to settle auction
function settleAuction(
uint256 _tokenId,
address _nftAddress,
address _auctioner
) public onlyOwner {
Auction storage NftOnAuction = mapAuction[_nftAddress][_tokenId][
_auctioner
];
require(
NftOnAuction.quantity > 0,
"settle auction: No live auction to settle"
);
if (NftOnAuction.currentBidder != address(0)) {
if (IERC721(_nftAddress).supportsInterface(ERC721InterfaceId)) {
IERC721(_nftAddress).transferFrom(
address(this),
NftOnAuction.currentBidder,
_tokenId
);
} else if (
IERC1155(_nftAddress).supportsInterface(ERC1155InterfaceId)
) {
IERC1155(_nftAddress).safeTransferFrom(
address(this),
NftOnAuction.currentBidder,
_tokenId,
NftOnAuction.quantity,
""
);
}
_payOut(
_nftAddress,
// NftOnAuction.tokenId,
NftOnAuction.bidAmount,
NftOnAuction.erc20Token,
NftOnAuction.auctioner,
false
);
} else {
if (IERC721(_nftAddress).supportsInterface(ERC721InterfaceId)) {
IERC721(_nftAddress).transferFrom(
address(this),
NftOnAuction.auctioner,
_tokenId
);
} else if (
IERC1155(_nftAddress).supportsInterface(ERC1155InterfaceId)
) {
IERC1155(_nftAddress).safeTransferFrom(
address(this),
NftOnAuction.auctioner,
_tokenId,
NftOnAuction.quantity,
""
);
}
}
emit AuctionSettled(
NftOnAuction.tokenId,
_nftAddress,
_auctioner,
NftOnAuction.currentBidder,
NftOnAuction.erc20Token,
NftOnAuction.quantity,
NftOnAuction.bidAmount
);
delete mapAuction[_nftAddress][_tokenId][_auctioner];
}
/*
* @notice this function is used to cancel the auction
* @param _tokenId
* @param _nftAddress
* @param _auctioner
*/
function cancelAuction(
uint256 _tokenId,
address _nftAddress,
address _auctioner
) external {
Auction storage NftOnAuction = mapAuction[_nftAddress][_tokenId][
_auctioner
];
require(
_isAdmin() || msg.sender == NftOnAuction.auctioner,
"CancelAuction: Admin or Auctioner can cancel auction"
);
require(
NftOnAuction.tokenId == _tokenId,
"Cancel Auction : Token id is not on auction"
);
require(
NftOnAuction.quantity > 0,
"Cancel Auction : Token id is not on auction"
);
// Return bid if there is any
if (NftOnAuction.currentBidder != address(0)) {
if (NftOnAuction.erc20Token == address(0)) {
payable(NftOnAuction.currentBidder).transfer(
NftOnAuction.bidAmount
);
} else {
IERC20(NftOnAuction.erc20Token).safeTransfer(
NftOnAuction.currentBidder,
NftOnAuction.bidAmount
);
}
}
// Transfer the escrowed tokens back to the seller
if (IERC721(_nftAddress).supportsInterface(ERC721InterfaceId)) {
IERC721(_nftAddress).transferFrom(
address(this),
NftOnAuction.auctioner,
_tokenId
);
} else if (
IERC1155(_nftAddress).supportsInterface(ERC1155InterfaceId)
) {
IERC1155(_nftAddress).safeTransferFrom(
address(this),
NftOnAuction.auctioner,
_tokenId,
NftOnAuction.quantity,
""
);
}
emit AuctionCancelled(
_tokenId,
_nftAddress,
_auctioner,
NftOnAuction.quantity,
NftOnAuction.erc20Token,
NftOnAuction.bidAmount,
NftOnAuction.currentBidder
);
delete mapAuction[_nftAddress][_tokenId][_auctioner];
}
/*
@notice This onERC721Received function is used to decode the sale
and auction data and used to start the auction and sale
when hit the safetransferfrom function from Erc721 Contracts
@param _from token sender address
@param _to token reciver address
@param _tokenId nfts id
@param _data bytes data from safetransfer from function
*/
function onERC721Received(
address from,
address to,
uint256 tokenId,
bytes memory data
) public virtual override returns (bytes4) {
(
uint256 price,
uint256 basePrice,
address erc20Token,
uint256 saleType
) = abi.decode(data, (uint256, uint256, address, uint256));
// The transfer function call will always be triggered from the token contract
address nftAddress = msg.sender;
if (saleType == 1) {
_listNft(tokenId, price, 1, erc20Token, from, nftAddress);
} else if (saleType == 2) {
_createAuction(
Auction(
tokenId,
basePrice,
price,
erc20Token,
1,
from,
address(0),
0
),
nftAddress
);
} else {
require(
saleType == TYPE_SALE || saleType == TYPE_AUCTION,
"Invalid Type"
);
}
return (this.onERC721Received.selector);
}
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
) external returns (bytes4) {
(
uint256 price,
uint256 basePrice,
address erc20Token,
uint256 saleType
) = abi.decode(data, (uint256, uint256, address, uint256));
// The transfer function call will always be triggered from the token contract
address nftAddress = msg.sender;
if (saleType == 1) {
_listNft(id, price, value, erc20Token, from, nftAddress);
} else if (saleType == 2) {
_createAuction(
Auction(
id,
basePrice,
price,
erc20Token,
value,
from,
address(0),
0
),
nftAddress
);
} else {
revert("Sale Type Is Invalid");
}
return (this.onERC1155Received.selector);
}
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
) public returns (bytes4) {
revert("NFTMarketplace: Dosn't support batch listing at the moment");
}
function supportsInterface(bytes4 interfaceId)
public
view
override(AccessControlEnumerable, IERC165)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
function _isAdmin() internal view returns (bool) {
return (hasRole(ADMIN_ROLE, _msgSender()) ||
hasRole(DEFAULT_ADMIN_ROLE, _msgSender()));
}
function _setPlatformFee(Fee memory _platformFee) internal {
require(
_platformFee.percentageValue <= 5000,
"Fee: max allowed perecentage is 50"
);
platformFee = _platformFee;
}
function _fundTransfer(
uint256 _price,
uint256 _royaltyValue,
address _tokenErc20,
bool _isDirectPurchase,
address _seller,
address _royaltyReceiver
) internal {
uint256 sellerProfit;
uint256 platformProfit;
uint256 royaltyAmount;
if (platformFee.percentageValue > 0) {
platformProfit = _price.mul(platformFee.percentageValue).div(10000);
}
if (_royaltyReceiver != address(0) && _royaltyValue > 0) {
royaltyAmount = _price.mul(_royaltyValue).div(10000);
}
sellerProfit = _price.sub(platformProfit.add(royaltyAmount));
if (_tokenErc20 == address(0)) {
if (_royaltyReceiver != address(0) && royaltyAmount > 0) {
(bool isRoyaltyTransferSuccess, ) = payable(_royaltyReceiver)
.call{value: royaltyAmount}("");
require(
isRoyaltyTransferSuccess,
"Fund Transfer: Transfer to royalty receiver failed."
);
}
if (platformFee.receiver != address(0) && platformProfit > 0) {
(bool isPlatformFeeTransferSuccess, ) = payable(
platformFee.receiver
).call{value: platformProfit}("");
require(
isPlatformFeeTransferSuccess,
"Fund Transfer: Transfer to platform fee receiver failed."
);
}
(bool isSellerTransferSuccess, ) = payable(_seller).call{
value: sellerProfit
}("");
require(
isSellerTransferSuccess,
"Fund Transfer: Transfer to seller failed."
);
} else {
if (_isDirectPurchase) {
if (_royaltyReceiver != address(0) && royaltyAmount > 0) {
IERC20(_tokenErc20).safeTransferFrom(
msg.sender,
_royaltyReceiver,
royaltyAmount
);
}
if (platformFee.receiver != address(0) && platformProfit > 0) {
IERC20(_tokenErc20).safeTransferFrom(
msg.sender,
platformFee.receiver,
platformProfit
);
}
IERC20(_tokenErc20).safeTransferFrom(
msg.sender,
_seller,
sellerProfit
);
} else {
if (_royaltyReceiver != address(0) && royaltyAmount > 0) {
IERC20(_tokenErc20).safeTransfer(
_royaltyReceiver,
royaltyAmount
);
}
if (platformFee.receiver != address(0) && platformProfit > 0) {
IERC20(_tokenErc20).safeTransfer(
platformFee.receiver,
platformProfit
);
}
IERC20(_tokenErc20).safeTransfer(_seller, sellerProfit);
}
}
emit FundTransfer(
Fee(_royaltyReceiver, royaltyAmount),
Fee(_seller, sellerProfit),
Fee(platformFee.receiver, platformProfit)
);
}
/*
@notice This function will fetch the details of the assocciated
Royalty informaion of an NFT
@param _contract token contract address
@param _tokenId token ID
@param _amount total amount paymable
*/
function _getRoyaltyInfo(
address _contract,
uint256 _tokenId,
uint256 _amount
) internal view returns (address user, uint256 royaltyAmount) {
if (IERC2981(_contract).supportsInterface(royaltyInterfaceId)) {
(user, royaltyAmount) = IERC2981(_contract).royaltyInfo(
_tokenId,
_amount
);
}
}
function _payOut(
address _nftAddress,
// uint256 _tokenId,
uint256 _totalAmount,
address _currency,
address _seller,
bool _isDirectPurchase
) internal {
address royaltyReceiver = creatorRoyalties[_nftAddress].receiver;
uint256 royaltyValue = creatorRoyalties[_nftAddress].percentageValue;
_fundTransfer(
_totalAmount,
royaltyValue,
_currency,
_isDirectPurchase,
_seller,
royaltyReceiver
);
}
// handle fixed price sale with direct payment
function _NftSaleFixedPrice(
uint256 _tokenId,
uint256 _quantity,
address _nftAddress,
address _sellerAddress
) internal {
Sale storage NftForSale = mapSale[_nftAddress][_tokenId][
_sellerAddress
];
require(
NftForSale.quantity > 0,
"Buy Nfts:No NFT is availabe for purchase"
);
require(
msg.sender != NftForSale.seller,
"Buy Nfts: Owner Is Not Allowed To Buy Nfts"
);
require(