How to retrieve a Basic NFT using Ternoa Indexer
Ternoa indexer is a record of the Ternoa Chain data. You can query data for some specific entities (NFT, Collection, Marketplace(...)) using GraphQL. In this exemple, we use the graphql-request library.
Step 1: NftEntity query preparation
You first need to prepare a stringified query to get NFT data from a specific NFT id.
Here are detailed the paramaters available for the NftEntity
:
`nftId`: The NFT id. - String
`auction`: The auction data if the NFT is auctionned; null otherwise. - AuctionEntity | null
`collection`: The collection data if the NFT is in a collection; null otherwise. - CollectionEntity | null
`owner`: The NFT owner; null if the NFT is burned. - String | null
`creator`: The NFT creator. - String
`delegatee`: The delegatee address if the NFT is delegated; null otherwise. - String | null
`royalty`: - The NFT royalty. - Number
`offchainData`: The NFT off-chain data (e.g. IPFS CID hash, a link or any string). - String!
`secretOffchainData`: The Secret NFT off-chain data (e.g. IPFS CID hash, a link or any string) if the NFT is a Secret NFT; null otherwise. - String | null
`capsuleOffchainData`: The Capsule NFT off-chain data (e.g. IPFS CID hash, a link or any string) if the NFT is a Capsule NFT; null otherwise. - String | null
`isCapsule`: Boolean flag: true if the NFT is a Capsule NFT; false otherwise. - Boolean
`isCapsuleSynced`: Boolean flag: true if the NFT is a synced Capsule NFT; false otherwise. - Boolean
`isSecret`: Boolean flag: true if the NFT is a Secret NFT; false otherwise. - Boolean
`isSecretSynced`: Boolean flag: true if the NFT is a synced Secret NFT; false otherwise. - Boolean
`isDelegated`: Boolean flag: true if the NFT is delegated; false otherwise. - Boolean
`isTransmission`: Boolean flag: true if a transmission protocol is set for the NFT; false otherwise. - Boolean
`isSoulbound`: Boolean flag: true if the NFT is a Soulbound NFT; false otherwise. - Boolean
`isListed`: Boolean flag: true if a transmission protocol is set for the NFT; false otherwise. - Boolean
`isRented`: Boolean flag: true if a transmission protocol is set for the NFT; false otherwise. - Boolean
`rentee`: The rentee address if the NFT is rented; null otherwise. - String | null
`rentalContract`: The rental contract data if the NFT is rented; null otherwise. - RentEntity | null
`price`: The sale listing price in BN value if the NFT is listed for a direct sale; null otherwise. - String | null
`priceRounded`: The sale listing price if the NFT is listed for a direct sale; null otherwise. - Number | null
`marketplace`: The marketplace data if the NFT is listed; null otherwise. - MarketplaceEntity
`typeOfListing`: The NFT listing type if the NFT is listed (sale or auction); null otherwise. - String | null
`transmissionRecipient`: The recipient address to whom the NFT will be transmitted if a transmission protocol is defined for the NFT; null otherwise. - String | null
`transmissionProtocol`: - The transmission protocol data if a transmission protocol is defined for the NFT; null otherwise. TransmissionEntity | null
`timestampCreated`: - The creation timestamp. Date
`timestampBurned`: - The burning timestamp. Date | null
`timestampListed`: - The last listing timestamp. Date | null
`timestampRented`: - The last renting timestamp. Date | null
`timestampSecretAdded`: The last secret addition timestamp. - Date | null
`timestampConvertedToCapsule`: The last capsule conversion timestamp. - Date | null
For example, if we want to get the NFT: owner / id / off-chain data / collection id / royalty; we have to prepare the following query by replacing NFT_ID with the NFT id you want to get the information from (e.g. the NFT id from the NFT minted previously in "How to mint a Basic NFT on-chain"):
{
nftEntity(id: "NFT_ID") {
owner
nftId
offchainData
collectionId
royalty
}
}
Step 2: Sending the request to the Indexer
Once the query is ready, you can make the request to our Indexer instances by providing both the indexer endpoint and the query.
Replace NFT_ID in the following code snippet with the NFT ID previouly generated in "How to mint a Basic NFT on-chain":
import { request, gql } from "graphql-request";
const NFT_ID = 0;
const query = (id: number) => gql`
{
nftEntity(id: "${id}") {
owner
nftId
offchainData
collectionId
royalty
}
}
`;
const getNftData = async () => {
try {
const response = await request<{ nftEntity: NftType }>(
"https://indexer-alphanet.ternoa.dev",
query(NFT_ID)
);
console.log(response);
} catch (error) {
console.error(error);
}
};
type NftType = {
owner: string;
nftId: string;
offchainData: string;
collectionId: string;
royalty: number;
};
The getNftData
function is an asynchronous function that sends a GraphQL request using the request
function from the "graphql-request" library. Here we are using the Ternoa Alphanet instance at "https://indexer-alphanet.ternoa.dev" with the NFT ID 0 as the query parameter (you can try with your id). The response from the server is an object with a property nftEntity that has the data of the requested NFT entity.
The response for the NFT id 0 the the Alphanet Network is:
{
"data": {
"nftEntity": {
"owner": "5DoaPm79MrWUQpFSDBhpmotRp344dc9eM4NV8aRb3vHEuzxH",
"nftId": "0",
"offchainData": "Hello",
"collectionId": "0",
"royalty": 100
}
}
}
How to retrieve the last NFT minted using Ternoa Indexer
Ternoa Indexer comes with filtering and ordering options. Instead of requesting a single NftEntity
with a specific NFT ID, you can filter all NftEnties
:
{
nftEntities(orderBy: [TIMESTAMP_CREATE_DESC]) {
nodes {
owner
nftId
timestampCreate
}
}
}
The NFT in the response will be ordered by creation timestamp descendent with orderBy: [TIMESTAMP_CREATE_DESC]
.
The response on the the Alphanet Network when this document is written is:
{
"data": {
"nftEntities": {
"nodes": [
{
"owner": "5EU1EDxRBpoq48HXLvLfVyGJfrz5p3hGkQC46XJFTHS1mohb",
"nftId": "72293",
"timestampCreate": "2023-02-22T10:05:06.001"
},
{
"owner": "5EU1EDxRBpoq48HXLvLfVyGJfrz5p3hGkQC46XJFTHS1mohb",
"nftId": "72294",
"timestampCreate": "2023-02-22T10:05:06.001"
},
...
]
}
}
}
The last NFT minted on the Ternoa chain is the NFT 72,293.
Support
If you face any trouble, feel free to reach out to our community engineers in our Discord.