Solana RPC API手册

getBlock

getBlock方法仅在solana-core v1.7或更高版本中有效,在solana-core v1.6 版本中请使用getConfirmedBlock方法。

getBlock方法返回帐本中的指定的已确认区块的标识和交易信息。

请求参数

  • <u64> - 区块槽位,u64整数
  • <object> - (可选) 包含以下可选字段的配置对象:
    • (可选) encoding: <string> - 返回交易的编码格式,可以是 "json", "jsonParsed", "base58" (慢), "base64"。 如果未指定该参数,则默认的编码格式为 "json"。 "jsonParsed" 编码尝试使用程序特定的解析器返回可读性 更好的交易数据。如果设置了"jsonParsed"但没有找到合适的解析器,则回退使用JSON编码(针对accounts, data 和programIdIndex字段).
    • (可选) transactionDetails: <string> - 交易详细程度级别, 可以是 "full", "signatures", 或 "none"。 如果为设置该参数,则默认等级为"full".
    • (可选) rewards: bool - 是否填充 rewards 数组。如果未设置该参数,则返回结果默认包含rewards.
    • (可选) Commitment; 不支持"processed"。 如果未设置该参数,默认值为"finalized".

响应结果

getBlock方法返回结果的result字段为以下值:

  • <null> - 如果指定的区块不存在则返回此结果
  • <object> - 如果区块已确认,则返回包含如下字段的对象:
    • blockhash: <string> - 区块的哈希,base-58编码字符串
    • previousBlockhash: <string> - 父区块哈希,base-58 编码; 如果由于账本清理导致父区块不存在, 则此字段返回 "11111111111111111111111111111111"
    • parentSlot: <u64> - 父区块的槽位索引
    • transactions: <array> - 如果请求"full" 交易则包含此字段,为JSON对象数组,每个成员结构如下:
      • transaction: <object|[string,encoding]> - 交易对象,JSON格式或编码二进制数据,取决于encoding参数
      • meta: <object> - 交易状态元数据对象,为null或:
        • err: <object | null> - 如果交易失败则为Error对象,否则为null
        • fee: <u64> - 此交易的手续费,u64整数
        • preBalances: <array> - 交易之前的账户余额数组
        • postBalances: <array> - 交易之后的账户余额数组
        • innerInstructions: <array|undefined> - 内层指令清单,或者为null
        • preTokenBalances: <array|undefined> - 交易之前的token余额列表,如果未启用token余额记录则没有该字段
        • postTokenBalances: <array|undefined> - 交易之后的token余额列表,如果未启用token余额记录则没有该字段
        • logMessages: <array> - 日志消息数组,如果未启用日志记录则没有此字段
        • 已弃用: status: <object> - 交易状态
          • "Ok": <null> - 交易成功
          • "Err": <ERR> - 交易失败
    • signatures: <array> - 如果请求signatures级别的交易详情则包含此字段,值为交易签名列表
    • rewards: <array> - 如果请求reward则包含此字段,为一组JSON对象,成员结构为:
      • pubkey: <string> - 接收reward的账号公钥的 base-58编码
      • lamports: <i64> - 账号收/欠的reward lamports数量
      • postBalance: <u64> - reward迭加之后的账号余额,以lamports计
      • rewardType: <string|undefined> - 奖励类型: "fee", "rent", "voting", "staking"
    • blockTime: <i64 | null> - 预估的出块时间,Unix时间戳。不可用时为 null
    • blockHeight: <u64 | null> - 区块高度

示例代码

请求:

curl http://localhost:8899 -X POST -H "Content-Type: application/json" -d '
  {"jsonrpc": "2.0","id":1,"method":"getBlock","params":[430, {"encoding": "json","transactionDetails":"full","rewards":false}]}

响应:

{
  "jsonrpc": "2.0",
  "result": {
    "blockHeight": 428,
    "blockTime": null,
    "blockhash": "3Eq21vXNB5s86c62bVuUfTeaMif1N2kUqRPBmGRJhyTA",
    "parentSlot": 429,
    "previousBlockhash": "mfcyqEXB3DnHXki6KjjmZck6YjmZLvpAByy2fj4nh6B",
    "transactions": [
      {
        "meta": {
          "err": null,
          "fee": 5000,
          "innerInstructions": [],
          "logMessages": [],
          "postBalances": [
            499998932500,
            26858640,
            1,
            1,
            1
          ],
          "postTokenBalances": [],
          "preBalances": [
            499998937500,
            26858640,
            1,
            1,
            1
          ],
          "preTokenBalances": [],
          "status": {
            "Ok": null
          }
        },
        "transaction": {
          "message": {
            "accountKeys": [
              "3UVYmECPPMZSCqWKfENfuoTv51fTDTWicX9xmBD2euKe",
              "AjozzgE83A3x1sHNUR64hfH7zaEBWeMaFuAN9kQgujrc",
              "SysvarS1otHashes111111111111111111111111111",
              "SysvarC1ock11111111111111111111111111111111",
              "Vote111111111111111111111111111111111111111"
            ],
            "header": {
              "numReadonlySignedAccounts": 0,
              "numReadonlyUnsignedAccounts": 3,
              "numRequiredSignatures": 1
            },
            "instructions": [
              {
                "accounts": [
                  1,
                  2,
                  3,
                  0
                ],
                "data": "37u9WtQpcm6ULa3WRQHmj49EPs4if7o9f1jSRVZpm2dvihR9C8jY4NqEwXUbLwx15HBSNcP1",
                "programIdIndex": 4
              }
            ],
            "recentBlockhash": "mfcyqEXB3DnHXki6KjjmZck6YjmZLvpAByy2fj4nh6B"
          },
          "signatures": [
            "2nBhEBYYvfaAe16UMNqRHre4YNSskvuYgx3M6E4JP1oDYvZEJHvoPzyUidNgNX5r9sTyN1J9UxtbCXy2rqYcuyuv"
          ]
        }
      }
    ]
  },
  "id": 1
}

