question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ERC721 "automatic" token URI

See original GitHub issue

🧐 Motivation Current ERC721 minting with token URI requires manually specifying the URI per token.

To reduce gas cost, the OpenSea example uses concatenation of a base token URI and the tokenID. (TradeableERC721Token.sol)

📝 Details

We want to provide an ERC721 extension ERC721DefaultURI with a base URI parameter, which overrides ERC721Metadata’s tokenURI() with the concatenation. To discuss the API please comment on the forum thread about this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:19 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
nachomazzaracommented, Oct 18, 2019

To put in here what we thought (omit non-changed code):

pragma solidity ^0.5.0;

import "../../GSN/Context.sol";
import "./ERC721.sol";
import "./IERC721Metadata.sol";
import "../../introspection/ERC165.sol";

contract ERC721Metadata is Context, ERC165, ERC721, IERC721Metadata {
    //...
    
    // Base Token URI
    string private _baseTokenURI;

    //...
    
    // **PROPOSED CHANGE**: 
    // - `external` to `public` (Allow 'super')
    // - Concat baseURI with tokenURI
    function tokenURI(uint256 tokenId) public view returns (string memory) { 
        return string(abi.encodePacked(_baseTokenURI, _tokenURI(tokenId)));
    }
    

    /**
     * @dev Internal returns an URI for a given token ID.
     * 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) internal view returns (string memory) { 
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
        return _tokenURIs[tokenId];
    }
    
    /**
     * @dev Internal function to set the base token URI.
     * @param uri string base URI to assign
     */
    function _setBaseTokenURI(string memory uri) internal {
        _baseTokenURI = uri;
    }
}

It continues following the _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f;.

The idea is not only to use the tokenId as the path for the URI as used here but just to have full control of what to save.

_setBaseTokenURI should be exposed to authorized users.

I’m wondering if we should emit an event once the baseTokenURI is changed. Indexers can be reactive to this, updating all the metadata references.

2reactions
nventurocommented, Oct 18, 2019

@nachomazzara from Decentraland commented that it is currently not possible for users to easily implement this feature (which they need) due to tokenURI() being external, which makes it impossible to call the base implementation via super. The workaround they’ve found is to override _setTokenURI and use a separate mapping, which is far from ideal and will probably be disallowed once Solidity 0.6.0 comes out.

If @logeekal will not end up tackling this, someone else should do it: we already have a concrete design proposal, and many users would be benefited by this feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ERC 721 - OpenZeppelin Docs
Internal function to set the base URI for all token IDs. It is automatically added as a prefix to the value returned in...
Read more >
Showing a Token URI - Ethereum Blockchain Developer
To make this a complete example, the last thing that's missing is the TokenURI. ... contract MinimalERC721 is ERC721, Ownable, ...
Read more >
Best way to set metadata in ERC721 contract on production
ERC721 contract provides a method called _setBaseURI(baseURI_) . Internal function to set the base URI for all token IDs. It is automatically ......
Read more >
ERC721 Example | Shimmer Wiki
Create and deploy a Solidity smart contract to mint NFTs using the ERC721 ... Since this contract uses auto-incremental token ids, your token...
Read more >
How to create and deploy an ERC-721 (NFT) - QuickNode
1. Digital collectibles compatible with the ERC-721 standard have become very popular since the launch o... 2. Fungible means to be the same...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found