简体   繁体   English

如何在solidity智能合约中解码和解析ERC721 tokenURI?

[英]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.我在智能合约中调用 tokenURI(tokenId) 来获取我的 ERC721 元数据,然后我取回编码的响应。 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?有没有办法在 Solidity 智能合约 function 中解码和解析它,以便我可以访问智能合约中的元数据? 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这个问题在 javascript 中回答了它,但我需要坚定地做: 如何访问 ERC721 令牌的特定元数据

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.在大多数情况下(ERC721) tokenURI 检索string memory ,所以string(tokenURI(tokenID))应该这样做。 Another popular option is abi.encodePacked(tokenURI(tokenID)) .另一个流行的选项是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.简而言之,查看您正在调用的 function 返回的类型,并尝试将值转换为该类型。

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) .您的下一个最佳选择是从源合同复制类型定义并使用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.最后一个选项,如果您确切知道字节的排序方式(前 4 个字节是 function 签名,rest 是字符串)是对它们进行切片和显式转换。

I recommend you take a look at Contract ABI Specification .我建议您查看Contract ABI Specification

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

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