简体   繁体   English

从合约中读取 NFT 的 JSON 元数据

[英]Reading JSON metadata of NFT from it's contract

are there any legal ways to get details about NFT metadata from inside its contract?有没有合法的方法可以从其合约中获取有关 NFT 元数据的详细信息? Let say I have NFT with id 1, which metadata is stored on IPFS and it has rarity attribute 10. Is it possible to fetch that data from inside contract?假设我有 ID 为 1 的 NFT,它的元数据存储在 IPFS 上并且它的稀有度属性为 10。是否可以从合同内部获取该数据? Or the only way is to introduce rarity attribute on contract and then populate it later with 3rd party utility?或者唯一的方法是在合同中引入稀有属性,然后用第 3 方实用程序填充它?

you can technically fetch data from the contract if there is an oracle service built specifically for fetching data from ipfs.如果有专门为从 ipfs 获取数据而构建的 oracle 服务,您可以在技术上从合同中获取数据。 however, this is too expensive and unnecessary service so there is no oracle service for this.但是,这是一项过于昂贵且不必要的服务,因此没有 oracle 服务。

Instead, we fetch the data from Ipfs off-chain from front end.相反,我们从前端的 Ipfs 链下获取数据。 We store the tokenUri inside the contract.我们将tokenUri存储在合约中。 On the front end, we get the tokenUri and fetch data from Ipfs using this tokenUri.在前端,我们获取tokenUri并使用此 tokenUri 从 Ipfs 中获取数据。

 const tokenURI = await contract.tokenURI(tokenId);
 const metaRes = await fetch(tokenURI);

Most of the NFTs with metadata is an implementation of ERC721Metadata interface , which has the following method:大多数带有元数据的 NFT 是ERC721Metadata 接口的实现,它具有以下方法:

    /// @notice A distinct Uniform Resource Identifier (URI) for a given asset.
    /// @dev Throws if `_tokenId` is not a valid NFT. URIs are defined in RFC
    ///  3986. The URI may point to a JSON file that conforms to the "ERC721
    ///  Metadata JSON Schema".
    function tokenURI(uint256 _tokenId) external view returns (string);

This method, tokenURI returns URI of metadata for given tokenId .此方法tokenURI返回给定tokenId的元数据 URI。

Metadata attributes usually follows opensea metadata standards元数据属性通常遵循opensea 元数据标准

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM