Add default implementation for metadata URI

This allows token implementation to be non-abstract
This commit is contained in:
Santiago Palladino
2018-03-09 13:25:45 -03:00
parent 54a1d2eacc
commit 851685c40e
3 changed files with 54 additions and 27 deletions

View File

@ -30,6 +30,9 @@ contract ERC721Token is ERC721, ERC721BasicToken {
// Mapping from token id to position in the allTokens array
mapping(uint256 => uint256) internal allTokensIndex;
// Optional mapping for token URIs
mapping(uint256 => string) internal tokenURIs;
/**
* @dev Constructor function
*/
@ -63,6 +66,27 @@ contract ERC721Token is ERC721, ERC721BasicToken {
return symbol_;
}
/**
* @dev Returns an URI for a given token ID
* @dev Throws if the token ID does not exist. May return an empty string.
* @param _tokenId uint256 ID of the token to query
*/
function tokenURI(uint256 _tokenId) public view returns (string) {
require(exists(_tokenId));
return tokenURIs[_tokenId];
}
/**
* @dev Internal function to set the token URI for a given token
* @dev Reverts if the token ID does not exist
* @param _tokenId uint256 ID of the token to set its URI
* @param _uri string URI to assign
*/
function doSetTokenURI(uint256 _tokenId, string _uri) internal {
require(exists(_tokenId));
tokenURIs[_tokenId] = _uri;
}
/**
* @dev Gets the token ID at a given index of the tokens list of the requested owner
* @param _owner address owning the tokens list to be accessed
@ -152,6 +176,12 @@ contract ERC721Token is ERC721, ERC721BasicToken {
function doBurn(address _owner, uint256 _tokenId) internal {
super.doBurn(_owner, _tokenId);
// Clear metadata (if any)
if (bytes(tokenURIs[_tokenId]).length == 0) {
delete tokenURIs[_tokenId];
}
// Reorg all tokens array
uint256 tokenIndex = allTokensIndex[_tokenId];
uint256 lastTokenIndex = allTokens.length.sub(1);
uint256 lastToken = allTokens[lastTokenIndex];