ERC-721 Metadata
Extension of ERC-721 that adds the optional metadata functions from the ERC721 standard.
Usage
In order to make ERC-721 Metadata
methods “external” so that other contracts can call them, you need to add the following code to your contract:
use openzeppelin_stylus::token::erc721::{
self, extensions::Erc721Metadata, Erc721,
};
#[entrypoint]
#[storage]
struct Erc721Example {
#[borrow]
erc721: Erc721,
#[borrow]
metadata: Erc721Metadata,
}
#[public]
#[inherit(Erc721, Erc721Metadata)]
impl Erc721Example {
// ...
#[selector(name = "tokenURI")]
fn token_uri(&self, token_id: U256) -> Result<String, erc721::Error> {
self.metadata.token_uri(token_id, &self.erc721)
}
}
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;
constructor(string memory name_, string memory symbol_, string memory baseUri_) {
// ...
_name = name_;
_symbol = symbol_;
_baseUri = baseUri_;
// ...
}
}