1

使用 Solana web3 发送交易时,有时会显示以下错误:
Error: failed to send transaction: Transaction simulation failed: Blockhash not found

除了重试x次之外,处理此错误的正确方法是什么?
有没有办法保证在发送交易时不会发生这个问题?

这是我如何发送交易的示例:

const web3 = require("@solana/web3.js")
const bs58 = require('bs58')

const publicKey = new web3.PublicKey(new Uint8Array(bs58.decode("BASE_58_PUBLIC_KEY").toJSON().data))
const secretKey = new Uint8Array(bs58.decode("BASE_58_SECRET_KEY").toJSON().data)

const connection = new web3.Connection(
  "https://api.mainnet-beta.solana.com", "finalized",
  {
    commitment: "finalized",
    confirmTransactionInitialTimeout: 30000
  }
)
const transaction = new web3.Transaction().add(
  web3.SystemProgram.transfer({
    fromPubkey: publicKey,
    toPubkey: publicKey,
    lamports: 1
  })
)
web3.sendAndConfirmTransaction(
  connection,
  transaction,
  [{publicKey: publicKey, secretKey: secretKey}],
  {commitment: "finalized"}
)


我该如何改进以避免Blockhash not found错误?

4

1 回答 1

1

重试不是坏事!在某些情况下,它实际上是处理丢弃事务的首选方式。

您可能想通读有关重试交易的食谱条目:https ://solanacookbook.com/guides/retrying-transactions.html

具体来说,它解释了如何实现一些重试逻辑:https ://solanacookbook.com/guides/retrying-transactions.html#customizing-rebroadcast-logic

重试具体意味着什么:https ://solanacookbook.com/guides/retrying-transactions.html#when-to-re-sign-transactions

于 2022-01-15T12:06:12.140 回答