-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMadCrows.sol
311 lines (271 loc) · 9.72 KB
/
MadCrows.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
import "./IERC20.sol";
import "./ERC721.sol";
import "./Ownable.sol";
import "./ERC721Enumerable.sol";
import "./Context.sol";
contract MADCROW is
Context,
Ownable,
ERC721Enumerable
{
//ERRORS
error SaleNotEnded();
error NoBalance();
error NoAllowance();
error SoldOut();
error AlreadyClaimed();
error URIFrozen();
error CrowNotOwned();
error SaleNotStarted();
error NotEnoughCrows();
error ZeroMint();
error TimeLocked();
error BadPrice();
//METADATA
bool public tokenURIFrozen = false;
string private baseTokenURI;
//MINT
uint256 constant public MAX_SUPPLY = 3333;
uint256 public idTracker;
//DUTCH
uint256 public startDate;
uint256 public endDate;
uint256 public startPrice = 100 ether;
uint256 public endPrice = 10 ether;
uint256 constant public SEGMENTS = 24;
uint256 public lastPrice;
bool public soldOut;
//TEAM CUT
bool public rewardsClaimed;
address private constant DEAD = 0x000000000000000000000000000000000000dEaD;
//REFUND
mapping(uint256 => uint256) public purchasePrices;
//AIRDROP
mapping(uint256 => bool) public airdropClaimed;
//CONTRACT
IERC721Enumerable public crocrow;
IERC20 public mad;
event MintEvent(address indexed buyer, uint256 currentSupply, uint256 amount, uint256 time, uint256 price);
constructor(
string memory name,
string memory symbol,
string memory uri,
address _crocrow,
address _mad,
uint256 _startDate
) ERC721(name, symbol) {
baseTokenURI = uri;
startDate = _startDate;
endDate = startDate + (60 * 60 * 2);
crocrow = IERC721Enumerable(_crocrow);
mad = IERC20(_mad);
_safeMint(_msgSender(), 0);
}
function mint(uint256 amount) external {
uint256 price = getCurrentPrice();
uint256 totalPrice = price * amount;
if(amount == 0) revert ZeroMint();
if((idTracker + amount) > MAX_SUPPLY) revert SoldOut();
if(block.timestamp < startDate) revert SaleNotStarted();
if(totalPrice > mad.allowance(_msgSender(),address(this))) revert NoAllowance();
if(mad.balanceOf(_msgSender()) < totalPrice) revert NoBalance();
if(crocrow.balanceOf(_msgSender()) < 2) revert NotEnoughCrows();
for (uint256 i = 1; i <= amount;) {
purchasePrices[idTracker + i] = price;
_safeMint(_msgSender(), idTracker + i);
unchecked{
++i;
}
}
idTracker += amount;
if(idTracker == MAX_SUPPLY){
purchasePrices[0] = price;
lastPrice = price;
soldOut = true;
}
mad.transferFrom(_msgSender(),address(this),totalPrice);
emit MintEvent(_msgSender(),idTracker,amount,block.timestamp,price);
}
//ADMIN CONTROLS
function changeStartPrice(uint256 price) external onlyOwner{
if(block.timestamp >= startDate - (60*60*24) ) revert TimeLocked();
if(price <= endPrice) revert BadPrice();
startPrice = price;
}
function changeEndPrice(uint256 price) external onlyOwner{
if(block.timestamp >= startDate - (60*60*24) ) revert TimeLocked();
if(price >= startPrice) revert BadPrice();
endPrice = price;
}
function changeStartDate(uint256 stamp) public onlyOwner{
if(block.timestamp >= startDate - (60*60*24) ) revert TimeLocked();
startDate = stamp;
endDate = stamp + (60 * 60 * 2);
}
function setBaseTokenURI(string memory uri) public onlyOwner {
if(tokenURIFrozen) revert URIFrozen();
baseTokenURI = uri;
}
function freezeBaseURI() public onlyOwner {
tokenURIFrozen = true;
}
function _baseURI() internal view virtual override returns (string memory) {
return baseTokenURI;
}
function walletOfOwner(address add) external view returns (uint256[] memory) {
uint256 ownerTokenCount = balanceOf(add);
uint256[] memory tokenIds = new uint256[](ownerTokenCount);
for (uint256 i; i < ownerTokenCount; i++) {
tokenIds[i] = tokenOfOwnerByIndex(add, i);
}
return tokenIds;
}
//TEAM FUNDS
function withdraw() public onlyOwner{
if(!soldOut) revert SaleNotEnded();
if(rewardsClaimed) revert AlreadyClaimed();
rewardsClaimed = true;
uint256 teamAmount = lastPrice * MAX_SUPPLY / 4;
mad.transfer(_msgSender(),teamAmount);
uint256 burnAmount = lastPrice * MAX_SUPPLY / 2;
mad.transfer(DEAD,burnAmount);
}
//REFUNDS
function refund(uint256 madCrow) external{
if(!soldOut) revert SaleNotEnded();
if(_msgSender() != ownerOf(madCrow)) revert CrowNotOwned();
uint256 amount = purchasePrices[madCrow] - lastPrice;
if(amount == 0) revert NoBalance();
purchasePrices[madCrow] = lastPrice;
mad.transfer(_msgSender(),amount);
}
function refundAll() public{
if(!soldOut) revert SaleNotEnded();
uint256 ownerTokenCount = balanceOf(_msgSender());
uint256 toSend;
uint256 tokenId;
for (uint256 i; i < ownerTokenCount;) {
unchecked{
tokenId = tokenOfOwnerByIndex(_msgSender(),i);
uint256 amount = purchasePrices[tokenId] - lastPrice;
purchasePrices[tokenId] = lastPrice;
toSend+= amount;
++i;
}
}
if(toSend == 0) revert NoBalance();
mad.transfer(_msgSender(),toSend);
}
function refundCheck(uint256 madCrow) external view returns(uint256){
if(!soldOut) revert SaleNotEnded();
return purchasePrices[madCrow] - lastPrice;
}
function refundMulticheck(address add) external view returns (uint256) {
if(!soldOut) revert SaleNotEnded();
uint256 ownerTokenCount = balanceOf(add);
uint256 toSend;
uint256 tokenId;
for (uint256 i; i < ownerTokenCount;) {
unchecked{
tokenId = tokenOfOwnerByIndex(add,i);
uint256 amount = purchasePrices[tokenId] - lastPrice;
toSend+= amount;
++i;
}
}
return (toSend);
}
//AIRDROPS
function claimAirdrop(uint256 crow) external{
if(!soldOut) revert SaleNotEnded();
if(_msgSender() != crocrow.ownerOf(crow)) revert CrowNotOwned();
if(airdropClaimed[crow]) revert AlreadyClaimed();
uint256 airdropAmount = lastPrice * MAX_SUPPLY / 4 / 7777;
airdropClaimed[crow] = true;
mad.transfer(_msgSender(),airdropAmount);
}
function claimAirdropAll() public{
if(!soldOut) revert SaleNotEnded();
uint256 airdropAmount = lastPrice * MAX_SUPPLY / 4 / 7777;
uint256 ownerTokenCount = crocrow.balanceOf(_msgSender());
uint256 toSend;
uint256 tokenId;
for (uint256 i; i < ownerTokenCount;) {
tokenId = crocrow.tokenOfOwnerByIndex(_msgSender(), i);
unchecked{
if(!airdropClaimed[tokenId]){
toSend += airdropAmount;
airdropClaimed[tokenId] = true;
}
++i;
}
}
if(toSend == 0) revert NoBalance();
mad.transfer(_msgSender(),toSend);
}
//Function for mass claiming for the whales with 200+ CRO CROWs.
function claimAirdrop100(uint256 index) external{
if(!soldOut) revert SaleNotEnded();
uint256 airdropAmount = lastPrice * MAX_SUPPLY / 4 / 7777;
uint256 ownerTokenCount = crocrow.balanceOf(_msgSender());
uint256 toSend;
uint256 tokenId;
if(ownerTokenCount < 100 * (index+1)) revert NotEnoughCrows();
for (uint256 i = 100 * index; i < 100 * (index+1);) {
tokenId = crocrow.tokenOfOwnerByIndex(_msgSender(), i);
unchecked{
if(!airdropClaimed[tokenId]){
toSend += airdropAmount;
airdropClaimed[tokenId] = true;
}
++i;
}
}
if(toSend == 0) revert NoBalance();
mad.transfer(_msgSender(),toSend);
}
function airdropCheck(uint256 crow) external view returns(bool){
if(!soldOut) revert SaleNotEnded();
return airdropClaimed[crow];
}
function airdropMulticheck(address add) external view returns (uint256) {
if(!soldOut) revert SaleNotEnded();
uint256 airdropAmount = lastPrice * MAX_SUPPLY / 4 / 7777;
uint256 ownerTokenCount = crocrow.balanceOf(add);
uint256 toSend;
uint256 tokenId;
for (uint256 i; i < ownerTokenCount;) {
tokenId = crocrow.tokenOfOwnerByIndex(add, i);
unchecked{
if(!airdropClaimed[tokenId]){
toSend += airdropAmount;
}
++i;
}
}
return toSend;
}
//TIME
function currentSegment() public view returns(uint256){
if(block.timestamp < startDate){
return 0;
}else{
uint256 passed = block.timestamp - startDate;
uint256 segmentSize = (endDate-startDate) / SEGMENTS;
uint256 current = ((passed - (passed % segmentSize)) / segmentSize) + 1;
if(current > SEGMENTS){
current = SEGMENTS+1;
}
return current;
}
}
function getCurrentPrice() public view returns(uint256){
uint256 segment = currentSegment();
if(segment == 0){
return startPrice;
}
return endPrice+((SEGMENTS - (segment-1)) * (startPrice-endPrice) /SEGMENTS);
}
}