我正在创建一个继承 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);
}
然后我有另一个智能合约,书店,这将是你可以买卖这些 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 在区块链上的反映,但当我尝试在同一 tokenID 上调用 BookStore 中的此函数时调用 BookCreation.ownerOf(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"){
}