Error while trying to get tokenId by index from Matic Polygon Testnet account
"maticProvider": "https://polygon-mumbai.infura.io/v3/<project_id>",
"parentProvider": "https://goerli.infura.io/v3/<project_id>"
Code:
const network = new Network(config.network, config.version);
// TODO do somthing about the unused
const MaticNetwork = network.Matic;
const MainNetwork = network.Main;
const matic = new Matic({
maticProvider: config.maticProvider,
parentProvider: config.parentProvider,
rootChain: MainNetwork.Contracts.RootChain,
withdrawManager: MainNetwork.Contracts.WithdrawManagerProxy,
depositManager: MainNetwork.Contracts.DepositManagerProxy,
registry: MainNetwork.Contracts.Registry
});
matic.tokenOfOwnerByIndexERC721(
'<accountAddress>',
'<tokenAddress>',
0, {
from: '<accountAddress>',
gas: 150000
})
.then(console.log)
This throws the error
(node:16166) UnhandledPromiseRejectionWarning: Error: Returned error: execution reverted
at Object.ErrorResponse (/Users/tcadmin/Documents/hub/trove/trove-matic/node_modules/web3-core-helpers/lib/errors.js:28:19)
at /Users/tcadmin/Documents/hub/trove/trove-matic/node_modules/web3-core-requestmanager/lib/index.js:303:36
at XMLHttpRequest.request.onreadystatechange (/Users/tcadmin/Documents/hub/trove/trove-matic/node_modules/web3-providers-http/lib/index.js:98:13)
at XMLHttpRequestEventTarget.dispatchEvent (/Users/tcadmin/Documents/hub/trove/trove-matic/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
at XMLHttpRequest._setReadyState (/Users/tcadmin/Documents/hub/trove/trove-matic/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
at XMLHttpRequest._onHttpResponseEnd (/Users/tcadmin/Documents/hub/trove/trove-matic/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
at IncomingMessage.<anonymous> (/Users/tcadmin/Documents/hub/trove/trove-matic/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1221:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Contract:
//Contract based on https://docs.openzeppelin.com/contracts/3.x/erc721
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
// implements the ERC721 standard
import "../node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";
// keeps track of the number of tokens issued
import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
// Accessing the Ownable method ensures that only the creator of the smart contract can interact with it
contract MyNFT is ERC721, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
using Strings for uint256;
// the name and symbol for the NFT
constructor()//(string memory _name, string memory _symbol)
ERC721("TroveNFT", "TNFT") {}
// Optional mapping for token URIs
mapping (uint256 => string) private _tokenURIs;
// Base URI
string private _baseURIextended;
string private _creatorSign;
function _setCreatorSignature(string memory signature_) internal virtual {
_creatorSign = signature_;
}
function _creatorSignature() public view returns (string memory) {
return _creatorSign;
}
function setBaseURI(string memory baseURI_) external onlyOwner() {
_baseURIextended = baseURI_;
}
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token");
_tokenURIs[tokenId] = _tokenURI;
}
function _baseURI() internal view virtual override returns (string memory) {
return _baseURIextended;
}
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
string memory _tokenURI = _tokenURIs[tokenId];
string memory base = _baseURI();
// If there is no base URI, return the token URI.
if (bytes(base).length == 0) {
return _tokenURI;
}
// If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
if (bytes(_tokenURI).length > 0) {
return string(abi.encodePacked(base, _tokenURI));
}
// If there is a baseURI but no tokenURI concatenate the tokenID to the baseURI.
return string(abi.encodePacked(base, tokenId.toString()));
}
// Create a function to mint/create the NFT
// receiver takes a type of address. This is the wallet address of the user that should receive the NFT minted using the smart contract
// tokenURI takes a string that contains metadata about the NFT
function mintNFT(address receiver,
string memory creator,
string memory tokenURI_) public onlyOwner returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(receiver, newItemId);
_setTokenURI(newItemId, tokenURI_);
_setCreatorSignature(creator);
// returns the id for the newly created token
return newItemId;
}
}
Please help !
Found the issue.
The contract needs to extend ERC721Enumerable
to have tokenOfOwnerByIndex
implemented in it.