ERC-721 Uri Storage
The OpenZeppelin ERC-721 URI Storage extension is needed to manage and store URIs for individual tokens. This extension allows each token to have its own unique URI, which can point to metadata about the token, such as images, descriptions, and other attributes. This is particularly useful for non-fungible tokens (NFTs) where each token is unique and may have different metadata.
Usage
In order to make an ERC-721 token with URI Storage flavour,
your token should also use ERC-721 Metadata
extension to provide additional metadata for each token.
You need to create a specified contract as follows:
use openzeppelin_stylus::token::erc721::{
self,
extensions::{Erc721Metadata, Erc721UriStorage, IErc721Burnable},
Erc721,
};
#[entrypoint]
#[storage]
struct Erc721MetadataExample {
#[borrow]
erc721: Erc721,
#[borrow]
metadata: Erc721Metadata,
uri_storage: Erc721UriStorage,
}
#[public]
#[inherit(Erc721, Erc721Metadata)]
impl Erc721MetadataExample {
fn mint(
&mut self,
to: Address,
token_id: U256,
) -> Result<(), erc721::Error> {
self.erc721._mint(to, token_id)
}
fn burn(&mut self, token_id: U256) -> Result<(), erc721::Error> {
self.erc721.burn(token_id)
}
#[selector(name = "tokenURI")]
fn token_uri(&self, token_id: U256) -> Result<String, erc721::Error> {
self.uri_storage.token_uri(token_id, &self.erc721, &self.metadata)
}
#[selector(name = "setTokenURI")]
fn set_token_uri(&mut self, token_id: U256, token_uri: String) {
self.uri_storage._set_token_uri(token_id, token_uri)
}
}
Additionally, you need to ensure proper initialization during contract deployment. Make sure to include the following code in your Solidity Constructor:
contract Erc721Example {
// ...
string private _name;
string private _symbol;
string private _baseUri;
mapping(uint256 => string) _tokenUris;
constructor(string memory name_, string memory symbol_, string memory baseUri_) {
// ...
_name = name_;
_symbol = symbol_;
_baseUri = baseUri_;
// ...
}
}