Skip to main content

Attributes

The Attributes extension allows creating a list of on-chain values (key/value pairs) for an asset. The on-chain aspect means these values can be read by other Solana programs and therefore be used by them (e.g., for gaming).

The extension consists of a list of Traits, where each trait has a name and a value field:

Creating Attributes

The Attributes extension can be created using either the allocate, create or update instructions.

import { allocate, attributes } from '@nifty-oss/asset';

await allocate(umi, {
asset,
payer,
extension: attributes([{ name: 'head', value: 'hat' }]),
}).sendAndConfirm(umi);

Fetching Attributes

Given an asset account, it is possible to retrieve the attributes of an asset. Note that not all assets might have the extension, therefore it is necessary to assert if the extension was found.

import {
ExtensionType,
fetchAsset,
getExtension
} from '@nifty-oss/asset';

const asset = await fetchAsset(umi, address);
const attributes = getExtension(asset, ExtensionType.Attributes);

if (attributes) {
attributes.values.forEach(({ name, value }) => {
console.log(name + '=' + value);
});
}