better ws and mainnet subscriptions

This commit is contained in:
Dobromir Popov 2024-10-11 15:08:08 +03:00
parent b473d0921f
commit 91e3a18a4d

View File

@ -1045,7 +1045,8 @@ async def follow_move(move):
except Exception as e:
logging.error(f"Error sending notification: {e}")
for retry in range(2):
for retry in range(3):
try:
private_key = Keypair.from_bytes(base58.b58decode(pk))
async_client = AsyncClient(SOLANA_WS_URL)
jupiter = Jupiter(async_client, private_key)
@ -1074,7 +1075,16 @@ async def follow_move(move):
break
else:
logging.warning(f"Failed to get transaction details for {transaction_id}. Probably transaction failed. Retrying again...")
await asyncio.sleep(5)
await asyncio.sleep(3)
except Exception as e:
error_message = f"<b>Move Failed:</b>\n{str(e)}"
logging.error(error_message)
# log the errors to /logs/errors.log
error_logger.error(error_message)
error_logger.exception(e)
await send_telegram_message(error_message)
amount = amount * 0.75
await get_wallet_balances(YOUR_WALLET, doGetTokenName=False)
try:
@ -1106,7 +1116,11 @@ async def follow_move(move):
logging.error(error_message)
# log the errors to /logs/errors.log
error_logger.error(error_message)
error_logger.exception(e)
error_logger.exception(e) \
# if error_message contains 'Program log: Error: insufficient funds'
if 'insufficient funds' in error_message:
await send_telegram_message("Insufficient funds. Cannot follow move. Please check your balance.")
else:
await send_telegram_message(error_message)
@ -1118,7 +1132,7 @@ SOLANA_ENDPOINTS = [
# "wss://mainnet.rpcpool.com",
]
PING_INTERVAL = 30
SUBSCRIBE_INTERVAL = 180 # Resubscribe every 3 minutes
SUBSCRIBE_INTERVAL = 10*60 # Resubscribe every 10 minutes
async def heartbeat(websocket):
@ -1148,10 +1162,26 @@ async def subscribe_to_wallet():
while True:
subscription_id = await subscribe(websocket)
if subscription_id:
await process_messages(websocket, subscription_id)
await asyncio.sleep(SUBSCRIBE_INTERVAL)
process_task = asyncio.create_task(process_messages(websocket, subscription_id))
while True:
try:
await asyncio.wait_for(process_task, timeout=SUBSCRIBE_INTERVAL)
except asyncio.TimeoutError:
# Timeout occurred, time to resubscribe
process_task.cancel()
try:
await process_task
except asyncio.CancelledError:
pass
await unsubscribe(websocket, subscription_id)
subscription_id = await subscribe(websocket)
if subscription_id:
process_task = asyncio.create_task(process_messages(websocket, subscription_id))
else:
break
else:
# process_messages completed (shouldn't happen unless there's an error)
break
heartbeat_task.cancel()
@ -1184,7 +1214,7 @@ async def subscribe(websocket):
if 'result' in response_data:
subscription_id = response_data['result']
logger.info(f"Subscription successful. Subscription id: {subscription_id}")
await send_telegram_message("Connected to Solana network. Watching for transactions now.")
await send_telegram_message("Solana mainnet connected... Listening for transactions.")
return subscription_id
else:
logger.warning(f"Unexpected response: {response_data}")