2

I'm playing around with web3 and frontend trying to read something off the ethereum blockchain. What i wanted to do is to verify the owner of a NFT (erc721).

I've used this piece of code here that enable me to check the balanceOf a certain ERC20 linked to the connected wallet address

  function getERC20TokenBalance(tokenAddress, walletAddress, callback) {

  let minABI = [
    // balanceOf
    {
      "constant":true,
      "inputs":[{"name":"_owner","type":"address"}],
      "name":"balanceOf",
      "outputs":[{"name":"balance","type":"uint256"}],
      "type":"function"
    },
    // decimals
    {
      "constant":true,
      "inputs":[],
      "name":"decimals",
      "outputs":[{"name":"","type":"uint8"}],
      "type":"function"
    }
  ];

  let contract = web3.eth.contract(minABI).at(tokenAddress);
  
  contract.balanceOf(walletAddress, (error, balance) => {
    // ERC20トークンの decimals を取得
    contract.decimals((error, decimals) => {
      balance = balance.div(10**decimals);
      console.log(balance.toString());
      callback(balance);
    });
  });
  
}

setInterval(function() {
  let tokenAddress = '0x50f5474724e0ee42d9a4e711ccfb275809fd6d4a';
    let walletAddress = web3.eth.accounts[0];
          if(tokenAddress != "" && walletAddress != "") {
        getERC20TokenBalance(tokenAddress, walletAddress, (balance) => {
          document.getElementById('text-bal').innerText = balance.toFixed(3);
        });        
      };
        }, 100);

now i would like to verify a certain NFT, which i used 0x50f5474724e0ee42d9a4e711ccfb275809fd6d4a (a SANDBOX land contract). With balanceOf, it does show me this address has LAND NFT in it, but i wanted to check for the tokenID, by changing it to

let contract = web3.eth.contract(minABI).at(tokenAddress);
contract.ownerOf('18429');

it returns nothing, can you guys point me to the right direction :) ?

2 Answers 2

3

Better late than never!

This is how I'm checking for the owner of an NFT in my dApp

async whoOwnThisToken(tokenId) {
    const contract = new web3.eth.Contract(abi, address);
    // Don't forget to use await and .call()
    const owner = await contract.methods.ownerOf(tokenId).call();
}
0

the minAbi needs to be modified to have the IERC721 function call you wish to use

    {
  "constant":true,
  "inputs":[{"name":"tokenId","type":"uint256"}],
  "name":"ownerOf",
  "outputs":[{"name":"owner","type":"address"}],
  "type":"function"
}

before you can call the method with

contract.methods.ownerOf(tokenId).call().then()...

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.