查看用户拥有的代币列表的简单方法?
我确信这可以在前端以及可靠性上完成。我看到一些看起来效率低下的帖子,根据我的理解,当 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在第一篇文章中修复了代码,问题是我必须声明一个名为 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!