-
Notifications
You must be signed in to change notification settings - Fork 1
/
NFT30Web3.sol
459 lines (389 loc) · 19.6 KB
/
NFT30Web3.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.4;
/*
30Web3
ERC721, Enumerable, Ownable, Adminlist
Core is ERC721 library from Solmate by @transmissions11, modified to support
Open Zeppelin Enumerable extension and hooks. Lilownable by @m1guelpf. Adminlist
based on a branch of Open Zeppelin ownership branch from 2018.
ERC721 Solmate: https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC721.sol
OZ Enumerable: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.4.2/contracts/token/ERC721/extensions/ERC721Enumerable.sol
Lilownable: https://github.com/m1guelpf/erc721-drop/blob/main/src/LilOwnable.sol
Adminlist: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/025c9bdcde412e65a307e4985151e7c023fd3870/contracts/ownership/Whitelist.sol
@author kethic, xavier, larkef
*/
/// ----------------------------------------------------------------------------
/// Imports
/// ----------------------------------------------------------------------------
import "./ERC721_Solmate_Hooked.sol";
import "./LilOwnable.sol";
import "./Adminlist.sol";
// Thank you tangert
// https://gist.github.com/tangert/1eceaf04f2877d84fb0e10681b39d7e3#file-renderer-sol
import "./Renderer.sol";
import "@openzeppelin/contracts/interfaces/IERC20.sol";
/// ----------------------------------------------------------------------------
/// Enums and Structs
/// ----------------------------------------------------------------------------
/// ----------------------------------------------------------------------------
/// Errors
/// ----------------------------------------------------------------------------
error InvalidToken();
error IndexOutOfBounds();
error TransferFailed();
error NoReentrancy();
contract NFT30Web3 is ERC721, LilOwnable, Adminlist, Renderer {
/// ------------------------------------------------------------------------
/// Events
/// ------------------------------------------------------------------------
/// ------------------------------------------------------------------------
/// Variables
/// ------------------------------------------------------------------------
uint256 private currentToken;
string public cohort;
mapping(uint256 => string) idToCohort;
uint8 private _mutex = 1;
/// ------------------------------------------------------------------------
/// Modifiers
/// ------------------------------------------------------------------------
modifier isValidToken(uint256 id) {
if(ownerOf[id] == address(0)) revert InvalidToken();
_;
}
modifier mutex() {
if(_mutex == 2) revert NoReentrancy();
_mutex = 2;
_;
_mutex = 1;
}
/// ------------------------------------------------------------------------
/// Functions
/// ------------------------------------------------------------------------
constructor(
address[] memory _adminlist,
string memory _cohort
) ERC721 (
"30Web3 Graduates",
"30Web3"
) {
// Deployer has Admin Rights
_setupAdmin(msg.sender);
// Add the other Admins
uint16 length = uint16(_adminlist.length);
for(uint16 i=0; i < length; i = uncheckedInc(i))
{
_setupAdmin(_adminlist[i]);
}
setCohort(_cohort);
}
function setCohort(
string memory _cohort
)
public
onlyAdmin
{
cohort = _cohort;
}
function mint(
address _target
)
external
payable
onlyAdmin
mutex
{
_mint(address(_target), currentToken);
idToCohort[currentToken] = cohort;
currentToken++;
}
function tokenURI(
uint256 id
)
public
view
override
isValidToken(id)
returns (string memory)
{
string memory svgString = _render(id, idToCohort[id]);
return buildSvg("30Web3", "Congratulations on your successful completion of 30Web3!", svgString);
}
function contractURI()
public
pure
returns (string memory)
{
return "data:application/json;base64,eyJuYW1lIjoiMzBXZWIzIEdyYWR1YXRlcyIsImRlc2NyaXB0aW9uIjoiMzAgZGF5cyBvZiBXZWIzIGlzIGFuIG9wcG9ydHVuaXR5IHRvIHdvcmsgd2l0aCB5b3VyIHBlZXJzLCBzaGlwIGEgcHJvamVjdCwgYW5kIGdhaW4gaGFuZHMtb24gV2ViMyBleHBlcmllbmNlIGluIHNtYWxsLCBjb2xsYWJvcmF0aXZlLCBmb2N1c2VkIGNvaG9ydHMuIFRoZXNlIHRva2VucyBjb21tZW1vcmF0ZSB0aGUgZ3JhZHVhdGVzIGZyb20gZWFjaCBjb2hvcnQuIiwiaW1hZ2UiOiAiZGF0YTppbWFnZS9zdmcreG1sO2Jhc2U2NCxQSE4yWnlCM2FXUjBhRDBpTlRSd2VDSWdhR1ZwWjJoMFBTSTBNSEI0SWlCMmFXVjNRbTk0UFNJd0lEQWdNelkySURJMk5pSWdabWxzYkQwaWJtOXVaU0lnZUcxc2JuTTlJbWgwZEhBNkx5OTNkM2N1ZHpNdWIzSm5Mekl3TURBdmMzWm5JajQ4Wld4c2FYQnpaU0JqZUQwaU1UTXlJaUJqZVQwaU1UTXp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Rlci5jb20vd3NseXZoIiwiZmVlX3JlY2lwaWVudCI6IjB4NjNjYWI2OTE4OWRCYTJmMTU0NGEyNWM4QzE5YjQzMDlmNDE1YzhBQSIsInNlbGxlcl9mZWVfYmFzaXNfcG9pbnRzIjowfQ==";
}
function withdraw()
public
onlyAdmin
{
uint256 balance = address(this).balance;
payable(msg.sender).transfer(balance);
}
function withdrawTokens(
IERC20 token
)
public
onlyAdmin
{
uint256 balance = token.balanceOf(address(this));
token.transfer(msg.sender, balance);
}
/// ------------------------------------------------------------------------
/// ERC721Enumerable Variables
/// ------------------------------------------------------------------------
// 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;
/// ------------------------------------------------------------------------
/// ERC721Enumerable Functions
/// ------------------------------------------------------------------------
function tokenOfOwnerByIndex(
address owner,
uint256 index
)
public
view
returns (uint256)
{
if(index >= ERC721.balanceOf[owner]) revert IndexOutOfBounds();
return _ownedTokens[owner][index];
}
function totalSupply()
public
view
returns (uint256)
{
return _allTokens.length;
}
function tokenByIndex(
uint256 index
)
public
view
returns (uint256)
{
if(index >= totalSupply()) revert IndexOutOfBounds();
return _allTokens[index];
}
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override {
super._beforeTokenTransfer(from, to, tokenId);
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 = ERC721.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 = ERC721.balanceOf[from] - 1;
uint256 tokenIndex = _ownedTokensIndex[tokenId];
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
}
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
delete _allTokensIndex[tokenId];
_allTokens.pop();
}
/// ------------------------------------------------------------------------
/// ERC165
/// ------------------------------------------------------------------------
function supportsInterface(bytes4 interfaceId)
public
pure
override(ERC721, LilOwnable)
returns (bool)
{
return
interfaceId == 0x01ffc9a7 || // ERC165 Interface ID for ERC165
interfaceId == 0x7f5828d0 || // ERC165 Interface ID for ERC173
interfaceId == 0x80ac58cd || // ERC165 Interface ID for ERC721
interfaceId == 0x5b5e139f || // ERC165 Interface ID for ERC721Metadata
interfaceId == 0x780e9d63; // ERC165 Interface ID for ERC721Enumerable
}
/// ------------------------------------------------------------------------
/// Utility Functions
/// ------------------------------------------------------------------------
//https://ethereum.stackexchange.com/questions/8346/convert-address-to-string
function toAsciiString(
address x
)
internal
pure
returns (string memory)
{
bytes memory s = new bytes(40);
for (uint256 i = 0; i < 20; i++) {
bytes1 b = bytes1(uint8(uint256(uint160(x)) / (2**(8 * (19 - i)))));
bytes1 hi = bytes1(uint8(b) / 16);
bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
s[2 * i] = char(hi);
s[2 * i + 1] = char(lo);
}
return string(s);
}
function buildSvg(
string memory nftName,
string memory nftDescription,
string memory svgString
)
public
pure
returns (string memory)
{
string memory imgEncoded = Base64.encode(bytes(svgString));
string memory imgURI = string(
abi.encodePacked("data:image/svg+xml;base64,", imgEncoded)
);
string memory nftJson = string(
abi.encodePacked(
'{"name": "',
nftName,
'", "description": "',
nftDescription,
'", "image": "',
imgURI,
'"}'
)
);
string memory nftEncoded = Base64.encode(bytes(nftJson));
string memory finalURI = string(
abi.encodePacked("data:application/json;base64,", nftEncoded)
);
return finalURI;
}
function char(
bytes1 b
)
internal
pure
returns (bytes1 c)
{
if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
else return bytes1(uint8(b) + 0x57);
}
// https://gist.github.com/hrkrshnn/ee8fabd532058307229d65dcd5836ddc#the-increment-in-for-loop-post-condition-can-be-made-unchecked
function uncheckedInc(
uint16 i
)
internal
pure
returns (uint16)
{
unchecked {
return i + 1;
}
}
}
/// ----------------------------------------------------------------------------
/// External Contracts
/// ----------------------------------------------------------------------------
// Primes NFT
//https://etherscan.io/address/0xBDA937F5C5f4eFB2261b6FcD25A71A1C350FdF20#code#L1507
library Base64 {
string internal constant TABLE_ENCODE =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function encode(bytes memory data) internal pure returns (string memory) {
if (data.length == 0) return "";
// load the table into memory
string memory table = TABLE_ENCODE;
// multiply by 4/3 rounded up
uint256 encodedLen = 4 * ((data.length + 2) / 3);
// add some extra buffer at the end required for the writing
string memory result = new string(encodedLen + 32);
assembly {
// set the actual output length
mstore(result, encodedLen)
// prepare the lookup table
let tablePtr := add(table, 1)
// input ptr
let dataPtr := data
let endPtr := add(dataPtr, mload(data))
// result ptr, jump over length
let resultPtr := add(result, 32)
// run over the input, 3 bytes at a time
for {
} lt(dataPtr, endPtr) {
} {
// read 3 bytes
dataPtr := add(dataPtr, 3)
let input := mload(dataPtr)
// write 4 characters
mstore8(
resultPtr,
mload(add(tablePtr, and(shr(18, input), 0x3F)))
)
resultPtr := add(resultPtr, 1)
mstore8(
resultPtr,
mload(add(tablePtr, and(shr(12, input), 0x3F)))
)
resultPtr := add(resultPtr, 1)
mstore8(
resultPtr,
mload(add(tablePtr, and(shr(6, input), 0x3F)))
)
resultPtr := add(resultPtr, 1)
mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
resultPtr := add(resultPtr, 1)
}
// padding with '='
switch mod(mload(data), 3)
case 1 {
mstore(sub(resultPtr, 2), shl(240, 0x3d3d))
}
case 2 {
mstore(sub(resultPtr, 1), shl(248, 0x3d))
}
}
return result;
}
}