简体   繁体   中英

How to decode and parse ERC721 tokenURI in solidity smart contract?

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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