I am trying to make a swap transaction on Raydium using Python. I always preparing the transaction and necessary instructions for the swap in advance:
print("Add Instructions To Transaction...")
swap_tx = Transaction()
signers = [payer]
swap_tx.add(modifyComputeUnits)
swap_tx.add(addPriorityFee)
swap_tx.fee_payer = payer.pubkey()
if swap_token_account_Instructions != None:
swap_tx.add(swap_token_account_Instructions)
swap_tx.add(instructions_swap)
Those instructions are not the issue, they work fine. I run needed code for sending the transaction in a loop and all I am changing is the recent_blockhash of the transaction. After that, I try to simulate the transaction to make sure it does not throw any errors. If there are no errors, I send the transaction. I am using max_retries=0 to customarily resend the transaction. I am attaching the relevant code.
while txnBool:
"""Send transaction"""
try:
blockhash_resp = solana_client.get_latest_blockhash(Confirmed)
swap_tx.recent_blockhash = blockhash_resp.value.blockhash
last_value = blockhash_resp.value.last_valid_block_height
print("5. Execute transaction...")
txn = solana_client.simulate_transaction(swap_tx, commitment=Processed)
if txn.value.err == None:
send_time = datetime.datetime.utcnow()
print(send_time)
blockheight = solana_client.get_block_height(commitment=Confirmed).value
while blockheight < last_value:
print("Trying to send")
new_txn = solana_client.send_transaction(swap_tx, *signers, opts=TxOpts(skip_preflight=True,
max_retries=0),
recent_blockhash=blockhash_resp.value.blockhash)
txid_sig = new_txn.value
try:
status = solana_client.get_signature_statuses([txid_sig])
status = json.loads(status.to_json())
if status["result"]["value"][0] != None:
print(status)
success_time = datetime.datetime.utcnow()
print(success_time)
print("Transaction Success", new_txn.value)
print(f"https://solscan.io/tx/{new_txn.value}")
txnBool = False
break
else:
blockheight = solana_client.get_block_height(commitment=Confirmed).value
except Exception as e:
print(e)
pass
else:
print(txn.value)
except Exception as e:
print("Exception, Retrying")
print(e)
The problem is that there can be a difference of up to 15-20 seconds between the send_time and success_time values.
Also, in the transaction, I am already using a priority fee, and there should be no issues with the rpc node as well. I see on solscan that people are able to send transactions quite quickly, literally tens of transactions per second, even though they use a priority fee much lower than mine, but their transactions reach the target at the right time, while mine do not. Please advise me on what I am doing wrong, perhaps I am using the wrong methods.