跳转到主要内容

参数

config
object

响应

result
object

代码示例

基本请求

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

使用 web3.js

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

const connection = new Connection('http://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
console.log(blockhash, lastValidBlockHeight);

使用 Python

from solana.rpc.api import Client

client = Client("http://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY")
response = client.get_latest_blockhash()
print(response)

注意事项

  1. 此方法返回最新的 blockhash 及其有效期
  2. 替代已弃用的 getRecentBlockhash 方法
  3. blockhash 用于交易处理和去重
  4. blockhash 在达到指定的 lastValidBlockHeight 之前有效
  5. 可以为响应指定不同的 commitment 级别

最佳实践

  1. 使用 blockhash 进行交易提交和签名
  2. 监控 lastValidBlockHeight 以确定 blockhash 有效性
  3. 如果达到或超过 lastValidBlockHeight,则刷新 blockhash
  4. 适当处理网络错误并重试
  5. 在有效期内缓存 blockhash 用于多个交易