-
Notifications
You must be signed in to change notification settings - Fork 11.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ERC721Enumerable - Add back _tokensOfOwner()
#3106
Comments
The removal was not accidental, it's explained in #2160. Working with arbitrary sized arrays in Solidity is not a good idea, as it's a potential attack vector. I don't think this is something we should add back. What is your use case? |
I can see the problem if someone tried to loop over a long array in Solidity, is there any other problem that I may be missing? The thing is that in a webapp, if the user has 20 or more NFTs, I have to make 20 requests to the RPC to get all the ids. Maybe 20 more two get the metadata, but that I can have previously cached in the server. And now thinking about it, maybe I could just create a secondary contract with |
So, I just tested this, and it works! I know it is an unbounded loop, but it doesn't seem to be a problem for a RPC. pragma solidity ^0.8.0;
// SPDX-License-Identifier: MIT
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
contract ListNFT {
function listUserNFTs(address contractAddress, address owner) external view returns (uint[] memory) {
uint balance = IERC721Enumerable(contractAddress).balanceOf(owner);
uint[] memory tokens = new uint[](balance);
for (uint i=0; i<balance; i++) {
tokens[i] = (IERC721Enumerable(contractAddress).tokenOfOwnerByIndex(owner, i));
}
return tokens;
}
} |
That's right, RPC |
For me this can be closed then, unless someone else has anything else to add. |
tokensOfOwner()
was first suggest on #1512 and implemented on #1522 and was removed in a bulk edit that makes me wonder if it was removed accidentally. bd07784This function helps when coding web3 apps. Instead of calling the RPC multiple times to get all NFTs owned by the user, it can be done at once by calling
tokensOfOwner()
The text was updated successfully, but these errors were encountered: