Getting started with the Ternoa SDK — easiest way to mint, query, and sell NFTs.

Pranjal Bhardwaj
Ternoa
Published in
7 min readApr 1, 2024

Unlocking the potential of NFTs has never been easier with the Ternoa SDK. Empowering developers with seamless integration, the Ternoa SDK revolutionises NFT operations. From minting to transferring, explore how this powerful SDK streamlines the creation and management of non-fungible tokens, shaping the future of digital ownership. Let’s dive right into the step-by-step process:

Step 1: Create your Ternoa wallet and claim Alphanet CAPS.

You can download the Ternoa Wallet app. It is available on the Google Play Store and the iOS App Store. To see detailed instructions, follow our documentation.

Step 2 : Install NodeJS v.14+, along with NPM.

Step 3: Generate IPFS key with Ternoa IPFS key manager

Step 4: Cloning the github repository

In this tutorial, we will be using the scripts available in the following GitHub repository:

git clone https://github.com/capsule-corp-ternoa/ternoa-sdk-starter
cd ternoa-sdk-starter

Step 5: Install Ternoa JS library and app dependencies using npm install

This is one of the most important steps in setting up our development environment. This step ensures that there are no dependency issues while compiling our scripts.

Execute the following commands to install the required libraries:

npm install ternoa-js
npm install

Step 6: Create a .env file with seed phrase and IPFS key

After you have generated the IPFS key, create a .env file in the project directory. You will need to specify the IPFS key and seed phrase of your wallet to execute the transactions:

SEED_TEST_FUNDS= "a b c d e f g"
IPFS_API_KEY= "pqrstuvwxyz"

Note: Make sure to not expose your private keys and IPFS key in a public github repository for safety reasons.

Step 7: In this tutorial, we will be interacting with the following files one by one:

  • 01_mintNFT.ts
  • 02_getNFT.ts
  • 03_sellNFT.ts

Firstly, to prepare the basic NFT asset, we will create a new typescript file named createNFT.ts . Here we will specify the metadata for the NFT by adding the image file in your project directory. You will also need to specify the file name and file type.

import fs from "fs";
import { TernoaIPFS, File } from "ternoa-js";

const main = async () => {
const file = new File(
[await fs.promises.readFile("FILE_NAME")],
"FILE_NAME",
{
type: "FILE_TYPE",
}
);

const ipfsClient = new TernoaIPFS(new URL("IPFS_NODE_URL"), "IPFS_API_KEY");

const nftMetadata = {
title: "NFT TITLE",
description: "NFT DESCRIPTION",
};

const { Hash } = await ipfsClient.storeNFT(file, nftMetadata);
console.log("The off-chain metadata CID hash is ", Hash);
};

Once the script is ready, you will be able to execute it using:

cd ternoa-sdk-starter-main/src/basics
npm run start src/basics/createNFT.ts

You will see an output with an off-chain metadata CID hash, which we will need for the next steps. The next steps can be found in Part 2 of this tutorial.

Step 8: Make sure you copy and paste the CID hash at ‘offchainData’ created in the previous step.

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

const mintNFT = async () => {
try {
const keyring = await getKeyringFromSeed("//TernoaTestAccount");
const nftData = await createNft(
"IPFS_CID",
0,
undefined,
false,
keyring,
WaitUntil.BlockInclusion
);
console.log("The on-chain NFT id is: ", nftData.nftId);
} catch (e) {
console.error(e);
}
};

Once that is done, to mint the NFT, we need to execute this file by

cd ternoa-sdk-starter-main/src/basics
npm run start src/basics/01_mintNFT.ts

You will see an output saying the NFT has been successfully minted, and it will return the NFT ID.

Step 9: You can visit the Polkadot UI explorer to see recent events, where you can see the recently minted NFT like this:

Note: Make sure you have the explorer open before minting the NFT. In some cases, you might not be able to see your mint under the recent events if you load the ternoa alphanet polkadot explorer after minting NFT.

Step 10: Check the Ternoa wallet app and go to the NFT section to check if the NFT has been minted successfully. Along with the NFT, you’ll be able to see the NFT metadata.

Step 11: In this step, we will query the on-chain NFT data. Firstly, Copy the NFT ID and specify it under

