Skip to main content

JavaScript Client

The Nifty Asset JS Client is a JavaScript client that provides a set of methods for interacting with the Nifty Asset program on the Solana Virtual Machine. This guide will help you get started with the Nifty Asset JS Client and show you how to use it with the Umi framework.

Requirements

  • Node.js version 18 or higher
  • NPM or other package manager (e.g., Yarn, PNPM)
  • TypeScript version 4.0 or higher installed
  • Solana CLI installed (for local development)

Umi

The Nifty Asset JS Client is built to work with the Umi framework. Umi is a lightweight JavaScript framework that is used to build Solana clients. Umi was built and is maintained by the Metaplex Foundation. The Nifty Asset JS Client is an Umi plugin that provides a set of methods for interacting with the Nifty Asset program.

Required Dependencies

To get started you will need to install the following libraries:

  • @metaplex-foundation/umi: The core Umi framework npmjs.com
  • @metaplex-foundation/umi-bundle-defaults: Default plug-ins bundle for Umi npmjs.com
  • @nifty-oss/asset: The Nifty Asset JS Client npmjs.com

Add these dependencies to your project by running the following command:

npm install \
@metaplex-foundation/umi \
@metaplex-foundation/umi-bundle-defaults \
@nifty-oss/asset

Umi Configuration

To use the Nifty Asset JS Client, you will need to configure Umi to use the network of your choice (e.g., Solana Mainnet, Devnet, Testnet, or Local) and the Nifty Asset plugin. Create an instance of Umi with the createUmi method, and pass in the network URL:

import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';

const umi = createUmi('https://api.mainnet-beta.solana.com');

To configure Umi on other networks, simply replace the URL in the createUmi method with the RPC endpoint of your choice.

RPC Endpoints

Here is a list of public RPC endpoints for Solana. Though public endpoints may be suitable for light use and testing, or production applications, we reccomend using an endpoint from a third party provider (e.g., QuickNode, Helius).

NetworkURL
Mainnethttps://api.mainnet-beta.solana.com
Devnethttps://api.devnet.solana.com
Testnethttps://api.testnet.solana.com
Localnet
(default)
http://127.0.0.1:8899

Additionally, Nifty Asset is available on the following networks:

NetworkURL
Eclipse Mainnethttps://mainnetbeta-rpc.eclipse.xyz
Eclipse Testnethttps://testnet.dev2.eclipsenetwork.xyz
Eclipse Devnethttps://staging-rpc.dev2.eclipsenetwork.xyz

Replace the URL in your createUmi method with the network of your choice.

Local Use

To interact with the Nifty Asset program on a local Solana network, you will need to deploy the program to your local network. You can deploy the program when you initiate your test validator, but you must first write the program data to a file. You can do this by running the following command:

solana program dump AssetGtQBTSgm5s91d1RAQod5JmaZiJDxqsgtqrZud73 nifty_asset.so -um

This command will write the current program data from mainnet (specified by use of the -um flag) to a file named nifty_asset.so in the current directory. You can then deploy the program to your local network by running the following command:

solana-test-validator -r --bpf-program AssetGtQBTSgm5s91d1RAQod5JmaZiJDxqsgtqrZud73 nifty_asset.so

This command will start a local Solana validator with the Nifty Asset program deployed. If set up correctly, you should be able to see the Nifty Asset program on your local Solana Explorer or by verifying the program with the Solana CLI:

solana program show AssetGtQBTSgm5s91d1RAQod5JmaZiJDxqsgtqrZud73 -ul

Use Nifty Asset JS Client

To use the Nifty Asset JS Client, you will need to import the client and add it to your Umi instance using the .use() this this:

import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { niftyAsset } from '@nifty-oss/asset';

const umi = createUmi('https://api.mainnet-beta.solana.com').use(niftyAsset());

The .use() method registers the Nifty Asset plugin with the Umi instance, enabling access to the Nifty Asset methods.

You are all set and ready to start using the Nifty Asset JS Client with Umi. We will use this same structure throughout the documentation to demonstrate how to use the Nifty Asset JS Client with Umi, so take a moment to familiarize yourself with this setup. If you have any questions, please reach out to the Nifty Asset team on Discord.

Example Use

Below is an example function that demonstrates how to use the mint method to create a new digital asset:

import { niftyAsset, mint, Standard, attributes } from '@nifty-oss/asset';
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { TransactionBuilderSendAndConfirmOptions, generateSigner, keypairIdentity, sol } from '@metaplex-foundation/umi';

const umi = createUmi('http://127.0.0.1:8899', { commitment: 'processed' }).use(niftyAsset());

async function mintAsset() {
const creator = generateSigner(umi);
const owner = generateSigner(umi);
const asset = generateSigner(umi);
umi.use(keypairIdentity(creator));

await umi.rpc.airdrop(creator.publicKey, sol(100));

await mint(umi, {
asset,
owner: owner.publicKey,
authority: creator.publicKey,
payer: umi.identity,
name: 'Example NFT',
standard: Standard.NonFungible,
mutable: false,
extensions: [
attributes([{ name: 'head', value: 'hat' }]),
]
}).sendAndConfirm(umi);
}

mintAsset();

Source Code

The source code for the Nifty Asset JS Client is available on GitHub.

Contribute

To make contributions, please check out the Contributing Guide.

Build from Source

To build the client from source, follow the following steps:

Clone the repository:

git clone https://github.com/nifty-oss/asset && cd asset

Install the dependencies:

pnpm install

Build the programs:

pnpm programs:build

Generate the JS and Rust clients:

pnpm generate:clients

or generate both the clients and the IDL:

pnpm generate

This will generate the JS client in the clients/js directory. You can use it to interact with the Nifty Asset program.