0

I created an API where I send sol. I put it in a try-catch block so if it succeeds or fails I will get a response. Right now the Solana blockchain is down so it would load way too long and my API timeout after 1 minute without giving any response. I want that any transaction that takes more than 50 seconds to fail. I saw when I create a connection with new web3.Connection() I can set a ConnectionConfig with the key confirmTransactionInitialTimeout but doesn't matter what number I put in (even with 1 millisecond) the API just keeps loading without the promise being rejected. How can I reject the promise if it takes more than 50 seconds? My code:

const handler = async (req, res) => {
  try {
    const connection = new Connection("https://ssc-dao.genesysgo.net/", {
      commitment: "confirmed",
      confirmTransactionInitialTimeout: 50000
    })
    //   do the transaction logic
    return res.status(200).json({ message: "success" })
  } catch (error) {
    return res.status(500).json({ message: "failed" })
  }
}

1 Answer 1

1

I did some testing by setting confirmTransactionInitialTimeout to values 10, 100 and 1000, pointing to solana devnet, it worked as expected. Since the timeout is low, it was throwing errors with message

Transaction was not confirmed in (0.01, 0.1 and 1.00) seconds. It is unknown if it succeeded or failed. Check signature

But the transactions were successful as devnet is running fine.

I guess, something is wrong in your JS code. Either you are missing an await before your async function call or you are not confirming the transaction.

await this.connection.confirmTransaction(txSig, 'singleGossip');
2
  • thank you for your answer. what is the difference between Commitment "confirmed" and "singleGossip"?
    – Noah
    Oct 1, 2022 at 6:33
  • It's how connection handles the confirmTransactionInitialTimeout value when it's not set, but it seems it's deprecated now. You can use 'confirmed' From web3.js let timeoutMs = this._confirmTransactionInitialTimeout || 60 * 1000; switch (subscriptionCommitment) { case 'processed': case 'recent': case 'single': case 'confirmed': case 'singleGossip': { timeoutMs = this._confirmTransactionInitialTimeout || 30 * 1000; break; } Oct 1, 2022 at 6:41

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.