【问题标题】:Not able to access ERC721 OpenZeppelin NFTs from separate contract that inherits NFT contract无法从继承 NFT 合约的单独合约访问 ERC721 OpenZeppelin NFT
【发布时间】:2021-08-03 15:45:06
【问题描述】:

我正在创建一个继承 openzeppelin ERC721 NFT 智能合约的 NFT。我有一个继承 ERC721 的合同 BookCreation。这个智能合约是我在 mintBook() 函数中铸造 NFT 的地方:

    function mintBook(uint256 _bookID, uint256 _editionID) external onlyBookAuthor(_bookID) {
        _tokenIds = _tokenIds + 1;
        books[_bookID]._numMinted = books[_bookID]._numMinted + 1;
        books[_bookID].editions[_editionID]._numMinted = books[_bookID].editions[_editionID]._numMinted + 1;

        emit BookMinted(_tokenIds, _editionID, _bookID, books[_bookID].authorID);
        _safeMint(msg.sender, _tokenIds);
    }

然后我有另一个智能合约 BookStore,它将成为您可以买卖这些 NFT 的市场。

我已经在我的 BookCreation 合约中覆盖了 ERC721 函数 ownerOf(uint256 tokenID)。

    function ownerOf(uint256 tokenID) public view virtual override returns (address) {
        return super.ownerOf(tokenID);
    }

然后我在 BookStore 中这样调用这个函数(我也试过用 super.ownerOf(_tokenID) 和 ownerOf(_tokenID) 代替 (BookCreation.ownerOf(_tokenID)):

    modifier onlyBookOwner(uint256 _tokenID) {
        require(BookCreation.ownerOf(_tokenID) == msg.sender,"This isn't your book!");
        _;
    }

我遇到了一个问题,虽然我可以在 BookCreation 智能合约中铸造一本书,并通过在 BookCreation 中调用 ownerOf(tokenId) 来查看该 NFT 反映在区块链上,但当我尝试在 BookStore 上调用此函数时调用 BookCreation.ownerOf(tokenId) 获取相同的 tokenID,无法看到创建的 NFT。

我有点不确定如何能够读取在单独的智能合约中创建的 NFT,任何指导都会有所帮助!

BookCreation 类的其他相关部分:

import "../node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";

contract BookCreation is ERC721, Ownable {
    uint256 private _tokenIds;
     /**
     * @dev represents a submitted book
     * Along with editions associated with it (initially empty)
     */
    struct Book {
        string title;
        uint256 authorID;
        uint256 _bookID;
        uint256 _numMinted;
        uint256 _numEditions;
        mapping (uint256 => Edition) editions;
    }
    /** @dev Represents specific edition of a specific book
     *    (Advanced Readers Copy, Initial Publishing, 1yr Special Edition, etc)
    */
    struct Edition{
        uint256 _editionID;
        uint256 _bookID;
        uint256 _numMinted;
        string editionName;
    }
    // BookId mapped to the Book it represents
    mapping (uint256 => Book) private books;
    /**
     * @dev Constructs ERC721 "Book" token collection with symbol "TLB"
     */
    constructor() ERC721("Book", "TLB"){
    }

【问题讨论】:

    标签: inheritance solidity smartcontracts openzeppelin nft


    【解决方案1】:

    如果您能提供更多有用的代码。否则,我的猜测是与 super 关键字有关。 https://ethereum.stackexchange.com/questions/12920/what-does-the-keyword-super-in-solidity-do

    【讨论】:

    • 谢谢!我用更多代码编辑了这篇文章,希望能更好地了解我想要做什么。
    • 合约 BookCreation 是 ERC721,Ownable Solidity 从右到左检查继承,从大多数派生到大多数。您可以尝试将这些顺序切换为:Ownable、ERC721
    猜你喜欢
    • 2022-12-12
    • 2021-11-05
    • 2022-08-12
    • 2021-11-14
    • 1970-01-01
    • 2021-10-18
    • 2023-01-25
    • 2021-10-08
    • 2022-08-14
    相关资源
    最近更新 更多