如何将整数从Solana Rust Program功能返回到前端?
我不知道如何在Solana中编写一份智能合约,该合同在执行逻辑后,将整数,字符串等返回给客户端,以及如何使用Web3获取它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我不知道如何在Solana中编写一份智能合约,该合同在执行逻辑后,将整数,字符串等返回给客户端,以及如何使用Web3获取它?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
有一个syscall可用于链接程序,称为
set_return_data
,它将数据放入缓冲区中,可以使用get_return_data
通过高级程序读取该缓冲区。这一切都是通过不透明字节缓冲区介导的,因此您需要知道如何解码响应。如果要从客户端获取数据,则可以模拟事务并从响应中的
return_data
字段中读回数据: https://ged.docs.solana.com/developing/clients/jsonrpc-api#results-50模拟交易中的RPC支持在版本1.11中非常新,但是返回数据可在早期版本中获得。
set_return_data
的源代码https://github.com/solana-labs/solana/blob/658752cda710cb358d7ccbbc2cee06bf8009c2d4/sdk/program/src/program.rs#L102Source code for
get_return_data
at < a href =“ https://github.com/solana-labs/solana/solana/blob/658752CDA710CB358D7CCBBC2CEE06BF8009C2D4/SDK/SDK/SDK/PROGRAGY/SRCROGAM/SRC/SRC/PROGRAGY.RSPROGM.RS.RS.RSPOGRAGR.RS.RSWL117 -labs/solana/blob/658752CDA710CB358D7CCBBC2CEE06BF8009C2D4/SDK/program/src/program.rs#l117There's a syscall available to on-chain programs called
set_return_data
, which puts data into a buffer that can be read by the higher-level programs usingget_return_data
. This all mediated through opaque byte buffers, so you'll need to know how to decode the response.If you want to fetch the data from the client side, you can simulate the transaction and read the data back from the
return_data
field in the response: https://edge.docs.solana.com/developing/clients/jsonrpc-api#results-50The RPC support in simulated transactions is very new in version 1.11, but the return data is available in earlier versions.
Source code for
set_return_data
at https://github.com/solana-labs/solana/blob/658752cda710cb358d7ccbbc2cee06bf8009c2d4/sdk/program/src/program.rs#L102Source code for
get_return_data
at https://github.com/solana-labs/solana/blob/658752cda710cb358d7ccbbc2cee06bf8009c2d4/sdk/program/src/program.rs#L117因此,程序不会返回数据(成功或失败除外)。
然而;大多数程序将数据写入程序拥有的帐户的
数据
字段,这可以从客户端应用程序(Rust,Python,TS/JS等)中读取。如果使用Solana Web3库,则可以在
Connection> Connection
对象上调用getAcCountInfo
。这将返回帐户的字节数组。然后,您将需要Deperialize
该数据。您必须知道程序如何序列化
数据以成功扭转数据。使用
Borsh
https://solanacookbook.com/guides/serialization.html#how-to-how-to-deserialize-account-count-data-on-on-the-clientSo, programs do not return data (other than success or failure).
However; most programs write data to a program owned account's
data
field and this could be read from client apps (Rust, Python, TS/JS, etc.).If using the Solana web3 library, you can call
getAccountInfo
on theConnection
object. This will return the byte array of the account. You will then need todeserialize
that data. You have to know how the programserializes
the data to reverse it successfully.Check the Solana Cookbook for overview using
borsh
https://solanacookbook.com/guides/serialization.html#how-to-deserialize-account-data-on-the-client