The following sample will show you how to sell a NFT using the Hedera Hashgraph and Filecoin to store the media. We will first generate a request for the end user. Once validated, you will get notified for the NFT's selling success.
In order to guarantee the process, the NFT will be transferred on an escrow account. User could withdraw anytime and get NFT back.
Prerequisites:
Step 1: Import the following modules
Let's continue building on the index.js from the previous example (Environment Set-up) and add the following modules:
When initializing a NFT's selling, a request will be sent to the end user
/* Update the fields with your informations */constfromAccountId=''/* Account Id of the Seller */consthbarAmount=10/* Unit Price per NFT in Hbar */constnftIds= ['1@0.0.123456'] /* Array of nftIds to sell *//* You can specify a quantity if you prefer to let us choose randomly for you *//* But nftIds field cannot be set with quantity */consttokenId=''; /* token Id of the NFT */constisCollection=''; /* Sell the nftIds under one QR Code *//* Optional NOT YET AVAILABLE - The NFT could only be sell to accountId present on that list */constaccountIds= ['',''];/* Optional - Add a middle men in order to collect fees */constmiddleMen= { middleManAccountId, middleManTypeOfFees:MiddleManFeesType.HBAR, middleManFees:10};/* If you're selling by nftIds replace quantity by ndtIds */awaitclient.sellNFT({fromAccountId, nftIds, hbarAmount, tokenId, accountIds, isCollection, middleMen});
Step 3: Listen for the NFT's sell
Finally get notified when your NFT has been successfully set in sale !
/* Subscribe to new sale NFT Validation */client.sellNFTValidation().subscribe(nft => {console.log('NFT successfully set in sale', nft);});
Code Check ✅
Your index.js file should look like this:
const { Client,CategoryNFT } =require("@xact-wallet-sdk/client");require("dotenv").config();asyncfunctionsellNFT() {//Grab your api key from your .env fileconstapiKey=process.env.API_KEY;// If we weren't able to grab it, we should throw a new errorif (apiKey ==null) {thrownewError("Environment variables API_KEY must be present"); }/* Create a new instance of Client */constclient=newClient({apiKey});/* Init the connexion */awaitclient.initConnexion();/* Update the fields with your informations */constfromAccountId=''/* Account Id of the Seller */consthbarAmount=10/* Unit Price per NFT in Hbar */constnftIds= ['1@0.0.123456'] /* Array of nftIds to sell */consttokenId=''; /* token Id of the NFT *//* If you're selling by nftIds replace quantity by ndtIds */awaitclient.sellNFT({fromAccountId, hbarAmount, tokenId, nftIds});/* Subscribe to new sale NFT Validation */client.sellNFTValidation().subscribe(nft => {console.log('NFT successfully set in sale', nft); });}sellNFT();