compiling after refactoring
This commit is contained in:
@ -13,8 +13,14 @@ import datetime
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
SOLANA_ENDPOINTS = ["your_endpoint_1", "your_endpoint_2"] # Add your endpoints here
|
||||
SUBSCRIBE_INTERVAL = 300 # 5 minutes in seconds
|
||||
SOLANA_ENDPOINTS = [
|
||||
"wss://api.mainnet-beta.solana.com",
|
||||
# "wss://solana-api.projectserum.com",
|
||||
# "wss://rpc.ankr.com/solana",
|
||||
# "wss://mainnet.rpcpool.com",
|
||||
]
|
||||
PING_INTERVAL = 30
|
||||
SUBSCRIBE_INTERVAL = 1*60 # Resubscribe every 10 minutes
|
||||
|
||||
from config import (
|
||||
FOLLOWED_WALLET, SOLANA_HTTP_URL
|
||||
@ -23,9 +29,9 @@ FOLLOWED_WALLET, SOLANA_HTTP_URL
|
||||
|
||||
class SolanaAPI:
|
||||
def __init__(self):
|
||||
self.websocket: Optional[websockets.WebSocketClientProtocol] = None
|
||||
self.subscription_id: Optional[int] = None
|
||||
self.message_queue: asyncio.Queue = asyncio.Queue()
|
||||
self.websocket = None
|
||||
self.subscription_id = None
|
||||
self.message_queue = asyncio.Queue()
|
||||
|
||||
async def connect(self):
|
||||
while True:
|
||||
@ -38,37 +44,50 @@ class SolanaAPI:
|
||||
logger.error(f"Failed to connect to {current_url}: {e}")
|
||||
await asyncio.sleep(5)
|
||||
|
||||
async def subscribe(self):
|
||||
async def ws_jsonrpc(self, method, params=None):
|
||||
if not isinstance(params, list):
|
||||
params = [params] if params is not None else []
|
||||
|
||||
request = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "logsSubscribe",
|
||||
"params": [
|
||||
{"mentions": [FOLLOWED_WALLET]},
|
||||
{"commitment": "confirmed"}
|
||||
]
|
||||
"method": method,
|
||||
"params": params
|
||||
}
|
||||
|
||||
await self.websocket.send(json.dumps(request))
|
||||
response = await self.websocket.recv()
|
||||
response_data = json.loads(response)
|
||||
|
||||
|
||||
if 'result' in response_data:
|
||||
self.subscription_id = response_data['result']
|
||||
logger.info(f"Subscription successful. Subscription id: {self.subscription_id}")
|
||||
return response_data['result']
|
||||
elif 'error' in response_data:
|
||||
logger.error(f"Error in WebSocket RPC call: {response_data['error']}")
|
||||
return None
|
||||
else:
|
||||
logger.warning(f"Unexpected response: {response_data}")
|
||||
return None
|
||||
|
||||
async def subscribe(self):
|
||||
params = [
|
||||
{"mentions": [FOLLOWED_WALLET]},
|
||||
{"commitment": "confirmed"}
|
||||
]
|
||||
result = await self.ws_jsonrpc("logsSubscribe", params)
|
||||
if result is not None:
|
||||
self.subscription_id = result
|
||||
logger.info(f"Subscription successful. Subscription id: {self.subscription_id}")
|
||||
else:
|
||||
logger.error("Failed to subscribe")
|
||||
|
||||
async def unsubscribe(self):
|
||||
if self.subscription_id:
|
||||
request = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "logsUnsubscribe",
|
||||
"params": [self.subscription_id]
|
||||
}
|
||||
await self.websocket.send(json.dumps(request))
|
||||
logger.info(f"Unsubscribed from subscription id: {self.subscription_id}")
|
||||
self.subscription_id = None
|
||||
result = await self.ws_jsonrpc("logsUnsubscribe", [self.subscription_id])
|
||||
if result:
|
||||
logger.info(f"Unsubscribed from subscription id: {self.subscription_id}")
|
||||
self.subscription_id = None
|
||||
else:
|
||||
logger.error(f"Failed to unsubscribe from subscription id: {self.subscription_id}")
|
||||
|
||||
async def receive_messages(self):
|
||||
while True:
|
||||
@ -83,6 +102,16 @@ class SolanaAPI:
|
||||
break
|
||||
|
||||
async def process_messages(self):
|
||||
while True:
|
||||
message = await self.message_queue.get()
|
||||
# Process the message here
|
||||
# You can add your message processing logic
|
||||
logger.info(f"Received message: {message}")
|
||||
|
||||
async def close(self):
|
||||
if self.websocket:
|
||||
await self.websocket.close()
|
||||
logger.info("WebSocket connection closed")
|
||||
while True:
|
||||
message = await self.message_queue.get()
|
||||
try:
|
||||
|
Reference in New Issue
Block a user