Skip to content

Commit

Permalink
feat: solve convention issue and NAT spec
Browse files Browse the repository at this point in the history
  • Loading branch information
huyhuynh3103 committed Apr 19, 2024
1 parent a7e5379 commit 84cc173
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 57 deletions.
68 changes: 35 additions & 33 deletions src/upgradeable/ERC721CommonUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,17 @@ abstract contract ERC721CommonUpgradeable is
error ErrInvalidArrayLength();
error ErrNonExistentToken();

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

/**
* @inheritdoc IERC721State
*/
function stateOf(uint256 _tokenId) external view virtual override returns (bytes memory) {
if (!_exists(_tokenId)) revert ErrNonExistentToken();
return abi.encodePacked(ownerOf(_tokenId), nonces[_tokenId], _tokenId);
}

/**
* @dev Override `ERC721Upgradeable-_baseURI`.
*/
function _baseURI()
internal
view
virtual
override(ERC721Upgradeable, ERC721PresetMinterPauserAutoIdCustomizedUpgradeable)
returns (string memory)
{
return super._baseURI();
function stateOf(uint256 tokenId) external view virtual override returns (bytes memory) {
if (!_exists(tokenId)) revert ErrNonExistentToken();
return abi.encodePacked(ownerOf(tokenId), nonces(tokenId), tokenId);
}

/**
Expand All @@ -51,17 +39,6 @@ abstract contract ERC721CommonUpgradeable is
return super.supportsInterface(interfaceId);
}

/**
* @dev Override `ERC721PresetMinterPauserAutoIdCustomizedUpgradeable-_beforeTokenTransfer`.
*/
function _beforeTokenTransfer(address _from, address _to, uint256 _firstTokenId, uint256 _batchSize)
internal
virtual
override(ERC721NonceUpgradeable, ERC721PresetMinterPauserAutoIdCustomizedUpgradeable)
{
super._beforeTokenTransfer(_from, _to, _firstTokenId, _batchSize);
}

/**
* @dev Bulk create new tokens for `_recipients`. Tokens ID will be automatically
* assigned (and available on the emitted {IERC721Upgradeable-Transfer} event), and the token
Expand All @@ -73,17 +50,42 @@ abstract contract ERC721CommonUpgradeable is
*
* - the caller must have the `MINTER_ROLE`.
*/
function bulkMint(address[] calldata _recipients)
function bulkMint(address[] calldata recipients)
external
virtual
onlyRole(MINTER_ROLE)
returns (uint256[] memory _tokenIds)
returns (uint256[] memory tokenIds)
{
if (_recipients.length == 0) revert ErrInvalidArrayLength();
_tokenIds = new uint256[](_recipients.length);
uint256 length = recipients.length;
if (length == 0) revert ErrInvalidArrayLength();
tokenIds = new uint256[](length);

for (uint256 _i = 0; _i < _recipients.length; _i++) {
_tokenIds[_i] = _mintFor(_recipients[_i]);
for (uint256 i; i < length; ++i) {
tokenIds[i] = _mintFor(recipients[i]);
}
}

/**
* @dev Override `ERC721Upgradeable-_baseURI`.
*/
function _baseURI()
internal
view
virtual
override(ERC721Upgradeable, ERC721PresetMinterPauserAutoIdCustomizedUpgradeable)
returns (string memory)
{
return super._baseURI();
}

/**
* @dev Override `ERC721PresetMinterPauserAutoIdCustomizedUpgradeable-_beforeTokenTransfer`.
*/
function _beforeTokenTransfer(address from, address to, uint256 firstTokenId, uint256 batchSize)
internal
virtual
override(ERC721NonceUpgradeable, ERC721PresetMinterPauserAutoIdCustomizedUpgradeable)
{
super._beforeTokenTransfer(from, to, firstTokenId, batchSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,23 @@ contract ERC721PresetMinterPauserAutoIdCustomizedUpgradeable is

string private _baseTokenURI;

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
*/
uint256[50] private __gap;

function initialize(string memory name, string memory symbol, string memory baseTokenURI) public virtual initializer {
__ERC721PresetMinterPauserAutoId_init(name, symbol, baseTokenURI);
}

/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* Token URIs will be autogenerated based on `baseURI` and their token IDs.
* See {ERC721-tokenURI}.
*/

function __ERC721PresetMinterPauserAutoId_init(string memory name, string memory symbol, string memory baseTokenURI)
internal
onlyInitializing
Expand All @@ -81,10 +87,6 @@ contract ERC721PresetMinterPauserAutoIdCustomizedUpgradeable is
_setupRole(PAUSER_ROLE, _msgSender());
}

function _baseURI() internal view virtual override returns (string memory) {
return _baseTokenURI;
}

/**
* @dev Creates a new token for `to`. Its token ID will be automatically
* assigned (and available on the emitted {IERC721Upgradeable-Transfer} event), and the token
Expand Down Expand Up @@ -135,14 +137,6 @@ contract ERC721PresetMinterPauserAutoIdCustomizedUpgradeable is
_unpause();
}

function _beforeTokenTransfer(address from, address to, uint256 firstTokenId, uint256 batchSize)
internal
virtual
override(ERC721Upgradeable, ERC721EnumerableUpgradeable, ERC721PausableUpgradeable)
{
super._beforeTokenTransfer(from, to, firstTokenId, batchSize);
}

/**
* @dev See {IERC165-supportsInterface}.
*/
Expand All @@ -156,6 +150,14 @@ contract ERC721PresetMinterPauserAutoIdCustomizedUpgradeable is
return super.supportsInterface(interfaceId);
}

function _beforeTokenTransfer(address from, address to, uint256 firstTokenId, uint256 batchSize)
internal
virtual
override(ERC721Upgradeable, ERC721EnumerableUpgradeable, ERC721PausableUpgradeable)
{
super._beforeTokenTransfer(from, to, firstTokenId, batchSize);
}

/**
* @dev Helper function to mint for address `to`.
*
Expand All @@ -171,9 +173,12 @@ contract ERC721PresetMinterPauserAutoIdCustomizedUpgradeable is
}

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
* @dev Helper function to mint for address `to`.
*
* See {ERC721Upgradeable-_mint}.
*
*/
uint256[48] private __gap;
function _baseURI() internal view virtual override returns (string memory) {
return _baseTokenURI;
}
}
18 changes: 11 additions & 7 deletions src/upgradeable/refs/ERC721NonceUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,32 @@ import "../../../lib/openzeppelin-contracts-upgradeable/contracts/token/ERC721/E
*/
abstract contract ERC721NonceUpgradeable is ERC721Upgradeable {
/// @dev Emitted when the token nonce is updated
event NonceUpdated(uint256 indexed _tokenId, uint256 indexed _nonce);
event NonceUpdated(uint256 indexed tokenId, uint256 indexed nonce);

/// @dev Mapping from token id => token nonce
mapping(uint256 => uint256) public nonces;
/// @dev Mapping from token id => token nonce.
mapping(uint256 => uint256) private _nonceOf;

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
*/
uint256[50] private ______gap;

function nonces(uint256 tokenId) public view returns (uint256) {
return _nonceOf[tokenId];
}

/**
* @dev Override `ERC721Upgradeable-_beforeTokenTransfer`.
*/
function _beforeTokenTransfer(address _from, address _to, uint256 _firstTokenId, uint256 _batchSize)
function _beforeTokenTransfer(address from, address to, uint256 firstTokenId, uint256 batchSize)
internal
virtual
override
{
for (uint256 _tokenId = _firstTokenId; _tokenId < _firstTokenId + _batchSize; _tokenId++) {
emit NonceUpdated(_tokenId, ++nonces[_tokenId]);
for (uint256 tokenId = firstTokenId; tokenId < firstTokenId + batchSize; tokenId++) {
emit NonceUpdated(tokenId, ++_nonceOf[tokenId]);
}
super._beforeTokenTransfer(_from, _to, _firstTokenId, _batchSize);
super._beforeTokenTransfer(from, to, firstTokenId, batchSize);
}
}

0 comments on commit 84cc173

Please sign in to comment.