fix: retry send_transaction

This commit is contained in:
Sheng-Yan
2025-06-24 10:50:23 +08:00
parent 83c7bb3414
commit fb571337c5
2 changed files with 107 additions and 113 deletions
+17 -23
View File
@@ -125,15 +125,12 @@ async def buy_token(
# Calculate maximum SOL to spend with slippage # Calculate maximum SOL to spend with slippage
max_amount_lamports = int(amount_lamports * (1 + slippage)) max_amount_lamports = int(amount_lamports * (1 + slippage))
for attempt in range(max_retries):
try:
accounts = [ accounts = [
AccountMeta(pubkey=PUMP_GLOBAL, is_signer=False, is_writable=False), AccountMeta(pubkey=PUMP_GLOBAL, is_signer=False, is_writable=False),
AccountMeta(pubkey=PUMP_FEE, is_signer=False, is_writable=True), AccountMeta(pubkey=PUMP_FEE, is_signer=False, is_writable=True),
AccountMeta(pubkey=mint, is_signer=False, is_writable=False), AccountMeta(pubkey=mint, is_signer=False, is_writable=False),
AccountMeta( AccountMeta(pubkey=bonding_curve, is_signer=False, is_writable=True),
pubkey=bonding_curve, is_signer=False, is_writable=True
),
AccountMeta( AccountMeta(
pubkey=associated_bonding_curve, pubkey=associated_bonding_curve,
is_signer=False, is_signer=False,
@@ -144,12 +141,8 @@ async def buy_token(
is_signer=False, is_signer=False,
is_writable=True, is_writable=True,
), ),
AccountMeta( AccountMeta(pubkey=payer.pubkey(), is_signer=True, is_writable=True),
pubkey=payer.pubkey(), is_signer=True, is_writable=True AccountMeta(pubkey=SYSTEM_PROGRAM, is_signer=False, is_writable=False),
),
AccountMeta(
pubkey=SYSTEM_PROGRAM, is_signer=False, is_writable=False
),
AccountMeta( AccountMeta(
pubkey=SYSTEM_TOKEN_PROGRAM, is_signer=False, is_writable=False pubkey=SYSTEM_TOKEN_PROGRAM, is_signer=False, is_writable=False
), ),
@@ -157,9 +150,7 @@ async def buy_token(
AccountMeta( AccountMeta(
pubkey=PUMP_EVENT_AUTHORITY, is_signer=False, is_writable=False pubkey=PUMP_EVENT_AUTHORITY, is_signer=False, is_writable=False
), ),
AccountMeta( AccountMeta(pubkey=PUMP_PROGRAM, is_signer=False, is_writable=False),
pubkey=PUMP_PROGRAM, is_signer=False, is_writable=False
),
] ]
discriminator = struct.pack("<Q", 16927863322537952870) discriminator = struct.pack("<Q", 16927863322537952870)
@@ -173,26 +164,28 @@ async def buy_token(
payer.pubkey(), payer.pubkey(), mint payer.pubkey(), payer.pubkey(), mint
) )
msg = Message( msg = Message(
[set_compute_unit_price(1_000), idempotent_ata_ix, buy_ix], [set_compute_unit_price(1_000), idempotent_ata_ix, buy_ix], payer.pubkey()
payer.pubkey(),
) )
recent_blockhash = await client.get_latest_blockhash()
opts = TxOpts(skip_preflight=True, preflight_commitment=Confirmed)
for attempt in range(max_retries):
try:
tx_buy = await client.send_transaction( tx_buy = await client.send_transaction(
Transaction( Transaction(
[payer], [payer],
msg, msg,
(await client.get_latest_blockhash()).value.blockhash, recent_blockhash.value.blockhash,
), ),
opts=TxOpts(skip_preflight=True, preflight_commitment=Confirmed), opts=opts,
) )
tx_hash = tx_buy.value
print( print(
f"Transaction sent: https://explorer.solana.com/tx/{tx_buy.value}" f"Transaction sent: https://explorer.solana.com/tx/{tx_hash}"
) )
await client.confirm_transaction(tx_hash, commitment="confirmed", sleep_seconds=1)
await client.confirm_transaction(tx_buy.value, commitment="confirmed")
print("Transaction confirmed") print("Transaction confirmed")
return # Success, exit the function return # Success, exit the function
except Exception as e: except Exception as e:
print(f"Attempt {attempt + 1} failed: {str(e)[:50]}") print(f"Attempt {attempt + 1} failed: {str(e)[:50]}")
if attempt < max_retries - 1: if attempt < max_retries - 1:
@@ -203,6 +196,7 @@ async def buy_token(
print("Max retries reached. Unable to complete the transaction.") print("Max retries reached. Unable to complete the transaction.")
def load_idl(file_path): def load_idl(file_path):
with open(file_path) as f: with open(file_path) as f:
return json.load(f) return json.load(f)
+10 -10
View File
@@ -157,9 +157,6 @@ async def sell_token(
print(f"Selling {token_balance_decimal} tokens") print(f"Selling {token_balance_decimal} tokens")
print(f"Minimum SOL output: {min_sol_output / LAMPORTS_PER_SOL:.10f} SOL") print(f"Minimum SOL output: {min_sol_output / LAMPORTS_PER_SOL:.10f} SOL")
# Continue with the sell transaction
for attempt in range(max_retries):
try:
accounts = [ accounts = [
AccountMeta(pubkey=PUMP_GLOBAL, is_signer=False, is_writable=False), AccountMeta(pubkey=PUMP_GLOBAL, is_signer=False, is_writable=False),
AccountMeta(pubkey=PUMP_FEE, is_signer=False, is_writable=True), AccountMeta(pubkey=PUMP_FEE, is_signer=False, is_writable=True),
@@ -208,21 +205,24 @@ async def sell_token(
sell_ix = Instruction(PUMP_PROGRAM, data, accounts) sell_ix = Instruction(PUMP_PROGRAM, data, accounts)
msg = Message([set_compute_unit_price(1_000), sell_ix], payer.pubkey()) msg = Message([set_compute_unit_price(1_000), sell_ix], payer.pubkey())
recent_blockhash = await client.get_latest_blockhash()
opts = TxOpts(skip_preflight=True, preflight_commitment=Confirmed)
# Continue with the sell transaction
for attempt in range(max_retries):
try:
tx = await client.send_transaction( tx = await client.send_transaction(
Transaction( Transaction(
[payer], [payer],
msg, msg,
(await client.get_latest_blockhash()).value.blockhash, recent_blockhash.value.blockhash,
), ),
opts=TxOpts(skip_preflight=True, preflight_commitment=Confirmed), opts=opts,
) )
tx_hash = tx.value
print(f"Transaction sent: https://explorer.solana.com/tx/{tx.value}") print(f"Transaction sent: https://explorer.solana.com/tx/{tx_hash}")
await client.confirm_transaction(tx_hash, commitment="confirmed", sleep_seconds=1)
await client.confirm_transaction(tx.value, commitment="confirmed")
print("Transaction confirmed") print("Transaction confirmed")
return # Success, exit the function return # Success, exit the function
except Exception as e: except Exception as e:
print(f"Attempt {attempt + 1} failed: {e!s}") print(f"Attempt {attempt + 1} failed: {e!s}")
if attempt < max_retries - 1: if attempt < max_retries - 1: