我正在使用 openzeppelin 的库来构建一个 erc721 智能合约,并且遇到以下问题:
在运行我的测试以查看智能合约是否正确设置了 IPFS 时,它已挂起。以下是摩卡测试:
it('Upon minting receives a IPFS CID from minter'), async() => {
await contract.mint("Episode 1", "https://ipfs.io/ipfs/QmXNEsnJy8tHNtH1S2wQT2k2zTSg8BNWzvtPZ6ca69nL4n?filename=battle%20scene%20example.jpg")
var result= await episodes(0).tokenURI(1);
var expected= "https://ipfs.io/ipfs/QmXNEsnJy8tHNtH1S2wQT2k2zTSg8BNWzvtPZ6ca69nL4n?filename=battle%20scene%20example.jpg";
assert.equal(result, expected)
}
与此测试一起运行的 Solidity 代码如下:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract TheCondemned_Episode is ERC721URIStorage, ERC721Enumerable, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
string[] public episodes;
mapping(string => bool) _episodeExists;
event episodeCreated (string _episodeName, string _tokenURI);
constructor() ERC721("TheCondemned_e1", "TCe1") {
}
function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory _tokenURI) {
super.tokenURI(tokenId);
}
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
super._burn(tokenId);
}
function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) {
super._beforeTokenTransfer(from, to, tokenId);
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
function mint(string memory _episodeName, string memory _tokenURI) external onlyOwner {
require(!_episodeExists[_episodeName]);
require(episodes.length < 13, "Cannot make more than 13 episodes");
episodes.push(_episodeName);
_tokenIds.increment();
uint256 newNftTokenId = _tokenIds.current();
address receiver= msg.sender;
_mint(receiver, newNftTokenId);
_setTokenURI(newNftTokenId, _tokenURI);
_episodeExists[_episodeName]= true;
emit episodeCreated(_episodeName, tokenURI(newNftTokenId));
}
}
在尝试编译智能合约时,我不断收到错误消息,指出我必须覆盖上面代码中显示的函数。我怀疑测试未决结果的原因是因为智能合约运行时间过长,试图执行 tokenURI 函数。我在正确的轨道上吗?如果是这样,有哪些可能的解决方案可以通过测试?