Get On-Chain NFT Data
This guide explains how to use the getNFTData
function within a smart contract to fetch information about a specific on-chain NFT on the IOTA network.
Mint an NFT
Mint your first NFT following our how to mint an NFT guide.
How to Use the getNFTData
Function
The getNFTData
function retrieves data for an NFT based on its identifier. It returns a struct of type ISCNFT
, which contains various properties of the NFT like the issuer, metadata, owner and NFTID. To use this function, you need to create a function in your smart contract that calls getNFTData
and processes its return value.
Here's an example of how to implement this:
function fetchNFTData(uint256 tokenId) public view returns (ISCNFT memory nftData) {
nftData = ISC.sandbox.getNFTData(ISCTypes.asNFTID(tokenId));
return nftData;
}
Full Example Code
Combining the above step, here’s a complete example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@iota/iscmagic/ISC.sol";
import "@iota/iscmagic/ISCTypes.sol";
contract NFTMetadata {
function fetchNFTData(uint256 tokenId) public view returns (ISCNFT memory nftData) {
nftData = ISC.sandbox.getNFTData(ISCTypes.asNFTID(tokenId));
return nftData;
}
}