forked from gyan0890/Reward-Points
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRewardPointsAuction.sol
More file actions
197 lines (145 loc) · 6.06 KB
/
RewardPointsAuction.sol
File metadata and controls
197 lines (145 loc) · 6.06 KB
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
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/AccessControl.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/AccessControlEnumerable.sol";
contract NFTBidding {
using Counters for Counters.Counter;
Counters.Counter private _bids;
enum NFTState {
ONBID,
FREE
}
mapping(uint256 => NFTState) tokenState;
mapping(uint256 => address) tokenBids;
event nftOnBid(address, uint256, address);
event bidLog(address, uint256, uint256, address);
function putOnBid(address nftAddress, uint256 tokenId) public returns(bool){
ERC721 nftContract = ERC721(nftAddress);
require(msg.sender == nftContract.ownerOf(tokenId), "Only the owner of the NFT can call the put on bid function");
Bidding bidding = new Bidding(tokenId, block.timestamp, nftAddress,msg.sender);
tokenBids[tokenId] = address(bidding);
tokenState[tokenId] = NFTState.ONBID;
emit nftOnBid(nftAddress, tokenId, msg.sender);
return true;
}
function closeBidding(uint256 _tokenId) public returns(bool){
tokenState[_tokenId] = NFTState.FREE;
return true;
}
function getBiddingContractAddress(uint256 _tokenId) public view returns(address) {
return tokenBids[_tokenId];
}
function getNFTState(uint256 _tokenId) public view returns(NFTState){
return tokenState[_tokenId];
}
}
/*
* This is the bidding contract
*/
contract Bidding {
// Parameters of the auction. Times are either
// absolute unix timestamps (seconds since 1970-01-01)
// or time periods in seconds.
uint public auctionEnd;
uint public tokenId;
address public parentNftAddress;
uint public tokenURI;
uint bidCounter;
address public selectedBidder;
address public owner;
struct Bid {
address bidder;
address nftAddress;
uint tokenId;
string tokenURI;
uint rewardPoints;
string expiry;
}
// Set to true at the end, disallows any change
bool ended;
// Recording all the bids
mapping(uint => Bid) bids;
event bidLog(address, uint256, uint256, address);
// Events that will be fired on changes.
event AuctionEnded(address winner, address nftAddress);
/*
* Create a simple bidding contract
* @param _tokenId: tokenId for which the bid is created
* @param _biddingTime: Time period for the bidding to be kept open
*/
constructor(
uint256 _tokenId,
uint _biddingTime,
address _nftAddress,
address _owner
) {
tokenId = _tokenId;
bidCounter = 0;
auctionEnd = block.timestamp + _biddingTime;
//Explicitly setting the owner to our address for now
// msg.sender is coming as the address of the contract
parentNftAddress = _nftAddress;
owner = _owner;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
//ONLY FOR TESTING
function getOwner() public view returns(address){
return owner;
}
function bid(address _nftAddress, uint256 _tokenId, string memory _tokenURI, uint256 _rewardPoints, string memory _expiryDate) public {
require(
block.timestamp <= auctionEnd,
"Auction already ended."
);
Bid storage newBid = bids[bidCounter+1];
newBid.bidder = msg.sender;
newBid.nftAddress = _nftAddress;
newBid.tokenId = _tokenId;
newBid.tokenURI = _tokenURI;
newBid.rewardPoints = _rewardPoints;
newBid.expiry = _expiryDate;
ERC721 nftContract = ERC721(_nftAddress);
address nftOwner = nftContract.ownerOf(tokenId);
nftContract.transferFrom(nftOwner, address(this), _tokenId);
emit bidLog( newBid.nftAddress,newBid.tokenId , block.timestamp, msg.sender);
bidCounter = bidCounter+1;
}
/*
* Get the address of the highest bidder
*/
function selectBidderAndClose(address nftAddress) onlyOwner public returns(bool) {
selectedBidder = nftAddress;
for(uint i = 0; i < bidCounter; i ++ ){
if(bids[i].nftAddress != selectedBidder){
ERC721 nftContract = ERC721(bids[i].nftAddress);
address nftOwner = nftContract.ownerOf(bids[i].tokenId);
nftContract.transferFrom(nftOwner, bids[i].bidder, bids[i].tokenId);
}
else if(bids[i].nftAddress == selectedBidder){
ERC721 selectedNftContract = ERC721(bids[i].nftAddress);
ERC721 parentContract = ERC721(parentNftAddress);
address selectedOwner = selectedNftContract.ownerOf(bids[i].tokenId);
address bidOwner = parentContract.ownerOf(tokenId);
selectedNftContract.transferFrom(selectedOwner, bidOwner, bids[i].tokenId);
parentContract.transferFrom(bidOwner, selectedOwner, tokenId);
}
}
return true;
}
// function transferNFT(address nftAddress, uint256 _tokenId) public {
// ERC721 nftContract = ERC721(nftAddress);
// address nftOwner = nftContract.ownerOf(_tokenId);
// nftContract.transferFrom(nftOwner, address(this), _tokenId);
// }
// function transferNFTBack(address nftAddress, uint256 _tokenId, address to) public {
// ERC721 nftContract = ERC721(nftAddress);
// address nftOwner = nftContract.ownerOf(_tokenId);
// nftContract.transferFrom(nftOwner, to, _tokenId);
// }
}