简体   繁体   English

在 ERC721 上按所有者列出所有令牌 ID

[英]list all token IDs by owner at ERC721

I have a simple ERC721 smart contract used for testing purposes, I was wondering if it is possible to query the token IDs a specific wallet owns in that smart contract.我有一个用于测试目的的简单 ERC721 智能合约,我想知道是否可以查询特定钱包在该智能合约中拥有的令牌 ID。

eventually I would like to display the NFTs minted by the user in the website.最终我想在网站上显示用户铸造的 NFT。

balanceOf in the contract returns the number of tokens owned by the user, but no further info.合约中的 balanceOf 返回用户拥有的代币数量,但没有更多信息。

my test smart contract:我的测试智能合约:

https://mumbai.polygonscan.com/token/0x263d3b61ad4f4c44f694a4464a8f2a0b6514c064?a=0x330d7ac5f24dc3570c2afe3f3a57d9c66e0795e8#readContract https://mumbai.polygonscan.com/token/0x263d3b61ad4f4c44f694a4464a8f2a0b6514c064?a=0x330d7ac5f24dc3570c2afe3f3a57d9c66e075合同

Looks like your contract inherits an extension of ERC721 called ERC721Enumerable.看起来你的合约继承了一个名为 ERC721Enumerable 的 ERC721 扩展。

This extension adds a function called tokenOfOwnerByIndex(address owner, uint256 index) , which can used to list all tokens owned by a user ( owner ).此扩展添加了一个名为tokenOfOwnerByIndex(address owner, uint256 index)的 function ,可用于列出用户( owner )拥有的所有令牌。 So you can call this function multiple times, looping index from 0 to balanceOf(owner) to get all tokens ids.因此,您可以多次调用此 function,将index0循环到balanceOf(owner)以获取所有令牌 ID。

I have required to fetch all NFT of specific user address from contract address.我要求从合约地址中获取特定用户地址的所有 NFT。

I am doing this, but still got error, first line is not working why?, please help:我正在这样做,但仍然出现错误,第一行不起作用为什么?请帮助:

  const Balance = await contract1.methods.balanceOf(accounts[0]).call();
  for(let i=0; i < Balance ; i++)
  {
    const tokenId = await contract1.methods.tokenOfOwnerByIndex(accounts[0],i).call();

    let tokenMetadataURI = await contract1.methods.tokenURI(tokenId).call();
    console.log(tokenMetadataURI);

    const tokenMetadata = await fetch(tokenMetadataURI).then((response) => response.json())
    console.log(tokenMetadata);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM