You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Solana主网转账遇TransactionExpiredBlockheightExceededError问题求助

问题

编写的SOL跨钱包转账程序在Devnet运行正常,但在Mainnet调用sendAndConfirmTransaction时持续触发TransactionExpiredBlockheightExceededError,错误信息如下:

TransactionExpiredBlockheightExceededError: Signature <signature> has expired: block height exceeded.

已尝试添加优先费用但未生效,现有代码如下:

const fromKeypair = Keypair.fromSecretKey(new Uint8Array(from_wallet_secret));
const toKeypair = Keypair.fromSecretKey(new Uint8Array(to_wallet__secret));

const PRIORITY_RATE = 100; // MICRO_LAMPORTS 
const SEND_AMT = 0.01 * LAMPORTS_PER_SOL;
const PRIORITY_FEE_IX = ComputeBudgetProgram.setComputeUnitPrice({microLamports: PRIORITY_RATE});

function generateTxExample() {
    return new Transaction().add(SystemProgram.transfer({
        fromPubkey: fromKeypair.publicKey,
        toPubkey: toKeypair.publicKey,
        lamports: SEND_AMT
    }));
}

async function createAndSendTransactions() {
    const txPriority = generateTxExample().add(PRIORITY_FEE_IX);
    const { blockhash, lastValidBlockHeight } = await SOLANA_CONNECTION.getLatestBlockhash();
    txPriority.recentBlockhash = blockhash;
    txPriority.lastValidBlockHeight = lastValidBlockHeight;

    sendAndConfirmTransaction(SOLANA_CONNECTION,txPriority,[fromKeypair]);
   
}

createAndSendTransactions();

请问主网环境中遗漏了哪些必要配置?

解答
  • 未等待异步操作完成:sendAndConfirmTransaction是异步函数,调用时未加await,会导致程序可能在交易确认前退出,主网区块确认逻辑更严格,这个问题会直接引发过期错误。修改代码:

    await sendAndConfirmTransaction(SOLANA_CONNECTION, txPriority, [fromKeypair]);
    
  • 优先费用竞争力不足:主网交易拥堵程度远高于Devnet,100 microLamports的优先费用不足以让交易快速被打包。建议提升优先费用至500-1000 microLamports,同时补充设置计算单元上限,明确交易资源需求:

    // 添加计算单元上限指令
    const COMPUTE_UNIT_LIMIT = ComputeBudgetProgram.setComputeUnitLimit({ units: 1000 });
    // 先添加计算单元上限,再设置优先费用
    const txPriority = generateTxExample().add(COMPUTE_UNIT_LIMIT).add(PRIORITY_FEE_IX);
    
  • 未指定交易确认参数:sendAndConfirmTransaction默认的确认策略在主网可能不够严谨,需要显式指定承诺级别和超时时间,给交易足够的确认窗口:

    await sendAndConfirmTransaction(
      SOLANA_CONNECTION,
      txPriority,
      [fromKeypair],
      {
        commitment: 'confirmed', // 或更高的'finalized'级别
        timeout: 30000 // 30秒超时,可根据主网情况调整
      }
    );
    
  • 区块哈希生命周期管理问题:获取区块哈希后到签名发送交易的间隔过长,主网区块生成速度快,易导致区块哈希过期。确保获取区块哈希后立即执行签名和发送操作,不要插入其他耗时逻辑。

内容的提问来源于stack exchange,提问作者user130198

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:41:26