Metadata
The Metadata
extension allows for assets to have on-chain description, symbol and uri pointing to off-chain metadata, for compatibility with existing NFT standards.
The extension consists of:
Field | Description |
---|---|
symbol | (optional) Symbol of the asset. |
description | (optional) Description of the asset. |
uri | (optional) URI for external metadata information. |
Each of these fields is limited to 255
length, but their size is not fixed — data is stored on-chain only when a value is set.
Creating Metadata
The Metadata
extension can be created using either the allocate
, create
or update
instructions.
- JavaScript
- Rust
- Rust (on-chain)
import { allocate, metadata } from '@nifty-oss/asset';
await allocate(umi, {
asset,
payer,
extension: metadata({
symbol: 'SMB',
description: 'A metadata extension',
uri: 'https://arweave.net/62Z5yOFbIeFqvoOl-aq75EAGSDzS-GxpIKC2ws5LVDc',
}),
}).sendAndConfirm(umi);
use nifty_asset::{
extensions::{ExtensionBuilder, MetadataBuilder},
instructions::AllocateBuilder,
types::{ExtensionInput, ExtensionType},
};
let mut metadata = MetadataBuilder::default();
metadata.set(
Some("SMB"),
None,
Some("https://arweave.net/62Z5yOFbIeFqvoOl-aq75EAGSDzS-GxpIKC2ws5LVDc"),
);
let ix = AllocateBuilder::new()
.asset(asset.pubkey())
.payer(Some(payer.pubkey()))
.system_program(Some(system_program::id()))
.extension(ExtensionInput {
extension_type: ExtensionType::Metadata,
length: metadata.len() as u32,
data: Some(metadata),
})
.instruction();
use nifty_asset::{
extensions::{ExtensionBuilder, MetadataBuilder},
instructions::AllocateCpiBuilder,
types::{ExtensionInput, ExtensionType},
};
let mut metadata = MetadataBuilder::default();
metadata.set(
Some("SMB"),
None,
Some("https://arweave.net/62Z5yOFbIeFqvoOl-aq75EAGSDzS-GxpIKC2ws5LVDc"),
);
AllocateCpiBuilder::new(ctx.accounts.nifty_asset_program)
.asset(ctx.accounts.group)
.payer(Some(ctx.accounts.payer))
.system_program(Some(ctx.accounts.system_program))
.extension(ExtensionInput {
extension_type: ExtensionType::Metadata,
length: metadata.len() as u32,
data: Some(metadata),
})
.invoke();
Fetching Metadata
Given an asset account, it is possible to retrieve the metadata information of an asset. Note that not all assets might have the extension, therefore it is necessary to assert if the extension was found.
- JavaScript
- Rust
- Rust (on-chain)
import {
ExtensionType,
fetchAsset,
getExtension
} from '@nifty-oss/asset';
const asset = await fetchAsset(umi, address);
const metadata = getExtension(asset, ExtensionType.Metadata);
if (metadata) {
console.log("Symbol: " + metadata.symbol);
console.log("Description: " + metadata.description);
console.log("URI: " + metadata.uri);
}
use nifty_asset::{
extensions::Metadata,
state::Asset,
};
let account = get_account(address)
.await
.unwrap();
let account_data = account.data.as_ref();
if let Some(metadata) = Asset::get::<Metadata>(account_data) {
println!("metadata: {:?}", metadata);
}
use nifty_asset::{
extensions::Metadata,
state::Asset,
};
let data = (*ctx.accounts.asset.data).borrow();
if let Some(metadata) = Asset::get::<Metadata>(&data) {
msg!("metadata: {:?}", metadata);
}