2025-03-05 16:44:55 +00:00
|
|
|
"""
|
|
|
|
|
Main trading coordinator for pump.fun tokens.
|
2025-03-06 16:14:17 +00:00
|
|
|
Refactored PumpTrader to only process fresh tokens from WebSocket.
|
2025-03-05 16:44:55 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
from datetime import datetime
|
2025-04-25 09:38:19 +00:00
|
|
|
from time import monotonic
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-29 11:56:55 +00:00
|
|
|
import uvloop
|
2025-03-27 21:11:48 +00:00
|
|
|
from solders.pubkey import Pubkey
|
|
|
|
|
|
|
|
|
|
from cleanup.modes import (
|
|
|
|
|
handle_cleanup_after_failure,
|
|
|
|
|
handle_cleanup_after_sell,
|
|
|
|
|
handle_cleanup_post_session,
|
|
|
|
|
)
|
2025-03-17 15:47:28 +00:00
|
|
|
from core.client import SolanaClient
|
|
|
|
|
from core.curve import BondingCurveManager
|
2025-03-12 22:11:31 +00:00
|
|
|
from core.priority_fee.manager import PriorityFeeManager
|
2025-03-17 15:47:28 +00:00
|
|
|
from core.pubkeys import PumpAddresses
|
|
|
|
|
from core.wallet import Wallet
|
2025-03-18 16:03:46 +00:00
|
|
|
from monitoring.block_listener import BlockListener
|
2025-04-14 20:12:10 +00:00
|
|
|
from monitoring.geyser_listener import GeyserListener
|
2025-03-18 16:03:46 +00:00
|
|
|
from monitoring.logs_listener import LogsListener
|
2025-06-15 11:48:31 +00:00
|
|
|
from monitoring.pumpportal_listener import PumpPortalListener
|
2025-03-17 15:47:28 +00:00
|
|
|
from trading.base import TokenInfo, TradeResult
|
|
|
|
|
from trading.buyer import TokenBuyer
|
2025-06-15 18:20:13 +00:00
|
|
|
from trading.position import Position
|
2025-03-17 15:47:28 +00:00
|
|
|
from trading.seller import TokenSeller
|
|
|
|
|
from utils.logger import get_logger
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-29 11:56:55 +00:00
|
|
|
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
|
|
|
|
|
2025-03-05 16:44:55 +00:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PumpTrader:
|
2025-03-06 16:14:17 +00:00
|
|
|
"""Coordinates trading operations for pump.fun tokens with focus on freshness."""
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-03-05 16:44:55 +00:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
rpc_endpoint: str,
|
|
|
|
|
wss_endpoint: str,
|
|
|
|
|
private_key: str,
|
|
|
|
|
buy_amount: float,
|
|
|
|
|
buy_slippage: float,
|
|
|
|
|
sell_slippage: float,
|
2025-04-14 20:12:10 +00:00
|
|
|
listener_type: str = "logs",
|
|
|
|
|
geyser_endpoint: str | None = None,
|
|
|
|
|
geyser_api_token: str | None = None,
|
2025-05-06 08:39:09 +00:00
|
|
|
geyser_auth_type: str = "x-token",
|
2025-06-15 11:48:31 +00:00
|
|
|
pumpportal_url: str = "wss://pumpportal.fun/api/data",
|
2025-04-24 20:32:47 +00:00
|
|
|
extreme_fast_mode: bool = False,
|
|
|
|
|
extreme_fast_token_amount: int = 30,
|
2025-06-15 18:20:13 +00:00
|
|
|
# Exit strategy configuration
|
|
|
|
|
exit_strategy: str = "time_based",
|
|
|
|
|
take_profit_percentage: float | None = None,
|
|
|
|
|
stop_loss_percentage: float | None = None,
|
|
|
|
|
max_hold_time: int | None = None,
|
|
|
|
|
price_check_interval: int = 10,
|
2025-04-24 20:32:47 +00:00
|
|
|
# Priority fee configuration
|
|
|
|
|
enable_dynamic_priority_fee: bool = False,
|
|
|
|
|
enable_fixed_priority_fee: bool = True,
|
|
|
|
|
fixed_priority_fee: int = 200_000,
|
|
|
|
|
extra_priority_fee: float = 0.0,
|
|
|
|
|
hard_cap_prior_fee: int = 200_000,
|
|
|
|
|
# Retry and timeout settings
|
|
|
|
|
max_retries: int = 3,
|
2025-07-18 12:03:00 +00:00
|
|
|
wait_time_after_creation: int = 15, # here and further - seconds
|
2025-04-24 20:32:47 +00:00
|
|
|
wait_time_after_buy: int = 15,
|
|
|
|
|
wait_time_before_new_token: int = 15,
|
|
|
|
|
max_token_age: int | float = 0.001,
|
|
|
|
|
token_wait_timeout: int = 30,
|
|
|
|
|
# Cleanup settings
|
|
|
|
|
cleanup_mode: str = "disabled",
|
|
|
|
|
cleanup_force_close_with_burn: bool = False,
|
|
|
|
|
cleanup_with_priority_fee: bool = False,
|
|
|
|
|
# Trading filters
|
|
|
|
|
match_string: str | None = None,
|
|
|
|
|
bro_address: str | None = None,
|
|
|
|
|
marry_mode: bool = False,
|
|
|
|
|
yolo_mode: bool = False,
|
2025-03-05 16:44:55 +00:00
|
|
|
):
|
|
|
|
|
"""Initialize the pump trader.
|
|
|
|
|
Args:
|
|
|
|
|
rpc_endpoint: RPC endpoint URL
|
|
|
|
|
wss_endpoint: WebSocket endpoint URL
|
|
|
|
|
private_key: Wallet private key
|
|
|
|
|
buy_amount: Amount of SOL to spend on buys
|
|
|
|
|
buy_slippage: Slippage tolerance for buys
|
|
|
|
|
sell_slippage: Slippage tolerance for sells
|
2025-04-24 20:32:47 +00:00
|
|
|
|
2025-06-15 11:48:31 +00:00
|
|
|
listener_type: Type of listener to use ('logs', 'blocks', 'geyser', or 'pumpportal')
|
2025-04-14 20:12:10 +00:00
|
|
|
geyser_endpoint: Geyser endpoint URL (required for geyser listener)
|
|
|
|
|
geyser_api_token: Geyser API token (required for geyser listener)
|
2025-05-06 08:39:09 +00:00
|
|
|
geyser_auth_type: Geyser authentication type ('x-token' or 'basic')
|
2025-06-15 11:48:31 +00:00
|
|
|
pumpportal_url: PumpPortal WebSocket URL (default: wss://pumpportal.fun/api/data)
|
2025-04-24 20:32:47 +00:00
|
|
|
|
|
|
|
|
extreme_fast_mode: Whether to enable extreme fast mode
|
|
|
|
|
extreme_fast_token_amount: Maximum token amount for extreme fast mode
|
|
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
exit_strategy: Exit strategy ("time_based", "tp_sl", or "manual")
|
|
|
|
|
take_profit_percentage: Take profit percentage (0.5 = 50% profit)
|
|
|
|
|
stop_loss_percentage: Stop loss percentage (0.2 = 20% loss)
|
|
|
|
|
max_hold_time: Maximum hold time in seconds
|
|
|
|
|
price_check_interval: How often to check price for TP/SL (seconds)
|
|
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
enable_dynamic_priority_fee: Whether to enable dynamic priority fees
|
|
|
|
|
enable_fixed_priority_fee: Whether to enable fixed priority fees
|
|
|
|
|
fixed_priority_fee: Fixed priority fee amount
|
|
|
|
|
extra_priority_fee: Extra percentage for priority fees
|
|
|
|
|
hard_cap_prior_fee: Hard cap for priority fees
|
|
|
|
|
|
|
|
|
|
max_retries: Maximum number of retry attempts
|
|
|
|
|
wait_time_after_creation: Time to wait after token creation (seconds)
|
|
|
|
|
wait_time_after_buy: Time to wait after buying a token (seconds)
|
|
|
|
|
wait_time_before_new_token: Time to wait before processing a new token (seconds)
|
|
|
|
|
max_token_age: Maximum age of token to process (seconds)
|
|
|
|
|
token_wait_timeout: Timeout for waiting for a token in single-token mode (seconds)
|
|
|
|
|
|
|
|
|
|
cleanup_mode: Cleanup mode ("disabled", "auto", or "manual")
|
|
|
|
|
cleanup_force_close_with_burn: Whether to force close with burn during cleanup
|
|
|
|
|
cleanup_with_priority_fee: Whether to use priority fees during cleanup
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
match_string: Optional string to match in token name/symbol
|
|
|
|
|
bro_address: Optional creator address to filter by
|
|
|
|
|
marry_mode: If True, only buy tokens and skip selling
|
|
|
|
|
yolo_mode: If True, trade continuously
|
2025-03-05 16:44:55 +00:00
|
|
|
"""
|
|
|
|
|
self.solana_client = SolanaClient(rpc_endpoint)
|
|
|
|
|
self.wallet = Wallet(private_key)
|
|
|
|
|
self.curve_manager = BondingCurveManager(self.solana_client)
|
2025-03-12 22:11:31 +00:00
|
|
|
self.priority_fee_manager = PriorityFeeManager(
|
|
|
|
|
client=self.solana_client,
|
2025-04-24 20:32:47 +00:00
|
|
|
enable_dynamic_fee=enable_dynamic_priority_fee,
|
|
|
|
|
enable_fixed_fee=enable_fixed_priority_fee,
|
|
|
|
|
fixed_fee=fixed_priority_fee,
|
|
|
|
|
extra_fee=extra_priority_fee,
|
|
|
|
|
hard_cap=hard_cap_prior_fee,
|
2025-03-12 22:11:31 +00:00
|
|
|
)
|
2025-03-05 16:44:55 +00:00
|
|
|
self.buyer = TokenBuyer(
|
|
|
|
|
self.solana_client,
|
|
|
|
|
self.wallet,
|
|
|
|
|
self.curve_manager,
|
2025-03-12 22:11:31 +00:00
|
|
|
self.priority_fee_manager,
|
2025-03-05 16:44:55 +00:00
|
|
|
buy_amount,
|
|
|
|
|
buy_slippage,
|
|
|
|
|
max_retries,
|
2025-04-24 20:32:47 +00:00
|
|
|
extreme_fast_token_amount,
|
2025-07-18 12:03:00 +00:00
|
|
|
extreme_fast_mode,
|
2025-03-05 16:44:55 +00:00
|
|
|
)
|
|
|
|
|
self.seller = TokenSeller(
|
|
|
|
|
self.solana_client,
|
|
|
|
|
self.wallet,
|
|
|
|
|
self.curve_manager,
|
2025-03-12 22:11:31 +00:00
|
|
|
self.priority_fee_manager,
|
2025-03-05 16:44:55 +00:00
|
|
|
sell_slippage,
|
|
|
|
|
max_retries,
|
|
|
|
|
)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-03-18 16:03:46 +00:00
|
|
|
# Initialize the appropriate listener type
|
2025-04-14 20:12:10 +00:00
|
|
|
listener_type = listener_type.lower()
|
|
|
|
|
if listener_type == "geyser":
|
|
|
|
|
if not geyser_endpoint or not geyser_api_token:
|
2025-07-18 12:03:00 +00:00
|
|
|
raise ValueError(
|
|
|
|
|
"Geyser endpoint and API token are required for geyser listener"
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-14 20:12:10 +00:00
|
|
|
self.token_listener = GeyserListener(
|
2025-07-18 12:03:00 +00:00
|
|
|
geyser_endpoint,
|
2025-05-06 08:39:09 +00:00
|
|
|
geyser_api_token,
|
2025-07-18 12:03:00 +00:00
|
|
|
geyser_auth_type,
|
|
|
|
|
PumpAddresses.PROGRAM,
|
2025-04-14 20:12:10 +00:00
|
|
|
)
|
|
|
|
|
logger.info("Using Geyser listener for token monitoring")
|
|
|
|
|
elif listener_type == "logs":
|
2025-03-18 16:03:46 +00:00
|
|
|
self.token_listener = LogsListener(wss_endpoint, PumpAddresses.PROGRAM)
|
|
|
|
|
logger.info("Using logsSubscribe listener for token monitoring")
|
2025-06-15 11:48:31 +00:00
|
|
|
elif listener_type == "pumpportal":
|
2025-07-18 12:03:00 +00:00
|
|
|
self.token_listener = PumpPortalListener(
|
|
|
|
|
PumpAddresses.PROGRAM, pumpportal_url
|
|
|
|
|
)
|
2025-06-15 11:48:31 +00:00
|
|
|
logger.info("Using PumpPortal listener for token monitoring")
|
2025-03-18 16:03:46 +00:00
|
|
|
else:
|
|
|
|
|
self.token_listener = BlockListener(wss_endpoint, PumpAddresses.PROGRAM)
|
|
|
|
|
logger.info("Using blockSubscribe listener for token monitoring")
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# Trading parameters
|
2025-03-05 16:44:55 +00:00
|
|
|
self.buy_amount = buy_amount
|
|
|
|
|
self.buy_slippage = buy_slippage
|
|
|
|
|
self.sell_slippage = sell_slippage
|
|
|
|
|
self.max_retries = max_retries
|
2025-04-24 20:32:47 +00:00
|
|
|
self.extreme_fast_mode = extreme_fast_mode
|
|
|
|
|
self.extreme_fast_token_amount = extreme_fast_token_amount
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
# Exit strategy parameters
|
|
|
|
|
self.exit_strategy = exit_strategy.lower()
|
|
|
|
|
self.take_profit_percentage = take_profit_percentage
|
|
|
|
|
self.stop_loss_percentage = stop_loss_percentage
|
|
|
|
|
self.max_hold_time = max_hold_time
|
|
|
|
|
self.price_check_interval = price_check_interval
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# Timing parameters
|
|
|
|
|
self.wait_time_after_creation = wait_time_after_creation
|
|
|
|
|
self.wait_time_after_buy = wait_time_after_buy
|
|
|
|
|
self.wait_time_before_new_token = wait_time_before_new_token
|
|
|
|
|
self.max_token_age = max_token_age
|
|
|
|
|
self.token_wait_timeout = token_wait_timeout
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# Cleanup parameters
|
|
|
|
|
self.cleanup_mode = cleanup_mode
|
|
|
|
|
self.cleanup_force_close_with_burn = cleanup_force_close_with_burn
|
|
|
|
|
self.cleanup_with_priority_fee = cleanup_with_priority_fee
|
2025-03-06 16:14:17 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# Trading filters/modes
|
|
|
|
|
self.match_string = match_string
|
|
|
|
|
self.bro_address = bro_address
|
|
|
|
|
self.marry_mode = marry_mode
|
|
|
|
|
self.yolo_mode = yolo_mode
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# State tracking
|
2025-03-27 21:11:48 +00:00
|
|
|
self.traded_mints: set[Pubkey] = set()
|
2025-04-24 20:32:47 +00:00
|
|
|
self.token_queue: asyncio.Queue = asyncio.Queue()
|
|
|
|
|
self.processing: bool = False
|
2025-03-06 21:52:43 +00:00
|
|
|
self.processed_tokens: set[str] = set()
|
|
|
|
|
self.token_timestamps: dict[str, float] = {}
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
async def start(self) -> None:
|
|
|
|
|
"""Start the trading bot and listen for new tokens."""
|
2025-03-05 16:44:55 +00:00
|
|
|
logger.info("Starting pump.fun trader")
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
f"Match filter: {self.match_string if self.match_string else 'None'}"
|
|
|
|
|
)
|
|
|
|
|
logger.info(
|
|
|
|
|
f"Creator filter: {self.bro_address if self.bro_address else 'None'}"
|
|
|
|
|
)
|
2025-04-24 20:32:47 +00:00
|
|
|
logger.info(f"Marry mode: {self.marry_mode}")
|
|
|
|
|
logger.info(f"YOLO mode: {self.yolo_mode}")
|
2025-06-15 18:20:13 +00:00
|
|
|
logger.info(f"Exit strategy: {self.exit_strategy}")
|
|
|
|
|
if self.exit_strategy == "tp_sl":
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
f"Take profit: {self.take_profit_percentage * 100 if self.take_profit_percentage else 'None'}%"
|
|
|
|
|
)
|
|
|
|
|
logger.info(
|
|
|
|
|
f"Stop loss: {self.stop_loss_percentage * 100 if self.stop_loss_percentage else 'None'}%"
|
|
|
|
|
)
|
|
|
|
|
logger.info(
|
|
|
|
|
f"Max hold time: {self.max_hold_time if self.max_hold_time else 'None'} seconds"
|
|
|
|
|
)
|
2025-03-06 16:14:17 +00:00
|
|
|
logger.info(f"Max token age: {self.max_token_age} seconds")
|
|
|
|
|
|
2025-04-25 09:38:19 +00:00
|
|
|
try:
|
|
|
|
|
health_resp = await self.solana_client.get_health()
|
|
|
|
|
logger.info(f"RPC warm-up successful (getHealth passed: {health_resp})")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning(f"RPC warm-up failed: {e!s}")
|
|
|
|
|
|
2025-03-05 16:44:55 +00:00
|
|
|
try:
|
2025-04-24 20:32:47 +00:00
|
|
|
# Choose operating mode based on yolo_mode
|
|
|
|
|
if not self.yolo_mode:
|
|
|
|
|
# Single token mode: process one token and exit
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
"Running in single token mode - will process one token and exit"
|
|
|
|
|
)
|
2025-04-24 20:32:47 +00:00
|
|
|
token_info = await self._wait_for_token()
|
|
|
|
|
if token_info:
|
|
|
|
|
await self._handle_token(token_info)
|
|
|
|
|
logger.info("Finished processing single token. Exiting...")
|
|
|
|
|
else:
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
f"No suitable token found within timeout period ({self.token_wait_timeout}s). Exiting..."
|
|
|
|
|
)
|
2025-04-24 20:32:47 +00:00
|
|
|
else:
|
|
|
|
|
# Continuous mode: process tokens until interrupted
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
"Running in continuous mode - will process tokens until interrupted"
|
2025-04-24 20:32:47 +00:00
|
|
|
)
|
2025-07-18 12:03:00 +00:00
|
|
|
processor_task = asyncio.create_task(self._process_token_queue())
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
try:
|
|
|
|
|
await self.token_listener.listen_for_tokens(
|
|
|
|
|
lambda token: self._queue_token(token),
|
|
|
|
|
self.match_string,
|
|
|
|
|
self.bro_address,
|
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Token listening stopped due to error: {e!s}")
|
|
|
|
|
finally:
|
|
|
|
|
processor_task.cancel()
|
|
|
|
|
try:
|
|
|
|
|
await processor_task
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
pass
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-03-05 16:44:55 +00:00
|
|
|
except Exception as e:
|
2025-03-27 15:19:52 +00:00
|
|
|
logger.error(f"Trading stopped due to error: {e!s}")
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-03-27 21:11:48 +00:00
|
|
|
finally:
|
2025-04-24 20:32:47 +00:00
|
|
|
await self._cleanup_resources()
|
|
|
|
|
logger.info("Pump trader has shut down")
|
2025-03-27 21:11:48 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
async def _wait_for_token(self) -> TokenInfo | None:
|
|
|
|
|
"""Wait for a single token to be detected.
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
Returns:
|
|
|
|
|
TokenInfo or None if timeout occurs
|
|
|
|
|
"""
|
|
|
|
|
# Create a one-time event to signal when a token is found
|
|
|
|
|
token_found = asyncio.Event()
|
|
|
|
|
found_token = None
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
async def token_callback(token: TokenInfo) -> None:
|
|
|
|
|
nonlocal found_token
|
|
|
|
|
token_key = str(token.mint)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# Only process if not already processed and fresh
|
|
|
|
|
if token_key not in self.processed_tokens:
|
|
|
|
|
# Record when the token was discovered
|
2025-04-25 09:38:19 +00:00
|
|
|
self.token_timestamps[token_key] = monotonic()
|
2025-04-24 20:32:47 +00:00
|
|
|
found_token = token
|
|
|
|
|
self.processed_tokens.add(token_key)
|
|
|
|
|
token_found.set()
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
listener_task = asyncio.create_task(
|
|
|
|
|
self.token_listener.listen_for_tokens(
|
|
|
|
|
token_callback,
|
|
|
|
|
self.match_string,
|
|
|
|
|
self.bro_address,
|
|
|
|
|
)
|
|
|
|
|
)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# Wait for a token with a timeout
|
|
|
|
|
try:
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
f"Waiting for a suitable token (timeout: {self.token_wait_timeout}s)..."
|
|
|
|
|
)
|
2025-04-24 20:32:47 +00:00
|
|
|
await asyncio.wait_for(token_found.wait(), timeout=self.token_wait_timeout)
|
|
|
|
|
logger.info(f"Found token: {found_token.symbol} ({found_token.mint})")
|
|
|
|
|
return found_token
|
|
|
|
|
except TimeoutError:
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
f"Timed out after waiting {self.token_wait_timeout}s for a token"
|
|
|
|
|
)
|
2025-04-24 20:32:47 +00:00
|
|
|
return None
|
|
|
|
|
finally:
|
|
|
|
|
listener_task.cancel()
|
|
|
|
|
try:
|
|
|
|
|
await listener_task
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def _cleanup_resources(self) -> None:
|
|
|
|
|
"""Perform cleanup operations before shutting down."""
|
|
|
|
|
if self.traded_mints:
|
|
|
|
|
try:
|
|
|
|
|
logger.info(f"Cleaning up {len(self.traded_mints)} traded token(s)...")
|
|
|
|
|
await handle_cleanup_post_session(
|
2025-07-18 12:03:00 +00:00
|
|
|
self.solana_client,
|
|
|
|
|
self.wallet,
|
|
|
|
|
list(self.traded_mints),
|
2025-04-24 20:32:47 +00:00
|
|
|
self.priority_fee_manager,
|
|
|
|
|
self.cleanup_mode,
|
|
|
|
|
self.cleanup_with_priority_fee,
|
2025-07-18 12:03:00 +00:00
|
|
|
self.cleanup_force_close_with_burn,
|
2025-04-24 20:32:47 +00:00
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error during cleanup: {e!s}")
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
old_keys = {k for k in self.token_timestamps if k not in self.processed_tokens}
|
|
|
|
|
for key in old_keys:
|
|
|
|
|
self.token_timestamps.pop(key, None)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
await self.solana_client.close()
|
|
|
|
|
|
2025-07-18 12:03:00 +00:00
|
|
|
async def _queue_token(self, token_info: TokenInfo) -> None:
|
2025-04-24 20:32:47 +00:00
|
|
|
"""Queue a token for processing if not already processed.
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
Args:
|
|
|
|
|
token_info: Token information to queue
|
|
|
|
|
"""
|
2025-03-06 16:14:17 +00:00
|
|
|
token_key = str(token_info.mint)
|
|
|
|
|
|
|
|
|
|
if token_key in self.processed_tokens:
|
|
|
|
|
logger.debug(f"Token {token_info.symbol} already processed. Skipping...")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Record timestamp when token was discovered
|
2025-04-25 09:38:19 +00:00
|
|
|
self.token_timestamps[token_key] = monotonic()
|
2025-03-06 16:14:17 +00:00
|
|
|
|
|
|
|
|
await self.token_queue.put(token_info)
|
2025-03-27 15:19:52 +00:00
|
|
|
logger.info(f"Queued new token: {token_info.symbol} ({token_info.mint})")
|
2025-03-06 16:14:17 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
async def _process_token_queue(self) -> None:
|
2025-03-06 16:14:17 +00:00
|
|
|
"""Continuously process tokens from the queue, only if they're fresh."""
|
|
|
|
|
while True:
|
2025-04-24 20:32:47 +00:00
|
|
|
try:
|
|
|
|
|
token_info = await self.token_queue.get()
|
|
|
|
|
token_key = str(token_info.mint)
|
2025-03-06 16:14:17 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# Check if token is still "fresh"
|
2025-04-25 09:38:19 +00:00
|
|
|
current_time = monotonic()
|
2025-04-24 20:32:47 +00:00
|
|
|
token_age = current_time - self.token_timestamps.get(
|
|
|
|
|
token_key, current_time
|
2025-03-06 16:14:17 +00:00
|
|
|
)
|
2025-04-24 20:32:47 +00:00
|
|
|
|
|
|
|
|
if token_age > self.max_token_age:
|
|
|
|
|
logger.info(
|
|
|
|
|
f"Skipping token {token_info.symbol} - too old ({token_age:.1f}s > {self.max_token_age}s)"
|
|
|
|
|
)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
self.processed_tokens.add(token_key)
|
|
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
|
f"Processing fresh token: {token_info.symbol} (age: {token_age:.1f}s)"
|
|
|
|
|
)
|
|
|
|
|
await self._handle_token(token_info)
|
|
|
|
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
# Handle cancellation gracefully
|
|
|
|
|
logger.info("Token queue processor was cancelled")
|
|
|
|
|
break
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error in token queue processor: {e!s}")
|
|
|
|
|
finally:
|
2025-03-06 16:14:17 +00:00
|
|
|
self.token_queue.task_done()
|
|
|
|
|
|
2025-07-18 12:03:00 +00:00
|
|
|
async def _handle_token(self, token_info: TokenInfo) -> None:
|
2025-03-05 16:44:55 +00:00
|
|
|
"""Handle a new token creation event.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
token_info: Token information
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2025-04-24 20:32:47 +00:00
|
|
|
# Wait for bonding curve to stabilize (unless in extreme fast mode)
|
|
|
|
|
if not self.extreme_fast_mode:
|
2025-04-25 09:38:19 +00:00
|
|
|
# Save token info to file
|
|
|
|
|
# await self._save_token_info(token_info)
|
2025-04-03 21:57:36 +00:00
|
|
|
logger.info(
|
2025-04-24 20:32:47 +00:00
|
|
|
f"Waiting for {self.wait_time_after_creation} seconds for the bonding curve to stabilize..."
|
2025-04-03 21:57:36 +00:00
|
|
|
)
|
2025-04-24 20:32:47 +00:00
|
|
|
await asyncio.sleep(self.wait_time_after_creation)
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# Buy token
|
2025-03-05 16:44:55 +00:00
|
|
|
logger.info(
|
|
|
|
|
f"Buying {self.buy_amount:.6f} SOL worth of {token_info.symbol}..."
|
|
|
|
|
)
|
2025-03-06 13:52:40 +00:00
|
|
|
buy_result: TradeResult = await self.buyer.execute(token_info)
|
2025-03-05 16:44:55 +00:00
|
|
|
|
|
|
|
|
if buy_result.success:
|
2025-04-24 20:32:47 +00:00
|
|
|
await self._handle_successful_buy(token_info, buy_result)
|
2025-03-05 16:44:55 +00:00
|
|
|
else:
|
2025-04-24 20:32:47 +00:00
|
|
|
await self._handle_failed_buy(token_info, buy_result)
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
# Only wait for next token in yolo mode
|
|
|
|
|
if self.yolo_mode:
|
2025-03-06 13:52:40 +00:00
|
|
|
logger.info(
|
2025-04-24 20:32:47 +00:00
|
|
|
f"YOLO mode enabled. Waiting {self.wait_time_before_new_token} seconds before looking for next token..."
|
2025-03-06 13:52:40 +00:00
|
|
|
)
|
2025-04-24 20:32:47 +00:00
|
|
|
await asyncio.sleep(self.wait_time_before_new_token)
|
2025-03-06 16:14:17 +00:00
|
|
|
|
2025-03-05 16:44:55 +00:00
|
|
|
except Exception as e:
|
2025-03-27 15:19:52 +00:00
|
|
|
logger.error(f"Error handling token {token_info.symbol}: {e!s}")
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
async def _handle_successful_buy(
|
|
|
|
|
self, token_info: TokenInfo, buy_result: TradeResult
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Handle successful token purchase.
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
Args:
|
|
|
|
|
token_info: Token information
|
|
|
|
|
buy_result: The result of the buy operation
|
|
|
|
|
"""
|
|
|
|
|
logger.info(f"Successfully bought {token_info.symbol}")
|
|
|
|
|
self._log_trade(
|
|
|
|
|
"buy",
|
|
|
|
|
token_info,
|
|
|
|
|
buy_result.price, # type: ignore
|
|
|
|
|
buy_result.amount, # type: ignore
|
|
|
|
|
buy_result.tx_signature,
|
|
|
|
|
)
|
|
|
|
|
self.traded_mints.add(token_info.mint)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
# Choose exit strategy
|
2025-04-24 20:32:47 +00:00
|
|
|
if not self.marry_mode:
|
2025-06-15 18:20:13 +00:00
|
|
|
if self.exit_strategy == "tp_sl":
|
|
|
|
|
await self._handle_tp_sl_exit(token_info, buy_result)
|
|
|
|
|
elif self.exit_strategy == "time_based":
|
|
|
|
|
await self._handle_time_based_exit(token_info)
|
|
|
|
|
elif self.exit_strategy == "manual":
|
|
|
|
|
logger.info("Manual exit strategy - position will remain open")
|
2025-04-24 20:32:47 +00:00
|
|
|
else:
|
|
|
|
|
logger.info("Marry mode enabled. Skipping sell operation.")
|
|
|
|
|
|
|
|
|
|
async def _handle_failed_buy(
|
|
|
|
|
self, token_info: TokenInfo, buy_result: TradeResult
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Handle failed token purchase.
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
Args:
|
|
|
|
|
token_info: Token information
|
|
|
|
|
buy_result: The result of the buy operation
|
|
|
|
|
"""
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.error(f"Failed to buy {token_info.symbol}: {buy_result.error_message}")
|
2025-04-24 20:32:47 +00:00
|
|
|
# Close ATA if enabled
|
|
|
|
|
await handle_cleanup_after_failure(
|
2025-07-18 12:03:00 +00:00
|
|
|
self.solana_client,
|
|
|
|
|
self.wallet,
|
|
|
|
|
token_info.mint,
|
2025-04-24 20:32:47 +00:00
|
|
|
self.priority_fee_manager,
|
|
|
|
|
self.cleanup_mode,
|
|
|
|
|
self.cleanup_with_priority_fee,
|
2025-07-18 12:03:00 +00:00
|
|
|
self.cleanup_force_close_with_burn,
|
2025-04-24 20:32:47 +00:00
|
|
|
)
|
|
|
|
|
|
2025-07-18 12:03:00 +00:00
|
|
|
async def _handle_tp_sl_exit(
|
|
|
|
|
self, token_info: TokenInfo, buy_result: TradeResult
|
|
|
|
|
) -> None:
|
2025-06-15 18:20:13 +00:00
|
|
|
"""Handle take profit/stop loss exit strategy.
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
Args:
|
|
|
|
|
token_info: Token information
|
|
|
|
|
buy_result: Result from the buy operation
|
|
|
|
|
"""
|
|
|
|
|
# Create position
|
|
|
|
|
position = Position.create_from_buy_result(
|
|
|
|
|
mint=token_info.mint,
|
|
|
|
|
symbol=token_info.symbol,
|
|
|
|
|
entry_price=buy_result.price, # type: ignore
|
2025-07-18 12:03:00 +00:00
|
|
|
quantity=buy_result.amount, # type: ignore
|
2025-06-15 18:20:13 +00:00
|
|
|
take_profit_percentage=self.take_profit_percentage,
|
|
|
|
|
stop_loss_percentage=self.stop_loss_percentage,
|
|
|
|
|
max_hold_time=self.max_hold_time,
|
|
|
|
|
)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
logger.info(f"Created position: {position}")
|
|
|
|
|
if position.take_profit_price:
|
|
|
|
|
logger.info(f"Take profit target: {position.take_profit_price:.8f} SOL")
|
|
|
|
|
if position.stop_loss_price:
|
|
|
|
|
logger.info(f"Stop loss target: {position.stop_loss_price:.8f} SOL")
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
# Monitor position until exit condition is met
|
|
|
|
|
await self._monitor_position_until_exit(token_info, position)
|
|
|
|
|
|
|
|
|
|
async def _handle_time_based_exit(self, token_info: TokenInfo) -> None:
|
|
|
|
|
"""Handle legacy time-based exit strategy.
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
Args:
|
|
|
|
|
token_info: Token information
|
|
|
|
|
"""
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(f"Waiting for {self.wait_time_after_buy} seconds before selling...")
|
2025-06-15 18:20:13 +00:00
|
|
|
await asyncio.sleep(self.wait_time_after_buy)
|
|
|
|
|
|
|
|
|
|
logger.info(f"Selling {token_info.symbol}...")
|
|
|
|
|
sell_result: TradeResult = await self.seller.execute(token_info)
|
|
|
|
|
|
|
|
|
|
if sell_result.success:
|
|
|
|
|
logger.info(f"Successfully sold {token_info.symbol}")
|
|
|
|
|
self._log_trade(
|
|
|
|
|
"sell",
|
|
|
|
|
token_info,
|
|
|
|
|
sell_result.price, # type: ignore
|
|
|
|
|
sell_result.amount, # type: ignore
|
|
|
|
|
sell_result.tx_signature,
|
|
|
|
|
)
|
|
|
|
|
# Close ATA if enabled
|
|
|
|
|
await handle_cleanup_after_sell(
|
2025-07-18 12:03:00 +00:00
|
|
|
self.solana_client,
|
|
|
|
|
self.wallet,
|
|
|
|
|
token_info.mint,
|
2025-06-15 18:20:13 +00:00
|
|
|
self.priority_fee_manager,
|
|
|
|
|
self.cleanup_mode,
|
|
|
|
|
self.cleanup_with_priority_fee,
|
2025-07-18 12:03:00 +00:00
|
|
|
self.cleanup_force_close_with_burn,
|
2025-06-15 18:20:13 +00:00
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
logger.error(
|
|
|
|
|
f"Failed to sell {token_info.symbol}: {sell_result.error_message}"
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-18 12:03:00 +00:00
|
|
|
async def _monitor_position_until_exit(
|
|
|
|
|
self, token_info: TokenInfo, position: Position
|
|
|
|
|
) -> None:
|
2025-06-15 18:20:13 +00:00
|
|
|
"""Monitor a position until exit conditions are met.
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
Args:
|
|
|
|
|
token_info: Token information
|
|
|
|
|
position: Position to monitor
|
|
|
|
|
"""
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
f"Starting position monitoring (check interval: {self.price_check_interval}s)"
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
while position.is_active:
|
|
|
|
|
try:
|
|
|
|
|
# Get current price from bonding curve
|
2025-07-18 12:03:00 +00:00
|
|
|
current_price = await self.curve_manager.calculate_price(
|
|
|
|
|
token_info.bonding_curve
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
# Check if position should be exited
|
|
|
|
|
should_exit, exit_reason = position.should_exit(current_price)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
if should_exit and exit_reason:
|
|
|
|
|
logger.info(f"Exit condition met: {exit_reason.value}")
|
|
|
|
|
logger.info(f"Current price: {current_price:.8f} SOL")
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
# Log PnL before exit
|
|
|
|
|
pnl = position.get_pnl(current_price)
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
f"Position PnL: {pnl['price_change_pct']:.2f}% ({pnl['unrealized_pnl_sol']:.6f} SOL)"
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
# Execute sell
|
|
|
|
|
sell_result = await self.seller.execute(token_info)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
if sell_result.success:
|
|
|
|
|
# Close position with actual exit price
|
|
|
|
|
position.close_position(sell_result.price, exit_reason) # type: ignore
|
2025-07-18 12:03:00 +00:00
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
|
f"Successfully exited position: {exit_reason.value}"
|
|
|
|
|
)
|
2025-06-15 18:20:13 +00:00
|
|
|
self._log_trade(
|
|
|
|
|
"sell",
|
|
|
|
|
token_info,
|
|
|
|
|
sell_result.price, # type: ignore
|
|
|
|
|
sell_result.amount, # type: ignore
|
|
|
|
|
sell_result.tx_signature,
|
|
|
|
|
)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
# Log final PnL
|
|
|
|
|
final_pnl = position.get_pnl()
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.info(
|
|
|
|
|
f"Final PnL: {final_pnl['price_change_pct']:.2f}% ({final_pnl['unrealized_pnl_sol']:.6f} SOL)"
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
# Close ATA if enabled
|
|
|
|
|
await handle_cleanup_after_sell(
|
2025-07-18 12:03:00 +00:00
|
|
|
self.solana_client,
|
|
|
|
|
self.wallet,
|
|
|
|
|
token_info.mint,
|
2025-06-15 18:20:13 +00:00
|
|
|
self.priority_fee_manager,
|
|
|
|
|
self.cleanup_mode,
|
|
|
|
|
self.cleanup_with_priority_fee,
|
2025-07-18 12:03:00 +00:00
|
|
|
self.cleanup_force_close_with_burn,
|
2025-06-15 18:20:13 +00:00
|
|
|
)
|
|
|
|
|
else:
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.error(
|
|
|
|
|
f"Failed to exit position: {sell_result.error_message}"
|
|
|
|
|
)
|
2025-06-15 18:20:13 +00:00
|
|
|
# Keep monitoring in case sell can be retried
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
# Log current status
|
|
|
|
|
pnl = position.get_pnl(current_price)
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.debug(
|
|
|
|
|
f"Position status: {current_price:.8f} SOL ({pnl['price_change_pct']:+.2f}%)"
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
# Wait before next price check
|
|
|
|
|
await asyncio.sleep(self.price_check_interval)
|
2025-07-18 12:03:00 +00:00
|
|
|
|
2025-06-15 18:20:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error monitoring position: {e}")
|
2025-07-18 12:03:00 +00:00
|
|
|
await asyncio.sleep(
|
|
|
|
|
self.price_check_interval
|
|
|
|
|
) # Continue monitoring despite errors
|
2025-06-15 18:20:13 +00:00
|
|
|
|
2025-07-18 12:03:00 +00:00
|
|
|
async def _save_token_info(self, token_info: TokenInfo) -> None:
|
2025-03-05 16:44:55 +00:00
|
|
|
"""Save token information to a file.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
token_info: Token information
|
|
|
|
|
"""
|
2025-04-24 20:32:47 +00:00
|
|
|
try:
|
|
|
|
|
os.makedirs("trades", exist_ok=True)
|
|
|
|
|
file_name = os.path.join("trades", f"{token_info.mint}.txt")
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
with open(file_name, "w") as file:
|
|
|
|
|
file.write(json.dumps(token_info.to_dict(), indent=2))
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
logger.info(f"Token information saved to {file_name}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to save token information: {e!s}")
|
2025-03-05 16:44:55 +00:00
|
|
|
|
|
|
|
|
def _log_trade(
|
2025-03-06 13:52:40 +00:00
|
|
|
self,
|
|
|
|
|
action: str,
|
|
|
|
|
token_info: TokenInfo,
|
|
|
|
|
price: float,
|
|
|
|
|
amount: float,
|
|
|
|
|
tx_hash: str | None,
|
2025-03-05 16:44:55 +00:00
|
|
|
) -> None:
|
|
|
|
|
"""Log trade information.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
action: Trade action (buy/sell)
|
|
|
|
|
token_info: Token information
|
|
|
|
|
price: Token price in SOL
|
2025-03-06 13:52:40 +00:00
|
|
|
amount: Trade amount in SOL
|
2025-03-05 16:44:55 +00:00
|
|
|
tx_hash: Transaction hash
|
|
|
|
|
"""
|
2025-04-24 20:32:47 +00:00
|
|
|
try:
|
|
|
|
|
os.makedirs("trades", exist_ok=True)
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
log_entry = {
|
|
|
|
|
"timestamp": datetime.utcnow().isoformat(),
|
|
|
|
|
"action": action,
|
|
|
|
|
"token_address": str(token_info.mint),
|
|
|
|
|
"symbol": token_info.symbol,
|
|
|
|
|
"price": price,
|
|
|
|
|
"amount": amount,
|
|
|
|
|
"tx_hash": str(tx_hash) if tx_hash else None,
|
|
|
|
|
}
|
2025-03-05 16:44:55 +00:00
|
|
|
|
2025-04-24 20:32:47 +00:00
|
|
|
with open("trades/trades.log", "a") as log_file:
|
|
|
|
|
log_file.write(json.dumps(log_entry) + "\n")
|
|
|
|
|
except Exception as e:
|
2025-07-18 12:03:00 +00:00
|
|
|
logger.error(f"Failed to log trade information: {e!s}")
|