请求:

curl http://localhost:8899 -X POST -H "Content-Type: application/json" -d '
  {"jsonrpc": "2.0","id":1,"method":"getBlock","params":[430, "base64"]}

响应:

{
  "jsonrpc": "2.0",
  "result": {
    "blockHeight": 428,
    "blockTime": null,
    "blockhash": "3Eq21vXNB5s86c62bVuUfTeaMif1N2kUqRPBmGRJhyTA",
    "parentSlot": 429,
    "previousBlockhash": "mfcyqEXB3DnHXki6KjjmZck6YjmZLvpAByy2fj4nh6B",
    "rewards": [],
    "transactions": [
      {
        "meta": {
          "err": null,
          "fee": 5000,
          "innerInstructions": [],
          "logMessages": [],
          "postBalances": [
            499998932500,
            26858640,
            1,
            1,
            1
          ],
          "postTokenBalances": [],
          "preBalances": [
            499998937500,
            26858640,
            1,
            1,
            1
          ],
          "preTokenBalances": [],
          "status": {
            "Ok": null
          }
        },
        "transaction": [
          "AVj7dxHlQ9IrvdYVIjuiRFs1jLaDMHixgrv+qtHBwz51L4/ImLZhszwiyEJDIp7xeBSpm/TX5B7mYzxa+fPOMw0BAAMFJMJVqLw+hJYheizSoYlLm53KzgT82cDVmazarqQKG2GQsLgiqktA+a+FDR4/7xnDX7rsusMwryYVUdixfz1B1Qan1RcZLwqvxvJl4/t3zHragsUp0L47E24tAFUgAAAABqfVFxjHdMkoVmOYaR1etoteuKObS21cc1VbIQAAAAAHYUgdNXR0u3xNdiTr072z2DVec9EQQ/wNo1OAAAAAAAtxOUhPBp2WSjUNJEgfvy70BbxI00fZyEPvFHNfxrtEAQQEAQIDADUCAAAAAQAAAAAAAACtAQAAAAAAAAdUE18R96XTJCe+YfRfUp6WP+YKCy/72ucOL8AoBFSpAA==",
          "base64"
        ]
      }
    ]
  },
  "id": 1
}

交易结构

Transactions are quite different from those on other blockchains. Be sure to review Anatomy of a Transaction to learn about transactions on Solana.

The JSON structure of a transaction is defined as follows:

  • signatures: <array[string]> - A list of base-58 encoded signatures applied to the transaction. The list is always of length message.header.numRequiredSignatures and not empty. The signature at index i corresponds to the public key at index i in message.account_keys. The first one is used as the transaction id.
  • message: <object> - Defines the content of the transaction.
  • accountKeys: <array[string]> - List of base-58 encoded public keys used by the transaction, including by the instructions and for signatures. The first message.header.numRequiredSignatures public keys must sign the transaction.
  • header: <object> - Details the account types and signatures required by the transaction.
  • numRequiredSignatures: <number> - The total number of signatures required to make the transaction valid. The signatures must match the first numRequiredSignatures of message.account_keys.
  • numReadonlySignedAccounts: <number> - The last numReadonlySignedAccounts of the signed keys are read-only accounts. Programs may process multiple transactions that load read-only accounts within a single PoH entry, but are not permitted to credit or debit lamports or modify account data. Transactions targeting the same read-write account are evaluated sequentially.
  • numReadonlyUnsignedAccounts: <number> - The last numReadonlyUnsignedAccounts of the unsigned keys are read-only accounts.
  • recentBlockhash: <string> - A base-58 encoded hash of a recent block in the ledger used to prevent transaction duplication and to give transactions lifetimes.
  • instructions: <array[object]> - List of program instructions that will be executed in sequence and committed in one atomic transaction if all succeed.
  • programIdIndex: <number> - Index into the message.accountKeys array indicating the program account that executes this instruction.
  • accounts: <array[number]> - List of ordered indices into the message.accountKeys array indicating which accounts to pass to the program.
  • data: <string> - The program input data encoded in a base-58 string.

内层指令结构

The Solana runtime records the cross-program instructions that are invoked during transaction processing and makes these available for greater transparency of what was executed on-chain per transaction instruction. Invoked instructions are grouped by the originating transaction instruction and are listed in order of processing.

The JSON structure of inner instructions is defined as a list of objects in the following structure:

  • index: number - Index of the transaction instruction from which the inner instruction(s) originated
  • instructions: <array[object]> - Ordered list of inner program instructions that were invoked during a single transaction instruction.
  • programIdIndex: <number> - Index into the message.accountKeys array indicating the program account that executes this instruction.
  • accounts: <array[number]> - List of ordered indices into the message.accountKeys array indicating which accounts to pass to the program.
  • data: <string> - The program input data encoded in a base-58 string.

token余额结构

The JSON structure of token balances is defined as a list of objects in the following structure:

  • accountIndex: <number> - Index of the account in which the token balance is provided for.
  • mint: <string> - Pubkey of the token's mint.
  • uiTokenAmount: <object> -
  • amount: <string> - Raw amount of tokens as a string, ignoring decimals.
  • decimals: <number> - Number of decimals configured for token's mint.
  • uiAmount: <number | null> - Token amount as a float, accounting for decimals. DEPRECATED
  • uiAmountString: <string> - Token amount as a string, accounting for decimals.