我正在用开放的齐柏林飞艇开发一份关于ETH的NFT合同,并且达到了在rinkeby上部署的阶段。然而,tokenURI和contractURI出现了一个问题,因为图像和元数据没有在开放海域显示。
我像这样实现了tokenURI和ContractURI:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
//access control
import "@openzeppelin/contracts/access/Ownable.sol";
// Helper functions OpenZeppelin provides.
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "./libraries/Base64.sol";
contract MetropolisWorldGenesis is ERC721, Ownable {
....
string private _contractURI;
function contractURI() public view returns (string memory) {
return _contractURI;
}
function tokenURI(uint256 _tokenId) public view override returns (string memory) {
PropertyAttributes memory propAttributes = nftHolderAttributes[_tokenId];
string memory json = Base64.encode(
abi.encodePacked(
'{"name": "',
propAttributes.name,
'", "description": "',propAttributes.description,'", "image": "',
propAttributes.image,
'", "attributes": [ { "trait_type": "Tower", "value": ',propAttributes.properties.tower,', "trait_type": "District", "value":',propAttributes.properties.district, ', "trait_type": "neighborhood", "value":',propAttributes.properties.neighborhood,',]}'
)
);
string memory output = string(
abi.encodePacked("data:application/json;base64,", json)
);
return output;
}
function setContractURI(
string memory name, string memory desc,
string memory image,string memory link,
uint royalty) public onlyOwner() {
string memory x = Base64.encode(
abi.encodePacked(
'{"name": "',
name,
'", "description": "',
desc,
'", "image": "',
image,
'", "external_link": "',
link,
'","seller_fee_basis_points":"', royalty, // 100 Indicates a 1% seller fee
'", "fee_recipient": "0xA97F337c39cccE66adfeCB2BF99C1DdC54C2D721" }' // Where seller fees will be paid to.}
)
);
_contractURI = string(abi.encodePacked(
"data:application/json;base64,", x
));
console.log("contract uri updated");
}
}当在本地运行时,返回tokenURI将给出以下输出,该输出将解析为更正浏览器中的数据:
data:application/json;base64,eyJuYW1lIjogIkdpbGRlZCBBdHRpYyIsICJkZXNjcmlwdGlvbiI6ICJUaGUgQXN0cm9ub21lcidzIFN0dWRpbwpVbmRlciB0aGUgZG9tZSBpcyBhIHBsYW5ldHJpdW0gClJlc2VhcmNoZXMgdGhlIG9yaWdpbnMgb2YgdGhlIE1ldHJvcG9saXMiLCAiaW1hZ2UiOiAiaXBmczovL1FtWnc3NzY1ZG04aHBIRndITDl3b29RSkNzSkd6TFR3WTIyNjVDR1lpeFB3aUciLCAiYXR0cmlidXRlcyI6IFsgeyAidHJhaXRfdHlwZSI6ICJUb3dlciIsICJ2YWx1ZSI6IFRvd2VyIDEsICJ0cmFpdF90eXBlIjogIkRpc3RyaWN0IiwgInZhbHVlIjpIaWdoLUZseWVyLCAidHJhaXRfdHlwZSI6ICJuZWlnaGJvcmhvb2QiLCAidmFsdWUiOkZyZWUgQWxsZXkgUXVhcnRlcnMsXX0=你知道我哪里出错了吗?
发布于 2022-04-17 20:41:38
因此,正如我认为这是创建动态元数据的一种完全可行的方法一样,我所需要的更改只是在令牌URI函数的末尾添加正确的方括号。所以这个函数就会变成这个。一个微小但令人沮丧的改变。
function tokenURI(uint256 _tokenId) public view override returns (string memory) {
PropertyAttributes memory propAttributes = nftHolderAttributes[_tokenId];
string memory json = Base64.encode(
abi.encodePacked(
'{"name": "',
propAttributes.name,
'", "description": "',propAttributes.description,'", "image": "',
propAttributes.image,
'", "attributes": [ { "trait_type": "Tower", "value": ',propAttributes.properties.tower,', "trait_type": "District", "value":',propAttributes.properties.district, ', "trait_type": "neighborhood", "value":',propAttributes.properties.neighborhood,'}]}'
)
);
string memory output = string(
abi.encodePacked("data:application/json;base64,", json)
);
return output;
}我还了解到,所有变量都需要是字符串,因此contractURI函数中的版税变量导致它不能工作。和设置的契约URI函数需要更改为此。
function setContractURI(
string memory name, string memory desc,
string memory image,string memory link,
string memory royalty) public onlyOwner() {
string memory x = Base64.encode(
abi.encodePacked(
'{"name": "',
name,
'", "description": "',
desc,
'", "image": "',
image,
'", "external_link": "',
link,
'","seller_fee_basis_points":"', royalty, // 100 Indicates a 1% seller fee
'", "fee_recipient": "0xA97F337c39cccE66adfeCB2BF99C1DdC54C2D721" }' // Where seller fees will be paid to.}
)
);
_contractURI = string(abi.encodePacked(
"data:application/json;base64,", x
));
console.log("contract uri updated");
}发布于 2022-03-28 10:56:12
令牌uri应该是URI,而不是JSON类型。您应该将您的jsons上传到服务器或类似IPFS之类的内容,并将您的URI带到您的合同中。
有关更多信息,请查看提案的原始网站。特别是检查元数据部分。
https://ethereum.stackexchange.com/questions/124874
复制相似问题