-
Notifications
You must be signed in to change notification settings - Fork 1
/
MarketBids.sol
638 lines (583 loc) · 25.1 KB
/
MarketBids.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
//*~~~> SPDX-License-Identifier: MIT make it better, stronger, faster
/*~~~>
Thank you Phunks for your inspiration and phriendship.
Never stop phighting, never surrender, always stand up for what is right and make the best of all situations towards all people.
Phunks are phreedom phighters!
"When the power of love overcomes the love of power the world will know peace." - Jimi Hendrix <3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@(((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@(((((((((((((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((((((((((((@@@@@##############################%%%%%@@@@@((((((((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((((((((((((@@@@@##############################%%%%%@@@@@((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((@@@@@########################################%%%%%@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((@@@@@########################################%%%%%@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###############@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###############@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@##########@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((@@PhunkyJON was here programming trustless unstoppable contracts@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////////////////////////////////////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////////////////////////////////////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@#PHUNKYJON///////////////#PHUNKYJON//////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@#PHUNKYJON///////////////#PHUNKYJON//////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////@EYES////////////////////@EYES///////////////@@@@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////@EYES////////////////////@EYES///////////////EAR@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@//////////////////////////////////////////////////EAR@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@//////////////////////////////////////////////////EAR@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////////////////////////////////////////////@@@@@@@@@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////////////////////////////////////////////@@@@@@@@@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@//////////NOSE@NOSE@////////////////////#####@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@//////////NOSE@NOSE@////////////////////#####@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((@@@@@#####//////////////////////////////##########@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((@@@@@#####//////////////////////////////##########@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((@SPLIFF@SPLIFF@SPLIFF@SPLIFF@@###################################@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((@SPLIFF@SPLIFF@SPLIFF@SPLIFF@@###################################@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((EMBER(((((,,,,,,,,,,,,,,,,,,,,,,,,,@@@@@MOUTH&&&&&####################@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((EMBER(((((,,,,,,,,,,,,,,,,,,,,,,,,,@@@@@MOUTH&&&&&####################@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((@SPLIFF@SPLIFF@SPLIFF@SPLIFF@@##############################/////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((@SPLIFF@SPLIFF@SPLIFF@SPLIFF@@##############################/////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((((((((((((@@@@@##############################//////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((((((((((((@@@@@##############################//////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@///////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@///////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((@@@@@///////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((@@@@@///////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@///////////////@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@///////////////@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
<~~~*/
/*~~~>
Thank you Chopper and OGKenobi for your inspiration on this contract and your hard work on NLL!
<~~~*/
pragma solidity 0.8.7;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "./interfaces/IEscrow.sol";
import "./interfaces/INFTMarket.sol";
import "./interfaces/IRoleProvider.sol";
import "./interfaces/IRewardsController.sol";
interface IERC721 {
function ownerOf(uint tokenId) external view returns(address);
function balanceOf(address owner) external view returns(uint);
}
contract MarketBids is ReentrancyGuard {
//*~~~> State variables
uint public bidMin;
uint private bidIds;
uint private blindBidIds;
uint[] private openStorage;
uint[] private blindOpenStorage;
address public roleAdd;
//*~~~> global address variable from Role Provider contract
bytes32 public constant MARKET = keccak256("MARKET");
bytes32 public constant NFT = keccak256("NFT");
bytes32 public constant REWARDS = keccak256("REWARDS");
bytes32 public constant OFFERS = keccak256("OFFERS");
bytes32 public constant TRADES = keccak256("TRADES");
//*~~~> Roles for designated accessibility
bytes32 public constant PROXY_ROLE = keccak256("PROXY_ROLE");
bytes32 public constant DEV = keccak256("DEV");
modifier hasAdmin(){
require(IRoleProvider(roleAdd).hasTheRole(PROXY_ROLE, msg.sender), "DOES NOT HAVE ADMIN ROLE");
_;
}
modifier hasContractAdmin(){
require(IRoleProvider(roleAdd).hasContractRole(msg.sender), "DOES NOT HAVE CONTRACT ROLE");
_;
}
modifier hasDevAdmin(){
require(IRoleProvider(roleAdd).hasTheRole(DEV, msg.sender), "DOES NOT HAVE DEV ROLE");
_;
}
constructor(address role) {
bidMin = 1e15;
roleAdd = role;
}
//*~~~> Declaring object structures for listed items with bids
struct Bid {
uint itemId;
uint tokenId;
uint bidId;
uint bidValue;
uint timestamp;
address payable bidder;
address payable seller;
}
//*~~~> Declaring object structure for blind bids
struct BlindBid {
bool specific;
uint tokenId;
uint bidId;
uint bidValue;
uint amount1155;
address payable collectionBid;
address payable bidder;
}
//*~~~> Memory array of all Bids internal id
mapping (uint256 => Bid) private idToNftBid;
//*~~~> Memory array of Market contract Ids to bid Ids
mapping (uint256 => uint256) private mktIdToBidId;
//*~~~> Memory array of all Blind Bids
mapping (uint256 => BlindBid) private idToBlindBid;
//*~~~> Declaring event object structures for bids
event BidEntered(
uint tokenId,
uint itemId,
uint bidId,
uint bidValue,
address indexed bidder,
address indexed seller
);
//*~~~> Declaring event object structure for blind bids
event BlindBidentered (
bool isSpecified,
uint indexed tokenId,
uint blindBidId,
uint bidValue,
uint amount1155,
address indexed collectionBid,
address indexed bidder
);
//*~~~> Declaring event object structures for Bid accepted
event BlindBidAccepted(
uint indexed tokenId,
uint blindBidId,
uint bidValue,
address indexed bidder,
address indexed seller
);
//*~~~> Declaring event object structures for Bid accepted
event BidAccepted(
uint indexed tokenId,
uint bidId,
uint bidValue,
address indexed bidder,
address indexed seller
);
//*~~~> Declaring event object structures for bids withdrawn
event BidWithdrawn(
uint256 indexed tokenId,
uint indexed bidId,
address indexed bidder
);
//*~~~> Declaring event object structures for blind bids withdrawn
event BlindBidWithdrawn(
uint indexed blindBidId,
address indexed bidder
);
//*~~~> Decalring event object structures for bids refunded
event BidRefunded(
uint indexed tokenId,
uint indexed bidId,
address indexed bidder
);
//~~~> To set the minimum bid price
function setBidMinimum(uint minWei) external hasDevAdmin returns(bool){
bidMin = minWei;
return true;
}
/// @notice
//*~~~> Allows user to enter bids on listed market items
/// @dev
/*~~~>
tokenId: token_id of the NFT to be bid on;
itemId: itemId for internal storage index in the Market Contract;
bidValue: Value of the bid entered;
seller: ownerOf NFT;
<~~~*/
/// @return Bool
function enterBidForNft(
uint[] memory tokenId,
uint[] memory itemId,
uint[] memory bidValue,
address[] memory seller
) external payable returns(bool){
uint total;
for (uint i=0;i < tokenId.length;i++){
total += bidValue[i];
require(bidValue[i] > bidMin, "Must be greater than min. bid.");
/*~~~>
Check for the case where there is a bid.
If the bid entered is lesser than the existing bid, revert.
If the existing bid is lesser than the bid entered,
transfer the existing biddder the existing bidValue of the bid.
<~~~*/
uint id = mktIdToBidId[itemId[i]];
if (id > 0) {
Bid memory existing = idToNftBid[id];
if (bidValue[i] <= existing.bidValue) revert();
if (existing.bidValue < bidValue[i]) {
//*~~~> Refund the failing bid
require(sendEther(existing.bidder, existing.bidValue));
}
}
uint bidId;
uint len = openStorage.length;
if (len>=1){
bidId = openStorage[len-1];
removeId(0);
} else {
bidId = bidIds+=1;
}
idToNftBid[bidId] = Bid(itemId[i], tokenId[i], bidId, bidValue[i], block.timestamp, payable(msg.sender), payable(seller[i]));
emit BidEntered(
tokenId[i],
itemId[i],
bidId,
bidValue[i],
msg.sender,
seller[i]);
}
require(total == msg.value);
return true;
}
/// @notice
//*~~~> External function for entering specific or collection wide blind bids
/// @dev
/*~~~>
isSpecific: Is bid for a specific NFT(true) or collection-wide bid(false?;
value: Bid value;
tokenId: token_id being bid on;
amount: Amount to be bid on if the specific item is an ERC1155;
bidAddress: Address of collection to be bid on;
<~~~*/
/// @return Bool
function enterBlindBid(
bool[] memory isSpecific,
uint[] memory value,
uint[] memory tokenId,
uint[] memory amount1155,
address[] memory bidAddress) external payable nonReentrant returns(bool){
uint total;
for (uint i=0;i<bidAddress.length;i++){
total += value[i];
require(value[i] > bidMin, "Must be greater than min. bid.");
uint bidId;
uint len = blindOpenStorage.length;
if (len>=1){
bidId=blindOpenStorage[len-1];
removeId(1);
} else {
bidId = blindBidIds+=1;
}
idToBlindBid[bidId] = BlindBid(isSpecific[i], tokenId[i], bidId, value[i], amount1155[i], payable(bidAddress[i]), payable(msg.sender));
emit BlindBidentered(
isSpecific[i],
tokenId[i],
bidId,
value[i],
amount1155[i],
bidAddress[i],
msg.sender
);
}
require(msg.value == total);
return true;
}
/// @notice
//*~~~> External function for accpeting specific or collection wide blind bids
/// @dev
/*~~~>
blindBidId: Id of the Bid for reference;
tokenId: token_id being bid on;
listedId: If the item is listed on Market,
and a regular bid is placed on it,
this allows us to update the market listings;
is1155: if 1155(true);
<~~~*/
/// @return Bool
function acceptBlindBid(
uint[] memory blindBidId,
uint[] memory tokenId,
uint[] memory listedId,
bool[] memory is1155) external nonReentrant returns(bool){
address rewardsAdd = IRoleProvider(roleAdd).fetchAddress(REWARDS);
address marketAdd = IRoleProvider(roleAdd).fetchAddress(MARKET);
uint balance = IERC721(marketAdd).balanceOf(msg.sender);
for (uint i=0; i<blindBidId.length; i++){
BlindBid memory bid = idToBlindBid[blindBidId[i]];
//*~~~> Disallow random acceptances if specific
if(bid.specific){
require(tokenId[i]==bid.tokenId,"Wrong item!");
}
if(balance<1){
/*~~~> Calculating the platform fee <~~~*/
uint256 saleFee = calcFee(bid.bidValue);
uint256 userAmnt = (bid.bidValue - saleFee);
/// send saleFee to rewards controller
require(sendEther(rewardsAdd, saleFee));
/// send (bidValue - saleFee) to user
require(sendEther(msg.sender, userAmnt));
} else {
require(sendEther(msg.sender, bid.bidValue));
}
if (!is1155[i]){
//*~~~> Disallow if the msg.sender is not the token owner
require(IERC721(bid.collectionBid).ownerOf(tokenId[i]) == msg.sender, "Not the token owner!");
if(listedId[i]>0){
require(INFTMarket(marketAdd).transferNftForSale(bid.bidder, listedId[i]));
} else {
require(transferFromERC721(bid.collectionBid, tokenId[i], bid.bidder));
}
} else {
uint bal = IERC1155(bid.collectionBid).balanceOf(msg.sender, tokenId[i]);
require( bal> 0, "Not the token owner!");
if(listedId[i]==0){
IERC1155(bid.collectionBid).safeTransferFrom(address(msg.sender), bid.bidder, tokenId[i], bid.amount1155, "");
} else {
require(INFTMarket(marketAdd).transferNftForSale(bid.bidder, listedId[i]));
}
}
blindOpenStorage.push(blindBidId[i]);
idToBlindBid[blindBidId[i]] = BlindBid(false, 0, blindBidId[i], 0, 0, payable(0x0), payable(0x0));
emit BlindBidAccepted(tokenId[i], blindBidId[i], bid.bidValue, bid.bidder, msg.sender);
}
return true;
}
/// @notice
//*~~~> External function for accepting bids
/// @dev
/*~~~>
bidId: Id of the Bid;
<~~~*/
/// @return Bool
function acceptBidForNft(
uint[] memory bidId
) external nonReentrant returns (bool) {
address marketNft = IRoleProvider(roleAdd).fetchAddress(NFT);
address marketAdd = IRoleProvider(roleAdd).fetchAddress(MARKET);
address offersAdd = IRoleProvider(roleAdd).fetchAddress(OFFERS);
address tradesAdd = IRoleProvider(roleAdd).fetchAddress(TRADES);
address rewardsAdd = IRoleProvider(roleAdd).fetchAddress(REWARDS);
uint balance = IERC721(marketNft).balanceOf(msg.sender);
for (uint i=0; i<bidId.length; i++){
Bid memory bid = idToNftBid[bidId[i]];
require(msg.sender == bid.seller);
if(balance<1) {
/*~~~> Calculating the platform fee <~~~*/
uint256 saleFee = calcFee(bid.bidValue);
uint256 userAmnt = (bid.bidValue - saleFee);
/// send saleFee to rewards controller
require(sendEther(rewardsAdd, saleFee));
/// send (bidValue - saleFee) to user
require(sendEther(bid.seller, userAmnt));
} else {
require(sendEther(bid.seller, bid.bidValue));
}
/*~~~> Check for the case where there is a trade and refund it. <~~~*/
uint offerId = IOffers(offersAdd).fetchOfferId(bid.itemId);
if (offerId > 0) {
/*~~~> Kill offer and refund amount <~~~*/
//*~~~> Call the contract to refund the NFT offered for trade
require(IOffers(offersAdd).refundOffer(bid.itemId, offerId));
}
/*~~~> Check for the case where there is an offer and refund it. <~~~*/
uint tradeId = ITrades(tradesAdd).fetchTradeId(bid.itemId);
if (tradeId > 0) {
/*~~~> Kill offer and refund amount <~~~*/
//*~~~> Call the contract to refund the ERC20 offered for trade
require(ITrades(tradesAdd).refundTrade(bid.itemId, tradeId));
}
require(INFTMarket(marketAdd).transferNftForSale(address(bid.bidder), bid.itemId));
openStorage.push(bidId[i]);
idToNftBid[bidId[i]] = Bid(0, 0, 0, 0, 0, payable(address(0x0)), payable(address(0x0)));
emit BidAccepted(bid.itemId, bidId[i], bid.bidValue, bid.bidder, bid.seller);
}
return true;
}
/// @notice
//*~~~> External function to withdraw both blind or regular bids
/// @dev
/*~~~>
bidId: Id of the bid in storage to be withdrawn;
isBlind: if it is a blind blind (true);
<~~~*/
/// @return Bool
function withdrawBid(uint[] memory bidId, bool[] memory isBlind) external nonReentrant returns(bool){
for (uint i=0;i<bidId.length;i++){
if (isBlind[i]){
BlindBid memory bid = idToBlindBid[bidId[i]];
if (bid.bidder != msg.sender) revert();
require(sendEther(bid.bidder, bid.bidValue));
blindOpenStorage.push(bidId[i]);
idToBlindBid[bidId[i]] = BlindBid(false, 0, 0, 0, 0, payable(address(0x0)), payable(address(0x0)));
emit BlindBidWithdrawn(bidId[i], msg.sender);
} else {
Bid memory bid = idToNftBid[bidId[i]];
require(bid.timestamp < block.timestamp - 1 days);
if (bid.bidder != msg.sender) revert();
require(sendEther(bid.bidder, bid.bidValue));
openStorage.push(bidId[i]);
idToNftBid[bidId[i]] = Bid(0, 0, 0, 0, 0, payable(address(0x0)), payable(address(0x0)));
emit BidWithdrawn(bid.tokenId, bidId[i], msg.sender);
}
}
return true;
}
/// @notice
//*~~~> only CONTRACT_ROLE function to refund regular bids if the item is bought
/// @dev
/*~~~>
tokenId: Id of the NFT to be refunded;
bidId: Id for the bid item to return;
<~~~*/
/// @return Bool
function refundBid(uint bidId) external nonReentrant hasContractAdmin returns(bool) {
Bid memory bid = idToNftBid[bidId];
require(sendEther(bid.bidder, bid.bidValue));
openStorage.push(bidId);
emit BidRefunded(bid.tokenId, bidId, msg.sender);
idToNftBid[bidId] = Bid(0, 0, 0, 0, 0, payable(address(0x0)), payable(address(0x0)));
return true;
}
/// @notice
/*~~~>
Internal function to transfer ERC721 NFTs, including crypto kitties/punks
<~~~*/
/// @dev
/*~~~>
assetAddr: address of the token to be transfered;
tokenId: Id of the token to be transfered;
to: to be transfered to;
<~~~*/
function transferFromERC721(address assetAddr, uint256 tokenId, address to) internal virtual returns(bool){
address kitties = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
address punks = 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB;
bytes memory data;
if (assetAddr == kitties) {
//*~~~> Cryptokitties.
data = abi.encodeWithSignature("transferFrom(address,address,uint256)", msg.sender, to, tokenId);
} else if (assetAddr == punks) {
//*~~~> CryptoPunks.
bytes memory punkIndexToAddress = abi.encodeWithSignature("punkIndexToAddress(uint256)", tokenId);
(bool checkSuccess, bytes memory result) = address(assetAddr).staticcall(punkIndexToAddress);
(address nftOwner) = abi.decode(result, (address));
require(checkSuccess && nftOwner == msg.sender, "Not the NFT owner");
data = abi.encodeWithSignature("transferPunk(address,uint256)", msg.sender, tokenId);
} else {
//*~~~> Default.
//*~~~> We push to avoid an unneeded transfer.
data = abi.encodeWithSignature("safeTransferFrom(address,address,uint256)", msg.sender, to, tokenId);
}
(bool success, bytes memory resultData) = address(assetAddr).call(data);
require(success, string(resultData));
return true;
}
/// @notice
/*~~~>
Internal function for sending ether
<~~~*/
/// @return Bool
function sendEther(address recipient, uint ethvalue) internal nonReentrant returns (bool){
(bool success, bytes memory data) = address(recipient).call{value: ethvalue}("");
return(success);
}
/// @notice
/*~~~>
Calculating the platform fee,
Base fee set at 2% (i.e. value * 200 / 10,000)
Future fees can be set by the controlling DAO
<~~~*/
/// @return platform fee
function calcFee(uint256 value) internal returns (uint256) {
address rewardsAdd = IRoleProvider(roleAdd).fetchAddress(REWARDS);
uint fee = IRewardsController(rewardsAdd).getFee();
uint256 percent = ((value * fee) / 10000);
return percent;
}
/// @notice
/*~~~>
Internal function for removing elements from an array
Only used for internal storage array index recycling
In order to reduce storage array size of listed items
while maintaining specific enumerable bidId's,
any sold or removed item spots are re-used by referring to their index,
else a new storage spot is created;
We use the last item in the storage (length of array - 1),
in order to pop off the item and avoid rewriting
<~~~*/
function removeId(uint store) internal {
if (store==0){
openStorage.pop();
} else if (store==1){
blindOpenStorage.pop();
}
}
/// @notice
//*~~~> External read functions
function fetchBidItems() external view returns (Bid[] memory) {
uint bidcount = bidIds;
Bid[] memory bids = new Bid[](bidcount);
for (uint i=0; i < bidcount; i++) {
if (idToNftBid[i + 1].itemId > 0) {
Bid storage currentItem = idToNftBid[i + 1];
bids[i] = currentItem;
}
}
return bids;
}
function fetchBidItemsByBidder(address bidder) external view returns (Bid[] memory) {
uint bidcount = bidIds;
Bid[] memory bids = new Bid[](bidcount);
for (uint i=0; i < bidcount; i++) {
if (idToNftBid[i + 1].bidder == bidder) {
Bid storage currentItem = idToNftBid[i + 1];
bids[i] = currentItem;
}
}
return bids;
}
function fetchBlindBidItems() external view returns (BlindBid[] memory) {
uint bidcount = blindBidIds;
BlindBid[] memory bids = new BlindBid[](bidcount);
for (uint i=0; i < bidcount; i++) {
if (idToBlindBid[i + 1].bidValue > 0) {
BlindBid storage currentItem = idToBlindBid[i + 1];
bids[i] = currentItem;
}
}
return bids;
}
function fetchBlindBidItemsByBidder(address bidder) external view returns (BlindBid[] memory) {
uint bidcount = blindBidIds;
BlindBid[] memory bids = new BlindBid[](bidcount);
for (uint i=0; i < bidcount; i++) {
if (idToBlindBid[i + 1].bidder == bidder) {
BlindBid storage currentItem = idToBlindBid[i + 1];
bids[i] = currentItem;
}
}
return bids;
}
function fetchBlindBidItemById(uint bidId) external view returns (BlindBid memory bid) {
BlindBid memory currentItem = idToBlindBid[bidId];
return currentItem;
}
function fetchBidItemById(uint tokenId) external view returns (Bid memory bid) {
uint bidcount = bidIds;
for (uint i=0; i < bidcount; i++) {
if (idToNftBid[i + 1].tokenId == tokenId) {
Bid memory currentItem = idToNftBid[i + 1];
return currentItem;
}
}
}
function fetchBidId(uint marketId) external view returns (uint id) {
uint _id = mktIdToBidId[marketId];
return _id;
}
///@notice
/*~~~> External ETH transfer forwarded to role provider contract <~~~*/
event FundsForwarded(uint value, address from, address to);
receive() external payable {
require(sendEther(roleAdd, msg.value));
emit FundsForwarded(msg.value, msg.sender, roleAdd);
}
}