2

I would like to get n ERC721 token with a specific "dna". 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. 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? Should I get this info from Opensea API or from the solidity side?

Ps: All my .json and .png files are hosted in IPFS.

Remzzer
  • 83
  • 2
  • 10

1 Answers1

3

EVM contracts are not able to read offchain data (the JSON file) directly. 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.

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:

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;
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Thanks a lot. That's what I thought! – Remzzer Feb 02 '22 at 14:34
  • Hi, is there a way to do this in the smart contract with solidity? Thanks. – jaredcohe Mar 19 '22 at 00:27
  • @jaredcohe Unfortunately not. Solidity code compiles to bytecode that is processed by the EVM. And the EVM is not able to access data outside of itself. – Petr Hejda Mar 19 '22 at 08:45
  • Thanks, @PetrHejda, but I have access to the data. I'm calling tokenURI in the contract and I'm logging the encoded data, so I know I can access the data. The question is how to decode and parse that so I can get specific metadata from the tokenURI. Sorry if I was unclear. Does that make sense? This is the question: https://stackoverflow.com/questions/71534499/how-to-decode-and-parse-erc721-tokenuri-in-solidity-smart-contract?noredirect=1#comment126432025_71534499 – jaredcohe Mar 20 '22 at 11:52