Long time making transactions

I have a Token-based game in Solana. Users usually transfer tokens from one wallet to another all the time.

I have managed to make this process work very well but everytime my code reach this step:
web3.sendAndConfirmTransaction(connection,transaction,[fromWallet] )

it often takes a LOT OF TIME to finish
Sometimes…it even gets longer than 60 seconds, causing a timeout.

Watching the solana website, it says it does about 1.7k transactions per seconds…sometimes even more sometimes less. And i can see they usually dont get more than 2k transactions per second. Meaning my transactions should end in 2 seconds…5 seconds tops.

So why this command
web3.sendAndConfirmTransaction(connection,transaction,[fromWallet] )

Takes more than 40 seconds to complete everytime?

Hi @torugor, it might be that you need to set the commitment mode of your connection so that it doesn’t wait for transactions to finalize, just for them to be confirmed by one node in the chain.

It’s technically possible for a block that has only been “confirmed” to become reverted later, as if it never happened, but it’s unlikely, so waiting for the transaction to be confirmed by one node is probably enough for you to register the transaction as a success to your users.

When creating a new Solana connection with the web3.js API try doing this:

const connection = new Connection(rpcServerUrl, 'confirmed');

This tells the API to only wait for the transaction to get confirmed by one node before returning success. If ‘confirmed’ is not specified, it will default to waiting for a supermajority of the blockchain nodes to all validate the the transaction and finalize it, which isn’t necessary unless you require absolute certainty that the transaction has been irreversibly committed to the chain.

Wow thank you for the fast answer

Changing:
var connection = new web3.Connection(web3.clusterApiUrl(“mainnet-beta”));

to:

var connection = new web3.Connection(web3.clusterApiUrl(“mainnet-beta”),‘confirmed’);

No problem, I just happened to be online. :slight_smile:


Yep, that should do it, I think. I haven’t tested this myself, but I’ve looked at the code and the docs so I think it should work.

Tell you if it works in a bit…

Glad to say my first test worked.

Tomorrow going to make more tests but it did it in way less than 10 seconds.

1 Like