-
Notifications
You must be signed in to change notification settings - Fork 10
/
AuctionDemo.sol
151 lines (124 loc) · 6.06 KB
/
AuctionDemo.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
// SPDX-License-Identifier: MIT
/**
*
* @title: Auction Demo Contract
* @date: 26-October-2023
* @version: 1.2
* @author: 6529 team
*/
pragma solidity ^0.8.19;
import "./IMinterContract.sol";
import "./IERC721.sol";
import "./Ownable.sol";
import "./INextGenAdmins.sol";
contract auctionDemo is Ownable {
//events
event ClaimAuction(address indexed _add, uint256 indexed tokenid, bool status, uint256 indexed funds);
event Refund(address indexed _add, uint256 indexed tokenid, bool status, uint256 indexed funds);
event CancelBid(address indexed _add, uint256 indexed tokenid, uint256 index, bool status, uint256 indexed funds);
IMinterContract public minter;
INextGenAdmins public adminsContract;
address gencore;
// certain functions can only be called by auction winner or admin
modifier WinnerOrAdminRequired(uint256 _tokenId, bytes4 _selector) {
require(msg.sender == returnHighestBidder(_tokenId) || adminsContract.retrieveFunctionAdmin(msg.sender, _selector) == true || adminsContract.retrieveGlobalAdmin(msg.sender) == true, "Not allowed");
_;
}
constructor (address _minter, address _gencore, address _adminsContract) public {
minter = IMinterContract(_minter);
gencore = _gencore;
adminsContract = INextGenAdmins(_adminsContract);
}
// auction Bidders
struct auctionInfoStru {
address bidder;
uint256 bid;
bool status;
}
// mapping of collectionSecondaryAddresses struct
mapping (uint256 => auctionInfoStru[]) public auctionInfoData;
// claim auctioned
mapping (uint256 => bool) public auctionClaim;
// participate to auction
function participateToAuction(uint256 _tokenid) public payable {
require(msg.value > returnHighestBid(_tokenid) && block.timestamp <= minter.getAuctionEndTime(_tokenid) && minter.getAuctionStatus(_tokenid) == true);
auctionInfoStru memory newBid = auctionInfoStru(msg.sender, msg.value, true);
auctionInfoData[_tokenid].push(newBid);
}
// get highest bid
function returnHighestBid(uint256 _tokenid) public view returns (uint256) {
uint256 index;
if (auctionInfoData[_tokenid].length > 0) {
uint256 highBid = 0;
for (uint256 i=0; i< auctionInfoData[_tokenid].length; i++) {
if (auctionInfoData[_tokenid][i].bid > highBid && auctionInfoData[_tokenid][i].status == true) {
highBid = auctionInfoData[_tokenid][i].bid;
index = i;
}
}
if (auctionInfoData[_tokenid][index].status == true) {
return highBid;
} else {
return 0;
}
} else {
return 0;
}
}
// get highest bidder
function returnHighestBidder(uint256 _tokenid) public view returns (address) {
uint256 highBid = 0;
uint256 index;
for (uint256 i=0; i< auctionInfoData[_tokenid].length; i++) {
if (auctionInfoData[_tokenid][i].bid > highBid && auctionInfoData[_tokenid][i].status == true) {
index = i;
}
}
if (auctionInfoData[_tokenid][index].status == true) {
return auctionInfoData[_tokenid][index].bidder;
} else {
revert("No Active Bidder");
}
}
// claim Token After Auction
function claimAuction(uint256 _tokenid) public WinnerOrAdminRequired(_tokenid,this.claimAuction.selector){
require(block.timestamp >= minter.getAuctionEndTime(_tokenid) && auctionClaim[_tokenid] == false && minter.getAuctionStatus(_tokenid) == true);
auctionClaim[_tokenid] = true;
uint256 highestBid = returnHighestBid(_tokenid);
address ownerOfToken = IERC721(gencore).ownerOf(_tokenid);
address highestBidder = returnHighestBidder(_tokenid);
for (uint256 i=0; i< auctionInfoData[_tokenid].length; i ++) {
if (auctionInfoData[_tokenid][i].bidder == highestBidder && auctionInfoData[_tokenid][i].bid == highestBid && auctionInfoData[_tokenid][i].status == true) {
IERC721(gencore).safeTransferFrom(ownerOfToken, highestBidder, _tokenid);
(bool success, ) = payable(owner()).call{value: highestBid}("");
emit ClaimAuction(owner(), _tokenid, success, highestBid);
} else if (auctionInfoData[_tokenid][i].status == true) {
(bool success, ) = payable(auctionInfoData[_tokenid][i].bidder).call{value: auctionInfoData[_tokenid][i].bid}("");
emit Refund(auctionInfoData[_tokenid][i].bidder, _tokenid, success, highestBid);
} else {}
}
}
// cancel a single Bid
function cancelBid(uint256 _tokenid, uint256 index) public {
require(block.timestamp <= minter.getAuctionEndTime(_tokenid), "Auction ended");
require(auctionInfoData[_tokenid][index].bidder == msg.sender && auctionInfoData[_tokenid][index].status == true);
auctionInfoData[_tokenid][index].status = false;
(bool success, ) = payable(auctionInfoData[_tokenid][index].bidder).call{value: auctionInfoData[_tokenid][index].bid}("");
emit CancelBid(msg.sender, _tokenid, index, success, auctionInfoData[_tokenid][index].bid);
}
// cancel All Bids
function cancelAllBids(uint256 _tokenid) public {
require(block.timestamp <= minter.getAuctionEndTime(_tokenid), "Auction ended");
for (uint256 i=0; i<auctionInfoData[_tokenid].length; i++) {
if (auctionInfoData[_tokenid][i].bidder == msg.sender && auctionInfoData[_tokenid][i].status == true) {
auctionInfoData[_tokenid][i].status = false;
(bool success, ) = payable(auctionInfoData[_tokenid][i].bidder).call{value: auctionInfoData[_tokenid][i].bid}("");
emit CancelBid(msg.sender, _tokenid, i, success, auctionInfoData[_tokenid][i].bid);
} else {}
}
}
// return Bids
function returnBids(uint256 _tokenid) public view returns(auctionInfoStru[] memory) {
return auctionInfoData[_tokenid];
}
}