首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确地为链上NFTs实现ContractURI

如何正确地为链上NFTs实现ContractURI
EN

Ethereum用户
提问于 2021-10-02 17:32:15
回答 2查看 3.9K关注 0票数 10

我正在尝试为OpenSea上的NFT集合实现OpenSea,但是,如果使用下面的代码,契约的元数据将不会在OpenSea集合的页面上更新,也不会更新令牌页面上的契约描述。

TokenURI也存储在链上,并在给定适当的数据时进行更新.在我的例子中,它将是一个SVG映像以及一些字符串以获取信息。(更详细的代码片段)

代码丢失了所有与此无关的内容。

代码语言:javascript
复制
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

import "./base64.sol";

contract Test is ERC721Enumerable, ERC721URIStorage, Ownable {
    using Counters for Counters.Counter;
    using Strings for uint256;

    string private _contractURI;

    constructor() ERC721("Test", "T") {}

    function svgToImageURI(string memory _source) public pure returns (string memory) {
        string memory baseURL = "data:image/svg+xml;base64,";
        string memory svgBase64Encoded = Base64.encode(bytes(string(abi.encodePacked(_source))));
        return string(abi.encodePacked(baseURL, svgBase64Encoded));
    }

    function formatTokenURI(string memory _imageURI, string memory _name, string memory _description, string memory _properties) public pure returns (string memory) {
        return string(
            abi.encodePacked(
                "data:application/json;base64,",
                Base64.encode(
                    bytes(
                        abi.encodePacked(
                            '{"name":"', _name,
                            '", "description": "', _description, '"',
                            ', "attributes": ', _properties,
                            ', "image":"', _imageURI, '"}'
                        )
                    )
                )
            )
        );
    }

    function setTokenURI(uint256 _tokenId, string memory _tokenURI) public onlyOwner() {
        _setTokenURI(_tokenId, _tokenURI);
        emit tokenChanged(_tokenId);
    }

    function setContractURI(string memory contractURI_) public onlyOwner() {
        _contractURI = string(abi.encodePacked(
            "data:application/json;base64,",
            Base64.encode(
                bytes(
                    abi.encodePacked(
                        contractURI_
                    )
                )
            )
        ));
    }

    function contractURI() public view returns (string memory) {
        return _contractURI;
    }
}

我提供给setContractURI函数的输入是来自OpenSea网站的一个简单的JSON片段,因此我确信我遵守了它的规则。

代码语言:javascript
复制
{
  "name": "OpenSea Creatures",
  "description": "OpenSea Creatures are adorable aquatic beings primarily for demonstrating what can be done using the OpenSea platform. Adopt one today to try out all the OpenSea buying, selling, and bidding feature set.",
  "image": "https://openseacreatures.io/image.png",
  "external_link": "https://openseacreatures.io",
  "seller_fee_basis_points": 100, 
  "fee_recipient": "0xA97F337c39cccE66adfeCB2BF99C1DdC54C2D721"
}

我做错了什么?我在波利贡孟买测试网上。

EN

回答 2

Ethereum用户

回答已采纳

发布于 2021-10-03 09:08:59

我的setContractURI函数似乎有一个encodepacket太多了。看起来它现在正在OpenSea上工作。

代码语言:javascript
复制
    function setContractURI(string memory contractURI_) public onlyOwner() {
        _contractURI = string(abi.encodePacked(
                "data:application/json;base64,",
                Base64.encode(
                    bytes(
                        contractURI_
                    )
                )
            ));
    }
票数 8
EN

Ethereum用户

发布于 2022-07-15 09:07:30

为了让Opensea读取我的合同元数据,我挣扎了一段时间,Maibe --这可以帮助我:

https://ethereum.stackexchange.com/a/131807/99976

编辑。增加了基本解释:

基本上,我使用一个简单的http服务器提供了一个.json文件。

即。我的元数据保存在metadata.json中

我的contractURI()返回以下内容:

代码语言:javascript
复制
https://www.myCoolDomain.com/metadata.json
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/110924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档