ERC-1155 Metadata URI
The OpenZeppelin ERC-1155 Metadata URI extension is needed to manage and store URIs for individual tokens. This extension allows each token to have its own unique URI, which points to a JSON file that conforms to the ERC-1155 Metadata URI JSON Schema. 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-1155 token with Metadata URI flavour, you need to add the following code to your contract:
use openzeppelin_stylus::{
token::{
erc1155,
erc1155::{
extensions::{Erc1155MetadataUri, IErc1155MetadataUri},
Erc1155, IErc1155,
},
},
utils::introspection::erc165::IErc165,
};
#[entrypoint]
#[storage]
struct Erc1155MetadataUriExample {
erc1155: Erc1155,
metadata_uri: Erc1155MetadataUri,
}
#[public]
#[implements(IErc1155<Error = erc1155::Error>, IErc1155MetadataUri, IErc165)]
impl Erc1155MetadataUriExample {
#[constructor]
fn constructor(&mut self, uri: String) {
self.metadata_uri.constructor(uri);
}
}
#[public]
impl IErc1155 for Erc1155MetadataUriExample {
type Error = erc1155::Error;
fn balance_of(&self, account: Address, id: U256) -> U256 {
self.erc1155.balance_of(account, id)
}
fn balance_of_batch(
&self,
accounts: Vec<Address>,
ids: Vec<U256>,
) -> Result<Vec<U256>, Self::Error> {
self.erc1155.balance_of_batch(accounts, ids)
}
fn set_approval_for_all(
&mut self,
operator: Address,
approved: bool,
) -> Result<(), Self::Error> {
self.erc1155.set_approval_for_all(operator, approved)
}
fn is_approved_for_all(&self, account: Address, operator: Address) -> bool {
self.erc1155.is_approved_for_all(account, operator)
}
fn safe_transfer_from(
&mut self,
from: Address,
to: Address,
id: U256,
value: U256,
data: Bytes,
) -> Result<(), Self::Error> {
self.erc1155.safe_transfer_from(from, to, id, value, data)
}
fn safe_batch_transfer_from(
&mut self,
from: Address,
to: Address,
ids: Vec<U256>,
values: Vec<U256>,
data: Bytes,
) -> Result<(), Self::Error> {
self.erc1155.safe_batch_transfer_from(from, to, ids, values, data)
}
}
#[public]
impl IErc1155MetadataUri for Erc1155MetadataUriExample {
fn uri(&self, token_id: U256) -> String {
self.metadata_uri.uri(token_id)
}
}
#[public]
impl IErc165 for Erc1155MetadataUriExample {
fn supports_interface(&self, interface_id: FixedBytes<4>) -> bool {
self.erc1155.supports_interface(interface_id)
|| self.metadata_uri.supports_interface(interface_id)
}
}