跳转到主要内容

参数

pubkey
string
必填
要查询的账户公钥(base-58 编码字符串)
config
object
包含以下可选字段的配置对象:

响应

result
object

代码示例

基本请求

curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getBalance",
  "params": [
    "83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri"
  ]
}'

带 Commitment 的请求

curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getBalance",
  "params": [
    "83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri",
    {
      "commitment": "finalized"
    }
  ]
}'

使用 web3.js

import { Connection, PublicKey } from '@solana/web3.js';

const connection = new Connection('https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');
const publicKey = new PublicKey('83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri');

const balance = await connection.getBalance(
  publicKey,
  'confirmed'
);

console.log(`Balance: ${balance / 1e9} SOL`);

注意事项

  1. 余额以 lamports 返回(1 SOL = 1,000,000,000 lamports)
  2. 对于新账户,余额为 0
  3. 余额包括所有 SOL 代币,包括委托用于质押的代币
  4. 响应是即时的,因为它从当前状态读取

最佳实践

  1. 根据需求使用适当的 commitment 级别:
    • processed 用于 UI 更新
    • confirmed 用于大多数操作
    • finalized 用于关键操作
  2. 向用户显示时,将 lamports 除以 1e9 转换为 SOL
  3. 如果需要更多账户详情,请考虑使用 getAccountInfo

常见错误

错误码消息解决方案
-32602Invalid param: WrongSize验证公钥是否有效
-32602Invalid param: not base58 encoded string确保公钥是 base58 编码的
-32007Account not found账户不存在(余额将为 0)