与智能合约互动以获取令牌ID

发布于 2025-02-13 07:37:01 字数 1165 浏览 0 评论 0原文

我的平台上有一个功能,可以发现用户是否是特定令牌的持有人,

const [tokenId, setTokenId] = useState(false);
const checkOwners = async () => {
    if (ethereum) {      
      const provider = new ethers.providers.Web3Provider(ethereum);
      const signer = provider.getSigner();
      const nftContract = new ethers.Contract(contractAddress, abi, signer);      
      if (currentAccount !== null && nftContract !== null) {
        let walletOfowners = await nftContract.tokensOfOwner(currentAccount);
        if (walletOfowners.length > 0) {
          setTokenId(true);
        } else {
          setTokenId(false);
        }
      }
    }
};

但我希望我的代码获得连接用户所拥有的令牌(IDS)。智能合约确实具有tokensofowner功能。

 //Returns all IDs owned by a particular holder.
    function tokensOfOwner(address _owner) external view returns (uint[] memory) {

        uint tokenCount = balanceOf(_owner);
        uint[] memory tokensId = new uint256[](tokenCount);

        for (uint i = 0; i < tokenCount; i++) {
            tokensId[i] = tokenOfOwnerByIndex(_owner, i);
        }
        return tokensId;
    }
    ```



I have a function in my platform that finds out if a user is the holder of a specific token

const [tokenId, setTokenId] = useState(false);
const checkOwners = async () => {
    if (ethereum) {      
      const provider = new ethers.providers.Web3Provider(ethereum);
      const signer = provider.getSigner();
      const nftContract = new ethers.Contract(contractAddress, abi, signer);      
      if (currentAccount !== null && nftContract !== null) {
        let walletOfowners = await nftContract.tokensOfOwner(currentAccount);
        if (walletOfowners.length > 0) {
          setTokenId(true);
        } else {
          setTokenId(false);
        }
      }
    }
};

But I want my code to get which tokens (Ids) the connected users hold. Smart contract does have a tokensOfOwner function.

 //Returns all IDs owned by a particular holder.
    function tokensOfOwner(address _owner) external view returns (uint[] memory) {

        uint tokenCount = balanceOf(_owner);
        uint[] memory tokensId = new uint256[](tokenCount);

        for (uint i = 0; i < tokenCount; i++) {
            tokensId[i] = tokenOfOwnerByIndex(_owner, i);
        }
        return tokensId;
    }
    ```



如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2025-02-20 07:37:01

我发现了我需要的功能:

  const tokensIdsOfOwner = async () => {
    if (ethereum) {
       const provider = new ethers.providers.Web3Provider(ethereum);
       const signer = provider.getSigner();
       const nftContract = new ethers.Contract(contractAddress, abi, signer);        
         if (currentAccount !== null && nftContract !== null) {            
           let ownerTokenId = await nftContract.tokensOfOwner(currentAccount);            
           console.log(ownerTokenId.toString());
           setOwnerIds(ownerTokenId.toString());
         }
    }
  };

I found out the function I needed:

  const tokensIdsOfOwner = async () => {
    if (ethereum) {
       const provider = new ethers.providers.Web3Provider(ethereum);
       const signer = provider.getSigner();
       const nftContract = new ethers.Contract(contractAddress, abi, signer);        
         if (currentAccount !== null && nftContract !== null) {            
           let ownerTokenId = await nftContract.tokensOfOwner(currentAccount);            
           console.log(ownerTokenId.toString());
           setOwnerIds(ownerTokenId.toString());
         }
    }
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文