solana-labs / solana-web3.js

Solana JavaScript SDK
https://solana-labs.github.io/solana-web3.js
MIT License
2.16k stars 862 forks source link

transfer spl token, block height exceeded error #2361

Closed amir-opcode closed 7 months ago

amir-opcode commented 7 months ago

Overview

transfer spl token in node js

Steps to reproduce

import * as splToken from "@solana/spl-token"; import { Keypair, PublicKey, sendAndConfirmTransaction, Transaction, } from "@solana/web3.js"; import bs58 from "bs58"; import createHttpError from "http-errors"; import tokens, { TokenName } from "~/constants/token.constant"; import { env } from "~/env"; import solanaConnection from "~/utils/solanaConnection.util";

const publicKey = new PublicKey(env.OWNER_SOLANA_WALLET_ADDRESS); const privateKeyBytes = bs58.decode(env.OWNER_SOLANA_WALLET_PRIVATE_KEY); const keypair = Keypair.fromSecretKey(privateKeyBytes); const secretKey = keypair.secretKey; const payer = { publicKey, secretKey };

const { mintId, decimals, programId } = tokens.evol; const mintPublicKey = new PublicKey(mintId);

export default async function transferSolanaToken(to: string, amount: number) { try { const fromTokenAccount = await splToken.getOrCreateAssociatedTokenAccount( solanaConnection, payer, mintPublicKey, publicKey, undefined, undefined, undefined, new PublicKey(programId), );

const destPublicKey = new PublicKey(to);

const associatedDestinationTokenAddr =
  await splToken.getOrCreateAssociatedTokenAccount(
    solanaConnection,
    payer,
    mintPublicKey,
    destPublicKey,
    undefined,
    undefined,
    undefined,
    new PublicKey(programId),
  );

const instruction = splToken.createTransferCheckedInstruction(
  fromTokenAccount.address,
  new PublicKey(mintId),
  associatedDestinationTokenAddr.address,
  publicKey,
  amount * decimals,
  9,
  [keypair],
  new PublicKey(programId),
);

const transaction = new Transaction().add(instruction);
transaction.feePayer = publicKey;

const blockhash =
  await solanaConnection.getLatestBlockhashAndContext("recent");

transaction.recentBlockhash = blockhash?.value.blockhash;
transaction.lastValidBlockHeight = blockhash.value.lastValidBlockHeight;
transaction.sign(payer);
console.log("before confirm");
const transactionSignature = await sendAndConfirmTransaction(
  solanaConnection,
  transaction,
  [keypair],
  {
    skipPreflight: true,
    commitment: "confirmed",
    maxRetries: 1,
    preflightCommitment: "confirmed",
  },
);
console.log("after confirm");

return {
  txHash: transactionSignature,
  tokenName: TokenName.EVOL,
};

} catch (error) { console.log("🚀 ~ transferSolanaToken ~ error:", error);

throw new createHttpError.InternalServerError(
  "Something went wrong, please try again later",
);

} }

Description of bug

🚀 ~ transferSolanaToken ~ error: TransactionExpiredBlockheightExceededError: Signature 2aCzLc1BfKQc99tDGZj5muDhu7eSimu7k2YTPh5nBrGVnNeta51qYkTQWSTUsSFPSfnEPgmLg87oNvrW4H62W6Q has expired: block height exceeded. at Connection.confirmTransactionUsingBlockHeightExceedanceStrategy (webpack-internal:///(rsc)/./node_modules/.pnpm/@solana+web3.js@1.91.1/node_modules/@solana/web3.js/lib/index.esm.js:5855:23) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Connection.confirmTransaction (webpack-internal:///(rsc)/./node_modules/.pnpm/@solana+web3.js@1.91.1/node_modules/@solana/web3.js/lib/index.esm.js:5695:20) at async sendAndConfirmTransaction (webpack-internal:///(rsc)/./node_modules/.pnpm/@solana+web3.js@1.91.1/node_modules/@solana/web3.js/lib/index.esm.js:2091:19) at async transferSolanaToken (webpack-internal:///(rsc)/./src/utils/transfer/transferSolanaToken.util.ts:47:38) at async withdraw (webpack-internal:///(rsc)/./src/server/services/withdraw.services.ts:77:36) at async POST (webpack-internal:///(rsc)/./src/app/api/withdraw/route.ts:9:12) at async D:\projects\coin-flip\nodemodules.pnpm\next@14.0.3@babel+core@7.23.5_react-dom@18.2.0_react@18.2.0\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:62609 { signature: '2aCzLc1BfKQc99tDGZj5muDhu7eSimu7k2YTPh5nBrGVnNeta51qYkTQWSTUsSFPSfnEPgmLg87oNvrW4H62W6Q' }

----------

I used to use this code without any problem, but this problem has appeared recently. It has been almost a month since it had a problem, otherwise it was working without problems

buffalojoec commented 7 months ago

Hello, I'm a bit skeptical that this is a bug with Web3.js, since you mention the code has been working for some time.

Have you recently upgraded your version of @solana/web3.js and can you isolate some specific version bump that causes this issue? Namely, the code works prior to the upgrade and fails immediately after.

It's more likely this is an issue with network congestion and lack of priority fees, which solutions are in development for. Could you share which network you're attempting to send this transaction to?

The fact that the transaction is submitting successfully, but is not getting confirmed within the blockhash time limit leads me to believe the problem is not within Web3.js.

mflanagan1041 commented 7 months ago

this is probably 100% caused by not using priority fees. lately when you submit a transaction if you are not including a priority fee in your transaction the validators will not pick up your transaction in time before the block expires which results in this error.

github-actions[bot] commented 7 months ago

Hi @shahram0102,

Thanks for your question!

We want to make sure to keep signal strong in the GitHub issue tracker – to make sure that it remains the best place to track issues that affect the development of the Solana JavaScript SDK itself.

Questions like yours deserve a purpose-built Q&A forum. Unless there exists evidence that this is a bug with the Solana JavaScript SDK itself, please post your question to the Solana Stack Exchange using this link: https://solana.stackexchange.com/questions/ask


This automated message is a result of having added the ‘question’ tag.

amir-opcode commented 7 months ago

Hello, I'm a bit skeptical that this is a bug with Web3.js, since you mention the code has been working for some time.

Have you recently upgraded your version of @solana/web3.js and can you isolate some specific version bump that causes this issue? Namely, the code works prior to the upgrade and fails immediately after.

It's more likely this is an issue with network congestion and lack of priority fees, which solutions are in development for. Could you share which network you're attempting to send this transaction to?

The fact that the transaction is submitting successfully, but is not getting confirmed within the blockhash time limit leads me to believe the problem is not within Web3.js.

this is probably 100% caused by not using priority fees. lately when you submit a transaction if you are not including a priority fee in your transaction the validators will not pick up your transaction in time before the block expires which results in this error.

Yes, that was the problem and I fixed it. Before, there was no need to set priority fees. Even now, if priority fees are not registered, sometimes the transaction is approved. Thank you for your guidance

github-actions[bot] commented 6 months ago

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.