Non Fungible Tokens

In this session, we will learn how to create digital art using Non-Fungible Tokens (NFTs) with their metadata and specific attributes.

Start coding

Create a new file or edit index.ts

import {  Client,  Wallet }  from "xrpl" 

const client = new Client("wss://s.altnet.rippletest.net:51233")


const main = async () => {
  console.log("lets get started...");
  await client.connect();

  // do something interesting here


  await client.disconnect();
  console.log("all done!");
};

main();

Running this to ensure everything works should log a few things to the console. If you are running this from your node environment, don't forget to runnpm i xrpl to add it to your project.

Creating NFT

First, to create digital art, you need to create an NFT using NFTokenMint. You need one available wallet for that. For an NFT to host metadata and have specific attributes, we usually provide a JSON file for the metadata. You can gain a better understanding of the OpenSea metadata standards (https://docs.opensea.io/docs/metadata-standards). When creating the NFT, you will use a URI field to include the URI to the metadata of the NFT you want to create.

import { convertStringToHex, NFTokenMint, NFTokenMintFlags, NFTokenCreateOffer } from 'xrpl';

const { wallet: wallet1, balance: balance1 } = await client.fundWallet()    
console.log('wallet1', wallet1)

const uri = "My super NFT URI"

const nftMintTx: NFTokenMint = {
    TransactionType: "NFTokenMint",
    Account: wallet.address,
    URI: convertStringToHex(uri),
    Flags: NFTokenMintFlags.tfBurnable + NFTokenMintFlags.tfTransferable, // Burnable in case no one is buying it
    NFTokenTaxon: 0, // Unique identifier for the NFT type
}

const prepared = await client.autofill(nftMintTx)
const signed = wallet.sign(prepared)
const result = await client.submitAndWait(signed.tx_blob)
const nftId = nftId: (result.result.meta as NFTokenMintMetadata)?.nftoken_id as string

console.log("NFT ID " + nftId + " created")

At this point, you should have one account with an NFT created.

Putting your NFT on sale

Once you have your NFT, you may want to put it on sale and allow people to bid on it. Thanks to XRPL built-in functions, the NFT marketplace is already provided within the ledger. We only need to use NFTokenCreateOffer to put our NFT up for sale.

import { NFTokenCreateOffer } from 'xrpl';

const nftCreateOfferTx: NFTokenCreateOffer = {
    TransactionType: "NFTokenCreateOffer",
    Account: wallet.address,
    Destination: destination, // Optional if precised it would precised the offer for a specific address
    NFTokenID: nftId,
    Amount: "0", // 0 would represent a gift to someone
    Flags: 1 // Sell offer
};

const prepared = await client.autofill(nftCreateOfferTx)
const signed = wallet.sign(prepared)
const result = await client.submitAndWait(signed.tx_blob)
const offerId = (result.result.meta as NFTokenCreateOfferMetadata)?.offer_id as string

console.log("Offer ID " + offerId + " created")

At this point, your NFT should be on sale, and it will be available for someone specific or anyone to buy.

Extra credits

Canceling your offer

You can cancel your NFT offer, you will need to use the NFTokenCancelOffer function to do so (https://xrpl.org/docs/references/protocol/transactions/types/nftokencanceloffer/)

Burning your NFT

You can burn your NFT, you will need to use the NFTokenBurn function to do so (https://xrpl.org/docs/references/protocol/transactions/types/nftokenburn/)

Last updated