使用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
错误?
发布于 2022-01-15 12:06:12
重试并不是件坏事!在某些情况下,它实际上是处理丢弃事务的首选方法。例如,这意味着:
// assuming you have a transaction named `transaction` already
const blockhashResponse = await connection.getLatestBlockhashAndContext();
const lastValidBlockHeight = blockhashResponse.context.slot + 150;
const rawTransaction = transaction.serialize();
let blockheight = await connection.getBlockHeight();
while (blockheight < lastValidBlockHeight) {
connection.sendRawTransaction(rawTransaction, {
skipPreflight: true,
});
await sleep(500);
blockheight = await connection.getBlockHeight();
}
您可能喜欢阅读有关重试事务: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
https://stackoverflow.com/questions/70717996
复制相似问题