Skip to main content

Properties

The Properties extension allows creating a list of on-chain "typed" values (key/value pairs) for an asset. It currently supports three different "types": text, number (as an u64 value) and boolean. 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 name and value pairs (Property):

Creating Properties

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

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

await allocate(umi, {
asset,
payer,
extension: properties([
{ name: 'name', value: 'nifty' },
{ name: 'version', value: 1n },
{ name: 'alpha', value: false }
]),
}).sendAndConfirm(umi);
info

The "type" of the property is determined by the type of the value — name is a text property; version is a number (u64) property; and alpha is a boolean property.

Fetching Properties

Given an asset account, it is possible to retrieve the properties 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);

// retrieves all properties
const properties = getExtension(asset, ExtensionType.Properties);

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

// retrieves a single property by name
const name = getProperty(assetAccount, 'name', Type.Text);
console.log('name=' + name);
tip

Recall that property values are typed, so values are retrived into their original type.