Neon EVM:用 QueryAccount 库以 Solidity 访问 Solana 帐户 (Neon EVM:Access Solana Accounts With Solidity Using the QueryAccount Library)

Neon EVM:用 QueryAccount 库以 Solidity 访问 Solana 帐户 (Neon EVM:Access Solana Accounts With Solidity Using the QueryAccount Library)


The Neon EVM is an Ethereum-like environment that makes it possible for Solidity contracts and Ethereum-like transactions to function on Solana. Although the Neon EVM is built on Solana, dApps on the platform can’t natively access Solana accounts to source on-chain data. To overcome this limitation, Neon Labs developed the QueryAccount Library.

Neon EVM 是一个类以太坊环境,它使得 Solidity 合约和类以太坊交易可以在 Solana 上运行。虽然 Neon EVM 是基于 Solana 构建的,但平台上的 dApp 无法原生访问 Solana 账户来获取链上数据。为了克服这个限制,Neon Labs 开发了 QueryAccount 库。

QueryAccount is a low-level Solidity library that provides developers with standardized functions to retrieve on-chain information, such as price feeds, for use on their Neon EVM dApps. Users can view QueryAccount as a one-way data channel that allows Neon EVM dApps to read information stored on Solana.

QueryAccount 是一个低级 Solidity 库,它为开发人员提供标准化函数来检索链上信息,例如价格推送,进而用于其 Neon EVM dApp。用户可以将 QueryAccount 视为一种单向数据通道,允许 Neon EVM dApp 读取存储在 Solana 上的信息。

Solidity 和 Rust 程序之间的通信 (Communication Between Solidity and Rust Programs)

The QueryAccount library facilitates communication between Solidity and Rust programs. The Solidity functions making up the library are supported by a pre-compiled Rust program at a fixed Solana address.

QueryAccount 库促进了 Solidity 和 Rust 程序之间的通信。构成库的 Solidity 函数由固定 Solana 地址的预编译 Rust 程序支持。

Each Solidity function within QueryAccount interacts with the pre-compiled Rust program via a static call, meaning the state of the Rust program won’t be modified. The Solidity functions are able to do so through the Solidity Application Binary Interface (ABI), a low-level feature of the language. An ABI is an interface between two binary programs (compiled programs). It defines the procedures behind invoking functions and how parameters are passed between two binary programs.

QueryAccount 中的每个 Solidity 函数通过静态调用与预编译的 Rust 程序交互,这意味着 Rust 程序的状态不会被修改。 Solidity 函数可以通过 Solidity 应用二进制接口 (ABI) 来实现,这是该语言的低级特性。 ABI 是两个二进制程序(编译程序)之间的接口。它定义了调用函数的背后过程以及如何在两个二进制程序之间传递参数。

When the Rust program is called via ABI, it grabs the requested information from the specified Solana account and writes it into memory as an unstructured set of bytes. The QueryAccount functions will then read those bytes from memory and translate them into data types that are more common to high-level programming (integer, string, etc.). These data types can then be easily read by other Solidity programs, such as dApps on the Neon EVM.

当通过 ABI 调用 Rust 程序时,它会从指定的 Solana 帐户中获取请求的信息,并将其作为非结构化字节集写入内存。然后 QueryAccount 函数将从内存中读取这些字节,并将它们转换为高级编程更常见的数据类型(整数、字符串等)。然后,其他 Solidity 程序(例如 Neon EVM 上的 dApp)可以轻松读取这些数据类型。

访问 Solana 帐户的重要性 (The Importance of Accessing the Solana Account)

Many Neon EVM dApps will require the use of volatile real-world data such as price feeds, sports statistics, weather, and more. However, due to the deterministic nature of blockchains, dApps can’t use APIs to obtain external data. Neon EVM dApps need to rely on Solana oracles to retrieve and publish the data on-chain for a consistent information reference.

许多 Neon EVM dApp 将需要使用波动大的现实世界数据,例如价格推送、体育统计数据、天气等。然而,由于区块链的确定性,dApps 不能使用 API 来获取外部数据。 Neon EVM dApp 需要依赖 Solana 预言机来检索和发布链上数据,来获得一致的信息参考。

The use of QueryAccount makes it possible for Neon EVM dApps to read information stored on Solana. Projects can choose to use the QueryAccount library directly to create customized data feeds or leverage QueryAccount-powered infrastructure, like PythNeon, to access Solana data via the traditional oracle model.

QueryAccount 的使用让 Neon EVM dApp 可以读取存储在 Solana 上的信息。项目可以选择直接使用 QueryAccount 库来创建自定义数据推送或利用 QueryAccount 支持的基础设施,如 PythNeon,通过传统的甲骨文数据模型来访问 Solana 。

QueryAccount函数说明 (Description of QueryAccount Functions)

cache(): The cache() function is used to retrieve and store a Solana account’s metadata and a portion of its data in a memory cache for the purposes of code optimization and data consistency.
cache(): cache() 函数用于检索 Solana 帐户的元数据及其部分数据,并将其存储在内存缓存中,来实现代码优化和数据一致性。

owner(): The owner() function is used to retrieve the address of a Solana account’s owner.
owner(): owner() 函数用于检索 Solana 帐户所有者的地址。

length(): The length() function is used to retrieve the length of a Solana account’s data.
length(): length() 函数用于检索 Solana 帐户数据的长度。

lamports(): The lamports() function is used to retrieve the amount of lamports stored within a Solana account.
lamports(): lamports() 函数用于检索存储在 Solana 帐户中的 lamports 数量。

executable(): The executable() function is used to retrieve a Solana account’s executable flag.
executable(): executable() 函数用于检索 Solana 帐户的可执行标志。

rent_epoch():  The rent_epoch() function is used to retrieve the rent epoch of a Solana account.
rent_epoch():  rent_epoch() 函数用于检索 Solana 账户的租期。

data(): The data() function is used to retrieve the data (in bytes) stored on a Solana account. The data could be real-world data brought in by an oracle.
data(): data() 函数用于检索存储在 Solana 帐户上的数据(以字节为单位)。数据可能是由预言机带来的真实世界数据。

If you’re interested in reviewing the integration tests for the QueryAccount functions, you can do so here.

如果您有兴趣查看 QueryAccount 函数的集成测试,可以在 此处

Neon 团队随时为您提供帮助 (The Neon Team Is Here to Help)

If this article leaves you with further questions, reach out to our team via Discord. We’ll be more than happy to help answer any questions regarding the QueryAccount library, and explain how it facilitates communication between Solidity and Rust programs.

如果本文给您留下了其他问题,请通过 Discord 与我们的团队联系。我们非常乐意帮助回答有关 QueryAccount 库的任何问题,并解释它如何促进 Solidity 和 Rust 程序之间的通信。

赞赏