Reputation: 398
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!
Upvotes: 1
Views: 1576
Reputation: 71
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.
Upvotes: 0