简体   繁体   English

如何访问 ERC721 令牌的特定元数据

[英]How to get access to specific metadata of a ERC721 token

I would like to get n ERC721 token with a specific "dna".我想获得带有特定“dna”的 n ERC721 令牌。 See the metadata below:请参阅下面的元数据:

{
  "dna": "602472F",
  "name": "Test #1",
  "description": "My Collectibles",
  "image": "ipfs://QmasMm8v9WkU11BtnWsybDW6/1.png",
  "edition": 1,
  "attributes": [
    {
      "trait": "type",
      "value": "Fire"
    },
    {
      "trait_type": "Eyes",
      "value": "Black"
    }
  ]
}

I know how to access a token using tokenURI .我知道如何使用tokenURI访问令牌。 Here is my code:这是我的代码:

  string public uri;
  string public uriSuffix = ".json";

  function _baseURI() internal view virtual override returns (string memory) {
    return uri;
  }

  function tokenURI(uint256 _tokenId) public view virtual override returns (string memory){
    require(_exists(_tokenId), "ERC721Metadata: URI query for nonexistent token");
      string memory currentBaseURI = _baseURI();
      return bytes(currentBaseURI).length > 0 ? string(abi.encodePacked(currentBaseURI, _tokenId.toString(), uriSuffix)) : "";
    }

Now, how can I check if a token has the dna I am looking for?现在,我如何检查令牌是否具有我正在寻找的dna Should I get this info from Opensea API or from the solidity side?我应该从Opensea API还是从solidity 方面获得这些信息?

Ps: All my.json and.png files are hosted in IPFS. ps:所有my.json和.png文件都托管在IPFS中。

EVM contracts are not able to read offchain data (the JSON file) directly. EVM 合约无法直接读取链下数据(JSON 文件)。 You'd need to use an offchain app (or an oracle provider such as Chainlink) for that to feed the offchain data to the contract.您需要使用链下应用程序(或 oracle 提供商,例如 Chainlink)将链下数据提供给合约。

So it's much easier to just query the data from an offchain app.因此,从链下应用程序中查询数据要容易得多。

Example using node.js and the web3 package for querying the contract:使用 node.js 和web3 package 查询合约的示例:

const contract = new web3.eth.Contract(abiJson, contractAddress);
const tokenURI = await contract.methods.tokenURI(tokenId);
const contents = (await axios.get(tokenURI)).data;
return contents.dna;

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

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