Jumping into Solidity — The ERC721 Standard (Part 6)

“A man welding a steel sheet in a workshop” by Uğur Gürcüoğlu on Unsplash

So what are these extensions?

The Metadata Extension

contract TokenERC721Metadata is TokenERC721, ERC721Metadata {
function name() external view returns (string _name);
function symbol() external view returns (string _symbol);
function tokenURI(uint256 _tokenId) external view returns (string);
string private __name;
string private __symbol;
bytes private __uriBase;
constructor(uint _initialSupply, string _name, string _symbol, string _uriBase) public TokenERC721(_initialSupply){
__name = _name;
__symbol = _symbol;
__uriBase = bytes(_uriBase);

//Add to ERC165 Interface Check
supportedInterfaces[
this.name.selector ^
this.symbol.selector ^
this.tokenURI.selector
] = true;
}
function name() external view returns (string _name){
_name = __name;
}

function symbol() external view returns (string _symbol){
_symbol = __symbol;
}
http://www.someTokenImplementation.com/tokens/19
http://www.someTokenImplementation.com/tokens/
function tokenURI(uint256 _tokenId) external view returns (string){
require(isValidToken(_tokenId));

//prepare our tokenId's byte array
uint maxLength = 78;
bytes memory reversed = new bytes(maxLength);
uint i = 0;
//loop through and add byte values to the array
while (_tokenId != 0) {
uint remainder = _tokenId % 10;
_tokenId /= 10;
reversed[i++] = byte(48 + remainder);
}
//prepare the final array
bytes memory s = new bytes(__uriBase.length + i);
uint j;
//add the base to the final array
for (j = 0; j < __uriBase.length; j++) {
s[j] = __uriBase[j];
}
//add the tokenId to the final array
for (j = 0; j < i; j++) {
s[j + __uriBase.length] = reversed[i - 1 - j];
}
//turn it into a string and return it
return string(s);
}
{
"title": "Asset Metadata",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Identifies the asset to which this NFT represents",
},
"description": {
"type": "string",
"description": "Describes the asset to which this NFT represents",
},
"image": {
"type": "string",
"description": "A URI pointing to a resource with mime type image/* representing the asset to which this NFT represents. Consider making any images at a width between 320 and 1080 pixels and aspect ratio between 1.91:1 and 4:5 inclusive.",
}
}
}

Wrapping up

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store