NFT Query

Query NFT assets through web3.js library.

Initialize the contract object

var Contract = require('web3-eth-contract');
Contract.setProvider('wss://polygon-mainnet.blastapi.io/1160f5ab-4dfa-4ac1-aec4-12c7214ee252');
//jsonInterface:Contract ABI,address:Contract Address
var myContract = new Contract(jsonInterface,address);

JsonInterface can be viewed in the blockchain browser, such as the polygon example:

Query NFT Count of ERC1155

//walletAddress:Wallet Address,tokenId:TokenId of ERC1155 NFT 
myContract.methods["balanceOf"](walletAddress,tokenId).call({}) 
    .then(function(result){ 
    ///Return the number of tokenIds held
});

Example:

const myFirstPromise = new Promise((resolve, reject) => {
    myContract.methods["balanceOf"]("0xa9e9412AE9761B6c8AF6Bc76498208F773c5aEc3",11).call({})
    .then(function(result){ 
    if(result >= 1){
        resolve(true);
    }else{
        resolve(false);
    }
    });
});

myFirstPromise.then((hasNFT) => {
    console.log(`${hasNFT}`);
})

Query NFT Metadata of ERC1155

const mySecondPromise = new Promise((resolve, reject) => {
    myContract.methods["uri"](tokenId).call({})
    .then(function(result){
        resolve(result);
    });
});

Example:

const mySecondPromise = new Promise((resolve, reject) => {
    myContract.methods["uri"](11).call({})
    .then(function(result){//result uri of metadata
        resolve(result);
    });
});

mySecondPromise.then((result) => {
    console.log(result);//return:https://static.pallet.zone/metadata/WorldcupChampion/11.json
})

Parsing Metadata Result from URI:https://static.pallet.zone/metadata/WorldcupChampion/11.json

{
    "name": "Bit - Qatar(QA)",
    "description": "Bit - Qatar(QA)",
    "image": "https://static.pallet.zone/wordcup/Qatar.png"
}

Query NFT Count of ERC721

myContract.methods["balanceOf"](walletAddress).call({}) 
    .then(function(result){ 
    if(result >= 1){
        resolve(true);
    }else{
        resolve(false);
    }
    });
});

myFirstPromise.then((hasNFT) => {
    console.log(`${hasNFT}`);
})

Example:

const myFirstPromise = new Promise((resolve, reject) => {
    myContract.methods["balanceOf"]("0xa9e9412AE9761B6c8AF6Bc76498208F773c5aEc3").call({})
    .then(function(result){ 
    if(result >= 1){
        resolve(true);
    }else{
        resolve(false);
    }
    });
});

myFirstPromise.then((hasNFT) => {
    console.log(`${hasNFT}`);
})

Query NFT Metadata of ERC721

const mySecondPromise = new Promise((resolve, reject) => {
    myContract.methods["tokenUri"](tokenId).call({})
    .then(function(result){
        resolve(result);
    });
});

mySecondPromise.then((result) => {
    console.log(result);
})

Example:

const mySecondPromise = new Promise((resolve, reject) => {
    myContract.methods["tokenUri"](2061).call({})
    .then(function(result){
        resolve(result);
    });
});

mySecondPromise.then((result) => {
    console.log(result);//return metadata uri:https://static.pallet.zone/metadata/GenesisNFT/pallet
})

Parsing Metadata Results from URI:

{
    "name": "Pallet Genesis",
    "description": "Pallet Genesis NFT is linked to user’s Pallet wallet address with plenty of benefits and privileges like - Free-mint NFT, airdrops, discounts in coming campaigns or activities on Pallet",
    "image": "https://static.pallet.zone/genesis/pallet.webp"
}

Last updated