[英]Solidity NFT metadata loading issue
I am creating an NFT collection, and am testing on ropsten.我正在创建一个 NFT 集合,并在 ropsten 上进行测试。 I have my NFT metadata on my website for example boredApes.com/tokens/3.json but the issue is the contract goes to boredApes.com/tokens/3 and not the.json part.
我的网站上有我的 NFT 元数据,例如 boredApes.com/tokens/3.json,但问题是合同转到了 boredApes.com/tokens/3 而不是 .json 部分。 How should I change to contract to load this metadata.
我应该如何更改合同以加载此元数据。 The current solidity function is
当前的 solidity function 是
function _baseURI() internal pure override returns (string memory) { return "https://boredApes.com/tokens/"; }
The _baseURI()
function sets just the beginning of the URI. _baseURI()
function 仅设置 URI 的开头。 You need to override the tokenURI()
function as well, appending the .json
string at the end of the returned URI.您还需要覆盖
tokenURI()
function,在返回的 URI 末尾附加.json
字符串。
pragma solidity ^0.8;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
contract MyCollection is ERC721 {
constructor() ERC721("MyCollection", "MyC") {
_mint(msg.sender, 1);
}
function _baseURI() internal pure override returns (string memory) {
return "https://boredApes.com/tokens/";
}
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
// the `super` keyword references the parent function of the same name
string memory uri = super.tokenURI(tokenId);
return string(abi.encodePacked(uri, ".json"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.