Last active
September 27, 2024 08:56
-
-
Save Vectorized/412ae9177319f4fe5e1cdb5cebb4f972 to your computer and use it in GitHub Desktop.
ERC721A Burnable (with configurable start index)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
// Creator: Chiru Labs | |
pragma solidity ^0.8.0; | |
import '@openzeppelin/contracts/token/ERC721/IERC721.sol'; | |
import '@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol'; | |
import '@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol'; | |
import '@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol'; | |
import '@openzeppelin/contracts/utils/Address.sol'; | |
import '@openzeppelin/contracts/utils/Context.sol'; | |
import '@openzeppelin/contracts/utils/Strings.sol'; | |
import '@openzeppelin/contracts/utils/introspection/ERC165.sol'; | |
/** | |
* @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including | |
* the Metadata and Enumerable extension. Built to optimize for lower gas during batch mints. | |
* | |
* Assumes serials are sequentially minted starting at 0 (e.g. 0, 1, 2, 3..). | |
* | |
* Assumes that an owner cannot have more than 2**64 - 1 (max value of uint64) of supply. | |
* | |
* Assumes that the maximum token id cannot exceed 2**128 - 1 (max value of uint128). | |
*/ | |
contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable { | |
using Address for address; | |
using Strings for uint256; | |
// Compiler will pack this into a single 256bit word. | |
struct TokenOwnership { | |
// The address of the owner. | |
address addr; | |
// Keeps track of the start time of ownership with minimal overhead for tokenomics. | |
uint64 startTimestamp; | |
// Whether the token has been burned. | |
bool burned; | |
} | |
// Compiler will pack this into a single 256bit word. | |
struct AddressData { | |
// Realistically, 2**64-1 is more than enough. | |
uint64 balance; | |
// Keeps track of mint count with minimal overhead for tokenomics. | |
uint64 numberMinted; | |
// Keeps track of burn count with minimal overhead for tokenomics. | |
uint64 numberBurned; | |
// For miscellaneous variables (e.g. number preSale minted). | |
// Please pack into 64 bits. | |
uint64 aux; | |
} | |
uint128 internal currentIndex; | |
uint128 internal burnCounter; | |
// Token name | |
string private _name; | |
// Token symbol | |
string private _symbol; | |
// Mapping from token ID to ownership details | |
// An empty struct value does not necessarily mean the token is unowned. See ownershipOf implementation for details. | |
mapping(uint256 => TokenOwnership) internal _ownerships; | |
// Mapping owner address to address data | |
mapping(address => AddressData) private _addressData; | |
// 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(string memory name_, string memory symbol_) { | |
_name = name_; | |
_symbol = symbol_; | |
currentIndex = _startIndex(); | |
} | |
/** | |
* To change the start offset, please override this function. | |
*/ | |
function _startIndex() internal virtual view returns (uint128) { | |
return 0; | |
} | |
/** | |
* @dev See {IERC721Enumerable-totalSupply}. | |
*/ | |
function totalSupply() public view override returns (uint256) { | |
// Counter underflow is impossible as burnCounter cannot be incremented | |
// more than currentIndex times | |
unchecked { | |
return currentIndex - burnCounter - _startIndex(); | |
} | |
} | |
/** | |
* @dev See {IERC721Enumerable-tokenByIndex}. | |
* This read function is O(totalSupply). If calling from a separate contract, be sure to test gas first. | |
* It may also degrade with extremely large collection sizes (e.g >> 10000), test for your use case. | |
*/ | |
function tokenByIndex(uint256 index) public view override returns (uint256) { | |
uint256 numMintedSoFar = currentIndex; | |
uint256 tokenIdsIdx; | |
// Counter overflow is impossible as the loop breaks when | |
// uint256 i is equal to another uint256 numMintedSoFar. | |
unchecked { | |
for (uint256 i = _startIndex(); i < numMintedSoFar; i++) { | |
TokenOwnership memory ownership = _ownerships[i]; | |
if (!ownership.burned) { | |
if (tokenIdsIdx == index) { | |
return i; | |
} | |
tokenIdsIdx++; | |
} | |
} | |
} | |
revert('ERC721A: global index out of bounds'); | |
} | |
/** | |
* @dev See {IERC721Enumerable-tokenOfOwnerByIndex}. | |
* This read function is O(totalSupply). If calling from a separate contract, be sure to test gas first. | |
* It may also degrade with extremely large collection sizes (e.g >> 10000), test for your use case. | |
*/ | |
function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) { | |
require(index < balanceOf(owner), 'ERC721A: owner index out of bounds'); | |
uint256 numMintedSoFar = currentIndex; | |
uint256 tokenIdsIdx; | |
address currOwnershipAddr; | |
// Counter overflow is impossible as the loop breaks when | |
// uint256 i is equal to another uint256 numMintedSoFar. | |
unchecked { | |
for (uint256 i = _startIndex(); i < numMintedSoFar; i++) { | |
TokenOwnership memory ownership = _ownerships[i]; | |
if (ownership.burned) { | |
continue; | |
} | |
if (ownership.addr != address(0)) { | |
currOwnershipAddr = ownership.addr; | |
} | |
if (currOwnershipAddr == owner) { | |
if (tokenIdsIdx == index) { | |
return i; | |
} | |
tokenIdsIdx++; | |
} | |
} | |
} | |
revert('ERC721A: unable to get token of owner by index'); | |
} | |
/** | |
* @dev See {IERC165-supportsInterface}. | |
*/ | |
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) { | |
return | |
interfaceId == type(IERC721).interfaceId || | |
interfaceId == type(IERC721Metadata).interfaceId || | |
interfaceId == type(IERC721Enumerable).interfaceId || | |
super.supportsInterface(interfaceId); | |
} | |
/** | |
* @dev See {IERC721-balanceOf}. | |
*/ | |
function balanceOf(address owner) public view override returns (uint256) { | |
require(owner != address(0), 'ERC721A: balance query for the zero address'); | |
return uint256(_addressData[owner].balance); | |
} | |
function _numberMinted(address owner) internal view returns (uint256) { | |
require(owner != address(0), 'ERC721A: number minted query for the zero address'); | |
return uint256(_addressData[owner].numberMinted); | |
} | |
function _numberBurned(address owner) internal view returns (uint256) { | |
require(owner != address(0), 'ERC721A: number burned query for the zero address'); | |
return uint256(_addressData[owner].numberBurned); | |
} | |
function _getAux(address owner) internal view returns (uint64) { | |
require(owner != address(0), 'ERC721A: aux query for the zero address'); | |
return _addressData[owner].aux; | |
} | |
function _setAux(address owner, uint64 aux) internal { | |
require(owner != address(0), 'ERC721A: aux query for the zero address'); | |
_addressData[owner].aux = aux; | |
} | |
/** | |
* 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 ownershipOf(uint256 tokenId) internal view returns (TokenOwnership memory) { | |
uint256 curr = tokenId; | |
// Underflow is impossible because curr must be > 0 before decrement. | |
unchecked { | |
if (_startIndex() <= curr && curr < currentIndex) { | |
TokenOwnership memory ownership = _ownerships[curr]; | |
if (!ownership.burned) { | |
if (ownership.addr != address(0)) { | |
return ownership; | |
} | |
while (curr > _startIndex()) { | |
curr--; | |
ownership = _ownerships[curr]; | |
if (ownership.addr != address(0)) { | |
return ownership; | |
} | |
} | |
} | |
} | |
} | |
revert('ERC721A: owner query for nonexistent token'); | |
} | |
/** | |
* @dev See {IERC721-ownerOf}. | |
*/ | |
function ownerOf(uint256 tokenId) public view override returns (address) { | |
return ownershipOf(tokenId).addr; | |
} | |
/** | |
* @dev See {IERC721Metadata-name}. | |
*/ | |
function name() public view virtual override returns (string memory) { | |
return _name; | |
} | |
/** | |
* @dev See {IERC721Metadata-symbol}. | |
*/ | |
function symbol() public view virtual override returns (string memory) { | |
return _symbol; | |
} | |
/** | |
* @dev See {IERC721Metadata-tokenURI}. | |
*/ | |
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { | |
require(_exists(tokenId), 'ERC721Metadata: URI query for nonexistent token'); | |
string memory baseURI = _baseURI(); | |
return bytes(baseURI).length != 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : ''; | |
} | |
/** | |
* @dev Base URI for computing {tokenURI}. If set, the resulting URI for each | |
* token will be the concatenation of the `baseURI` and the `tokenId`. Empty | |
* by default, can be overriden in child contracts. | |
*/ | |
function _baseURI() internal view virtual returns (string memory) { | |
return ''; | |
} | |
/** | |
* @dev See {IERC721-approve}. | |
*/ | |
function approve(address to, uint256 tokenId) public override { | |
address owner = ERC721A.ownerOf(tokenId); | |
require(to != owner, 'ERC721A: approval to current owner'); | |
require( | |
_msgSender() == owner || isApprovedForAll(owner, _msgSender()), | |
'ERC721A: approve caller is not owner nor approved for all' | |
); | |
_approve(to, tokenId, owner); | |
} | |
/** | |
* @dev See {IERC721-getApproved}. | |
*/ | |
function getApproved(uint256 tokenId) public view override returns (address) { | |
require(_exists(tokenId), 'ERC721A: approved query for nonexistent token'); | |
return _tokenApprovals[tokenId]; | |
} | |
/** | |
* @dev See {IERC721-setApprovalForAll}. | |
*/ | |
function setApprovalForAll(address operator, bool approved) public override { | |
require(operator != _msgSender(), 'ERC721A: approve to caller'); | |
_operatorApprovals[_msgSender()][operator] = approved; | |
emit ApprovalForAll(_msgSender(), operator, approved); | |
} | |
/** | |
* @dev See {IERC721-isApprovedForAll}. | |
*/ | |
function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) { | |
return _operatorApprovals[owner][operator]; | |
} | |
/** | |
* @dev See {IERC721-transferFrom}. | |
*/ | |
function transferFrom( | |
address from, | |
address to, | |
uint256 tokenId | |
) public virtual override { | |
_transfer(from, to, tokenId); | |
} | |
/** | |
* @dev See {IERC721-safeTransferFrom}. | |
*/ | |
function safeTransferFrom( | |
address from, | |
address to, | |
uint256 tokenId | |
) public virtual override { | |
safeTransferFrom(from, to, tokenId, ''); | |
} | |
/** | |
* @dev See {IERC721-safeTransferFrom}. | |
*/ | |
function safeTransferFrom( | |
address from, | |
address to, | |
uint256 tokenId, | |
bytes memory _data | |
) public override { | |
_transfer(from, to, tokenId); | |
require( | |
_checkOnERC721Received(from, to, tokenId, _data), | |
'ERC721A: transfer to non ERC721Receiver implementer' | |
); | |
} | |
/** | |
* @dev Returns whether `tokenId` exists. | |
* | |
* Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}. | |
* | |
* Tokens start existing when they are minted (`_mint`), | |
*/ | |
function _exists(uint256 tokenId) internal view returns (bool) { | |
return _startIndex() <= tokenId && tokenId < currentIndex && !_ownerships[tokenId].burned; | |
} | |
function _safeMint(address to, uint256 quantity) internal { | |
_safeMint(to, quantity, ''); | |
} | |
/** | |
* @dev Safely mints `quantity` tokens and transfers them to `to`. | |
* | |
* Requirements: | |
* | |
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called for each safe transfer. | |
* - `quantity` must be greater than 0. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function _safeMint( | |
address to, | |
uint256 quantity, | |
bytes memory _data | |
) internal { | |
_mint(to, quantity, _data, true); | |
} | |
/** | |
* @dev Mints `quantity` tokens and transfers them to `to`. | |
* | |
* Requirements: | |
* | |
* - `to` cannot be the zero address. | |
* - `quantity` must be greater than 0. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function _mint( | |
address to, | |
uint256 quantity, | |
bytes memory _data, | |
bool safe | |
) internal { | |
uint256 startTokenId = currentIndex; | |
require(to != address(0), 'ERC721A: mint to the zero address'); | |
require(quantity != 0, 'ERC721A: quantity must be greater than 0'); | |
_beforeTokenTransfers(address(0), to, startTokenId, quantity); | |
// Overflows are incredibly unrealistic. | |
// balance or numberMinted overflow if current value of either + quantity > 3.4e38 (2**128) - 1 | |
// updatedIndex overflows if currentIndex + quantity > 3.4e38 (2**128) - 1 | |
unchecked { | |
_addressData[to].balance += uint64(quantity); | |
_addressData[to].numberMinted += uint64(quantity); | |
_ownerships[startTokenId].addr = to; | |
_ownerships[startTokenId].startTimestamp = uint64(block.timestamp); | |
uint256 updatedIndex = startTokenId; | |
for (uint256 i; i < quantity; i++) { | |
emit Transfer(address(0), to, updatedIndex); | |
if (safe) { | |
require( | |
_checkOnERC721Received(address(0), to, updatedIndex, _data), | |
'ERC721A: transfer to non ERC721Receiver implementer' | |
); | |
} | |
updatedIndex++; | |
} | |
require(updatedIndex <= type(uint128).max, 'ERC721A: safecast overflow'); | |
currentIndex = uint128(updatedIndex); | |
} | |
_afterTokenTransfers(address(0), to, startTokenId, quantity); | |
} | |
/** | |
* @dev Transfers `tokenId` from `from` to `to`. | |
* | |
* Requirements: | |
* | |
* - `to` cannot be the zero address. | |
* - `tokenId` token must be owned by `from`. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function _transfer( | |
address from, | |
address to, | |
uint256 tokenId | |
) private { | |
TokenOwnership memory prevOwnership = ownershipOf(tokenId); | |
bool isApprovedOrOwner = (_msgSender() == prevOwnership.addr || | |
getApproved(tokenId) == _msgSender() || | |
isApprovedForAll(prevOwnership.addr, _msgSender())); | |
require(isApprovedOrOwner, 'ERC721A: transfer caller is not owner nor approved'); | |
require(prevOwnership.addr == from, 'ERC721A: transfer from incorrect owner'); | |
require(to != address(0), 'ERC721A: transfer to the zero address'); | |
_beforeTokenTransfers(from, to, tokenId, 1); | |
// Clear approvals from the previous owner | |
_approve(address(0), tokenId, prevOwnership.addr); | |
// Underflow of the sender's balance is impossible because we check for | |
// ownership above and the recipient's balance can't realistically overflow. | |
// Counter overflow is incredibly unrealistic as tokenId would have to be 2**128. | |
unchecked { | |
_addressData[from].balance -= 1; | |
_addressData[to].balance += 1; | |
_ownerships[tokenId].addr = to; | |
_ownerships[tokenId].startTimestamp = uint64(block.timestamp); | |
// If the ownership slot of tokenId+1 is not explicitly set, that means the transfer initiator owns it. | |
// Set the slot of tokenId+1 explicitly in storage to maintain correctness for ownerOf(tokenId+1) calls. | |
uint256 nextTokenId = tokenId + 1; | |
if (_ownerships[nextTokenId].addr == address(0)) { | |
if (_exists(nextTokenId)) { | |
_ownerships[nextTokenId].addr = prevOwnership.addr; | |
_ownerships[nextTokenId].startTimestamp = prevOwnership.startTimestamp; | |
} | |
} | |
} | |
emit Transfer(from, to, tokenId); | |
_afterTokenTransfers(from, to, tokenId, 1); | |
} | |
/** | |
* @dev Destroys `tokenId`. | |
* The approval is cleared when the token is burned. | |
* | |
* Requirements: | |
* | |
* - `tokenId` must exist. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function _burn(uint256 tokenId) internal virtual { | |
TokenOwnership memory prevOwnership = ownershipOf(tokenId); | |
_beforeTokenTransfers(prevOwnership.addr, address(0), tokenId, 1); | |
// Clear approvals from the previous owner | |
_approve(address(0), tokenId, prevOwnership.addr); | |
// Underflow of the sender's balance is impossible because we check for | |
// ownership above and the recipient's balance can't realistically overflow. | |
// Counter overflow is incredibly unrealistic as tokenId would have to be 2**128. | |
unchecked { | |
_addressData[prevOwnership.addr].balance -= 1; | |
_addressData[prevOwnership.addr].numberBurned += 1; | |
// Keep track of who burnt the token, and when is it burned. | |
_ownerships[tokenId].addr = prevOwnership.addr; | |
_ownerships[tokenId].startTimestamp = uint64(block.timestamp); | |
_ownerships[tokenId].burned = true; | |
// If the ownership slot of tokenId+1 is not explicitly set, that means the burn initiator owns it. | |
// Set the slot of tokenId+1 explicitly in storage to maintain correctness for ownerOf(tokenId+1) calls. | |
uint256 nextTokenId = tokenId + 1; | |
if (_ownerships[nextTokenId].addr == address(0)) { | |
if (_exists(nextTokenId)) { | |
_ownerships[nextTokenId].addr = prevOwnership.addr; | |
_ownerships[nextTokenId].startTimestamp = prevOwnership.startTimestamp; | |
} | |
} | |
} | |
emit Transfer(prevOwnership.addr, address(0), tokenId); | |
_afterTokenTransfers(prevOwnership.addr, address(0), tokenId, 1); | |
burnCounter++; | |
} | |
/** | |
* @dev Approve `to` to operate on `tokenId` | |
* | |
* Emits a {Approval} event. | |
*/ | |
function _approve( | |
address to, | |
uint256 tokenId, | |
address owner | |
) private { | |
_tokenApprovals[tokenId] = to; | |
emit Approval(owner, to, tokenId); | |
} | |
/** | |
* @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.isContract()) { | |
try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) { | |
return retval == IERC721Receiver(to).onERC721Received.selector; | |
} catch (bytes memory reason) { | |
if (reason.length == 0) { | |
revert('ERC721A: transfer to non ERC721Receiver implementer'); | |
} else { | |
assembly { | |
revert(add(32, reason), mload(reason)) | |
} | |
} | |
} | |
} else { | |
return true; | |
} | |
} | |
/** | |
* @dev Hook that is called before a set of serially-ordered token ids are about to be transferred. This includes minting. | |
* | |
* startTokenId - the first token id to be transferred | |
* quantity - the amount to be transferred | |
* | |
* Calling conditions: | |
* | |
* - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be | |
* transferred to `to`. | |
* - When `from` is zero, `tokenId` will be minted for `to`. | |
*/ | |
function _beforeTokenTransfers( | |
address from, | |
address to, | |
uint256 startTokenId, | |
uint256 quantity | |
) internal virtual {} | |
/** | |
* @dev Hook that is called after a set of serially-ordered token ids have been transferred. This includes | |
* minting. | |
* | |
* startTokenId - the first token id to be transferred | |
* quantity - the amount to be transferred | |
* | |
* Calling conditions: | |
* | |
* - when `from` and `to` are both non-zero. | |
* - `from` and `to` are never both zero. | |
*/ | |
function _afterTokenTransfers( | |
address from, | |
address to, | |
uint256 startTokenId, | |
uint256 quantity | |
) internal virtual {} | |
} | |
/** | |
* @title ERC721A Burnable Token | |
* @dev ERC721A Token that can be irreversibly burned (destroyed). | |
*/ | |
abstract contract ERC721ABurnable is Context, ERC721A { | |
/** | |
* @dev Burns `tokenId`. See {ERC721A-_burn}. | |
* | |
* Requirements: | |
* | |
* - The caller must own `tokenId` or be an approved operator. | |
*/ | |
function burn(uint256 tokenId) public virtual { | |
TokenOwnership memory prevOwnership = ownershipOf(tokenId); | |
bool isApprovedOrOwner = (_msgSender() == prevOwnership.addr || | |
getApproved(tokenId) == _msgSender() || | |
isApprovedForAll(prevOwnership.addr, _msgSender())); | |
require(isApprovedOrOwner, 'ERC721A: caller is not owner nor approved'); | |
_burn(tokenId); | |
} | |
} | |
abstract contract ERC721AOwnersExplicit is ERC721A { | |
uint256 public nextOwnerToExplicitlySet; | |
constructor() { | |
nextOwnerToExplicitlySet = _startIndex(); | |
} | |
/** | |
* @dev Explicitly set `owners` to eliminate loops in future calls of ownerOf(). | |
*/ | |
function _setOwnersExplicit(uint256 quantity) internal { | |
require(quantity != 0, 'quantity must be nonzero'); | |
require(currentIndex != 0, 'no tokens minted yet'); | |
uint256 _nextOwnerToExplicitlySet = nextOwnerToExplicitlySet; | |
require(_nextOwnerToExplicitlySet < currentIndex, 'all ownerships have been set'); | |
// Index underflow is impossible. | |
// Counter or index overflow is incredibly unrealistic. | |
unchecked { | |
uint256 endIndex = _nextOwnerToExplicitlySet + quantity - 1; | |
// Set the end index to be the last token index | |
if (endIndex + 1 > currentIndex) { | |
endIndex = currentIndex - 1; | |
} | |
for (uint256 i = _nextOwnerToExplicitlySet; i <= endIndex; i++) { | |
if (_ownerships[i].addr == address(0) && !_ownerships[i].burned) { | |
TokenOwnership memory ownership = ownershipOf(i); | |
_ownerships[i].addr = ownership.addr; | |
_ownerships[i].startTimestamp = ownership.startTimestamp; | |
} | |
} | |
nextOwnerToExplicitlySet = endIndex + 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment