Get NFTs Owned by an Account
This guide will show you how to get the L2 NFTs owned by an account using the ISCAccounts.getL2NFTs
function from the ISCAccounts
magic contract.
Mint your first NFT following our how to mint an NFT guide.
Understanding the getL2NFTs
Function
The getL2NFTs
function is a view function provided by the ISCAccounts
interface. It takes an ISCAgentID
as an argument and returns an array of NFTID
objects. This function allows users to query and retrieve the list of L2 NFTs owned by a specified agent.
The ISCAgentID
represents the identifier of the agent (user or contract) whose NFTs you want to retrieve. You can get the AgentID
from the sender by calling ISC.sandbox.getSenderAccount()
.
The returned array contains the identifiers of the NFTs, which can then be used to fetch more details about each NFT if needed.
Implement the getL2NFTs
Function
Here’s a sample implementation to retrieve L2 NFTs owned by an account:
function getOwnedNFTs(ISCAgentID memory agentID) public view returns (NFTID[] memory) {
// Call the getL2NFTs function from the ISCAccounts contract
NFTID[] memory ownedNFTs = ISC.accounts.getL2NFTs(agenID);
return ownedNFTs;
}
Full Example Contract
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import "@iota/iscmagic/ISC.sol";
contract MyNFTContract {
function getOwnedNFTs(ISCAgentID memory agentID) public view returns (NFTID[] memory) {
// Call the getL2NFTs function from the ISCAccounts contract
NFTID[] memory ownedNFTs = ISC.accounts.getL2NFTs(agenID);
return ownedNFTs;
}
}