Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AINFT contract template #1

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

245 changes: 245 additions & 0 deletions contracts/AINFT721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

import "@openzeppelin/contracts/utils/Strings.sol";

import "./interfaces/IERC4906.sol";
import "./interfaces/IAINFT.sol";

/**
*@dev AINFT721 contract
*/
contract AINFT721 is
ERC721Enumerable,
Pausable,
AccessControl,
ERC721Burnable,
IERC4906,
IAINFT
{
using Counters for Counters.Counter;
using Strings for uint256;

struct MetadataContainer {
address updater;
string metadataURI;
}

bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

Counters.Counter private _tokenIdCounter;
string private baseURI;
mapping(bytes32 => MetadataContainer) private _metadataStorage; // keccak256(bytes(tokenId, <DELIMETER>, version)) => MetadataContainer
mapping(uint256 => uint256) private _tokenURICurrentVersion; // tokenId => tokenURIVersion

constructor() ERC721("Name", "SYMBOL") {
jakepyo marked this conversation as resolved.
Show resolved Hide resolved
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_grantRole(PAUSER_ROLE, msg.sender);
_grantRole(MINTER_ROLE, msg.sender);
}

/**
* @dev See {IERC4906}.
*/
function supportsInterface(
bytes4 interfaceId
)
public
view
override(
AccessControl,
minho-comcom-ai marked this conversation as resolved.
Show resolved Hide resolved
ERC721Enumerable,
ERC721,
IERC165
)
returns (bool)
{
// IERC4906 interface added
return interfaceId == bytes4(0x49064906) || super.supportsInterface(interfaceId);
jakepyo marked this conversation as resolved.
Show resolved Hide resolved
}

function pause() public onlyRole(PAUSER_ROLE) {
_pause();
}

function unpause() public onlyRole(PAUSER_ROLE) {
_unpause();
}

function safeMint(
address to
)
public
onlyRole(MINTER_ROLE)
{
uint256 tokenId = _tokenIdCounter.current();
_tokenIdCounter.increment();
_safeMint(to, tokenId);
}

function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId,
uint256 batchSize
)
internal
override(ERC721, ERC721Enumerable)
whenNotPaused
{
super._beforeTokenTransfer(from, to, tokenId, batchSize);
}

/**
* @dev Reverts if the `tokenId` has not been minted yet.
*/
function _requireMinted(uint256 tokenId) internal view virtual override(ERC721) {
super._requireMinted(tokenId);
}

function _isApprovedOrOwner(
address spender,
uint256 tokenId
) internal view virtual override(ERC721) returns (bool)
{
return super._isApprovedOrOwner(spender, tokenId);
}

function burn(uint256 tokenId) public override(ERC721Burnable) {
super.burn(tokenId);
}

////
// URI & METADATA RELATED FUNCTIONS
////

function _baseURI() internal view override(ERC721) returns (string memory) {
return baseURI;
}

function tokenURI(
uint256 tokenId
) public view override(ERC721) returns (string memory) {
_requireMinted(tokenId);
uint256 currentVersion = _tokenURICurrentVersion[tokenId];

return tokenURIByVersion(tokenId, currentVersion);
}

function _metadataStorageKey(
uint256 tokenId,
uint256 version
) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(tokenId.toString(), "AINFT delimeter", version));
}

function metadataStorageByVersion(
uint256 tokenId,
uint256 version
) public view returns (MetadataContainer memory) {

bytes32 key = _metadataStorageKey(tokenId, version);
return _metadataStorage[key];
}

function tokenURICurrentVersion(
uint256 tokenId
) public view returns (uint256) {
_requireMinted(tokenId);
return _tokenURICurrentVersion[tokenId];
jakepyo marked this conversation as resolved.
Show resolved Hide resolved
}

function tokenURIByVersion(
uint256 tokenId,
uint256 uriVersion
) public view returns (string memory) {
_requireMinted(tokenId);
if (uriVersion == 0) {
return string(abi.encodePacked(baseURI, tokenId.toString()));
} else {
MetadataContainer memory metadata = metadataStorageByVersion(tokenId, uriVersion);
return metadata.metadataURI;
}
}

