I was wondering what is the best way to send a transaction and ensure it gets processed. At the moment i am using this python code that sends an already signed transaction:
from solana.rpc.commitment import Confirmed
from solana.rpc.core import UnconfirmedTxError
from solana.rpc.types import TxOpts
from solders.transaction import VersionedTransaction
async def send_transaction(async_client: AsyncClient, signed_txn: VersionedTransaction) -> str:
opts = TxOpts(
skip_preflight=False,
preflight_commitment=Confirmed,
skip_confirmation=False,
)
try:
result = await async_client.send_raw_transaction(txn=bytes(signed_txn), opts=opts)
transaction_id = json.loads(result.to_json())["result"]
return transaction_id
except UnconfirmedTxError:
raise Exception("Transaction confirmation timed out. Please set a higher priority fee")
Sometimes this code works well and transactions are processed and confirmed, but other times I get the UnconfirmedTxError, which contains this message: "Unable to confirm transaction <transaction_id>". Sometimes this happens even if I set a very high priority fee for the transaction.
My question is: when i catch the UnconfirmedTxError, is it safe to retry sending the transaction right away, or is there the risk that the same transaction will be executed twice? Should i perform any additional checks before retrying?
Overall, am I using the correct approach for sending transactions? Any advice would be very much appreciated.