-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarketplace.sol
479 lines (451 loc) · 16.3 KB
/
Marketplace.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
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
error PriceNotMet(address nftAddress, uint256 tokenId, uint256 price);
error NotListed(address nftAddress, uint256 tokenId);
error AlreadyListed(address nftAddress, uint256 tokenId);
error NotOwner();
error NotApprovedForMarketplace();
error PriceMustBeAboveZero();
contract Marketplace is ReentrancyGuard {
//Fixed Listing structure and Mapping
struct Listing {
uint256 price;
address seller;
}
mapping(address => mapping(uint256 => Listing)) private f_listings;
//English Aucton Structure and Mapping
struct EngListing {
uint256 basePrice;
address seller;
uint256 startAt;
uint256 endAt;
}
mapping(address => mapping(uint256 => EngListing)) private e_listings;
//Dutch Auction Structure and Mapping
struct DutchListing {
uint256 startPrice;
uint256 endPrice;
uint256 discountRate;
address seller;
uint256 startAt;
uint256 endAt;
uint256 duration;
}
mapping(address => mapping(uint256 => DutchListing)) private d_listings;
//CANCELLED
mapping(address => mapping(uint256 => bool)) private CancelledEngAuction;
mapping(address => mapping(uint256 => bool)) private CancelledFixedPriceMarket;
mapping(address => mapping(uint256 => bool)) private CancelledDutchAuction;
//ENGLISH AUCTION VARIABLES
struct Bidding {
address[] previousBidder;
uint256[] previousBid;
address highestBidder;
uint256 highestBid;
}
mapping(address => mapping(uint256 => Bidding)) private bidding;
//EVENTS
//Fixed Listing Event
event f_ItemListed(
address indexed seller,
address indexed nftAddress,
uint256 indexed tokenId,
uint256 price
);
event f_ItemDeleted(
address indexed seller,
address indexed nftAddress,
uint256 indexed tokenId
);
event f_ItemBought(
address indexed buyer,
address indexed nftAddress,
uint256 indexed tokenId,
uint256 price
);
//English Listing Event
event EngItemListed(
address indexed seller,
address indexed nftAddress,
uint256 indexed nftId,
uint256 basePrice,
uint256 startAt,
uint256 endAt
);
event EngItemDeleted(
address indexed seller,
address indexed nftAddress,
uint256 indexed tokenId
);
event Bid(
address indexed nftAddress,
uint256 indexed nftId,
address indexed highestBidder,
uint256 highestBid
);
event EndEngAuction(
address indexed nftAddress,
uint256 indexed nftId,
address indexed highestBidder,
uint256 highestBid
);
//Dutch Listing Event
event DutchItemListed(
address indexed seller,
address indexed nftAddress,
uint256 indexed nftId,
uint256 startPrice,
uint256 endPrice,
uint256 discountRate,
uint256 startAt,
uint256 endAt,
uint256 duration
);
event d_ItemDeleted(
address indexed seller,
address indexed nftAddress,
uint256 indexed nftId
);
event d_ItemBought(
address indexed buyer,
address indexed nftAddress,
uint256 indexed tokenId,
uint256 price
);
//Fixed Listing modifier
modifier f_notListed(
address nftAddress,
uint256 nftId,
address owner
) {
// Listing memory listing = f_listings[nftAddress][tokenId];
if (f_listings[nftAddress][nftId].price > 0) {
revert AlreadyListed(nftAddress, nftId);
}
_;
}
modifier f_isOwner(
address nftAddress,
uint256 tokenId,
address spender
) {
// IERC721 nft = IERC721(nftAddress);
address owner = IERC721(nftAddress).ownerOf(tokenId);
if (spender != owner) {
revert NotOwner();
}
_;
}
modifier f_isListed(address nftAddress, uint256 tokenId) {
Listing memory listing = f_listings[nftAddress][tokenId];
if (listing.price <= 0) {
revert NotListed(nftAddress, tokenId);
}
_;
}
//English Listing Modifiers
modifier e_notListed(
address nftAddress,
uint256 nftId,
address owner
) {
if(e_listings[nftAddress][nftId].basePrice > 0) {
revert AlreadyListed(nftAddress, nftId);
}
_;
}
modifier e_isOwner(
address nftAddress,
uint256 nftId,
address spender
) {
address owner = IERC721(nftAddress).ownerOf(nftId);
if (spender != owner) {
revert NotOwner();
}
_;
}
modifier e_isListed(address _nftAddress, uint256 _nftId) {
EngListing memory e_listing = e_listings[_nftAddress][_nftId];
if (e_listing.basePrice <= 0) {
revert NotListed(_nftAddress, _nftId);
}
_;
}
//Dutch Listing Modifiers
modifier d_notListed(
address nftAddress,
uint256 nftId,
address owner
) {
if (d_listings[nftAddress][nftId].startPrice > 0) {
revert AlreadyListed(nftAddress, nftId);
}
_;
}
modifier d_isOwner(
address nftAddress,
uint256 nftId,
address spender
) {
address owner = IERC721(nftAddress).ownerOf(nftId);
if (spender != owner) {
revert NotOwner();
}
_;
}
modifier d_isListed(address _nftAddress, uint256 _nftId) {
DutchListing memory d_listing = d_listings[_nftAddress][_nftId];
if (d_listing.startPrice <= 0 && d_listing.endPrice <= 0) {
revert NotListed(_nftAddress, _nftId);
}
_;
}
//ADD LISTING at FIXED PRICE
function addItem(
address _nftAddress,
uint256 _nftId,
uint256 _price
)
external
f_notListed(_nftAddress, _nftId, msg.sender)
f_isOwner(_nftAddress, _nftId, msg.sender)
{
if (_price <= 0) {
revert PriceMustBeAboveZero();
}
IERC721 nft = IERC721(_nftAddress);
if (nft.getApproved(_nftId) != address(this)) {
revert NotApprovedForMarketplace();
}
f_listings[_nftAddress][_nftId] = Listing(_price, msg.sender);
emit f_ItemListed(msg.sender, _nftAddress, _nftId, _price);
}
//ADD LISTING IN ENGLISH AUCTION
function addEngAuction(
address _nftAddress,
uint256 _nftId,
uint256 _startingBid,
uint256 _startAt,
uint256 _endAt
) external
// f_notListed(_nftAddress, _nftId, msg.sender)
e_notListed(_nftAddress, _nftId, msg.sender)
e_isOwner(_nftAddress, _nftId, msg.sender) {
if (_startingBid <= 0) {
revert PriceMustBeAboveZero();
}
// IERC721 nft = IERC721(_nftAddress);
if (IERC721(_nftAddress).getApproved(_nftId) != address(this)) {
revert NotApprovedForMarketplace();
}
e_listings[_nftAddress][_nftId] = EngListing(_startingBid, msg.sender, _startAt, _endAt);
emit EngItemListed(
msg.sender,
_nftAddress,
_nftId,
_startingBid,
_startAt,
_endAt
);
}
//ADD LISTING IN DUTCH AUCTION
function addDutchAuction(
address _nftAddress,
uint256 _nftId,
uint256 _startPrice,
uint256 _endPrice,
uint256 _startAt,
uint256 _endAt
) external
// d_notListed(_nftAddress, _nftId, msg.sender)
d_isOwner(_nftAddress, _nftId, msg.sender) {
if (_startPrice <= 0 && _endPrice <= 0) {
revert PriceMustBeAboveZero();
}
if (IERC721(_nftAddress).getApproved(_nftId) != address(this)) {
revert NotApprovedForMarketplace();
}
d_listings[_nftAddress][_nftId] = DutchListing(_startPrice, _endPrice, (_startPrice - _endPrice)/(_endAt - _startAt), msg.sender, _startAt, _endAt, (_endAt - _startAt));
emit DutchItemListed(
msg.sender,
_nftAddress,
_nftId,
_startPrice,
_endPrice,
((_startPrice-_endPrice)/(_endAt-_startAt)),
_startAt,
_endAt,
(_endAt-_startAt)
);
}
//CANCEL FIXED LISTING
function delListing(address _nftAddress, uint256 _nftId)
external
f_isOwner(_nftAddress, _nftId, msg.sender)
f_isListed(_nftAddress, _nftId)
{
delete (f_listings[_nftAddress][_nftId]);
CancelledFixedPriceMarket[_nftAddress][_nftId] = true;
emit f_ItemDeleted(msg.sender, _nftAddress, _nftId);
}
// CANCEL ENGLISH LISTING
function delEngListing(address _nftAddress, uint256 _nftId)
external
e_isOwner(_nftAddress, _nftId, msg.sender)
e_isListed(_nftAddress, _nftId)
{
// require(bidding[_nftAddress][_nftId].highestBidder != address(0),"there is already a bidder in auction");
delete (e_listings[_nftAddress][_nftId]);
CancelledEngAuction[_nftAddress][_nftId] = true;
emit EngItemDeleted(msg.sender, _nftAddress, _nftId);
}
//CANCEL DUTCH AUCTION
function delDutchListing(address _nftAddress, uint256 _nftId)
external
d_isOwner(_nftAddress, _nftId, msg.sender)
d_isListed(_nftAddress, _nftId)
{
delete (d_listings[_nftAddress][_nftId]);
CancelledDutchAuction[_nftAddress][_nftId] = true;
emit d_ItemDeleted(msg.sender, _nftAddress, _nftId);
}
//buy nft at fixed price set by the seller
function buyItemAtFixed(address _nftAddress, uint256 _nftId) external payable nonReentrant f_isListed(_nftAddress, _nftId) {
Listing memory listedItem = f_listings[_nftAddress][_nftId];
if(msg.value < listedItem.price) {
revert PriceNotMet(_nftAddress,_nftId,listedItem.price);
}
IERC721(_nftAddress).safeTransferFrom(listedItem.seller, msg.sender, _nftId);
(bool success, ) = payable(listedItem.seller).call{value: msg.value}("");
require(success, "Transfer Failed!");
emit f_ItemBought(msg.sender, _nftAddress, _nftId, listedItem.price);
delete(f_listings[_nftAddress][_nftId]);
}
// BID
function bidFor(address _nftAddress, uint256 _nftId) external payable e_isListed(_nftAddress, _nftId) {
require(!CancelledEngAuction[_nftAddress][_nftId],"AUCTION CANCELLED");
EngListing memory e_listing = e_listings[_nftAddress][_nftId];
require(e_listing.startAt < block.timestamp && e_listing.endAt >= block.timestamp, "reverted!");
require(
msg.value > e_listing.basePrice,
"value must be greater than basePrice!"
);
if (bidding[_nftAddress][_nftId].highestBidder != address(0)) {
require(
msg.value > bidding[_nftAddress][_nftId].highestBid,
"value is less than highest bid!"
);
bidding[_nftAddress][_nftId].previousBidder.push(
bidding[_nftAddress][_nftId].highestBidder
);
bidding[_nftAddress][_nftId].previousBid.push(
bidding[_nftAddress][_nftId].highestBid
);
bidding[_nftAddress][_nftId].highestBidder = msg.sender;
bidding[_nftAddress][_nftId].highestBid = msg.value;
}
if (bidding[_nftAddress][_nftId].highestBidder == address(0)) {
bidding[_nftAddress][_nftId].highestBidder = msg.sender;
bidding[_nftAddress][_nftId].highestBid = msg.value;
}
emit Bid(_nftAddress, _nftId, msg.sender, msg.value);
}
//END function only called by owner to send nftId to highestBidder, nftAmount to seller and send bid's amount back to participants
function end(address _nftAddress, uint256 _nftId) external e_isOwner(_nftAddress, _nftId, msg.sender) {
// require(
// block.timestamp < e_listings[_nftAddress][_nftId].startAt,
// "Auction has not Started!"
// );
require(
block.timestamp >= e_listings[_nftAddress][_nftId].endAt ||
CancelledEngAuction[_nftAddress][_nftId],
"Please wait till Auction is Expired!"
);
IERC721(_nftAddress).safeTransferFrom(
e_listings[_nftAddress][_nftId].seller,
bidding[_nftAddress][_nftId].highestBidder,
_nftId
);
payable(e_listings[_nftAddress][_nftId].seller).transfer(
bidding[_nftAddress][_nftId].highestBid
);
uint256 transactionCount = 0;
for(uint256 i = 0; i < bidding[_nftAddress][_nftId].previousBidder.length; i++) {
(bool success,) = bidding[_nftAddress][_nftId].previousBidder[i].call{value: bidding[_nftAddress][_nftId].previousBid[i]}("");
require(success,"Transfer Failed");
transactionCount++;
}
emit EndEngAuction(
_nftAddress,
_nftId,
bidding[_nftAddress][_nftId].highestBidder,
bidding[_nftAddress][_nftId].highestBid
);
delete(e_listings[_nftAddress][_nftId]);
}
//price function to get the current price of item in dutch auction
function dutchPrice(address _nftAddress, uint256 _nftId) public view returns (uint256) {
if(block.timestamp >= d_listings[_nftAddress][_nftId].endAt) {
return d_listings[_nftAddress][_nftId].endPrice;
}
uint256 elapsedTime = (block.timestamp - d_listings[_nftAddress][_nftId].startAt)/60;
uint256 discount = elapsedTime * d_listings[_nftAddress][_nftId].discountRate;
uint256 currentPrice = (d_listings[_nftAddress][_nftId].startPrice - discount);
return currentPrice;
}
//buy item at current price
function buyFromDutch(address _nftAddress, uint256 _nftId) external payable {
require(block.timestamp <= d_listings[_nftAddress][_nftId].endAt, "Dutch Auction Expired!");
uint256 currentPrice = dutchPrice(_nftAddress,_nftId);
require(msg.value >= currentPrice, "Eth is less than price");
IERC721(_nftAddress).safeTransferFrom(d_listings[_nftAddress][_nftId].seller, msg.sender, _nftId);
uint256 refund = msg.value - currentPrice;
if(refund > 0) {
(bool refundSent, ) = payable(msg.sender).call{value: refund}("");
require(refundSent, "Refund Transfer Failed!");
//payable(msg.sender).transfer(refund);
}
(bool success, ) = payable(d_listings[_nftAddress][_nftId].seller).call{value: msg.value}("");
require(success, "Transfer Failed!");
//payable(d_listings[_nftAddress][_nftId].seller).transfer(msg.value);
emit d_ItemBought(msg.sender,_nftAddress,_nftId,msg.value);
delete(d_listings[_nftAddress][_nftId]);
}
//Getter functions to reduce gas fees
function isCancelledFixedPriceItem(address _nftAddress, uint256 _nftId) external view f_isListed(_nftAddress, _nftId) returns(bool) {
return CancelledFixedPriceMarket[_nftAddress][ _nftId];
}
function isCancelledEngAuction(address _nftAddress, uint256 _nftId) external view e_isListed(_nftAddress, _nftId) returns(bool) {
return CancelledEngAuction[_nftAddress][_nftId];
}
function isCancelledDutchAuction(address _nftAddress, uint256 _nftId) external view d_isListed(_nftAddress, _nftId) returns(bool) {
return CancelledDutchAuction[_nftAddress][_nftId];
}
function getFixedListing(address _nftAddress, uint256 _nftId) external view returns (Listing memory) {
return f_listings[_nftAddress][_nftId];
}
function getEngAuctionListing(address _nftAddress, uint256 _nftId)
external
view
returns (EngListing memory)
{
return e_listings[_nftAddress][_nftId];
}
function getDutchAuctionListing(address _nftAddress, uint256 _nftId)
external
view
returns (DutchListing memory)
{
return d_listings[_nftAddress][_nftId];
}
function getHighestBid(address _nftAddress, uint256 _nftId)
external
view
returns (Bidding memory)
{
return bidding[_nftAddress][_nftId];
}
}