> ## Documentation Index
> Fetch the complete documentation index at: https://www.helius.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 如何使用 getAccountInfo

> 了解 getAccountInfo 的使用案例、代码示例、请求参数、响应结构和提示。

[此](https://www.helius.dev/docs/api-reference/rpc/http/getaccountinfo) RPC 方法是查询 Solana 区块链的基本工具。它允许您检索与特定账户公钥关联的所有存储信息。这包括账户的 lamport 余额、拥有它的程序、是否可执行以及其存储的数据。

## 常见用例

* **检查 SOL 余额：** 确定任何账户的本机 SOL 余额。
* **验证账户存在性：** 检查具有给定公钥的账户是否已初始化（即，是否有 lamports 或数据）。
* **检查程序账户：** 检索由程序拥有的账户中存储的数据，这对于理解程序的状态至关重要。
* **识别账户所有者：** 找出哪个程序是账户的所有者。这有助于确定如何解释账户的数据或它是否是系统拥有的账户。
* **检查账户是否可执行：** 确定账户是否包含已部署的程序。

## 参数

1. `publicKey` (字符串, 必需): 要查询的账户的 base-58 编码公钥。

2. `config` (对象, 可选): 包含以下字段的配置对象:
   * `commitment` (字符串, 可选): 指定用于查询的[承诺级别](https://www.helius.dev/blog/solana-commitment-levels)。默认为 `finalized`。
     * `finalized`: 节点将查询被集群的绝大多数确认已达到最大锁定的最新区块。
     * `confirmed`: 节点将查询由集群绝大多数投票的最新区块。
     * `processed`: 节点将查询其最新区块。注意，该区块可能不完整。
   * `encoding` (字符串, 可选): 账户数据的编码。默认为 `base64`。
     * `base58` (慢)
     * `base64`
     * `base64+zstd` (如果数据被压缩)
     * `jsonParsed`: 如果账户数据是已知的程序状态（例如，代币账户、质押账户），节点将尝试将其解析为 JSON 结构。对于通用程序账户，这通常会退回到二进制（base64）。
   * `dataSlice` (对象, 可选): 将返回的账户数据限制为特定的切片。仅对 `base58`, `base64`, 或 `base64+zstd` 编码可用。
     * `offset` (数字): 从账户数据起始处开始切片的字节数。
     * `length` (数字): 要返回的字节数。
   * `minContextSlot` (数字, 可选): 请求可以在其上评估的最小槽。

## 响应

如果找到账户，`result`字段将包含一个具有两个主要属性的对象：

* `context`（对象）：包含有关请求的元数据。
  * `slot`（数字）：检索信息的槽位。
  * `apiVersion`（字符串，可选）：RPC API版本。

* `value`（对象 | null）：如果账户不存在，这将是`null`。否则，它是一个包含以下内容的对象：
  * `lamports`（数字）：账户拥有的lamports数量（1 SOL = 1,000,000,000 lamports）。
  * `owner`（字符串）：拥有此账户的程序的base-58编码公钥。
  * `data`（数组 | 对象 | 字符串）：存储在账户中的数据。格式取决于请求中使用的`encoding`参数。
    * 对于`base64`（默认），`base58`，`base64+zstd`：这通常是一个数组`[encoded_string, encoding_format]`，例如`["string_data", "base64"]`。
    * 对于`jsonParsed`：如果数据可由RPC节点解析，这可以是一个JSON对象（例如，用于SPL Token账户）。否则，如果数据未被识别为标准布局，则可能默认为`["", "base64"]`或类似格式。
  * `executable`（布尔值）：如果账户包含程序，则为`true`，否则为`false`。
  * `rentEpoch`（数字）：账户将在下一个周期欠租金。
  * `space`（数字，可选）：数据的字节长度。（注意：官方Solana文档列出`space`，而某些RPC提供商可能会包括它。它表示分配给账户数据的总空间）。有关[账户数据和反序列化](https://www.helius.dev/blog/solana-dev-101-deserializing-account-data-on-solana)的更多详细信息，请参阅我们的详细指南。

如果未找到账户，结果中的 `value` 字段将为 `null`。

## 示例：获取账户信息

让我们在主网上获取 Serum Program V3 ID (`9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin`) 的信息。

**注意：** 在下面的示例中，将 `YOUR_API_KEY` 替换为您实际的 Helius API 密钥。

<CodeGroup>
  ```bash curl theme={"system"}
  curl https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY -X POST -H "Content-Type: application/json" -d \
  '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getAccountInfo",
    "params": [
      "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin",
      {
        "encoding": "jsonParsed"
      }
    ]
  }'
  ```

  ```javascript JavaScript (using @solana/web3.js) theme={"system"}
  const { Connection, PublicKey } = require('@solana/web3.js');

  async function getAccountDetails() {
    const rpcUrl = 'https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY'; // Replace YOUR_API_KEY
    const connection = new Connection(rpcUrl, 'confirmed');
    const accountPubKey = new PublicKey('9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin');

    try {
      const accountInfo = await connection.getAccountInfo(accountPubKey);

      if (accountInfo === null) {
        console.log('Account not found.');
        return;
      }

      console.log('Account Info:');
      console.log(`   Lamports: ${accountInfo.lamports}`);
      console.log(`   Owner: ${accountInfo.owner.toBase58()}`);
      console.log(`   Executable: ${accountInfo.executable}`);
      console.log(`   Rent Epoch: ${accountInfo.rentEpoch}`);
      // Data is a Buffer, you might need to deserialize it based on the account type
      // console.log(`   Data: ${accountInfo.data.toString()}`); 
    } catch (error) {
      console.error('Error fetching account info:', error);
    }
  }

  getAccountDetails();
  ```

  ```typescript Kit theme={"system"}
  import { address, createSolanaRpc } from "@solana/kit";

  const rpc_url = "https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY";
  const rpc = createSolanaRpc(rpc_url);

  const publicKey = address("vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg");
  const accountInfo = await rpc.getAccountInfo(publicKey).send();

  console.log("Account Info:", accountInfo);
  ```

  ```rust Rust theme={"system"}
  use solana_client::nonblocking::rpc_client::RpcClient;
  use solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey};
  use anyhow::Result;
  use std::str::FromStr;

  #[tokio::main]
  async fn main() -> Result<()> {
      let client = RpcClient::new_with_commitment(
          String::from("https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY"),
          CommitmentConfig::confirmed()
      );
      let pubkey = Pubkey::from_str("vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg")?;
      let account = client.get_account(&pubkey).await?;

      println!("{:#?}", account);

      Ok(())
  }
  ```
</CodeGroup>

## 开发者提示

* **性能：** 对于需要频繁检查多个账户的应用程序，考虑使用 `getMultipleAccounts` 来批量请求并减少往返。
* **数据反序列化：** `data` 字段通常需要根据拥有程序的数据结构进行反序列化。通常需要特定于程序的工具和库（例如，针对代币账户的 SPL Token 库）。我们关于[账户数据反序列化](https://www.helius.dev/blog/solana-dev-101-deserializing-account-data-on-solana)的博客文章提供了有用的技术和示例。
* **速率限制：** 注意 RPC 节点的速率限制，特别是在查询大量账户或频繁请求时。
* **成本管理：** `getAccountInfo` 通常是一个低成本的查询，但频繁轮询可能会增加。优化您的查询模式。
* **明智使用 `jsonParsed`：** 虽然 `jsonParsed` 很方便，但可能不支持所有账户类型，如果程序更新其数据结构，输出可能会改变。对于关键应用程序，使用已知布局解析二进制数据提供了更高的稳定性。
* **考虑使用 `dataSlice`：** 如果您只需要账户数据的一小部分，则使用 `dataSlice` 来减少传输的数据量并可能降低查询成本。

## 相关方法

<CardGroup cols={2}>
  <Card title="getMultipleAccounts" href="/docs/zh/api-reference/rpc/http/getmultipleaccounts">
    在单个请求中批量获取多个账户以提高性能
  </Card>

  <Card title="getBalance" href="/docs/zh/api-reference/rpc/http/getbalance">
    获取仅限SOL的余额，而不需要完整的账户详细信息
  </Card>
</CardGroup>
