【问题标题】:Why does my transaction revert when calling another contract function?为什么调用另一个合约函数时我的交易会恢复?
【发布时间】:2021-12-09 19:37:40
【问题描述】:

我正在尝试调用部署在以太坊上的 NFT 合约中的一个函数,以及 Remix 中的另一个合约,旨在将其部署在 Polygon 上。

这是我要调用的 NFT 函数:

function allGenzeesOfWallet(address owner)
        public
        view
        returns (uint256[] memory)
    {
        uint256 tokenCount = balanceOf(owner);

        uint256[] memory tokensId = new uint256[](tokenCount);
        for (uint256 i; i < tokenCount; i++) {
            tokensId[i] = tokenOfOwnerByIndex(owner, i);
        }
        return tokensId;
    }

这是我正在写的与它互动的新合同:

interface IGenzees {
    function allGenzeesOfWallet(address owner) external view returns(uint[] memory);
}

contract CallNFT {
    IGenzees public NFTcontract;

    constructor () {
        NFTcontract = IGenzees(0x201675fBFAAAC3A51371E4C31FF73Ac14ceE2A5A);
    }

    function getNoNFTsOwned(address user) public view returns(uint[] memory) {
        return NFTcontract.allGenzeesOfWallet(user);
    }
    function setNFTcontract(address addr) external { // Set NFT contract
        NFTcontract = IGenzees(addr);
    }
}

但是当我调用 getNoNFTsOwned 函数时,控制台会说:

call to CallNFT.getNoNFTsOwned errored: VM error: revert.

我做错了什么?

【问题讨论】:

    标签: solidity


    【解决方案1】:

    Remix VM 和以太坊主网是两个独立的网络。无法从另一个网络达成部署在一个网络上的合同。

    您可以从主网设置一个本地分叉,它可以有效地让您与来自 Remix VM 的主网合约的本地副本进行交互。您可以在this answer 找到更多信息。注意:它不会影响实际的主网,只会影响您本地的主网副本。

    【讨论】:

    • 谢谢,我的代码看起来不错吗?这一定意味着如果我在 Polygon 上部署我的 ERC20 代币,我就不能在以太坊上调用 NFT 合约?
    • @cryptoboi 您的代码很好 - 问题在于代码的应用程序(特别是无法访问的网络)......我在 Polygon 方面的经验并不多。据我了解,应该可以以某种方式与 Polygon 的 Ethereum 主网交互,因为 Polygon 网络和 Ethereum 之间有一座桥梁。但很可能不是从一开始,它可能需要一些额外的设置。
    猜你喜欢
    • 1970-01-01
    • 2023-02-03
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2022-09-20
    • 2018-10-31
    • 2018-12-09
    相关资源
    最近更新 更多