1

Assuming I have a contract to mint my own NFTs: (I'm thinking ERC-721, but not necessarily).

I want to create another smart contract that before doing anything, verifies that the address interacting with it (msg.sender) currently owns an NFT minted by that specific contract. Even better, if it could read an attribute, like the NFT id.

Is that even possible? I did some research on it, but apparently the address doesn't actually stores the balances/tokens, only eth. But I'm guessing it must be a way, given how those NFTs gallery works. If it's possible, how could I do it?

1 Answer 1

3

If contracts is an ERC721 then you can query how many tokens a user owns with balanceOf

uint count = token.balanceOf(user);
if (count > 0) {
    // User owns at least 1 index
}

If the tokens implements the optional ERC721Enumerable interface you can use tokenOfOwnerByIndex to retrieve the tokenId of owned tokens by a user.

try token.tokenOfOwnerByIndex(user, 0) returns (uint tokenId) {
    // First token owned by user
} catch (bytes memory) {
    // No tokens owned by user
}

For other details see the EIP 721, or EIP 1155.

2
  • Thanks Ismael, do you mind a follow up? How is token defined in your example? Since this is another contract, Does it need to be the one who deploys the ERC712 contract or is there someway to interface with it? Commented Mar 29, 2021 at 1:21
  • @Justcurious Something like IERC721Enumerable token = IERC721Enumerable(0x12341234...) should work if the token supports IERC721Enumerable.
    – Ismael
    Commented Mar 29, 2021 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.