This is my function code:
async sendAndConfirmTransactionWithRetry(connection: Connection, instructions: TransactionInstruction[], signer: Wallet, feePayer: Wallet = null) { let latestBlockHash = await connection.getLatestBlockhash('confirmed') let messageV0 = new TransactionMessage({ payerKey: feePayer ? feePayer.publicKey : signer.publicKey, recentBlockhash: latestBlockHash.blockhash, instructions, }).compileToV0Message(); let versionedTransaction = new VersionedTransaction(messageV0); versionedTransaction.sign(feePayer ? [signer.payer, feePayer.payer] : [signer.payer]); console.log('fee: ', await connection.getFeeForMessage(versionedTransaction.message)) return promiseRetry(async (retry, number) => { // for (let number = 1; number <= 5; number++) { console.log(`Attempt number ${number} to send transaction`); try { const epochInfo = await connection.getEpochInfo('confirmed') if (number > 1 && epochInfo.blockHeight >= latestBlockHash.lastValidBlockHeight) { console.log('epochInfo:', epochInfo) latestBlockHash = await connection.getLatestBlockhash('confirmed') messageV0 = new TransactionMessage({ payerKey: feePayer ? feePayer.publicKey : signer.publicKey, recentBlockhash: latestBlockHash.blockhash, instructions, }).compileToV0Message(); versionedTransaction = new VersionedTransaction(messageV0); versionedTransaction.sign(feePayer ? [signer.payer, feePayer.payer] : [signer.payer]); } console.log('latestBlockHash:', latestBlockHash) const rawTransaction = versionedTransaction.serialize() const txid = await connection.sendRawTransaction(rawTransaction, { skipPreflight: true, maxRetries: 3, preflightCommitment: 'processed' }); console.log(`Transaction Submitted: ${txid}`); await connection.confirmTransaction( { signature: txid, blockhash: latestBlockHash.blockhash, lastValidBlockHeight: latestBlockHash.lastValidBlockHeight, } ); return txid } catch (error) { // console.error(`Attempt ${number} failed with error: ${error}`); // if (number === 5) { // // Last attempt failed, throw the error // throw new Error(`Transaction failed after ${number} attempts.`); // } // // Wait for 2 seconds before retrying // await new Promise((resolve) => setTimeout(resolve, 2000)); console.error('Transaction failed, retrying...', error.message); retry(error); } }, { retries: 5, // Number of retries factor: 2, // Exponential backoff factor minTimeout: 1000, // Minimum timeout between retries in milliseconds maxTimeout: 5000, // Maximum timeout between retries in milliseconds }) }
const sourceAccount = await getOrCreateAssociatedTokenAccount(this.connection, this.wallet.payer, new PublicKey(this.usdtAddress), this.wallet.publicKey) const desAccount = await getOrCreateAssociatedTokenAccount(this.connection, this.wallet.payer, new PublicKey(this.usdtAddress), this.walletPayFee.publicKey) const feeFromApi = await this.getPriorityFee() console.log('feeFromApi', feeFromApi) const instructions = [ createTransferInstruction( sourceAccount.address, desAccount.address, this.wallet.publicKey, Number(0.01) * Math.pow(10, this.usdtDecimals) ) ] // Simulate the transaction to check for errors const computeUnits = await getSimulationComputeUnits(this.connection, instructions, this.walletPayFee.publicKey, []) const unitPrice = ComputeBudgetProgram.setComputeUnitPrice({ microLamports: feeFromApi * 20 }) const unitLimit = ComputeBudgetProgram.setComputeUnitLimit({ units: Math.ceil(computeUnits * 1.5) }) instructions.push(unitLimit) instructions.push(unitPrice) console.log('unitLimit', computeUnits * 1.5) console.log('unitPrice', feeFromApi * 20) const result = await this.sendAndConfirmTransactionWithRetry(this.connection, instructions, this.wallet, this.walletPayFee) console.log('result: ', result)
result is
Signature 5rJT3EC6ckgXcF9kYkMskRaKUJE4tVpjq4BMSPhG9VfuR4WGvrEu2Pe9TRkF8937Z2nRMP8gbk8KKHFh1TdWYVZi has expired: block height exceeded.