为什么我的 NFT 元数据没有显示在 opensea 上?
Why is my NFT metadata not showing on opensea?
我正在使用开放式 Zeppelin 在 ETH 上开发 NFT 合约,并且到了部署在 rinkeby 上的阶段。然而,tokenURI 和 contractURI 似乎存在问题,因为图像和元数据未在 opensea 上显示。
我这样实现 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");
}
}
当 运行 在本地 return 时,tokenURI 给出以下输出,解析为浏览器中的正确数据:
data:application/json;base64,eyJuYW1lIjogIkdpbGRlZCBBdHRpYyIsICJkZXNjcmlwdGlvbiI6ICJUaGUgQXN0cm9ub21lcidzIFN0dWRpbwpVbmRlciB0aGUgZG9tZSBpcyBhIHBsYW5ldHJpdW0gClJlc2VhcmNoZXMgdGhlIG9yaWdpbnMgb2YgdGhlIE1ldHJvcG9saXMiLCAiaW1hZ2UiOiAiaXBmczovL1FtWnc3NzY1ZG04aHBIRndITDl3b29RSkNzSkd6TFR3WTIyNjVDR1lpeFB3aUciLCAiYXR0cmlidXRlcyI6IFsgeyAidHJhaXRfdHlwZSI6ICJUb3dlciIsICJ2YWx1ZSI6IFRvd2VyIDEsICJ0cmFpdF90eXBlIjogIkRpc3RyaWN0IiwgInZhbHVlIjpIaWdoLUZseWVyLCAidHJhaXRfdHlwZSI6ICJuZWlnaGJvcmhvb2QiLCAidmFsdWUiOkZyZWUgQWxsZXkgUXVhcnRlcnMsXX0=
我哪里出错了?
您的 JSON 属性数组格式不正确,值没有用双引号括起来,数组中的每一项都应该是一个带有 trait-type
和 value
的对象键。
当前:
[ { "trait_type": "Tower", "value": Tower 1, "trait_type": "District", "value":High-Flyer, "trait_type": "neighborhood", "value":Free Alley Quarters,]
应该是:
[
{"trait_type":"Tower","value":"Tower 1"},
{"trait_type":"District","value":"High-Flyer"},
{"trait_type":"neighborhood","value":"Free Alley Quarters"}
]
虽然我还没有测试过,但这段代码应该会生成正确的输出。
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, '"}]}'
)
);
我正在使用开放式 Zeppelin 在 ETH 上开发 NFT 合约,并且到了部署在 rinkeby 上的阶段。然而,tokenURI 和 contractURI 似乎存在问题,因为图像和元数据未在 opensea 上显示。
我这样实现 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");
}
}
当 运行 在本地 return 时,tokenURI 给出以下输出,解析为浏览器中的正确数据:
data:application/json;base64,eyJuYW1lIjogIkdpbGRlZCBBdHRpYyIsICJkZXNjcmlwdGlvbiI6ICJUaGUgQXN0cm9ub21lcidzIFN0dWRpbwpVbmRlciB0aGUgZG9tZSBpcyBhIHBsYW5ldHJpdW0gClJlc2VhcmNoZXMgdGhlIG9yaWdpbnMgb2YgdGhlIE1ldHJvcG9saXMiLCAiaW1hZ2UiOiAiaXBmczovL1FtWnc3NzY1ZG04aHBIRndITDl3b29RSkNzSkd6TFR3WTIyNjVDR1lpeFB3aUciLCAiYXR0cmlidXRlcyI6IFsgeyAidHJhaXRfdHlwZSI6ICJUb3dlciIsICJ2YWx1ZSI6IFRvd2VyIDEsICJ0cmFpdF90eXBlIjogIkRpc3RyaWN0IiwgInZhbHVlIjpIaWdoLUZseWVyLCAidHJhaXRfdHlwZSI6ICJuZWlnaGJvcmhvb2QiLCAidmFsdWUiOkZyZWUgQWxsZXkgUXVhcnRlcnMsXX0=
我哪里出错了?
您的 JSON 属性数组格式不正确,值没有用双引号括起来,数组中的每一项都应该是一个带有 trait-type
和 value
的对象键。
当前:
[ { "trait_type": "Tower", "value": Tower 1, "trait_type": "District", "value":High-Flyer, "trait_type": "neighborhood", "value":Free Alley Quarters,]
应该是:
[
{"trait_type":"Tower","value":"Tower 1"},
{"trait_type":"District","value":"High-Flyer"},
{"trait_type":"neighborhood","value":"Free Alley Quarters"}
]
虽然我还没有测试过,但这段代码应该会生成正确的输出。
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, '"}]}'
)
);