const NFT_ID = 83179;

The final file would look something like:

import { u128 } from "@polkadot/types";
import { request, gql } from "graphql-request";

// Change the nftId provided by default with the one created in step one (01_mintNFT.ts).
const NFT_ID = ["NFT_ID"];

// The function below prepares a stringified query to get NFT data from a specific NFT id.
const query = (id: number) => gql`
{
nftEntity(id: "${id}") {
owner
nftId
offchainData
collectionId
royalty
isSoulbound
}
}
`;

const getNftData = async () => {
try {
console.log("Sending query to the indexer...");
if (NFT_ID === undefined) throw new Error('Change the const NFT_ID with the one created in step one (01_mintNFT.ts).')

// Here we make the request to our indexer by providing both the endpoint and the query.
const response = await request<{ nftEntity: NftType }>(
"https://indexer-alphanet.ternoa.dev",
query(NFT_ID)
);

console.log(response);
console.log("Step 2 is over! See you later 👋");
} catch (error) {
console.error(error);
} finally {
process.exit();
}
};

getNftData();

type NftType = {
owner: string;
nftId: string;
offchainData: string;
collectionId: string;
royalty: number;
isSoulbound: boolean;
};

Then execute the 02_getNFT.ts file using the following command:

cd ternoa-sdk-starter-main/src/basics
npm run start src/basics/02_getNFT.ts

Here you will see an output like the following:

You can see the owner wallet address, nftId, offchainData, collectionId, royalty, and isSoulbound properties associated with the NFT.

Step 12: Head over to https://indexer-alphanet.ternoa.dev/ to access the graphQL indexer. Here we will be executing a simple query to check the NFT created and held in our wallet. You can specify your wallet address by replacing “YOUR_WALLET_ADDRESS”.

query {
nftEntities(
first: 5,
filter: {
owner: { equalTo: "YOUR_WALLET_ADDRESS" }
}
) {
totalCount
nodes {
nftId
owner
creator
collectionId
offchainData
}
}
}

Upon execution, we will be able to see the NFT IDs along with NFT data.

Step 13: In this step, we will be listing our NFT on a marketplace. For this, we will need the nftId and the marketplaceId. Firstly, specify the NFT ID, like: const NFT_ID = 83179;

Then fill out the required parameters, like Marketplace ID and selling price and then execute the file.

import {
initializeApi,
getKeyringFromSeed,
WaitUntil,
safeDisconnect,
listNft,
} from "ternoa-js";
import dotenv from "dotenv";
dotenv.config();

const NFT_ID = ["NFT_ID"];

const sellNFT = async () => {
try {
if (NFT_ID === undefined) throw new Error('Change the const NFT_ID with the one created in step one (01_mintNFT.ts).')

await initializeApi();
console.log(
"Ternoa-JS API initialized: you're connected to the Ternoa Alphanet Network"
);

const SEED_TEST_FUNDS = process.env.SEED_TEST_FUNDS;
if (!SEED_TEST_FUNDS) throw new Error("SEED_UNDEFINED: Verify your .env variables")

const keyring = await getKeyringFromSeed("leg system coach toss first wood dutch neither stable drift absorb squeeze");
console.log("Keyring set and ready to use.");

const listedNft = await listNft(
NFT_ID,
MARKETPLACE_ID,
SELLING_PRICE,
keyring,
WaitUntil.BlockInclusion
);
console.log(
`Your NFT ${listedNft.nftId} is now listed for sale at: ${listedNft.priceRounded} CAPS`
);

console.log(
"Congratulation folks! This is the end of your journey with us! 👋"
);
} catch (e) {
console.error(e);
} finally {
await safeDisconnect();
process.exit();
}
};

sellNFT();
cd ternoa-sdk-starter-main/src/basics
npm run start src/basics/03_sellNFT.ts

Upon execution, you will be able to see an output returning the NFT ID and the selling price with a confirmation message.

Step 14: You can verify the event by visiting the polkadot UI explorer and checking out the recent events. The 'sell’ event for your NFT will look like this:

Congratulations! You have completed all the steps and successfully minted your NFT. We encourage everyone to tweak different NFT properties to explore functionalities which we will be covering in the upcoming tutorials.

--

--