-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreasureHuntNFT.sol
364 lines (298 loc) · 11 KB
/
TreasureHuntNFT.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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9 <0.9.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Royalty.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import 'abdk-libraries-solidity/ABDKMathQuad.sol';
/**
* @title TreasureHunt
* @author csurbier
* @dev
*/
contract TreasureHuntNFT is ERC721Royalty,Ownable, ReentrancyGuard {
using Strings for uint256;
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
uint256 private cost = 0.01 ether ; //
bool public privateSale = false;
bool public publicSale = false;
bool public revealed = false ;
uint256 public maxSupply = 1023 ; // 1024 NFTs available (starting at 0)
uint256 public publicSaleStartDate ;
uint256 public unlockDate ;
string private baseURI = '';
string private hiddenMetadataUri = 'https://bafybeicej5f4a6h6h62a53ekkikzrzvgu4dleraxybynnrkyyptrglp4mm.ipfs.w3s.link/waiting_json/';
bytes32 private hashedSecretCode ;
bytes32 private hashedSecretWords;
address payable TreasureHuntCreatorAddress;
address payable public WinnerAddress;
mapping(address => bool) public whitelist;
struct NftItem {
uint256 tokenId;
address payable owner;
}
mapping(uint256 => NftItem) private ethNFTS;
event NewMint(address buyer, uint256 tokenId);
event AddedToWhitelist(address user);
constructor() ERC721("ETH TreasureHunt NFT Game", "ETHTreasure") {
_setDefaultRoyalty(payable(msg.sender),500); //5% of royalties
TreasureHuntCreatorAddress = payable(msg.sender);
}
// checkCode : check NFT revealed , code, and if ok
function checkCode(string memory _code) public view returns (bool) {
if (revealed){
//check own nft
if (this.balanceOf(msg.sender) > 0) {
bytes32 hashedCode = sha256(bytes(_code));
if (hashedCode == hashedSecretCode){
return true;
}
}
}
return false;
}
// check secretKey : check Contrat NFT revealed , check own nft, check unlock date and check secret
function checkWin(string memory _secret) public {
require(revealed,"NFT not yet revealed");
require(WinnerAddress==address(0),"Already a winner");
require(this.balanceOf(msg.sender) > 0,"Dont have an nft");
require(block.timestamp >= unlockDate,"Treasure not yet unlock");
bytes32 hashedCode = sha256(bytes(_secret));
if (hashedCode == hashedSecretWords){
WinnerAddress = payable(msg.sender);
//Send money to winner
WinnerAddress.transfer(address(this).balance);
}
}
function setMaxSupply(uint256 _maxSupply) public onlyOwner {
maxSupply = _maxSupply;
}
function setBaseUri(string memory _baseurl) public onlyOwner {
baseURI = _baseurl;
}
function setHashes(bytes32 _cc, bytes32 _wd) public onlyOwner {
hashedSecretCode = _cc;
hashedSecretWords = _wd;
}
modifier mintCompliance(uint256 _mintAmount) {
require(_mintAmount>0,"1 mint minimum");
require(
_tokenIds.current() + _mintAmount <= maxSupply,
"Max supply exceeded!"
);
_;
}
modifier mintPriceCompliance(uint256 _mintAmount) {
require(msg.value >= (cost * _mintAmount), "Not paid enough !");
_;
}
function addWhitelist(address _newEntry) public {
require(!privateSale, "Private sale started");
whitelist[_newEntry] = true;
emit AddedToWhitelist(_newEntry);
}
function mintNFT(address _to, uint256 _mintAmount) internal{
for (uint256 i = 0; i < _mintAmount; i++) {
uint256 tokenId = _tokenIds.current();
_safeMint(_to, tokenId);
ethNFTS[tokenId] = NftItem(
tokenId,
payable(msg.sender)
);
_tokenIds.increment();
emit NewMint(msg.sender, tokenId);
}
}
function mint(uint256 _mintAmount)
public
payable
mintCompliance(_mintAmount)
mintPriceCompliance(_mintAmount)
{
require(privateSale || publicSale,"Private or Public sale not started");
// Si private mint , whitelist required
if (privateSale){
// check si public sale ouvert
if (publicSale==false){
// NON => check date et ouvre si besoin
if(block.timestamp >= publicSaleStartDate) {
publicSale = true;
privateSale = false;
}
else{
// si pas ouvert check dans whitelist
require( whitelist[msg.sender],"Not in whitlist for minting");
}
}
}
mintNFT(_msgSender(), _mintAmount);
// Send 31% to TreasureHuntCreatorAddress
uint256 amountPercent = mulDiv(31,msg.value, 100);
payable(TreasureHuntCreatorAddress).transfer(amountPercent);
//70% stays in the contract and is for the winner of the hunt
// check if max supply reached and if so, revealed MAP
uint256 tokenId = _tokenIds.current();
if (tokenId>= maxSupply){
revealed = true;
//Calculer la date de unlock du smart contract (+48H)
unlockDate = block.timestamp+2 days; //
}
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
function getNbTokenMinted() public view returns (uint256) {
return _tokenIds.current();
}
function tokenURI(uint256 _tokenId)
public
view
virtual
override
returns (string memory)
{
require(
_exists(_tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
if (revealed == false) {
string memory currentBaseURI = hiddenMetadataUri;
return
bytes(currentBaseURI).length > 0
? string(
abi.encodePacked(
currentBaseURI,
_tokenId.toString(),
'.json'
)
)
: "";
}
else{
string memory currentBaseURI = _baseURI();
return
bytes(currentBaseURI).length > 0
? string(
abi.encodePacked(
currentBaseURI,
_tokenId.toString(),
'.json'
)
)
: "";
}
}
function setPrivateSale(bool _state) public onlyOwner {
privateSale = _state;
//Calculer la date de public Sales = private Sales + 4 days
publicSaleStartDate = block.timestamp+4 days;
}
function setCost(uint256 _newcost) public onlyOwner {
cost = _newcost;
}
function fetchTokenForOwner(address owner)
public
view
returns (NftItem[] memory)
{
uint256 totalItemCount = _tokenIds.current();
uint256 itemCount = 0;
uint256 currentIndex = 0;
for (uint256 i = 0; i <totalItemCount; i++) {
if (ethNFTS[i].owner == owner) {
itemCount += 1;
}
}
NftItem[] memory items = new NftItem[](itemCount);
for (uint256 i = 0; i < totalItemCount; i++) {
if (ethNFTS[i].owner == owner) {
uint256 currentId = i;
NftItem storage currentItem = ethNFTS[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
function mulDiv (uint x, uint y, uint z)
public pure returns (uint) {
return
ABDKMathQuad.toUInt (
ABDKMathQuad.div (
ABDKMathQuad.mul (
ABDKMathQuad.fromUInt (x),
ABDKMathQuad.fromUInt (y)
),
ABDKMathQuad.fromUInt (z)
)
);
}
function withdrawIfNoWinner() public onlyOwner nonReentrant {
require(revealed,"Not all NFT minted");
require (block.timestamp >= unlockDate,"not yet");
//Pick a random winner
uint256 randomWinner = uint(keccak256(abi.encodePacked(block.timestamp,block.difficulty,msg.sender))) % 1024;
randomWinner = randomWinner-1;
WinnerAddress = ethNFTS[randomWinner].owner;
WinnerAddress.transfer(address(this).balance);
}
function random(uint num) public view returns(uint){
return uint(keccak256(abi.encodePacked(block.timestamp,block.difficulty,msg.sender))) % num;
}
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes memory data
) public virtual override {
//call the original function that you wanted.
super.safeTransferFrom(from, to, tokenId, data);
//update
uint256 totalItemCount = _tokenIds.current();
for (uint256 i = 0; i < totalItemCount; i++) {
if (ethNFTS[i].tokenId == tokenId) {
NftItem storage currentItem = ethNFTS[i];
currentItem.owner = payable(to);
break;
}
}
}
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) public virtual override {
//call the original function that you wanted.
super.safeTransferFrom(from, to, tokenId);
//update
uint256 totalItemCount = _tokenIds.current();
for (uint256 i = 0; i < totalItemCount; i++) {
if (ethNFTS[i].tokenId == tokenId) {
NftItem storage currentItem = ethNFTS[i];
currentItem.owner = payable(to);
break;
}
}
}
function transferFrom(
address from,
address to,
uint256 tokenId
) public virtual override {
//call the original function that you wanted.
super.transferFrom(from, to, tokenId);
//update
uint256 totalItemCount = _tokenIds.current();
for (uint256 i = 0; i < totalItemCount; i++) {
if (ethNFTS[i].tokenId == tokenId) {
NftItem storage currentItem = ethNFTS[i];
currentItem.owner = payable(to);
break;
}
}
}
}