function setBaseURI(
string memory newBaseURI
) public onlyRole(DEFAULT_ADMIN_ROLE) returns (bool) {
require(
bytes(newBaseURI).length > 0,
"AINFT721::setBaseURI() - Empty newBaseURI"
);
require(
keccak256(bytes(newBaseURI)) != keccak256(bytes(baseURI)),
"AINFT721::setBaseURI() - Same newBaseURI as baseURI"
);

baseURI = newBaseURI;
//TODO: emit BatchMetadataUpdate(start, end). Consider after updateTokenURI() calls.
return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should emit BatchMetadataUpdate(start, end) with counter's help?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered it before. However the setBaseURI() call occurs after the someone calls updateTokenURI(), the baseURI could be ignored when calling tokenURI(). I will leave it behind, with remaining TODO comment

Copy link

@minho-comcom-ai minho-comcom-ai Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MetadataUpdate / BatchMetadataUpdate is for the NFT marketplaces or other 3rd parties, so I think this should be emitted on here.
If not, we don't have to implement the ERC-4906 at first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of ERC721 contracts set the baseURI in initialization phase and no implementation for setBaseURI(). In the perspective of structuring the base template, setBaseURI() don't need to be necessary. However, the versioning of metadata is important concept of template and it'd better to put ERC4906 inside the template.

As a alternative way, I'm trying to add pre-condition check of setBaseURI(): if the number of having been called updateTokenURI() is equal or more than once, setBaseURI() cannot be executed. In this case, the BatchMetadataUpdate can be added.

What do you think of this way?

}

/**
* @dev version up & upload the metadata. You should call this function externally when the token is updated.
*/
function updateTokenURI(
uint256 tokenId,
string memory newTokenURI
) external returns (bool) {
require(
(_isApprovedOrOwner(_msgSender(), tokenId) ||
hasRole(DEFAULT_ADMIN_ROLE, _msgSender())),
"AINFT721::updateTokenURI() - not owner of tokenId or contract owner"
);
_requireMinted(tokenId);

uint256 updatedVersion = ++_tokenURICurrentVersion[tokenId];
bytes32 metadataKey = _metadataStorageKey(tokenId, updatedVersion);
_metadataStorage[metadataKey] = MetadataContainer({
updater: _msgSender(),
metadataURI: newTokenURI
});

emit MetadataUpdate(tokenId);
return true;
}

/**
* @dev if you've ever updated the metadata more than once, rollback the metadata to the previous one and return true.
* if its metadata has not been updated yet or failed to update, return false
*/
function rollbackTokenURI(uint256 tokenId) public returns (bool) {
require(
(_msgSender() == ownerOf(tokenId)) ||
hasRole(DEFAULT_ADMIN_ROLE, _msgSender()),
"AINFT721::rollbackTokenURI() - only contract owner or token holder can call this funciton."
);
_requireMinted(tokenId);

uint256 currentVersion = _tokenURICurrentVersion[tokenId];
if (currentVersion == 0) return false;
else {
//delete the currentVersion of _metadataStorage
bytes32 currentMetadataKey = _metadataStorageKey(
tokenId,
currentVersion
);
delete _metadataStorage[currentMetadataKey];

//rollback the version
_tokenURICurrentVersion[tokenId]--;
emit MetadataUpdate(tokenId);
return true;
}
}

////
////
}
34 changes: 34 additions & 0 deletions contracts/AINFT721LogicV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "./AINFT721Upgradeable.sol";

/**@notice this contract is only for example of how the logic contract works & interacts with proxy
* Please DO NOT USE this in production as-is.
*/
contract AINFT721LogicV2 is AINFT721Upgradeable {
jakepyo marked this conversation as resolved.
Show resolved Hide resolved
/**
* @dev execute the additional function updated to proxy contract.
*/
function example__resetTokenURI(uint256 tokenId) external returns (bool) {
require(
(_isApprovedOrOwner(_msgSender(), tokenId) ||
hasRole(DEFAULT_ADMIN_ROLE, _msgSender())),
"AINFT721LogicV2::example__resetTokenURI() - only contract owner or token holder can call this funciton."
);
bool ret = false;
uint256 currentVersion = tokenURICurrentVersion(tokenId);
for (uint256 i = currentVersion; i > 0; i--) {
bool success = rollbackTokenURI(tokenId);
ret = ret || success;
}
return ret;
}

/**
* @dev get the logic contract version
*/
function logicVersion() external pure virtual override returns (uint256) {
return 2;
}
}
Loading