I'm new to solana development but I'm facing an issue. I'm writing a function to send spl tokens to other wallets using the private keys. I'm coding in Javascript. The issue I've been facing is that it goes sometimes but doesn't most times. I tried increasing priority fee like this (Hope I'm doing it right. Solana's docs are down, including this)
I get this error whenever it fails. After taking a long time, it returns
Error occurred: TransactionExpiredBlockheightExceededError: Signature 2FbKMGHSrcrV1xxxxxxxxxxxxxxxxxxxxx has expired: block height exceeded.
at Connection.confirmTransactionUsingBlockHeightExceedanceStrategy
Here is my code
const sendSplTokenWithBase58 = async (
privateKeyBase58: string,
toKeyBase58: string,
numOfToken: number,
tokenAddress: string,
priorityRateMicroLamports: number = 25000, // New parameter for the priority rate in microLamports
) => {
try {
const splToken = await import("@solana/spl-token");
const solana = await import("@solana/web3.js");
const privateKey = Buffer.from(bs58.decode(privateKeyBase58));
const fromKeyPair = solana.Keypair.fromSecretKey(privateKey);
const mintAddress = new solana.PublicKey(tokenAddress);
const tokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
connection,
fromKeyPair,
mintAddress,
fromKeyPair.publicKey,
);
console.log("1");
const destinationAccount = await splToken.getOrCreateAssociatedTokenAccount(
connection,
fromKeyPair,
mintAddress,
new solana.PublicKey(toKeyBase58),
);
console.log("2");
// Instruction to set the compute unit price for priority fee
const priorityFeeInstructions = solana.ComputeBudgetProgram.setComputeUnitPrice({
microLamports: priorityRateMicroLamports,
});
console.log("3");
// Create a transaction with the priority fee instruction and token transfer instruction
const transaction = new solana.Transaction().add(
priorityFeeInstructions, // Add the priority fee instruction
splToken.createTransferInstruction(
tokenAccount.address,
destinationAccount.address,
fromKeyPair.publicKey,
numOfToken * Math.pow(10, decimal),
),
);
console.log("4");
// const latestBlockHash = await connection.getLatestBlockhash("confirmed");
// transaction.recentBlockhash = latestBlockHash.blockhash;
console.log("4.5");
const signature = await solana.sendAndConfirmTransaction(connection, transaction, [fromKeyPair]);
const transactionInfo = await connection.getTransaction(signature);
console.log("5");
// Extract relevant information from the transaction details
const transactionHash = signature;
const transactionStatus = transactionInfo?.meta?.err ? false : true;
const splTokensSent = numOfToken;
return { transactionHash, transactionStatus, splTokensSent };
} catch (error) {
console.error("Error occurred:", error);
return { transactionHash: false };
}
};
It does all the console.logs up till 4.5 and then waits a long time to fail and show that error.
The confusing thing is that I've had a few times it just went through. I'm confused with the inconsistency...