DEV Community

Dris Hammani
Dris Hammani

Posted on • Edited on

Creating NFTs with Ternoa-js SDK: A Step-by-Step Guide

Introduction

As a developer, you may be interested in creating your NFT projects. Still, you may feel intimidated by the need to learn complex languages like Solidity. At Ternoa, we understand this challenge. We've made it easier for developers to create NFTs with our javascript SDK, which doesn't require using smart contracts.

In this article, I'll walk you through creating NFTs using the Ternoa-js SDK. We'll cover the necessary prerequisites, describe the functions and provide instructions to help you successfully create NFTs. Whether you're an experienced developer or new to NFTs, this tutorial will give you everything you need to get started. So let's dive in and learn how to create an NFT with Ternoa-js SDK!

What is Ternoa Blockchain ?

Ternoa is an open-source, substrate-based layer 1 blockchain that enables the widespread adoption of utility NFTs. It provides a comprehensive technological infrastructure for developers to easily create next-generation utility NFTs with out-of-the-box features like Delegation, Renting, SoulBound, Royalties, Auctions, Secret, Capsule, and more.

Prerequisites

This tutorial covers how to create NFTs on Ternoa Blockchain using the ternoa-js SDK. To get started, you will need the following:

  1. An account which you can easily create using the Ternoa Wallet App.

  2. Some test CAPS, which you can obtain from the Ternoa Alphanet Faucet

  3. The ternoa-js SDK package, which you can install by running the following command on your terminal:

yarn add ternoa-js
// or
npm install ternoa-js
Enter fullscreen mode Exit fullscreen mode

Function description

This function creates an NFT and returns a promise that resolves to an NFTCreatedEvent, representing the blockchain event emitted when an NFT is created.

Params

// Function signature
createNft(
  offchainData: string,
  royalty: number = 0,
  collectionId: undefined | number = undefined,
  isSoulbound: boolean = false,
  keyring: IKeyringPair,
  waitUntil: TernoaConstants.WaitUntil
): Promise<TernoaEvents.NFTCreatedEvent>

`offchainData`: a string representing off-chain related NFT metadata, such as an IPFS hash, an URL, or plain text.
`royalty`: a number that represents the percentage of all second sales that the creator will receive. It should be a decimal number in the range [0, 100]. The default value is 0.
`collectionId` (number | undefined) : an optional parameter representing the collection to which this NFT will belong. It should be a number.
`isSoulbound` (boolean): indicates if the NFT should be untransferable. The default value is false.
`keyring` (IKeyringPair): the account that will sign the transaction.
`waitUntil` (WaitUntil): execution trigger that can be set either to BlockInclusion or BlockFinalization.
Enter fullscreen mode Exit fullscreen mode

Response

NFTCreatedEvent includes the following properties:

`nftId` (number): ID of the NFT.
`owner` (string): address of the owner of the NFT.
`offchainData` (string): off-chain data associated with the NFT.
`royalty` (number): royalty fee for the NFT.
`collectionId` (number | null): ID of the collection the NFT belongs to, or null if the NFT does not belong to a collection.
`isSoulbound` (boolean): a boolean indicating whether the NFT is soulbound (cannot be traded or transferred).
`mintFee`: a string representing the minting fee for the NFT.
`mintFeeRounded`: a number representing the minting fee for the NFT, rounded to the nearest whole number.
Enter fullscreen mode Exit fullscreen mode

Create an NFT

This code snippet introduces the full flow of how to create an NFT.

import {
  initializeApi
  getKeyringFromSeed
  createNft
  WaitUntil
} from "ternoa-js";

const create = async () => {
  // Initialize the blockchain API, default to Ternoa Alphanet.
  // For Mainnet initializeApi('wss://mainnet.ternoa.network');
  await initializeApi();

  // Create a keyring pair object from a seed phrase. The keyring pair will
  // be used to sign and submit transactions.
  const keyring = await getKeyringFromSeed("seed phrase");

  // Create an NFT using the provided metadata and the
  // keyring pair to sign the transaction. The function returns a promise
  // that resolves to an NFTCreatedEvent object when the transaction is
  // included in a block and finalized
  const newNFT = await createNft(
    "offchain-data", 
    0, 
    undefined, 
    false, 
    keyring,
    WaitUntil.BlockFinalization
  );

  console.log(newNFT)
}
Enter fullscreen mode Exit fullscreen mode

Find your NFT

Once you've created an NFT, you may want to find it and view its details. There are several ways to find your NFT, in this tutorial we will learn 2 different ways.

With SDK

The getNftData function is used to retrieve the data related to a specific NFT by its id. It returns a promise that resolves to a JSON object containing the NFT data or null if the NFT does not exist.

Parameters

The getNftData function takes a single parameter:

`nftId` (number): id of the NFT for which you want to retrieve data.
Enter fullscreen mode Exit fullscreen mode

Returns

The getNftData function returns a promise that resolves to an object with the following properties:

`owner` (string): address of the owner of the NFT.
`creator` (string): address of the creator of the NFT.
`offchainData` (string): off-chain data associated with the NFT.
`collectionId` (number | null): ID of the collection the NFT belongs to, or null if the NFT does not belong to a collection.
`royalty` (number): royalty fee for the NFT.
`state` (NftState): NFT state.

NftState includes the following properties:

`isCapsule` (boolean): is this NFT a capsule?
`isListed` (boolean): is this NFT listed?
`isSecret` (boolean): is this NFT a secret?
`isDelegated` (boolean): is this NFT delegated?
`isSoulbound` (boolean): is the NFT a soulbound?
`isSyncing` (boolean): is this NFT sync?
`isRented` (boolean): is this NFT rented?
Enter fullscreen mode Exit fullscreen mode

Example

import { getNftData } from "ternoa-js";

const nftData = await getNftData(nftId);
Enter fullscreen mode Exit fullscreen mode

With Indexer

The indexer is an API that offers a convenient way for developers to search and query data stored on the blockchain.

import { request } from 'graphql-request';

// For Mainnet indexer: https://indexer-mainnet.ternoa.dev
const endpoint = 'https://indexer-alphanet.ternoa.dev';

// Your NFT id as string
const nftId = "";

const query = `
  query getNFT {
    nftEntity(id: "${nftId}") {
      nftId
      creator
      owner
      offchainData
      isSoulbound
      royalty
      createdAt
    }
  }
`;

const getNFT = async () => {
  try {
    const result = await request(endpoint, query);
    // handle result
  } catch (err) {
    // handle error
  }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

The Ternoa-js SDK simplifies creating NFTs on the Ternoa blockchain by offering a user-friendly interface and a comprehensive set of functions. With just a few lines of code, developers can initialize a blockchain API connection, generate a keyring pair, and create an NFT. The Ternoa-js SDK helps developers to efficiently build powerful NFT projects.

What's next ?

In the following articles, we'll explore other Ternoa NFT features like Delegation, Renting, SoulBound, Royalties, Auctions, Secret and Capsule, and some exciting use cases. If you have any questions or requests or want to learn more, please comment on this article and follow me on Twitter (@drishammani) to stay informed about the release of future articles.

Top comments (0)