【问题标题】:Execution reverted error while tokenOfOwnerByIndexERC721tokenOfOwnerByIndexERC721 时执行恢复错误
【发布时间】:2021-06-19 19:05:35
【问题描述】:

尝试从 Matic Polygon 测试网帐户按索引获取 tokenId 时出错

"maticProvider": "https://polygon-mumbai.infura.io/v3/<project_id>",
"parentProvider": "https://goerli.infura.io/v3/<project_id>"

代码:

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)

这会引发错误

(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 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;
        }
    }

请帮忙!

【问题讨论】:

  • 如何初始化matic 变量?我从错误堆栈中看到它是一个 web3js Contract 实例,但是您使用的是 web3 包还是一些包装器?你能分享Contract 实例参数(ABI 和地址),以及调用参数(&lt;account address&gt;&lt;my nft address&gt;)吗? ...错误源自合同(不一定是matic 合同,可能是嵌套调用),而不是您的代码。但它会在没有任何自定义消息的情况下引发一般错误,因此如果没有特定数据就很难进行故障排除。
  • @PetrHejda 更新了您询问的详细信息。感谢您的帮助
  • 我正在尝试解决问题的可能方法。但找不到任何。我的 getBalance API 工作正常并返回 9。这是我帐户中 NFT 的确切数量。在最新版本的 openzepplin 中,他们弃用了 setTokenURI 和相关 API。他们是否也弃用了tokenOfOwnerByIndex 或等效的?

标签: ethereum matic


【解决方案1】:

发现问题。

合约需要扩展ERC721Enumerable 以在其中实现tokenOfOwnerByIndex

【讨论】:

    猜你喜欢
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2015-02-06
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多