I have a function in my platform that finds out if a user is the holder of a specific token
const [tokenId, setTokenId] = useState(false);
const checkOwners = async () => {
if (ethereum) {
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
const nftContract = new ethers.Contract(contractAddress, abi, signer);
if (currentAccount !== null && nftContract !== null) {
let walletOfowners = await nftContract.tokensOfOwner(currentAccount);
if (walletOfowners.length > 0) {
setTokenId(true);
} else {
setTokenId(false);
}
}
}
};
But I want my code to get which tokens (Ids) the connected users hold. Smart contract does have a tokensOfOwner function.
//Returns all IDs owned by a particular holder.
function tokensOfOwner(address _owner) external view returns (uint[] memory) {
uint tokenCount = balanceOf(_owner);
uint[] memory tokensId = new uint256[](tokenCount);
for (uint i = 0; i < tokenCount; i++) {
tokensId[i] = tokenOfOwnerByIndex(_owner, i);
}
return tokensId;
}
```
I found out the function I needed:
const tokensIdsOfOwner = async () => {
if (ethereum) {
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
const nftContract = new ethers.Contract(contractAddress, abi, signer);
if (currentAccount !== null && nftContract !== null) {
let ownerTokenId = await nftContract.tokensOfOwner(currentAccount);
console.log(ownerTokenId.toString());
setOwnerIds(ownerTokenId.toString());
}
}
};