dexorder
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/ERC721Burnable.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC721} from "../ERC721.sol";
|
||||
import {Context} from "../../../utils/Context.sol";
|
||||
|
||||
/**
|
||||
* @title ERC-721 Burnable Token
|
||||
* @dev ERC-721 Token that can be burned (destroyed).
|
||||
*/
|
||||
abstract contract ERC721Burnable is Context, ERC721 {
|
||||
/**
|
||||
* @dev Burns `tokenId`. See {ERC721-_burn}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - The caller must own `tokenId` or be an approved operator.
|
||||
*/
|
||||
function burn(uint256 tokenId) public virtual {
|
||||
// Setting an "auth" arguments enables the `_isAuthorized` check which verifies that the token exists
|
||||
// (from != 0). Therefore, it is not needed to verify that the return value is not 0 here.
|
||||
_update(address(0), tokenId, _msgSender());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/ERC721Consecutive.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC721} from "../ERC721.sol";
|
||||
import {IERC2309} from "../../../interfaces/IERC2309.sol";
|
||||
import {BitMaps} from "../../../utils/structs/BitMaps.sol";
|
||||
import {Checkpoints} from "../../../utils/structs/Checkpoints.sol";
|
||||
|
||||
/**
|
||||
* @dev Implementation of the ERC-2309 "Consecutive Transfer Extension" as defined in
|
||||
* https://eips.ethereum.org/EIPS/eip-2309[ERC-2309].
|
||||
*
|
||||
* This extension allows the minting of large batches of tokens, during contract construction only. For upgradeable
|
||||
* contracts this implies that batch minting is only available during proxy deployment, and not in subsequent upgrades.
|
||||
* These batches are limited to 5000 tokens at a time by default to accommodate off-chain indexers.
|
||||
*
|
||||
* Using this extension removes the ability to mint single tokens during contract construction. This ability is
|
||||
* regained after construction. During construction, only batch minting is allowed.
|
||||
*
|
||||
* IMPORTANT: This extension does not call the {_update} function for tokens minted in batch. Any logic added to this
|
||||
* function through overrides will not be triggered when token are minted in batch. You may want to also override
|
||||
* {_increaseBalance} or {_mintConsecutive} to account for these mints.
|
||||
*
|
||||
* IMPORTANT: When overriding {_mintConsecutive}, be careful about call ordering. {ownerOf} may return invalid
|
||||
* values during the {_mintConsecutive} execution if the super call is not called first. To be safe, execute the
|
||||
* super call before your custom logic.
|
||||
*/
|
||||
abstract contract ERC721Consecutive is IERC2309, ERC721 {
|
||||
using BitMaps for BitMaps.BitMap;
|
||||
using Checkpoints for Checkpoints.Trace160;
|
||||
|
||||
Checkpoints.Trace160 private _sequentialOwnership;
|
||||
BitMaps.BitMap private _sequentialBurn;
|
||||
|
||||
/**
|
||||
* @dev Batch mint is restricted to the constructor.
|
||||
* Any batch mint not emitting the {IERC721-Transfer} event outside of the constructor
|
||||
* is non ERC-721 compliant.
|
||||
*/
|
||||
error ERC721ForbiddenBatchMint();
|
||||
|
||||
/**
|
||||
* @dev Exceeds the max amount of mints per batch.
|
||||
*/
|
||||
error ERC721ExceededMaxBatchMint(uint256 batchSize, uint256 maxBatch);
|
||||
|
||||
/**
|
||||
* @dev Individual minting is not allowed.
|
||||
*/
|
||||
error ERC721ForbiddenMint();
|
||||
|
||||
/**
|
||||
* @dev Batch burn is not supported.
|
||||
*/
|
||||
error ERC721ForbiddenBatchBurn();
|
||||
|
||||
/**
|
||||
* @dev Maximum size of a batch of consecutive tokens. This is designed to limit stress on off-chain indexing
|
||||
* services that have to record one entry per token, and have protections against "unreasonably large" batches of
|
||||
* tokens.
|
||||
*
|
||||
* NOTE: Overriding the default value of 5000 will not cause on-chain issues, but may result in the asset not being
|
||||
* correctly supported by off-chain indexing services (including marketplaces).
|
||||
*/
|
||||
function _maxBatchSize() internal view virtual returns (uint96) {
|
||||
return 5000;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {ERC721-_ownerOf}. Override that checks the sequential ownership structure for tokens that have
|
||||
* been minted as part of a batch, and not yet transferred.
|
||||
*/
|
||||
function _ownerOf(uint256 tokenId) internal view virtual override returns (address) {
|
||||
address owner = super._ownerOf(tokenId);
|
||||
|
||||
// If token is owned by the core, or beyond consecutive range, return base value
|
||||
if (owner != address(0) || tokenId > type(uint96).max || tokenId < _firstConsecutiveId()) {
|
||||
return owner;
|
||||
}
|
||||
|
||||
// Otherwise, check the token was not burned, and fetch ownership from the anchors
|
||||
// Note: no need for safe cast, we know that tokenId <= type(uint96).max
|
||||
return _sequentialBurn.get(tokenId) ? address(0) : address(_sequentialOwnership.lowerLookup(uint96(tokenId)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Mint a batch of tokens of length `batchSize` for `to`. Returns the token id of the first token minted in the
|
||||
* batch; if `batchSize` is 0, returns the number of consecutive ids minted so far.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - `batchSize` must not be greater than {_maxBatchSize}.
|
||||
* - The function is called in the constructor of the contract (directly or indirectly).
|
||||
*
|
||||
* CAUTION: Does not emit a `Transfer` event. This is ERC-721 compliant as long as it is done inside of the
|
||||
* constructor, which is enforced by this function.
|
||||
*
|
||||
* CAUTION: Does not invoke `onERC721Received` on the receiver.
|
||||
*
|
||||
* Emits a {IERC2309-ConsecutiveTransfer} event.
|
||||
*/
|
||||
function _mintConsecutive(address to, uint96 batchSize) internal virtual returns (uint96) {
|
||||
uint96 next = _nextConsecutiveId();
|
||||
|
||||
// minting a batch of size 0 is a no-op
|
||||
if (batchSize > 0) {
|
||||
if (address(this).code.length > 0) {
|
||||
revert ERC721ForbiddenBatchMint();
|
||||
}
|
||||
if (to == address(0)) {
|
||||
revert ERC721InvalidReceiver(address(0));
|
||||
}
|
||||
|
||||
uint256 maxBatchSize = _maxBatchSize();
|
||||
if (batchSize > maxBatchSize) {
|
||||
revert ERC721ExceededMaxBatchMint(batchSize, maxBatchSize);
|
||||
}
|
||||
|
||||
// push an ownership checkpoint & emit event
|
||||
uint96 last = next + batchSize - 1;
|
||||
_sequentialOwnership.push(last, uint160(to));
|
||||
|
||||
// The invariant required by this function is preserved because the new sequentialOwnership checkpoint
|
||||
// is attributing ownership of `batchSize` new tokens to account `to`.
|
||||
_increaseBalance(to, batchSize);
|
||||
|
||||
emit ConsecutiveTransfer(next, last, address(0), to);
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {ERC721-_update}. Override version that restricts normal minting to after construction.
|
||||
*
|
||||
* WARNING: Using {ERC721Consecutive} prevents minting during construction in favor of {_mintConsecutive}.
|
||||
* After construction, {_mintConsecutive} is no longer available and minting through {_update} becomes available.
|
||||
*/
|
||||
function _update(address to, uint256 tokenId, address auth) internal virtual override returns (address) {
|
||||
address previousOwner = super._update(to, tokenId, auth);
|
||||
|
||||
// only mint after construction
|
||||
if (previousOwner == address(0) && address(this).code.length == 0) {
|
||||
revert ERC721ForbiddenMint();
|
||||
}
|
||||
|
||||
// record burn
|
||||
if (
|
||||
to == address(0) && // if we burn
|
||||
tokenId < _nextConsecutiveId() && // and the tokenId was minted in a batch
|
||||
!_sequentialBurn.get(tokenId) // and the token was never marked as burnt
|
||||
) {
|
||||
_sequentialBurn.set(tokenId);
|
||||
}
|
||||
|
||||
return previousOwner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Used to offset the first token id in {_nextConsecutiveId}
|
||||
*/
|
||||
function _firstConsecutiveId() internal view virtual returns (uint96) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the next tokenId to mint using {_mintConsecutive}. It will return {_firstConsecutiveId}
|
||||
* if no consecutive tokenId has been minted before.
|
||||
*/
|
||||
function _nextConsecutiveId() private view returns (uint96) {
|
||||
(bool exists, uint96 latestId, ) = _sequentialOwnership.latestCheckpoint();
|
||||
return exists ? latestId + 1 : _firstConsecutiveId();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/ERC721Enumerable.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC721} from "../ERC721.sol";
|
||||
import {IERC721Enumerable} from "./IERC721Enumerable.sol";
|
||||
import {IERC165} from "../../../utils/introspection/ERC165.sol";
|
||||
|
||||
/**
|
||||
* @dev This implements an optional extension of {ERC721} defined in the ERC that adds enumerability
|
||||
* of all the token ids in the contract as well as all token ids owned by each account.
|
||||
*
|
||||
* CAUTION: {ERC721} extensions that implement custom `balanceOf` logic, such as {ERC721Consecutive},
|
||||
* interfere with enumerability and should not be used together with {ERC721Enumerable}.
|
||||
*/
|
||||
abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
|
||||
mapping(address owner => mapping(uint256 index => uint256)) private _ownedTokens;
|
||||
mapping(uint256 tokenId => uint256) private _ownedTokensIndex;
|
||||
|
||||
uint256[] private _allTokens;
|
||||
mapping(uint256 tokenId => uint256) private _allTokensIndex;
|
||||
|
||||
/**
|
||||
* @dev An `owner`'s token query was out of bounds for `index`.
|
||||
*
|
||||
* NOTE: The owner being `address(0)` indicates a global out of bounds index.
|
||||
*/
|
||||
error ERC721OutOfBoundsIndex(address owner, uint256 index);
|
||||
|
||||
/**
|
||||
* @dev Batch mint is not allowed.
|
||||
*/
|
||||
error ERC721EnumerableForbiddenBatchMint();
|
||||
|
||||
/**
|
||||
* @dev See {IERC165-supportsInterface}.
|
||||
*/
|
||||
function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC721) returns (bool) {
|
||||
return interfaceId == type(IERC721Enumerable).interfaceId || super.supportsInterface(interfaceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
|
||||
*/
|
||||
function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual returns (uint256) {
|
||||
if (index >= balanceOf(owner)) {
|
||||
revert ERC721OutOfBoundsIndex(owner, index);
|
||||
}
|
||||
return _ownedTokens[owner][index];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC721Enumerable-totalSupply}.
|
||||
*/
|
||||
function totalSupply() public view virtual returns (uint256) {
|
||||
return _allTokens.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC721Enumerable-tokenByIndex}.
|
||||
*/
|
||||
function tokenByIndex(uint256 index) public view virtual returns (uint256) {
|
||||
if (index >= totalSupply()) {
|
||||
revert ERC721OutOfBoundsIndex(address(0), index);
|
||||
}
|
||||
return _allTokens[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {ERC721-_update}.
|
||||
*/
|
||||
function _update(address to, uint256 tokenId, address auth) internal virtual override returns (address) {
|
||||
address previousOwner = super._update(to, tokenId, auth);
|
||||
|
||||
if (previousOwner == address(0)) {
|
||||
_addTokenToAllTokensEnumeration(tokenId);
|
||||
} else if (previousOwner != to) {
|
||||
_removeTokenFromOwnerEnumeration(previousOwner, tokenId);
|
||||
}
|
||||
if (to == address(0)) {
|
||||
_removeTokenFromAllTokensEnumeration(tokenId);
|
||||
} else if (previousOwner != to) {
|
||||
_addTokenToOwnerEnumeration(to, tokenId);
|
||||
}
|
||||
|
||||
return previousOwner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Private function to add a token to this extension's ownership-tracking data structures.
|
||||
* @param to address representing the new owner of the given token ID
|
||||
* @param tokenId uint256 ID of the token to be added to the tokens list of the given address
|
||||
*/
|
||||
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
|
||||
uint256 length = balanceOf(to) - 1;
|
||||
_ownedTokens[to][length] = tokenId;
|
||||
_ownedTokensIndex[tokenId] = length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Private function to add a token to this extension's token tracking data structures.
|
||||
* @param tokenId uint256 ID of the token to be added to the tokens list
|
||||
*/
|
||||
function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
|
||||
_allTokensIndex[tokenId] = _allTokens.length;
|
||||
_allTokens.push(tokenId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that
|
||||
* while the token is not assigned a new owner, the `_ownedTokensIndex` mapping is _not_ updated: this allows for
|
||||
* gas optimizations e.g. when performing a transfer operation (avoiding double writes).
|
||||
* This has O(1) time complexity, but alters the order of the _ownedTokens array.
|
||||
* @param from address representing the previous owner of the given token ID
|
||||
* @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
|
||||
*/
|
||||
function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
|
||||
// To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and
|
||||
// then delete the last slot (swap and pop).
|
||||
|
||||
uint256 lastTokenIndex = balanceOf(from);
|
||||
uint256 tokenIndex = _ownedTokensIndex[tokenId];
|
||||
|
||||
mapping(uint256 index => uint256) storage _ownedTokensByOwner = _ownedTokens[from];
|
||||
|
||||
// When the token to delete is the last token, the swap operation is unnecessary
|
||||
if (tokenIndex != lastTokenIndex) {
|
||||
uint256 lastTokenId = _ownedTokensByOwner[lastTokenIndex];
|
||||
|
||||
_ownedTokensByOwner[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 _ownedTokensByOwner[lastTokenIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Private function to remove a token from this extension's token tracking data structures.
|
||||
* This has O(1) time complexity, but alters the order of the _allTokens array.
|
||||
* @param tokenId uint256 ID of the token to be removed from the tokens list
|
||||
*/
|
||||
function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
|
||||
// To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and
|
||||
// then delete the last slot (swap and pop).
|
||||
|
||||
uint256 lastTokenIndex = _allTokens.length - 1;
|
||||
uint256 tokenIndex = _allTokensIndex[tokenId];
|
||||
|
||||
// When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so
|
||||
// rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding
|
||||
// an 'if' statement (like in _removeTokenFromOwnerEnumeration)
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* See {ERC721-_increaseBalance}. We need that to account tokens that were minted in batch
|
||||
*/
|
||||
function _increaseBalance(address account, uint128 amount) internal virtual override {
|
||||
if (amount > 0) {
|
||||
revert ERC721EnumerableForbiddenBatchMint();
|
||||
}
|
||||
super._increaseBalance(account, amount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/ERC721Pausable.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC721} from "../ERC721.sol";
|
||||
import {Pausable} from "../../../utils/Pausable.sol";
|
||||
|
||||
/**
|
||||
* @dev ERC-721 token with pausable token transfers, minting and burning.
|
||||
*
|
||||
* Useful for scenarios such as preventing trades until the end of an evaluation
|
||||
* period, or having an emergency switch for freezing all token transfers in the
|
||||
* event of a large bug.
|
||||
*
|
||||
* IMPORTANT: This contract does not include public pause and unpause functions. In
|
||||
* addition to inheriting this contract, you must define both functions, invoking the
|
||||
* {Pausable-_pause} and {Pausable-_unpause} internal functions, with appropriate
|
||||
* access control, e.g. using {AccessControl} or {Ownable}. Not doing so will
|
||||
* make the contract pause mechanism of the contract unreachable, and thus unusable.
|
||||
*/
|
||||
abstract contract ERC721Pausable is ERC721, Pausable {
|
||||
/**
|
||||
* @dev See {ERC721-_update}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - the contract must not be paused.
|
||||
*/
|
||||
function _update(
|
||||
address to,
|
||||
uint256 tokenId,
|
||||
address auth
|
||||
) internal virtual override whenNotPaused returns (address) {
|
||||
return super._update(to, tokenId, auth);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/ERC721Royalty.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC721} from "../ERC721.sol";
|
||||
import {ERC2981} from "../../common/ERC2981.sol";
|
||||
|
||||
/**
|
||||
* @dev Extension of ERC-721 with the ERC-2981 NFT Royalty Standard, a standardized way to retrieve royalty payment
|
||||
* information.
|
||||
*
|
||||
* Royalty information can be specified globally for all token ids via {ERC2981-_setDefaultRoyalty}, and/or individually
|
||||
* for specific token ids via {ERC2981-_setTokenRoyalty}. The latter takes precedence over the first.
|
||||
*
|
||||
* IMPORTANT: ERC-2981 only specifies a way to signal royalty information and does not enforce its payment. See
|
||||
* https://eips.ethereum.org/EIPS/eip-2981#optional-royalty-payments[Rationale] in the ERC. Marketplaces are expected to
|
||||
* voluntarily pay royalties together with sales, but note that this standard is not yet widely supported.
|
||||
*/
|
||||
abstract contract ERC721Royalty is ERC2981, ERC721 {
|
||||
/**
|
||||
* @dev See {IERC165-supportsInterface}.
|
||||
*/
|
||||
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, ERC2981) returns (bool) {
|
||||
return super.supportsInterface(interfaceId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/ERC721URIStorage.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC721} from "../ERC721.sol";
|
||||
import {Strings} from "../../../utils/Strings.sol";
|
||||
import {IERC4906} from "../../../interfaces/IERC4906.sol";
|
||||
import {IERC165} from "../../../interfaces/IERC165.sol";
|
||||
|
||||
/**
|
||||
* @dev ERC-721 token with storage based token URI management.
|
||||
*/
|
||||
abstract contract ERC721URIStorage is IERC4906, ERC721 {
|
||||
using Strings for uint256;
|
||||
|
||||
// Interface ID as defined in ERC-4906. This does not correspond to a traditional interface ID as ERC-4906 only
|
||||
// defines events and does not include any external function.
|
||||
bytes4 private constant ERC4906_INTERFACE_ID = bytes4(0x49064906);
|
||||
|
||||
// Optional mapping for token URIs
|
||||
mapping(uint256 tokenId => string) private _tokenURIs;
|
||||
|
||||
/**
|
||||
* @dev See {IERC165-supportsInterface}
|
||||
*/
|
||||
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, IERC165) returns (bool) {
|
||||
return interfaceId == ERC4906_INTERFACE_ID || super.supportsInterface(interfaceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC721Metadata-tokenURI}.
|
||||
*/
|
||||
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
|
||||
_requireOwned(tokenId);
|
||||
|
||||
string memory _tokenURI = _tokenURIs[tokenId];
|
||||
string memory base = _baseURI();
|
||||
|
||||
// If there is no base URI, return the token URI.
|
||||
if (bytes(base).length == 0) {
|
||||
return _tokenURI;
|
||||
}
|
||||
// If both are set, concatenate the baseURI and tokenURI (via string.concat).
|
||||
if (bytes(_tokenURI).length > 0) {
|
||||
return string.concat(base, _tokenURI);
|
||||
}
|
||||
|
||||
return super.tokenURI(tokenId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
|
||||
*
|
||||
* Emits {MetadataUpdate}.
|
||||
*/
|
||||
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
|
||||
_tokenURIs[tokenId] = _tokenURI;
|
||||
emit MetadataUpdate(tokenId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/ERC721Votes.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC721} from "../ERC721.sol";
|
||||
import {Votes} from "../../../governance/utils/Votes.sol";
|
||||
|
||||
/**
|
||||
* @dev Extension of ERC-721 to support voting and delegation as implemented by {Votes}, where each individual NFT counts
|
||||
* as 1 vote unit.
|
||||
*
|
||||
* Tokens do not count as votes until they are delegated, because votes must be tracked which incurs an additional cost
|
||||
* on every transfer. Token holders can either delegate to a trusted representative who will decide how to make use of
|
||||
* the votes in governance decisions, or they can delegate to themselves to be their own representative.
|
||||
*/
|
||||
abstract contract ERC721Votes is ERC721, Votes {
|
||||
/**
|
||||
* @dev See {ERC721-_update}. Adjusts votes when tokens are transferred.
|
||||
*
|
||||
* Emits a {IVotes-DelegateVotesChanged} event.
|
||||
*/
|
||||
function _update(address to, uint256 tokenId, address auth) internal virtual override returns (address) {
|
||||
address previousOwner = super._update(to, tokenId, auth);
|
||||
|
||||
_transferVotingUnits(previousOwner, to, 1);
|
||||
|
||||
return previousOwner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the balance of `account`.
|
||||
*
|
||||
* WARNING: Overriding this function will likely result in incorrect vote tracking.
|
||||
*/
|
||||
function _getVotingUnits(address account) internal view virtual override returns (uint256) {
|
||||
return balanceOf(account);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {ERC721-_increaseBalance}. We need that to account tokens that were minted in batch.
|
||||
*/
|
||||
function _increaseBalance(address account, uint128 amount) internal virtual override {
|
||||
super._increaseBalance(account, amount);
|
||||
_transferVotingUnits(address(0), account, amount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/ERC721Wrapper.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC721, ERC721} from "../ERC721.sol";
|
||||
import {IERC721Receiver} from "../IERC721Receiver.sol";
|
||||
|
||||
/**
|
||||
* @dev Extension of the ERC-721 token contract to support token wrapping.
|
||||
*
|
||||
* Users can deposit and withdraw an "underlying token" and receive a "wrapped token" with a matching tokenId. This is
|
||||
* useful in conjunction with other modules. For example, combining this wrapping mechanism with {ERC721Votes} will allow
|
||||
* the wrapping of an existing "basic" ERC-721 into a governance token.
|
||||
*/
|
||||
abstract contract ERC721Wrapper is ERC721, IERC721Receiver {
|
||||
IERC721 private immutable _underlying;
|
||||
|
||||
/**
|
||||
* @dev The received ERC-721 token couldn't be wrapped.
|
||||
*/
|
||||
error ERC721UnsupportedToken(address token);
|
||||
|
||||
constructor(IERC721 underlyingToken) {
|
||||
_underlying = underlyingToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Allow a user to deposit underlying tokens and mint the corresponding tokenIds.
|
||||
*/
|
||||
function depositFor(address account, uint256[] memory tokenIds) public virtual returns (bool) {
|
||||
uint256 length = tokenIds.length;
|
||||
for (uint256 i = 0; i < length; ++i) {
|
||||
uint256 tokenId = tokenIds[i];
|
||||
|
||||
// This is an "unsafe" transfer that doesn't call any hook on the receiver. With underlying() being trusted
|
||||
// (by design of this contract) and no other contracts expected to be called from there, we are safe.
|
||||
// slither-disable-next-line reentrancy-no-eth
|
||||
underlying().transferFrom(_msgSender(), address(this), tokenId);
|
||||
_safeMint(account, tokenId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Allow a user to burn wrapped tokens and withdraw the corresponding tokenIds of the underlying tokens.
|
||||
*/
|
||||
function withdrawTo(address account, uint256[] memory tokenIds) public virtual returns (bool) {
|
||||
uint256 length = tokenIds.length;
|
||||
for (uint256 i = 0; i < length; ++i) {
|
||||
uint256 tokenId = tokenIds[i];
|
||||
// Setting an "auth" arguments enables the `_isAuthorized` check which verifies that the token exists
|
||||
// (from != 0). Therefore, it is not needed to verify that the return value is not 0 here.
|
||||
_update(address(0), tokenId, _msgSender());
|
||||
// Checks were already performed at this point, and there's no way to retake ownership or approval from
|
||||
// the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line.
|
||||
// slither-disable-next-line reentrancy-no-eth
|
||||
underlying().safeTransferFrom(address(this), account, tokenId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Overrides {IERC721Receiver-onERC721Received} to allow minting on direct ERC-721 transfers to
|
||||
* this contract.
|
||||
*
|
||||
* In case there's data attached, it validates that the operator is this contract, so only trusted data
|
||||
* is accepted from {depositFor}.
|
||||
*
|
||||
* WARNING: Doesn't work with unsafe transfers (eg. {IERC721-transferFrom}). Use {ERC721Wrapper-_recover}
|
||||
* for recovering in that scenario.
|
||||
*/
|
||||
function onERC721Received(address, address from, uint256 tokenId, bytes memory) public virtual returns (bytes4) {
|
||||
if (address(underlying()) != _msgSender()) {
|
||||
revert ERC721UnsupportedToken(_msgSender());
|
||||
}
|
||||
_safeMint(from, tokenId);
|
||||
return IERC721Receiver.onERC721Received.selector;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Mint a wrapped token to cover any underlyingToken that would have been transferred by mistake. Internal
|
||||
* function that can be exposed with access control if desired.
|
||||
*/
|
||||
function _recover(address account, uint256 tokenId) internal virtual returns (uint256) {
|
||||
address owner = underlying().ownerOf(tokenId);
|
||||
if (owner != address(this)) {
|
||||
revert ERC721IncorrectOwner(address(this), tokenId, owner);
|
||||
}
|
||||
_safeMint(account, tokenId);
|
||||
return tokenId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the underlying token.
|
||||
*/
|
||||
function underlying() public view virtual returns (IERC721) {
|
||||
return _underlying;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/IERC721Enumerable.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC721} from "../IERC721.sol";
|
||||
|
||||
/**
|
||||
* @title ERC-721 Non-Fungible Token Standard, optional enumeration extension
|
||||
* @dev See https://eips.ethereum.org/EIPS/eip-721
|
||||
*/
|
||||
interface IERC721Enumerable is IERC721 {
|
||||
/**
|
||||
* @dev Returns the total amount of tokens stored by the contract.
|
||||
*/
|
||||
function totalSupply() external view returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev Returns a token ID owned by `owner` at a given `index` of its token list.
|
||||
* Use along with {balanceOf} to enumerate all of ``owner``'s tokens.
|
||||
*/
|
||||
function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev Returns a token ID at a given `index` of all the tokens stored by the contract.
|
||||
* Use along with {totalSupply} to enumerate all tokens.
|
||||
*/
|
||||
function tokenByIndex(uint256 index) external view returns (uint256);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/extensions/IERC721Metadata.sol)
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC721} from "../IERC721.sol";
|
||||
|
||||
/**
|
||||
* @title ERC-721 Non-Fungible Token Standard, optional metadata extension
|
||||
* @dev See https://eips.ethereum.org/EIPS/eip-721
|
||||
*/
|
||||
interface IERC721Metadata is IERC721 {
|
||||
/**
|
||||
* @dev Returns the token collection name.
|
||||
*/
|
||||
function name() external view returns (string memory);
|
||||
|
||||
/**
|
||||
* @dev Returns the token collection symbol.
|
||||
*/
|
||||
function symbol() external view returns (string memory);
|
||||
|
||||
/**
|
||||
* @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
|
||||
*/
|
||||
function tokenURI(uint256 tokenId) external view returns (string memory);
|
||||
}
|
||||
Reference in New Issue
Block a user