Links
The Links
extension represents a generic way to point to off-chain data. It consists of a list of link pairs, each of which contains a name
and a uri
. This can be used to point to any additional off-chain data or resources.
Field | Description |
---|---|
values[0..n] | List of Link objects. |
Each Link object is represented by: | |
name | Name of the link. |
uri | URI value. |
Creating Links
The Links
extension can be created using either the allocate
, create
or update
instruction.
- JavaScript
- Rust
- Rust (on-chain)
import { allocate, links } from '@nifty-oss/asset';
await allocate(umi, {
asset,
payer,
extension: links([
{ name: 'homepage', value: 'https://nifty-oss.org' },
{ name: 'twitter', value: 'https://twitter.com/nifty_oss' }
]),
}).sendAndConfirm(umi);
use nifty_asset::{
extensions::{Attributes, LinksBuilder, ExtensionBuilder},
instructions::AllocateBuilder,
types::{ExtensionInput, ExtensionType},
};
let mut links = LinksBuilder::default();
links.add("homepage", "https://nifty-oss.org");
links.add("twitter", "https://twitter.com/nifty_oss");
let ix = AllocateBuilder::new()
.asset(asset.pubkey())
.payer(Some(payer.pubkey()))
.system_program(Some(system_program::id()))
.extension(ExtensionInput {
extension_type: ExtensionType::Links,
length: links.len() as u32,
data: Some(links),
})
.instruction();
use nifty_asset::{
extensions::{Attributes, AllocateCpiBuilder, ExtensionBuilder},
instructions::AllocateBuilder,
types::{ExtensionInput, ExtensionType},
};
let mut links = LinksBuilder::default();
links.add("homepage", "https://nifty-oss.org");
links.add("twitter", "https://twitter.com/nifty_oss");
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::Attributes,
length: links.len() as u32,
data: Some(links),
})
.invoke();
Fetching Links
Given an asset account, it is possible to retrieve the links 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 links = getExtension(asset, ExtensionType.Links);
if (links) {
links.values.forEach(({ name, uri }) => {
console.log(name + '=' + uri);
});
}
use nifty_asset::{
extensions::Links,
state::Asset,
};
let account = get_account(address)
.await
.unwrap();
let account_data = account.data.as_ref();
if let Some(links) = Asset::get::<Links>(account_data) {
println!("links: {:?}", links);
}
use nifty_asset::{
extensions::Links,
state::Asset,
};
let data = (*ctx.accounts.asset.data).borrow();
if let Some(links) = Asset::get::<Links>(&data) {
msg!("links: {:?}", links);
}