跳转到主要内容

参数

message
string
必填
Base-64 编码的消息
commitment
string
费用计算使用的 commitment 级别

响应

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": "getFeeForMessage",
  "params": [
    "AQABAyRn/PA8jzJtN6oAyB3VR0nfOF0xnfkYaKwY4Ir3nrHFJGqhAQICAAEDAgABDAIAAAAEAAAAAAAA",
    {"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 message = 'AQABAyRn/PA8jzJtN6oAyB3VR0nfOF0xnfkYaKwY4Ir3nrHFJGqhAQICAAEDAgABDAIAAAAEAAAAAAAA';
const fee = await connection.getFeeForMessage(message);
console.log(fee);

使用 Python

from solana.rpc.api import Client

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

注意事项

  1. 返回网络对特定消息收取的手续费
  2. 对于更准确地计算交易费用非常有用
  3. 优于旧的费用计算方法
  4. 可以指定不同的 commitment 级别
  5. 如果消息无效或无法处理则返回 null

最佳实践

  1. 在发送交易之前使用此方法准确预测费用
  2. 提供有效的 base64 编码消息以避免 null 响应
  3. 在应用程序中适当处理 null 响应
  4. 适当处理网络错误并重试
  5. 当需要精确费用计算时使用此方法