2

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

2

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.

3
  • How do you ensure that txSig is of type BlockheightBasedTransactionConfirmationStrategy?
    – Burger Bob
    Feb 21, 2023 at 11:09
  • 1
    BlockheightBasedTransactionConfirmationStrategy is actually an object that contains a signature property that must be of type TransactionSignature. The TransactionSignature is basically an alias for string. Taken all together, the BlockheightBasedTransactionConfirmationStrategy is an object with three properties in it: signature, blockhash, and lastValidBlockHeight. Does that make sense? Feb 22, 2023 at 6:35
  • 1
    That makes sense, I just tried creating an object of {signature, blockhash, lastValidBlockHeight} and it no longer directs me to the deprecated function. Ty:)
    – Burger Bob
    Feb 22, 2023 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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