Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieving web3 contract.ownerOf NFT ERC721

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);

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 someone point me to the right direction?

like image 389
Crays Avatar asked Oct 31 '25 09:10

Crays


1 Answers

token are not on the balance of the contract, they are inside of it. According to the erc721 - you have to invoke the balanceOf method of the contract.

like image 150
Mihey Mik Avatar answered Nov 02 '25 00:11

Mihey Mik