波场链TRX上NFT发行教程——NFT中元数据合约源代码实现【pdf+视频TRX链NFT发行教程下载】

  • A+
所属分类:NFT
163Hash
Bitop
pi network
波场链TRX上NFT发行教程——NFT中元数据合约源代码实现【pdf+视频TRX链NFT发行教程下载】
一、准备
1、构造元数据主要是初始化nft的名称、简介、头像链接、外部扩展URL等属性,通过配置json串的相关属性值来实现。
2、元数据json文件构造完成后,需要上传到IPFS网络,生产对应的CID,作为NFT铸造时的tokenURI
3、元数据中的图片必须指向IPFS链上的内容,不能指向其他中心化服务器中保存的图片。json文件和其引起的图片文件必须实现完全的去中心化,保证文件访问时的高可用性。
二、操作流程
1、构造元数据json串,通过元数据模板,修改其中的键值对
元数据构造如下:
{
  "name": "Dave Starbelly",
  "description": "Friendly OpenSea Creature that enjoys long swims in the ocean.", 
  ....
  ....
  ....
  "attributes": [
    {
      "trait_type": "Base", 
      "value": "Starfish"
    }, 
    {
      "trait_type": "Mouth", 
      "value": "Surprised"
    }, 
    {
      "trait_type": "Level", 
      "value": 5
    }, 
    {
      "trait_type": "Stamina", 
      "value": 1.4
    }, 
    {
      "trait_type": "Personality", 
      "value": "Sad"
    }, 
    {
      "display_type": "boost_number", 
      "trait_type": "Aqua Power", 
      "value": 40
    }, 
    {
      "display_type": "number", 
      "trait_type": "Generation", 
      "value": 2
    }
  ]
}
nft作品的名称:Dave Starbelly
nft作品的简介:Friendly OpenSea Creature that enjoys long swims in the ocean.
2、同步tokenURI至IPFS
nft元数据源代码实现如下:
pragma solidity ^0.5.5;

import "./Counters.sol";
import "./TRC165.sol";
import "./TRC721.sol";
import "./ITRC721Metadata.sol";

contract TRC721Metadata is Context, TRC165, TRC721, ITRC721Metadata {
    // Token name
    string private _name;

    // Token symbol
    string private _symbol;

    // Base URI
    string private _baseURI;

    // Optional mapping for token URIs
    mapping(uint256 => string) private _tokenURIs;

    /*
     *     bytes4(keccak256('name()')) == 0x06fdde03
     *     bytes4(keccak256('symbol()')) == 0x95d89b41
     *     bytes4(keccak256('tokenURI(uint256)')) == 0xc87b56dd
     *
     *     => 0x06fdde03 ^ 0x95d89b41 ^ 0xc87b56dd == 0x5b5e139f
     */
    bytes4 private constant _INTERFACE_ID_TRC721_METADATA = 0x5b5e139f;

    /**
     * @dev Constructor function
     */
    constructor (string memory name, string memory symbol) public {
        _name = name;
        _symbol = symbol;

        // register the supported interfaces to conform to TRC721 via TRC165
        _registerInterface(_INTERFACE_ID_TRC721_METADATA);
    }

    /**
     * @dev Gets the token name.
     * @return string representing the token name
     */
    function name() external view returns (string memory) {
        return _name;
    }

    /**
     * @dev Gets the token symbol.
     * @return string representing the token symbol
     */
    function symbol() external view returns (string memory) {
        return _symbol;
    }

    /**
     * @dev Returns the URI for a given token ID. May return an empty string.
     *
     * If the token's URI is non-empty and a base URI was set (via
     * {_setBaseURI}), it will be added to the token ID's URI as a prefix.
     *
     * Reverts if the token ID does not exist.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory) {
        require(_exists(tokenId), "TRC721Metadata: URI query for nonexistent token");

        string memory _tokenURI = _tokenURIs[tokenId];

        // Even if there is a base URI, it is only appended to non-empty token-specific URIs
        if (bytes(_tokenURI).length == 0) {
            return "";
        } else {
            // abi.encodePacked is being used to concatenate strings
            return string(abi.encodePacked(_baseURI, _tokenURI));
        }
    }

    /**
     * @dev Internal function to set the token URI for a given token.
     *
     * Reverts if the token ID does not exist.
     *
     * TIP: if all token IDs share a prefix (e.g. if your URIs look like
     * `http://api.myproject.com/token/<id>`), use {_setBaseURI} to store
     * it and save gas.
     */
    function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal {
        require(_exists(tokenId), "TRC721Metadata: URI set of nonexistent token");
        _tokenURIs[tokenId] = _tokenURI;
    }

    /**
     * @dev Internal function to set the base URI for all token IDs. It is
     * automatically added as a prefix to the value returned in {tokenURI}.
     *
     * _Available since v2.5.0._
     */
    function _setBaseURI(string memory baseURI) internal {
        _baseURI = baseURI;
    }

    /**
    * @dev Returns the base URI set via {_setBaseURI}. This will be
    * automatically added as a preffix in {tokenURI} to each token's URI, when
    * they are non-empty.
    *
    * _Available since v2.5.0._
    */
    function baseURI() external view returns (string memory) {
        return _baseURI;
    }

    /**
     * @dev Internal function to burn a specific token.
     * Reverts if the token does not exist.
     * Deprecated, use _burn(uint256) instead.
     * @param owner owner of the token to burn
     * @param tokenId uint256 ID of the token being burned by the msg.sender
     */
    function _burn(address owner, uint256 tokenId) internal {
        super._burn(owner, tokenId);

        // Clear metadata (if any)
        if (bytes(_tokenURIs[tokenId]).length != 0) {
            delete _tokenURIs[tokenId];
        }
    }
}

对应元数据的tokenURI即为该元数据对外访问链接。

至此,完成构造NFT元数据并上传IPFS,获取tokenURI的操作流程。

pdf+视频TRX链NFT发行教程下载:

波场链TRX上NFT发行教程——NFT中元数据合约源代码实现【pdf+视频TRX链NFT发行教程下载】

pdf+视频TRX链NFT发行教程下载地址:

此处为隐藏的内容!
登录后才能查看!

添加VX或者telegram获取全程线上免费指导

波场链TRX上NFT发行教程——NFT中元数据合约源代码实现【pdf+视频TRX链NFT发行教程下载】

免责声明

发文时比特币价格:$41249

当前比特币价格:[crypto coins=”BTC” type=”text” show=”price”]

当前比特币涨幅:[crypto coins=”BTC” type=”text” show=”percent”]

免责声明:

本文不代表漫兮网立场,且不构成投资建议,请谨慎对待。用户由此造成的损失由用户自行承担,与漫兮网没有任何关系;

漫兮网不对网站所发布内容的准确性,真实性等任何方面做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,漫兮网对项目的真实性,准确性等任何方面均不做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,漫兮网不对其构成任何投资建议,用户由此造成的损失由用户自行承担,与漫兮网没有任何关系;

漫兮区块链研究院声明:漫兮区块链研究院内容由漫兮网发布,部分来源于互联网和行业分析师投稿收录,内容为漫兮区块链研究院加盟专职分析师独立观点,不代表漫兮网立场。

 

本文是全系列中第4 / 6篇:NFT发行

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • App下载
  • 漫兮网App下载
  • weinxin
漫兮

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: