1

I'm calling tokenURI(tokenId) in a smart contract to get my ERC721 metadata, and I get back the encoded response. Is there a way to decode and parse that in a solidity smart contract function so that I can access the metadata in the smart contract? This question answers it in javascript, but I need to do it in solidity: How to get access to specific metadata of a ERC721 token

Thanks!

jaredcohe
  • 378
  • 1
  • 3
  • 11
  • if you can do the work in front end, do it in front end. I am sure you can get using oracle service but it will cost you. In smart contract every operation, calculation costs u – Yilmaz Mar 19 '22 at 01:05
  • Thanks. I have to do it in the contract itself because I'm trying to compare the date an ERC721 was created to the date the contract function is run. – jaredcohe Mar 20 '22 at 11:50

1 Answers1

0

There are a few ways to decode the return.
In most cases (ERC721) tokenURI retrieves a string memory, so string(tokenURI(tokenID)) should do. Another popular option is abi.encodePacked(tokenURI(tokenID)). In short, look at the type returned by the function you are calling and try to convert the value to that type.

For cases where contracts return custom reference types (structs etc.) your best bet is using an interface. Your next best option is to copy the type definition from the source contract and convert your bytes using abi.decode(external_call_returned, myCustomStruct). Last option, if you know exactly how the bytes are ordered (first 4 bytes are function signature, the rest are string) is to slice and convert them explicitly.

I recommend you take a look at Contract ABI Specification.

parseb
  • 71
  • 5