如何将整数从Solana Rust Program功能返回到前端?

发布于 2025-01-25 02:52:33 字数 69 浏览 0 评论 0 原文

我不知道如何在Solana中编写一份智能合约,该合同在执行逻辑后,将整数,字符串等返回给客户端,以及如何使用Web3获取它?

I don't know how can I write a smart contract in Solana that after executing the logic, returns an array of integers, strings, ... to the client, and how can I fetch it using Web3?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

給妳壹絲溫柔 2025-02-01 02:52:33

有一个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#L102

Source 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#l117

There'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 using get_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-50

The 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#L102

Source code for get_return_data at https://github.com/solana-labs/solana/blob/658752cda710cb358d7ccbbc2cee06bf8009c2d4/sdk/program/src/program.rs#L117

墨小沫ゞ 2025-02-01 02:52:33

因此,程序不会返回数据(成功或失败除外)。

然而;大多数程序将数据写入程序拥有的帐户的数据字段,这可以从客户端应用程序(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-client

So, 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 the Connection object. This will return the byte array of the account. You will then need to deserialize that data. You have to know how the program serializes 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文