-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathChickenRunV5.sol
370 lines (318 loc) · 12.3 KB
/
ChickenRunV5.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
//SPDX-License-Identifier: MIT
// contracts/ERC721.sol
// upgradeable contract
pragma solidity >=0.8.0;
import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract ChickenRunV5 is ERC721Upgradeable {
// Royalty
address private _owner;
address private _royaltiesAddr; // royality receiver
uint256 public royaltyPercentage; // royalty based on sales price
mapping(address => bool) public excludedList; // list of people who dont have to pay fee
//GB HOLDERS EARLY ACCESS TOKENS
uint256 public constant gbHoldersMaxMint = 900;
mapping(address => bool) public gbholders;
address public GB_erc20_contract;
uint256[] private _gbTokenSupply;
// cost to mint
uint256 public mintFeeAmount;
// // NFT Meta data
string public baseURL;
uint256 public constant maxSupply = 10000;
// enable flag for public and GB
bool public openForGB;
bool public openForPublic;
// define ChickenRun struct
struct ChickenRun {
uint256 tokenId;
// string tokenURI;
address mintedBy;
address currentOwner;
uint256 previousPrice;
uint256 price;
uint256 numberOfTransfers;
bool forSale;
uint256 kg;
}
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
// map id to ChickenRun obj
mapping(uint256 => ChickenRun) public allChickenRun;
// implement the IERC721Enumerable which no longer come by default in openzeppelin 4.x
// Mapping from owner to list of owned token IDs
mapping(address => mapping(uint256 => uint256)) private _ownedTokens;
// Mapping from token ID to index of the owner tokens list
mapping(uint256 => uint256) private _ownedTokensIndex;
// Array with all token ids, used for enumeration
uint256[] private _allTokens;
// Mapping from token id to position in the allTokens array
mapping(uint256 => uint256) private _allTokensIndex;
//================================= Events ================================= //
event GBERC20Checked(address indexed _holder, uint256 _bal, bool _result);
event SaleToggle(uint256 chiknNumber, bool isForSale, uint256 price);
event PurchaseEvent(
uint256 chiknNumber,
address from,
address to,
uint256 price
);
event LevelUp(uint256 chiknNumber, uint256 newKg);
event NameChange(uint256 chiknNumber, string name);
function initialize(
address _contractOwner,
address _royaltyReceiver,
uint256 _royaltyPercentage,
uint256 _mintFeeAmount,
string memory _baseURL,
address _gbContractAddress,
bool _openForGB,
bool _openForPublic
) public initializer {
// __ERC721_init("swng", "swng");
__ERC721_init("chikn", "chikn");
royaltyPercentage = _royaltyPercentage;
_owner = _contractOwner;
_royaltiesAddr = _royaltyReceiver;
mintFeeAmount = _mintFeeAmount;
excludedList[_contractOwner] = true; // add owner to exclude list
excludedList[_royaltyReceiver] = true; // add artist to exclude list
baseURL = _baseURL;
GB_erc20_contract = _gbContractAddress;
openForGB = _openForGB;
openForPublic = _openForPublic;
}
function mint(uint256 numberOfToken) public payable {
// check if thic fucntion caller is not an zero address account
require(openForPublic == true, "not open");
require(msg.sender != address(0));
require(_allTokens.length + numberOfToken <= maxSupply, "max supply");
require(numberOfToken > 0, "Min 1");
require(numberOfToken <= 12, "Max 12");
uint256 price = 0;
// pay for minting cost
if (excludedList[msg.sender] == false) {
// send token's worth of ethers to the owner
price = mintFeeAmount * numberOfToken;
require(msg.value == price, "Not enough fee");
payable(_royaltiesAddr).transfer(msg.value);
} else {
// return money to sender // since its free
payable(msg.sender).transfer(msg.value);
}
for (uint256 i = 1; i <= numberOfToken; i++) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_safeMint(msg.sender, newItemId);
ChickenRun memory newChickenRun = ChickenRun(
newItemId,
msg.sender,
msg.sender,
0,
price,
0,
false,
100
);
// add the token id to the allChickenRun
allChickenRun[newItemId] = newChickenRun;
}
}
function changeUrl(string memory url) external {
require(msg.sender == _owner, "Only owner");
baseURL = url;
}
function totalSupply() public view returns (uint256) {
return _allTokens.length;
}
// GB holder related functions
function gbTotalSupply() public view returns (uint256) {
return _gbTokenSupply.length;
}
function setPriceForSale(
uint256 _tokenId,
uint256 _newPrice,
bool isForSale
) external {
require(_exists(_tokenId), "token not found");
address tokenOwner = ownerOf(_tokenId);
require(tokenOwner == msg.sender, "not owner");
ChickenRun memory chick = allChickenRun[_tokenId];
chick.price = _newPrice;
chick.forSale = isForSale;
allChickenRun[_tokenId] = chick;
emit SaleToggle(_tokenId, isForSale, _newPrice);
}
function getAllSaleTokens() public view returns (uint256[] memory) {
uint256 _totalSupply = totalSupply();
uint256[] memory _tokenForSales = new uint256[](_totalSupply);
uint256 counter = 0;
for (uint256 i = 1; i <= _totalSupply; i++) {
if (allChickenRun[i].forSale == true) {
_tokenForSales[counter] = allChickenRun[i].tokenId;
counter++;
}
}
return _tokenForSales;
}
// by a token by passing in the token's id
function buyToken(uint256 _tokenId) public payable {
// check if the token id of the token being bought exists or not
require(_exists(_tokenId));
// get the token's owner
address tokenOwner = ownerOf(_tokenId);
// token's owner should not be an zero address account
require(tokenOwner != address(0));
// the one who wants to buy the token should not be the token's owner
require(tokenOwner != msg.sender);
// get that token from all ChickenRun mapping and create a memory of it defined as (struct => ChickenRun)
ChickenRun memory chick = allChickenRun[_tokenId];
// price sent in to buy should be equal to or more than the token's price
require(msg.value >= chick.price);
// token should be for sale
require(chick.forSale);
uint256 amount = msg.value;
uint256 _royaltiesAmount = (amount * royaltyPercentage) / 100;
uint256 payOwnerAmount = amount - _royaltiesAmount;
payable(_royaltiesAddr).transfer(_royaltiesAmount);
payable(chick.currentOwner).transfer(payOwnerAmount);
chick.previousPrice = chick.price;
allChickenRun[_tokenId] = chick;
_transfer(tokenOwner, msg.sender, _tokenId);
emit PurchaseEvent(
_tokenId,
chick.currentOwner,
msg.sender,
chick.price
);
}
function tokenOfOwnerByIndex(address owner, uint256 index)
public
view
returns (uint256)
{
require(index < balanceOf(owner), "out of bounds");
return _ownedTokens[owner][index];
}
// URI Storage override functions
/** Overrides ERC-721's _baseURI function */
function _baseURI()
internal
view
virtual
override(ERC721Upgradeable)
returns (string memory)
{
return baseURL;
}
function _burn(uint256 tokenId) internal override(ERC721Upgradeable) {
super._burn(tokenId);
}
function tokenURI(uint256 tokenId)
public
view
override(ERC721Upgradeable)
returns (string memory)
{
return super.tokenURI(tokenId);
}
/**
* @dev Hook that is called before any token transfer. This includes minting
* and burning.
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override(ERC721Upgradeable) {
super._beforeTokenTransfer(from, to, tokenId);
ChickenRun memory chick = allChickenRun[tokenId];
chick.currentOwner = to;
chick.numberOfTransfers += 1;
chick.forSale = false;
allChickenRun[tokenId] = chick;
if (from == address(0)) {
_addTokenToAllTokensEnumeration(tokenId);
} else if (from != to) {
_removeTokenFromOwnerEnumeration(from, tokenId);
}
if (to == address(0)) {
_removeTokenFromAllTokensEnumeration(tokenId);
} else if (to != from) {
_addTokenToOwnerEnumeration(to, tokenId);
}
}
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
uint256 length = balanceOf(to);
_ownedTokens[to][length] = tokenId;
_ownedTokensIndex[tokenId] = length;
}
function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
_allTokensIndex[tokenId] = _allTokens.length;
_allTokens.push(tokenId);
}
function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId)
private
{
uint256 lastTokenIndex = balanceOf(from) - 1;
uint256 tokenIndex = _ownedTokensIndex[tokenId];
// When the token to delete is the last token, the swap operation is unnecessary
if (tokenIndex != lastTokenIndex) {
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
_ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
_ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
}
// This also deletes the contents at the last position of the array
delete _ownedTokensIndex[tokenId];
delete _ownedTokens[from][lastTokenIndex];
}
function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
uint256 lastTokenIndex = _allTokens.length - 1;
uint256 tokenIndex = _allTokensIndex[tokenId];
uint256 lastTokenId = _allTokens[lastTokenIndex];
_allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
_allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
// This also deletes the contents at the last position of the array
delete _allTokensIndex[tokenId];
_allTokens.pop();
}
// upgrade contract to support authorized
mapping(address => bool) public authorized;
modifier onlyAuthorized() {
require(
authorized[msg.sender] || msg.sender == _owner,
"Not authorized"
);
_;
}
function addAuthorized(address _toAdd) public {
require(msg.sender == _owner, "Not owner");
require(_toAdd != address(0));
authorized[_toAdd] = true;
}
function removeAuthorized(address _toRemove) public {
require(msg.sender == _owner, "Not owner");
require(_toRemove != address(0));
require(_toRemove != msg.sender);
authorized[_toRemove] = false;
}
// upgrade the contract
function setKg(uint256 _tokenId, uint256 _newKg) external onlyAuthorized {
ChickenRun memory chick = allChickenRun[_tokenId];
chick.kg = _newKg;
// set and update that token in the mapping
allChickenRun[_tokenId] = chick;
emit LevelUp(_tokenId, _newKg);
}
// add chikn naming service
mapping(uint256 => string) public cns;
function changeName(uint256 _tokenId, string memory name)
external
onlyAuthorized
{
cns[_tokenId] = name;
emit NameChange(_tokenId, name);
}
}