You've created a wallet feature using the Web3js SDK.
I created functions related to Solana and SPL-token retrieval and transfer and used them well. However, recently I am getting an error when transferring SPL-tokens.
I tried various methods to fix the error, but it was not possible to fix the error.
Below is the Javascript code I used before and the code I have modified now.
Do any of these codes require additional settings or other methods to fix the error?
Or is this a bug in the Solana node network?
old code
const myMint = new solanaWeb3.PublicKey(req.query.contract);
const myToken = new splToken.Token(
conn,
myMint,
splToken.TOKEN_PROGRAM_ID,
fromWallet
);
let toWallet = new solanaWeb3.PublicKey(req.query.to);
const fromTokenAccount = await myToken.getOrCreateAssociatedAccountInfo(
fromWallet.publicKey
);
const toTokenAccount = await myToken.getOrCreateAssociatedAccountInfo(
toWallet
);
amount = req.query.amount * Math.pow(10, 8)
// Add token transfer instructions to transaction
const transaction = new solanaWeb3.Transaction()
.add(
splToken.Token.createTransferInstruction(
splToken.TOKEN_PROGRAM_ID,
fromTokenAccount.address,
toTokenAccount.address,
fromWallet.publicKey,
[],
amount
)
);
// Sign transaction, broadcast, and confirm
const signature = await solanaWeb3.sendAndConfirmTransaction(
conn,
transaction,
[fromWallet] ,
{ commitment: 'confirmed', skipPreflight: true}
);
new code1
const modifyComputeUnits = solanaWeb3.ComputeBudgetProgram.setComputeUnitLimit({
units: 300_000
});
const addPriorityFee = solanaWeb3.ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 10
});
let latestBlockhash = await conn.getLatestBlockhash("confirmed");
const transaction = new solanaWeb3.Transaction({
feePayer : fromWallet.publicKey ,
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight
})
.add(modifyComputeUnits)
.add(addPriorityFee)
.add(
splToken.Token.createTransferInstruction(
splToken.TOKEN_PROGRAM_ID,
fromTokenAccount.address,
toTokenAccount.address,
fromWallet.publicKey,
[],
amount
)
);
const transactionSignature = await solanaWeb3.sendAndConfirmTransaction(
conn,
transaction,
[fromWallet] ,
{ commitment: 'confirmed', skipPreflight: true}
);
new code2
let latestBlockhash = await conn.getLatestBlockhash("confirmed");
const transaction = new solanaWeb3.Transaction({
feePayer : fromWallet.publicKey ,
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
})
.add(modifyComputeUnits)
.add(addPriorityFee)
.add(
splToken.Token.createTransferInstruction(
splToken.TOKEN_PROGRAM_ID,
fromTokenAccount.address,
toTokenAccount.address,
fromWallet.publicKey,
[],
amount
)
);
const message = transaction.serializeMessage();
const signature = nacl.sign.detached(message, fromWallet.secretKey);
transaction.addSignature(fromWallet.publicKey, Buffer.from(signature));
const transactionSignature = await conn.sendRawTransaction(
transaction.serialize(),
{ commitment: 'confirmed', skipPreflight: true , maxRetries: 150 }
);
const latestBlockHash = await conn.getLatestBlockhash();
const tx = await conn.confirmTransaction(transactionSignature, {
blockhash: latestBlockHash.blockhash,
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
commitment: 'confirmed', skipPreflight: true , maxRetries: 150
});