refactor: tx building, getting dynamic prior fee

This commit is contained in:
smypmsa
2025-03-13 21:01:26 +00:00
parent ac3d463d20
commit a1d5966f29
9 changed files with 170 additions and 91 deletions
+19
View File
@@ -8,6 +8,8 @@ from typing import Any
from solders.pubkey import Pubkey
from core.pubkeys import PumpAddresses
@dataclass
class TokenInfo:
@@ -80,3 +82,20 @@ class Trader(ABC):
TradeResult with operation outcome
"""
pass
def _get_relevant_accounts(self, token_info: TokenInfo) -> list[Pubkey]:
"""
Get the list of accounts relevant for calculating the priority fee.
Args:
token_info: Token information for the buy/sell operation.
Returns:
list[Pubkey]: List of relevant accounts.
"""
return [
token_info.mint, # Token mint address
token_info.bonding_curve, # Bonding curve address
PumpAddresses.PROGRAM, # Pump.fun program address
PumpAddresses.FEE, # Pump.fun fee account
]
+26 -34
View File
@@ -69,16 +69,13 @@ class TokenBuyer(Trader):
TradeResult with buy outcome
"""
try:
# Extract token info
mint = token_info.mint
bonding_curve = token_info.bonding_curve
associated_bonding_curve = token_info.associated_bonding_curve
# Convert amount to lamports
amount_lamports = int(self.amount * LAMPORTS_PER_SOL)
# Fetch token price
curve_state = await self.curve_manager.get_curve_state(bonding_curve)
curve_state = await self.curve_manager.get_curve_state(
token_info.bonding_curve
)
token_price_sol = curve_state.calculate_price()
token_amount = self.amount / token_price_sol
@@ -92,14 +89,16 @@ class TokenBuyer(Trader):
f"Total cost: {self.amount:.6f} SOL (max: {max_amount_lamports / LAMPORTS_PER_SOL:.6f} SOL)"
)
associated_token_account = self.wallet.get_associated_token_address(mint)
associated_token_account = self.wallet.get_associated_token_address(
token_info.mint
)
await self._ensure_associated_token_account(mint, associated_token_account)
await self._ensure_associated_token_account(
token_info.mint, associated_token_account
)
tx_signature = await self._send_buy_transaction(
mint,
bonding_curve,
associated_bonding_curve,
token_info,
associated_token_account,
token_amount,
max_amount_lamports,
@@ -128,7 +127,7 @@ class TokenBuyer(Trader):
async def _ensure_associated_token_account(
self, mint: Pubkey, associated_token_account: Pubkey
) -> None:
"""Ensure associated token account exists.
"""Ensure associated token account exists, else create it.
Args:
mint: Token mint
@@ -147,18 +146,14 @@ class TokenBuyer(Trader):
payer=self.wallet.pubkey, owner=self.wallet.pubkey, mint=mint
)
# recent_blockhash: Hash = await self.client.get_latest_blockhash()
# create_ata_msg = Message([create_ata_ix], self.wallet.keypair.pubkey())
# create_ata_tx = Transaction(
# [self.wallet.keypair], create_ata_msg, recent_blockhash
# )
tx_sig = await self.client.build_and_send_transaction(
[create_ata_ix],
self.wallet.keypair,
skip_preflight=True,
max_retries=self.max_retries,
priority_fee=await self.priority_fee_manager.calculate_priority_fee(), # Get priority fee from manager
priority_fee=await self.priority_fee_manager.calculate_priority_fee(
[mint, SystemAddresses.PROGRAM, SystemAddresses.TOKEN_PROGRAM]
),
)
await self.client.confirm_transaction(tx_sig)
@@ -176,9 +171,7 @@ class TokenBuyer(Trader):
async def _send_buy_transaction(
self,
mint: Pubkey,
bonding_curve: Pubkey,
associated_bonding_curve: Pubkey,
token_info: TokenInfo,
associated_token_account: Pubkey,
token_amount: float,
max_amount_lamports: int,
@@ -186,9 +179,7 @@ class TokenBuyer(Trader):
"""Send buy transaction.
Args:
mint: Token mint
bonding_curve: Bonding curve address
associated_bonding_curve: Associated bonding curve address
token_info: Token information
associated_token_account: User's token account
token_amount: Amount of tokens to buy
max_amount_lamports: Maximum SOL to spend in lamports
@@ -204,10 +195,14 @@ class TokenBuyer(Trader):
pubkey=PumpAddresses.GLOBAL, is_signer=False, is_writable=False
),
AccountMeta(pubkey=PumpAddresses.FEE, is_signer=False, is_writable=True),
AccountMeta(pubkey=mint, is_signer=False, is_writable=False),
AccountMeta(pubkey=bonding_curve, is_signer=False, is_writable=True),
AccountMeta(pubkey=token_info.mint, is_signer=False, is_writable=False),
AccountMeta(
pubkey=associated_bonding_curve, is_signer=False, is_writable=True
pubkey=token_info.bonding_curve, is_signer=False, is_writable=True
),
AccountMeta(
pubkey=token_info.associated_bonding_curve,
is_signer=False,
is_writable=True,
),
AccountMeta(
pubkey=associated_token_account, is_signer=False, is_writable=True
@@ -239,18 +234,15 @@ class TokenBuyer(Trader):
)
buy_ix = Instruction(PumpAddresses.PROGRAM, data, accounts)
# Prepare buy transaction data
# recent_blockhash: Hash = await self.client.get_latest_blockhash()
# buy_message = Message([buy_ix], self.wallet.keypair.pubkey())
# buy_tx = Transaction([self.wallet.keypair], buy_message, recent_blockhash)
try:
return await self.client.build_and_send_transaction(
[buy_ix],
self.wallet.keypair,
skip_preflight=True,
max_retries=self.max_retries,
priority_fee=await self.priority_fee_manager.calculate_priority_fee(), # Get priority fee from manager
priority_fee=await self.priority_fee_manager.calculate_priority_fee(
self._get_relevant_accounts(token_info)
),
)
except Exception as e:
logger.error(f"Buy transaction failed: {str(e)}")
+19 -25
View File
@@ -65,13 +65,10 @@ class TokenSeller(Trader):
TradeResult with sell outcome
"""
try:
# Extract token info
mint = token_info.mint
bonding_curve = token_info.bonding_curve
associated_bonding_curve = token_info.associated_bonding_curve
# Get associated token account
associated_token_account = self.wallet.get_associated_token_address(mint)
associated_token_account = self.wallet.get_associated_token_address(
token_info.mint
)
# Get token balance
token_balance = await self.client.get_token_account_balance(
@@ -86,7 +83,9 @@ class TokenSeller(Trader):
return TradeResult(success=False, error_message="No tokens to sell")
# Fetch token price
curve_state = await self.curve_manager.get_curve_state(bonding_curve)
curve_state = await self.curve_manager.get_curve_state(
token_info.bonding_curve
)
token_price_sol = curve_state.calculate_price()
logger.info(f"Price per Token: {token_price_sol:.8f} SOL")
@@ -106,9 +105,7 @@ class TokenSeller(Trader):
)
tx_signature = await self._send_sell_transaction(
mint,
bonding_curve,
associated_bonding_curve,
token_info,
associated_token_account,
amount,
min_sol_output,
@@ -136,9 +133,7 @@ class TokenSeller(Trader):
async def _send_sell_transaction(
self,
mint: Pubkey,
bonding_curve: Pubkey,
associated_bonding_curve: Pubkey,
token_info: TokenInfo,
associated_token_account: Pubkey,
token_amount: int,
min_sol_output: int,
@@ -146,9 +141,7 @@ class TokenSeller(Trader):
"""Send sell transaction.
Args:
mint: Token mint
bonding_curve: Bonding curve address
associated_bonding_curve: Associated bonding curve address
mint: Token information
associated_token_account: User's token account
token_amount: Amount of tokens to sell in raw units
min_sol_output: Minimum SOL to receive in lamports
@@ -165,10 +158,14 @@ class TokenSeller(Trader):
pubkey=PumpAddresses.GLOBAL, is_signer=False, is_writable=False
),
AccountMeta(pubkey=PumpAddresses.FEE, is_signer=False, is_writable=True),
AccountMeta(pubkey=mint, is_signer=False, is_writable=False),
AccountMeta(pubkey=bonding_curve, is_signer=False, is_writable=True),
AccountMeta(pubkey=token_info.mint, is_signer=False, is_writable=False),
AccountMeta(
pubkey=associated_bonding_curve, is_signer=False, is_writable=True
pubkey=token_info.bonding_curve, is_signer=False, is_writable=True
),
AccountMeta(
pubkey=token_info.associated_bonding_curve,
is_signer=False,
is_writable=True,
),
AccountMeta(
pubkey=associated_token_account, is_signer=False, is_writable=True
@@ -201,18 +198,15 @@ class TokenSeller(Trader):
)
sell_ix = Instruction(PumpAddresses.PROGRAM, data, accounts)
# Prepare sell transaction data
# recent_blockhash: Hash = await self.client.get_latest_blockhash()
# sell_message = Message([sell_ix], self.wallet.keypair.pubkey())
# sell_tx = Transaction([self.wallet.keypair], sell_message, recent_blockhash)
try:
return await self.client.build_and_send_transaction(
[sell_ix],
self.wallet.keypair,
skip_preflight=True,
max_retries=self.max_retries,
priority_fee=await self.priority_fee_manager.calculate_priority_fee(), # Get priority fee from manager
priority_fee=await self.priority_fee_manager.calculate_priority_fee(
self._get_relevant_accounts(token_info)
),
)
except Exception as e:
logger.error(f"Sell transaction failed: {str(e)}")