1

Our web app charges users X amount of SPL token. Users sign through their connected wallet. After the tx charges them, a request is sent to the back end where our server changes some metadata on their nft.

Goal: I would like to confirm that the tx executed correctly on the server. The reason is to prevent users from sending bogus requests to our backend and bypassing the front end SPL token tx.

Problem: The following code does not work. It never finds the tx. Oh, I also don't know what maxSupportedTransactionVersion should be set to lol

const tx = await connection.getTransaction(txSig, {
       maxSupportedTransactionVersion: 100000000000000,
     });

     if (!tx) {
       throw new TRPCError({
         code: "INTERNAL_SERVER_ERROR",
         message: "Invalid TX",
         // optional: pass the original error to retain stack trace
       });
     }

Is there a better way to do this?

Follow Up It was suggested that we use connection.confirmTransaction(). The code above was failing because getTransaction defaults to only giving you transactions at finalized commitment (eg. they have achieved the maximum number of confirmations).

Not sure how to use connection.confirmTransaction() on our server. There is no parameter to specify the tx we are confirming!

Thank you for your help!

1 Answer 1

0

This is the method signature for confirmTransaction():

export type BlockhashWithExpiryBlockHeight = Readonly<{
  blockhash: Blockhash;
  lastValidBlockHeight: number;
}>;

export type BlockheightBasedTransactionConfirmationStrategy = {
  signature: TransactionSignature;
} & BlockhashWithExpiryBlockHeight;

confirmTransaction(
  strategy: BlockheightBasedTransactionConfirmationStrategy,
  commitment?: Commitment,
): Promise<RpcResponseAndContext<SignatureResult>>;

So, essentially, your call will look like this:

try {
  await connection.confirmTransaction({
    signature: txSig,
    blockhash: '...', // The latest blockhash that you used to submit it.
    lastValidBlockHeight: ..., // The block height after which that hash is no longer valid
  });
} catch (e) {
  // The transaction failed to confirm
}

You obtained blockhash and lastValidBlockHeight when you called connection.getLatestBlockhash() to construct the transaction in the first place.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.