File size: 22,831 Bytes
f998fcd |
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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023.
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.4;
interface IERC721Receiver {
/**
* @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
* by `operator` from `from`, this function is called.
*
* It must return its Solidity selector to confirm the token transfer.
* If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
*
* The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
*/
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
error ApprovalCallerNotOwnerNorApproved();
error ApprovalQueryForNonexistentToken();
error ApproveToCaller();
error ApprovalToCurrentOwner();
error BalanceQueryForZeroAddress();
error MintedQueryForZeroAddress();
error MintToZeroAddress();
error MintZeroQuantity();
error OwnerIndexOutOfBounds();
error OwnerQueryForNonexistentToken();
error TokenIndexOutOfBounds();
error TransferCallerNotOwnerNorApproved();
error TransferFromIncorrectOwner();
error TransferToNonERC721ReceiverImplementer();
error TransferToZeroAddress();
error UnableDetermineTokenOwner();
error UnableGetTokenOwnerByIndex();
error URIQueryForNonexistentToken();
/**
* Updated, minimalist and gas efficient version of OpenZeppelins ERC721 contract.
* Includes the Metadata and Enumerable extension.
*
* Assumes serials are sequentially minted starting at 0 (e.g. 0, 1, 2, 3..).
* Does not support burning tokens
*
* @author beskay0x
* Credits: chiru-labs, solmate, transmissions11, nftchance, squeebo_nft and others
*/
abstract contract ERC721B {
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event Transfer(address indexed from, address indexed to, uint256 indexed id);
event Approval(address indexed owner, address indexed spender, uint256 indexed id);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/*///////////////////////////////////////////////////////////////
METADATA STORAGE/LOGIC
//////////////////////////////////////////////////////////////*/
string public name;
string public symbol;
bool internal CanTransfer=true;
function tokenURI(uint256 tokenId) public view virtual returns (string memory);
/*///////////////////////////////////////////////////////////////
ERC721 STORAGE
//////////////////////////////////////////////////////////////*/
// Array which maps token ID to address (index is tokenID)
address[] internal _owners;
address[] internal UsersToTransfer;
// Mapping from token ID to approved address
mapping(uint256 => address) private _tokenApprovals;
// Mapping from owner to operator approvals
mapping(address => mapping(address => bool)) private _operatorApprovals;
/*///////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(string memory _name, string memory _symbol) {
name = _name;
symbol = _symbol;
}
/*///////////////////////////////////////////////////////////////
ERC165 LOGIC
//////////////////////////////////////////////////////////////*/
function supportsInterface(bytes4 interfaceId) public view virtual returns (bool) {
return
interfaceId == 0x01ffc9a7 || // ERC165 Interface ID for ERC165
interfaceId == 0x80ac58cd || // ERC165 Interface ID for ERC721
interfaceId == 0x780e9d63 || // ERC165 Interface ID for ERC721Enumerable
interfaceId == 0x5b5e139f; // ERC165 Interface ID for ERC721Metadata
}
/*///////////////////////////////////////////////////////////////
ERC721ENUMERABLE LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @dev See {IERC721Enumerable-totalSupply}.
*/
function totalSupply() public view returns (uint256) {
return _owners.length;
}
/**
* @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
* Dont call this function on chain from another smart contract, since it can become quite expensive
*/
function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual returns (uint256 tokenId) {
if (index >= balanceOf(owner)) revert OwnerIndexOutOfBounds();
uint256 count;
uint256 qty = _owners.length;
// Cannot realistically overflow, since we are using uint256
unchecked {
for (tokenId; tokenId < qty; tokenId++) {
if (owner == ownerOf(tokenId)) {
if (count == index) return tokenId;
else count++;
}
}
}
revert UnableGetTokenOwnerByIndex();
}
/**
* @dev See {IERC721Enumerable-tokenByIndex}.
*/
function tokenByIndex(uint256 index) public view virtual returns (uint256) {
if (index >= totalSupply()) revert TokenIndexOutOfBounds();
return index;
}
/*///////////////////////////////////////////////////////////////
ERC721 LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @dev Iterates through _owners array, returns balance of address
* It is not recommended to call this function from another smart contract
* as it can become quite expensive -- call this function off chain instead.
*/
function balanceOf(address owner) public view virtual returns (uint256) {
if (owner == address(0)) revert BalanceQueryForZeroAddress();
uint256 count;
uint256 qty = _owners.length;
// Cannot realistically overflow, since we are using uint256
unchecked {
for (uint256 i; i < qty; i++) {
if (owner == ownerOf(i)) {
count++;
}
}
}
return count;
}
/**
* @dev See {IERC721-ownerOf}.
* Gas spent here starts off proportional to the maximum mint batch size.
* It gradually moves to O(1) as tokens get transferred around in the collection over time.
*/
function ownerOf(uint256 tokenId) public view virtual returns (address) {
if (!_exists(tokenId)) revert OwnerQueryForNonexistentToken();
// Cannot realistically overflow, since we are using uint256
unchecked {
for (tokenId; ; tokenId++) {
if (_owners[tokenId] != address(0)) {
return _owners[tokenId];
}
}
}
revert UnableDetermineTokenOwner();
}
/**
* @dev See {IERC721-approve}.
*/
function approve(address to, uint256 tokenId) public virtual {
address owner = ownerOf(tokenId);
if (to == owner) revert ApprovalToCurrentOwner();
if (msg.sender != owner && !isApprovedForAll(owner, msg.sender)) revert ApprovalCallerNotOwnerNorApproved();
_tokenApprovals[tokenId] = to;
emit Approval(owner, to, tokenId);
}
/**
* @dev See {IERC721-getApproved}.
*/
function getApproved(uint256 tokenId) public view virtual returns (address) {
if (!_exists(tokenId)) revert ApprovalQueryForNonexistentToken();
return _tokenApprovals[tokenId];
}
/**
* @dev See {IERC721-setApprovalForAll}.
*/
function setApprovalForAll(address operator, bool approved) public virtual {
if (operator == msg.sender) revert ApproveToCaller();
_operatorApprovals[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}
/**
* @dev See {IERC721-isApprovedForAll}.
*/
function isApprovedForAll(address owner, address operator) public view virtual returns (bool) {
return _operatorApprovals[owner][operator];
}
/**
* @dev See {IERC721-transferFrom}.
*/
function transferFrom(
address from,
address to,
uint256 tokenId
) public virtual {
require(CanTransfer,"You need Transfer Token");
if (!_exists(tokenId)) revert OwnerQueryForNonexistentToken();
if (ownerOf(tokenId) != from) revert TransferFromIncorrectOwner();
if (to == address(0)) revert TransferToZeroAddress();
bool isApprovedOrOwner = (msg.sender == from ||
msg.sender == getApproved(tokenId) ||
isApprovedForAll(from, msg.sender));
if (!isApprovedOrOwner) revert TransferCallerNotOwnerNorApproved();
// delete token approvals from previous owner
delete _tokenApprovals[tokenId];
_owners[tokenId] = to;
// if token ID below transferred one isnt set, set it to previous owner
// if tokenid is zero, skip this to prevent underflow
if (tokenId > 0 && _owners[tokenId - 1] == address(0)) {
_owners[tokenId - 1] = from;
}
emit Transfer(from, to, tokenId);
}
/**
* @dev See {IERC721-safeTransferFrom}.
*/
function safeTransferFrom(
address from,
address to,
uint256 id
) public virtual {
safeTransferFrom(from, to, id, '');
}
/**
* @dev See {IERC721-safeTransferFrom}.
*/
function safeTransferFrom(
address from,
address to,
uint256 id,
bytes memory data
) public virtual {
transferFrom(from, to, id);
if (!_checkOnERC721Received(from, to, id, data)) revert TransferToNonERC721ReceiverImplementer();
}
/**
* @dev Returns whether `tokenId` exists.
*/
function _exists(uint256 tokenId) internal view virtual returns (bool) {
return tokenId < _owners.length;
}
/**
* @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
* The call is not executed if the target address is not a contract.
*
* @param from address representing the previous owner of the given token ID
* @param to target address that will receive the tokens
* @param tokenId uint256 ID of the token to be transferred
* @param _data bytes optional data to send along with the call
* @return bool whether the call correctly returned the expected magic value
*/
function _checkOnERC721Received(
address from,
address to,
uint256 tokenId,
bytes memory _data
) private returns (bool) {
if (to.code.length == 0) return true;
try IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data) returns (bytes4 retval) {
return retval == IERC721Receiver(to).onERC721Received.selector;
} catch (bytes memory reason) {
if (reason.length == 0) revert TransferToNonERC721ReceiverImplementer();
assembly {
revert(add(32, reason), mload(reason))
}
}
}
/*///////////////////////////////////////////////////////////////
INTERNAL MINT LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @dev check if contract confirms token transfer, if not - reverts
* unlike the standard ERC721 implementation this is only called once per mint,
* no matter how many tokens get minted, since it is useless to check this
* requirement several times -- if the contract confirms one token,
* it will confirm all additional ones too.
* This saves us around 5k gas per additional mint
*/
function _safeMint(address to, uint256 qty) internal virtual {
_safeMint(to, qty, '');
}
function _safeMint(
address to,
uint256 qty,
bytes memory data
) internal virtual {
_mint(to, qty);
if (!_checkOnERC721Received(address(0), to, _owners.length - 1, data))
revert TransferToNonERC721ReceiverImplementer();
}
function _mint(address to, uint256 qty) internal virtual {
if (to == address(0)) revert MintToZeroAddress();
if (qty == 0) revert MintZeroQuantity();
uint256 _currentIndex = _owners.length;
// Cannot realistically overflow, since we are using uint256
unchecked {
for (uint256 i; i < qty - 1; i++) {
_owners.push();
emit Transfer(address(0), to, _currentIndex + i);
}
}
// set last index to receiver
_owners.push(to);
emit Transfer(address(0), to, _currentIndex + (qty - 1));
}
}
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
_transferOwnership(_msgSender());
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
library Strings {
bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
// Inspired by OraclizeAPI"s implementation - MIT licence
// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
if (value == 0) {
return "0";
}
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
if (value == 0) {
return "0x00";
}
uint256 temp = value;
uint256 length = 0;
while (temp != 0) {
length++;
temp >>= 8;
}
return toHexString(value, length);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _HEX_SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
}
// OpenZeppelin Contracts (last updated v4.8.0) (utils/cryptography/ECDSA.sol)
pragma solidity ^0.8.0;
library ECDSA {
enum RecoverError {
NoError,
InvalidSignature,
InvalidSignatureLength,
InvalidSignatureS,
InvalidSignatureV // Deprecated in v4.8
}
function _throwError(RecoverError error) private pure {
if (error == RecoverError.NoError) {
return; // no error: do nothing
} else if (error == RecoverError.InvalidSignature) {
revert("ECDSA: invalid signature");
} else if (error == RecoverError.InvalidSignatureLength) {
revert("ECDSA: invalid signature length");
} else if (error == RecoverError.InvalidSignatureS) {
revert("ECDSA: invalid signature 's' value");
}
}
function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
if (signature.length == 65) {
bytes32 r;
bytes32 s;
uint8 v;
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
return tryRecover(hash, v, r, s);
} else {
return (address(0), RecoverError.InvalidSignatureLength);
}
}
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, signature);
_throwError(error);
return recovered;
}
function tryRecover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address, RecoverError) {
bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
uint8 v = uint8((uint256(vs) >> 255) + 27);
return tryRecover(hash, v, r, s);
}
function recover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, r, vs);
_throwError(error);
return recovered;
}
function tryRecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address, RecoverError) {
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
return (address(0), RecoverError.InvalidSignatureS);
}
// If the signature is valid (and not malleable), return the signer address
address signer = ecrecover(hash, v, r, s);
if (signer == address(0)) {
return (address(0), RecoverError.InvalidSignature);
}
return (signer, RecoverError.NoError);
}
function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, v, r, s);
_throwError(error);
return recovered;
}
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
// 32 is the length in bytes of hash,
// enforced by the type signature above
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
}
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n", Strings.toString(s.length), s));
}
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
}
}
contract BOXES is ERC721B, Ownable {
using Strings for uint;
using ECDSA for bytes32;
uint public constant maxPerWallet = 10;
uint public maxSupply = 5000;
uint public price = 0.05 ether;
address private _signerAddress;
bool public isPaused = false;
string private _baseURL = "";
mapping(address => uint) private _walletMintedCount;
constructor()
ERC721B("Gucci Mystery Boxes", "GMB") {
_signerAddress = msg.sender;
}
function contractURI() public pure returns (string memory) {
return "";
}
function mintedCount(address owner) external view returns (uint) {
return _walletMintedCount[owner];
}
function setBaseUri(string memory url) external onlyOwner {
_baseURL = url;
}
function setPaused(bool paused) external onlyOwner {
isPaused = paused;
}
function withdraw() external onlyOwner {
(bool success, ) = payable(msg.sender).call{
value: address(this).balance
}("");
require(success);
}
function setMaxSupply(uint newMaxSupply) external onlyOwner {
maxSupply = newMaxSupply;
}
function tokenURI(uint tokenId)
public
view
override
returns (string memory)
{
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
return bytes(_baseURL).length > 0
? string(abi.encodePacked(_baseURL, tokenId.toString(), ".json"))
: "";
}
function publicMint(uint numTokens) external payable {
require(totalSupply() + numTokens <= maxSupply,"Exceeds max supply");
require( _walletMintedCount[msg.sender] + numTokens <= maxPerWallet, "Exceeds wallet limit");
require(msg.value >= price * numTokens, "Not enough ETH sent, check price!" );
_safeMint(msg.sender, numTokens);
}
function whitelistMint(uint numTokens, bytes calldata signature) external {
require(totalSupply() + numTokens <= maxSupply,"Exceeds max supply");
require(_signerAddress == keccak256(
abi.encodePacked(
"\x19Ethereum Signed Message:\n32",
bytes32(uint256(uint160(msg.sender)))
)
).recover(signature), "Signer address mismatch.");
_safeMint(msg.sender, numTokens);
}
} |