Get the list of SkyIDs
To get the list of SkyIDs for a user address, run this code:
const contractService: SkyContractService = skynet.contractService;
const nftCount = Number(await contractService.AppNFT.balanceOf(skynet.contractService.selectedAccount));
let newNftList: any = []
for (let i = 1; i < nftCount + 1; i++) {
const nftRequest = contractService.AppNFT.tokenOfOwnerByIndex(
skynet.contractService.selectedAccount,
nftCount - i
);
newNftList.push(nftRequest);
}
Run this function to get the SkyID metadata:
export const fetchNFTMetadata = async (nftID: string, skynet: SkyMainBrowser, checkBundle: boolean = false, tokenURI: string = "") => {
try {
const contractService: SkyContractService = skynet.contractService;
let cid = tokenURI;
if (cid || cid === '') {
cid = await contractService.AppNFT.tokenURI(nftID);
}
const cachedMetadata = localStorage.getItem(nftID);
if (cachedMetadata && JSON.parse(cachedMetadata).cid === cid) {
return JSON.parse(cachedMetadata);
}
if (!cid || cid === '') {
return {
name: '-',
image: '',
description: '-',
collectionId: null,
nftID: nftID,
};
}
let ipfsCID = cid;
const nftMetadataResp: any = await axios.get(ipfsCID.replace('ipfs://', 'https://gateway.mesh3.network/ipfs/'))
if (nftMetadataResp.data) {
let nftMetadata: NFTMetadata;
if (typeof nftMetadataResp.data === 'string') {
nftMetadata = await JSON.parse(nftMetadataResp.data);
} else {
nftMetadata = nftMetadataResp.data;
}
nftMetadata.nftID = nftID;
if (checkBundle) {
if (nftMetadata.bundleCID) {
const response = await fetch(nftMetadata.bundleCID.replace('ipfs://', 'https://gateway.mesh3.network/ipfs/'));
if (!response.ok) return nftMetadata;
const data = await response.json();
nftMetadata.bundleObject = data;
localStorage.setItem(nftID, JSON.stringify({ ...nftMetadata, cid }));
return nftMetadata
} else {
localStorage.setItem(nftID, JSON.stringify({ ...nftMetadata, cid }));
return nftMetadata
}
} else {
localStorage.setItem(nftID, JSON.stringify({ ...nftMetadata, cid }));
return nftMetadata;
}
} else {
return {
name: '-',
image: '',
description: '-',
collectionId: null,
nftID,
};
}
} catch (error) {
return {
name: '-',
image: '',
description: '-',
collectionId: null,
nftID,
};
}
};
Last updated