Per eip-721, there's an optional "enumeration extension". So what I did was I called balanceOf
to get the number of tokens owned by the account and called tokenOfOwnerByIndex
in a loop to get each owned token ID.
for(var i = 0; i < balance.toNumber(); i++) {
myERC721Token.tokenOfOwnerByIndex.call(web3.eth.accounts[0], i)
.then((id) => { ... });
}
As a result, it actually listed tokens owned by a user.
However, I am not sure how the code actually works because tokenOfOwnerByIndex
returns mapping variable ownedTokens
, but I've never assigned any value to ownedTokens
! But how can it still return the tokens of an owner? FYI, I mint the token like below.
import "openzeppelin-solidity/contracts/token/ERC721/ERC721Token.sol";
contract MyERC721Token is ERC721Token {
function createNewToken(...) {
_mint(msg.sender, index);
}
}
If you take a look at openzeppelin's contract, _mint
that I called here never assigns any value to ownedTokens
. Can anyone see the relationships between the contracts that I can find out why tokenOfOwnerByIndex
is working for me?