Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transfer spl token, block height exceeded error #2361

Closed
amir-opcode opened this issue Mar 22, 2024 · 5 comments
Closed

transfer spl token, block height exceeded error #2361

amir-opcode opened this issue Mar 22, 2024 · 5 comments
Labels
question Add this to close an issue with instructions on how to repost as a question on Stack Exchange

Comments

@amir-opcode
Copy link

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\node_modules.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

@amir-opcode amir-opcode added the bug Something isn't working label Mar 22, 2024
@buffalojoec
Copy link
Contributor

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
Copy link

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.

@steveluscher steveluscher added question Add this to close an issue with instructions on how to repost as a question on Stack Exchange and removed bug Something isn't working labels Mar 23, 2024
Copy link
Contributor

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
Copy link
Author

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

Copy link
Contributor

github-actions bot commented Apr 2, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Add this to close an issue with instructions on how to repost as a question on Stack Exchange
Projects
None yet
Development

No branches or pull requests

4 participants