查看用户拥有的代币列表的简单方法?

发布于 2025-01-18 03:20:31 字数 552 浏览 2 评论 0原文

我确信这可以在前端以及可靠性上完成。我看到一些看起来效率低下的帖子,根据我的理解,当 ERC721 包已经具有获取这些信息所需的功能时,他们正在创建新的映射并将不必要的数据存储到区块链中。

找到第一部分的答案了!

 function ownerOfTokenIds(address tokenOwner) external view returns (uint256[] memory) {
        uint256[] memory result = new uint256[](balanceOf(tokenOwner));
        uint256 counter = 0;
        for (uint256 i = 0; i < tokenCounter; i++) {
            if (ownerOf(i) == tokenOwner) {
                result[counter] = i;
                counter++;
            }
        }
        return result;
    }

I'm sure this could be done on the front end as well as from solidity. I saw a few posts that seemed inefficient, where they are creating a new mapping and storing unnecessary data to the blockchain when the ERC721 package already has the functions it needs in order to procure this information, from my understanding.

Figured out the answer to the first part!!

 function ownerOfTokenIds(address tokenOwner) external view returns (uint256[] memory) {
        uint256[] memory result = new uint256[](balanceOf(tokenOwner));
        uint256 counter = 0;
        for (uint256 i = 0; i < tokenCounter; i++) {
            if (ownerOf(i) == tokenOwner) {
                result[counter] = i;
                counter++;
            }
        }
        return result;
    }

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

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

发布评论

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

评论(1

烟沫凡尘 2025-01-25 03:20:31

我在第一篇文章中修复了代码,问题是我必须声明一个名为 result 的 uint[] 内存变量,并将其设置为等于一个新的 uint 数组。数组的长度应该是该所有者拥有的令牌数量。

然后,我想这可以在“扩展运算符”的前端使用!

I fixed the code in my first post, the issue was that I had to declare a uint[] memory variable called result and set it equal to a new uint array. The length of the array should be however many tokens this owner owns.

This can then be used on the front end in a "spread operator" i'd presume!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文