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

269 changes: 269 additions & 0 deletions contracts/AINFT721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
// 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");
bytes32 public constant UPGRADER_ROLE = keccak256("UPGRADER_ROLE");
minho-comcom-ai marked this conversation as resolved.
Show resolved Hide resolved
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);
}

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)
{
return super.supportsInterface(interfaceId);
}

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);
}

// The following functions are overrides required by Solidity.
function _burn(uint256 tokenId) internal override(ERC721) {
super._burn(tokenId);
}

////
// URI VIEW FUNCTIONS
////

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

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

function tokenURI(
uint256 tokenId
) public view override(ERC721) returns (string memory) {
_requireMinted(tokenId);
return getRecentTokenURI(tokenId);
}

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

function getMetadataStorage(
uint256 tokenId
) public view returns (MetadataContainer memory) {
//TODO
jakepyo marked this conversation as resolved.
Show resolved Hide resolved
uint256 currentVersion = _tokenURICurrentVersion[tokenId];
bytes32 key = _getMetadataStorageKey(tokenId, currentVersion);

return _metadataStorage[key];
}

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

function getOriginTokenURI(
uint256 tokenId
) public view override returns (string memory) {
return string(abi.encodePacked(_baseURI(), "/", tokenId.toString()));
minho-comcom-ai marked this conversation as resolved.
Show resolved Hide resolved
}

function getRecentTokenURI(
uint256 tokenId
) public view override returns (string memory) {
uint256 currentVersion = _tokenURICurrentVersion[tokenId];
if (currentVersion == 0) {
return getOriginTokenURI(tokenId);
} else {
bytes32 metadataKey = _getMetadataStorageKey(
tokenId,
currentVersion
);
return _metadataStorage[metadataKey].metadataURI;
}
}

function getCertainTokenURI(
jakepyo marked this conversation as resolved.
Show resolved Hide resolved
uint256 tokenId,
uint256 uriVersion
) public view override returns (string memory) {
if (uriVersion == 0) {
return getOriginTokenURI(tokenId);
} else {
bytes32 metadataKey = _getMetadataStorageKey(tokenId, uriVersion);
return _metadataStorage[metadataKey].metadataURI;
}
}

////
////

////
// UPDATE URI(METADATA) FUNCTIONS
////

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;
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(
(_msgSender() == ownerOf(tokenId)) ||
hasRole(DEFAULT_ADMIN_ROLE, _msgSender()),
"AINFT721::updateTokenURI() - not owner of tokenId or contract owner"
);

uint256 updatedVersion = ++_tokenURICurrentVersion[tokenId];
bytes32 metadataKey = _getMetadataStorageKey(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) internal returns (bool) {
require(
(_msgSender() == ownerOf(tokenId)) ||
hasRole(DEFAULT_ADMIN_ROLE, _msgSender()),
"AINFT721::rollbackTokenURI() - only contract owner or token holder can call this funciton."
);
uint256 currentVersion = _tokenURICurrentVersion[tokenId];
if (currentVersion == 0) return false;
else {
//delete the currentVersion of _metadataStorage
bytes32 currentMetadataKey = _getMetadataStorageKey(
tokenId,
currentVersion
);
delete _metadataStorage[currentMetadataKey];

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

function rollbackTokenURI(uint256 tokenId) external returns (bool) {
return _rollbackTokenURI(tokenId);
minho-comcom-ai marked this conversation as resolved.
Show resolved Hide resolved
}

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

import "./AINFT721Upgradeable.sol";

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(
(_msgSender() == ownerOf(tokenId)) ||
hasRole(DEFAULT_ADMIN_ROLE, _msgSender()),
"AINFT721LogicV2::example__resetTokenURI() - only contract owner or token holder can call this funciton."
);
bool ret = false;
uint256 currentVersion = getTokenURICurrentVersion(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