How to check ERC721 NFT ownership using web3 in JavaScript?

I’m new to web3 and trying to figure out how to verify the owner of an ERC721 NFT using JavaScript. I’ve got a working example for checking ERC20 token balances, but I’m stuck on how to adapt it for NFTs.

Here’s a simplified version of what I’m working with:

function checkNFTOwnership(contractAddress, tokenId) {
  const abi = [
    {
      inputs: [{ name: 'tokenId', type: 'uint256' }],
      name: 'ownerOf',
      outputs: [{ name: '', type: 'address' }],
      type: 'function'
    }
  ];

  const contract = new web3.eth.Contract(abi, contractAddress);
  
  contract.methods.ownerOf(tokenId).call((error, result) => {
    if (error) {
      console.error('Error:', error);
    } else {
      console.log('Owner:', result);
    }
  });
}

// Usage
checkNFTOwnership('0x1234...', '42');

When I run this, I’m not getting any result. What am I doing wrong? How can I correctly check the owner of a specific NFT token ID?

Your approach is on the right track, but there are a few potential issues to address. First, ensure your Web3 provider is correctly set up and connected to the appropriate network. This is crucial for interacting with the blockchain.

Secondly, verify that the contract ABI you’re using is complete and accurate for the specific NFT contract you’re querying. Some contracts may have slightly different function signatures or additional methods.

Consider using the OpenZeppelin ERC721 ABI as a reliable standard. You can import it directly or use a minimal version focusing on the ‘ownerOf’ function.

Lastly, error handling is important. Wrap your function call in a try-catch block to capture and log any errors that occur during execution. This will provide more insight into what might be going wrong.

If you’re still encountering issues, you may want to use a blockchain explorer to confirm the contract address and token ID are valid and exist on the network you’re querying.

hey there nina! i’ve been dabbling with nft stuff too, so i feel your pain :sweat_smile: your code looks pretty close actually! a couple things to consider:

have you made sure your web3 instance is connected to the right network? sometimes that trips me up.

also, double-check that contract address - it’s gotta be exact or nothing works (learned that the hard way lol).

one thing that helped me was using await instead of callbacks. makes the code a bit cleaner. maybe try something like:

async function checkNFTOwnership(contractAddress, tokenId) {
  try {
    const owner = await contract.methods.ownerOf(tokenId).call();
    console.log('Owner:', owner);
  } catch (error) {
    console.error('oops:', error);
  }
}

oh, and make sure you’re using a recent version of web3.js. some older versions can be finicky with newer contracts.

let me know if any of that helps! what nft project are you working on, btw? sounds interesting!

yo nina, ur code looks good! but make sure ur web3 provider is set up right. also, double-check the contract address - gotta be exact. try using async/await instead of callbacks, it’s cleaner:

async function checkNFTOwnership(contractAddress, tokenId) {
  try {
    const owner = await contract.methods.ownerOf(tokenId).call();
    console.log('Owner:', owner);
  } catch (error) {
    console.error('Oops:', error);
  }
}

hope this helps! what NFT project u workin on?