feat: add priority fee support with dynamic/fixed options, hard cap, and extra fee

This commit is contained in:
smypmsa
2025-03-12 22:11:31 +00:00
parent 3fb5d15104
commit ac3d463d20
10 changed files with 266 additions and 40 deletions
+28 -12
View File
@@ -8,7 +8,11 @@ from typing import Any
from solana.rpc.async_api import AsyncClient
from solana.rpc.commitment import Confirmed
from solana.rpc.types import TxOpts
from solders.compute_budget import set_compute_unit_limit, set_compute_unit_price
from solders.hash import Hash
from solders.instruction import Instruction
from solders.keypair import Keypair
from solders.message import Message
from solders.pubkey import Pubkey
from solders.transaction import Transaction
@@ -88,34 +92,46 @@ class SolanaClient:
response = await client.get_latest_blockhash()
return response.value.blockhash
async def send_transaction(
async def build_and_send_transaction(
self,
transaction: Transaction,
instructions: list[Instruction],
signer_keypair: Keypair,
skip_preflight: bool = True,
max_retries: int = 3,
priority_fee: int | None = None,
) -> str:
"""Send a transaction to the network.
"""
Send a transaction with optional priority fee.
Args:
transaction: Prepared transaction
skip_preflight: Whether to skip preflight checks
max_retries: Maximum number of sending attempts
instructions: List of instructions to include in the transaction.
skip_preflight: Whether to skip preflight checks.
max_retries: Maximum number of retry attempts.
priority_fee: Optional priority fee in lamports.
Returns:
Transaction signature
Raises:
Exception: If transaction fails after all retries
Transaction signature.
"""
client = await self.get_client()
# Attempt to send with retries
# Add priority fee instructions if applicable
if priority_fee is not None:
fee_instructions = [
set_compute_unit_limit(200_000), # Default compute unit limit
set_compute_unit_price(priority_fee),
]
instructions = fee_instructions + instructions
recent_blockhash = await self.get_latest_blockhash()
message = Message(instructions, signer_keypair.pubkey())
transaction = Transaction([signer_keypair], message, recent_blockhash)
for attempt in range(max_retries):
try:
tx_opts = TxOpts(
skip_preflight=skip_preflight, preflight_commitment=Confirmed
)
response = await client.send_transaction(transaction, opts=tx_opts)
response = await client.send_transaction(transaction, tx_opts)
return response.value
except Exception as e: