Solana链Raydium兑换遇TransactionExpiredBlockheightExceededError求助
问题描述
在NodeJS环境中使用Solana web3.js开发时,尝试通过Raydium的USDC-SOL池(地址:58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2)将USDC(地址:EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v)兑换为SOL,持续出现TransactionExpiredBlockheightExceededError错误。
错误栈信息:
/Users/carlosmoralescliment/Projects/raydium-swap/node_modules/@solana/web3.js/src/connection.ts:3957 throw new TransactionExpiredBlockheightExceededError(signature); ^ TransactionExpiredBlockheightExceededError: Signature 5MqEu2Vv5LY2JN932z6jWher65YbW3zgNNwgcSAz7du37CdBomACFX1XWPcLsHuZzygdCDBmZrvCo7kdJnMeZMHF has expired: block height exceeded. at Connection.confirmTransactionUsingBlockHeightExceedanceStrategy (/Users/carlosmoralescliment/Projects/raydium-swap/node_modules/@solana/web3.js/src/connection.ts:3957:15) at runMicrotasks (<anonymous>) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async Connection.confirmTransaction (/Users/carlosmoralescliment/Projects/raydium-swap/node_modules/@solana/web3.js/src/connection.ts:3768:14) at async sendAndConfirmTransaction (/Users/carlosmoralescliment/Projects/raydium-swap/node_modules/@solana/web3.js/src/utils/send-and-confirm-transaction.ts:47:7) { signature: '5MqEu2Vv5LY2JN932z6jWher65YbW3zgNNwgcSAz7du37CdBomACFX1XWPcLsHuZzygdCDBmZrvCo7kdJnMeZMHF' }
错误触发代码片段:
const hash = await sendAndConfirmTransaction(connection, txn, [keyPair], { skipPreflight: true, preflightCommitment: "confirmed", });
当前Solana RPC连接配置:
const connection = new Connection("https://api.mainnet-beta.solana.com", 'confirmed'); const secretKey = Uint8Array.from( JSON.parse(fs.readFileSync(`./keypair.json`) as unknown as string), );
询问需调整哪些配置解决问题,比如更换RPC、调整滑点或修改连接时的commitment参数?
解决方案
1. 更换为专用RPC
官方公共RPC(https://api.mainnet-beta.solana.com)存在请求限制、响应延迟问题,容易导致交易确认超时。更换为付费专用RPC可提升连接稳定性与交易处理效率。
修改连接代码示例:
// 替换为你的专用RPC地址(如Alchemy、QuickNode等服务商提供的地址) const connection = new Connection("https://solana-mainnet.g.alchemy.com/v2/你的API_KEY", 'confirmed');
2. 调整交易确认参数
当前sendAndConfirmTransaction配置存在两处问题:关闭预检查会导致交易提交后才暴露错误,无重试和超时设置易触发超时。修改如下:
const hash = await sendAndConfirmTransaction(connection, txn, [keyPair], { skipPreflight: false, // 开启预检查,提前排查交易错误 preflightCommitment: "confirmed", commitment: "confirmed", maxRetries: 5, // 增加交易重试次数 timeout: 30000, // 将超时时间延长至30秒 });
3. 延长交易有效区块高度范围
网络拥堵时,默认的交易有效区块高度阈值过低易触发超时。构建交易时手动扩大有效范围:
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash('confirmed'); const txn = new Transaction({ recentBlockhash: blockhash, lastValidBlockHeight: lastValidBlockHeight + 1000, // 延长有效区块高度范围 }).add(/* 你的Raydium兑换指令 */);
4. 调整滑点设置
滑点过小会因价格波动导致交易无法满足执行条件,最终触发超时。建议将滑点调整至0.5%-1%区间(可根据市场流动性调整),确保交易能在合理价格范围内被执行。
内容的提问来源于stack exchange,提问作者CarlosMC
相关产品推荐
相关产品推荐

