solana API in module
This commit is contained in:
parent
6b6018cd85
commit
8d714a9801
@ -26,12 +26,14 @@ from config import (
|
||||
FOLLOWED_WALLET, SOLANA_HTTP_URL
|
||||
)
|
||||
|
||||
from modules.utils import telegram_utils
|
||||
|
||||
class SolanaAPI:
|
||||
def __init__(self):
|
||||
class SolanaWS:
|
||||
def __init__(self, on_message: Optional[callable] = None):
|
||||
self.websocket = None
|
||||
self.subscription_id = None
|
||||
self.message_queue = asyncio.Queue()
|
||||
self.on_message = on_message
|
||||
|
||||
async def connect(self):
|
||||
while True:
|
||||
@ -104,8 +106,7 @@ class SolanaAPI:
|
||||
async def process_messages(self):
|
||||
while True:
|
||||
message = await self.message_queue.get()
|
||||
# Process the message here
|
||||
# You can add your message processing logic
|
||||
await self.on_message(message)
|
||||
logger.info(f"Received message: {message}")
|
||||
|
||||
async def close(self):
|
||||
@ -171,21 +172,19 @@ async def solana_jsonrpc(method, params = None, jsonParsed = True):
|
||||
logging.error(f"Error fetching data from Solana RPC: {e}")
|
||||
return None
|
||||
|
||||
class SolanaAPI:
|
||||
|
||||
async def process_log(log):
|
||||
# Implement your log processing logic here
|
||||
pass
|
||||
def __init__(self, process_log_callback, send_telegram_message_callback, list_initial_wallet_states_callback):
|
||||
self.process_log = process_log_callback
|
||||
self.list_initial_wallet_states = list_initial_wallet_states_callback
|
||||
|
||||
async def send_telegram_message(message):
|
||||
# Implement your Telegram message sending logic here
|
||||
pass
|
||||
|
||||
async def list_initial_wallet_states():
|
||||
# Implement your initial wallet state listing logic here
|
||||
pass
|
||||
async def process_messages(self, solana_ws):
|
||||
while True:
|
||||
message = await solana_ws.message_queue.get()
|
||||
await self.process_log(message)
|
||||
|
||||
async def wallet_watch_loop():
|
||||
solana_ws = SolanaAPI()
|
||||
solana_ws = SolanaWS(on_message=process_log)
|
||||
first_subscription = True
|
||||
|
||||
while True:
|
||||
@ -194,10 +193,10 @@ async def wallet_watch_loop():
|
||||
await solana_ws.subscribe()
|
||||
|
||||
if first_subscription:
|
||||
asyncio.create_task(list_initial_wallet_states())
|
||||
asyncio.create_task(self.list_initial_wallet_states())
|
||||
first_subscription = False
|
||||
|
||||
await send_telegram_message(f"Solana mainnet connected ({solana_ws.subscription_id})...")
|
||||
await telegram_utils.send_telegram_message(f"Solana mainnet connected ({solana_ws.subscription_id})...")
|
||||
|
||||
receive_task = asyncio.create_task(solana_ws.receive_messages())
|
||||
process_task = asyncio.create_task(solana_ws.process_messages())
|
||||
@ -216,9 +215,14 @@ async def wallet_watch_loop():
|
||||
await solana_ws.unsubscribe()
|
||||
if solana_ws.websocket:
|
||||
await solana_ws.websocket.close()
|
||||
await send_telegram_message("Reconnecting...")
|
||||
await telegram_utils.send_telegram_message("Reconnecting...")
|
||||
await asyncio.sleep(5)
|
||||
|
||||
async def process_transaction(signature):
|
||||
# Implement your logic to process each transaction
|
||||
print(f"Processing transaction: {signature['signature']}")
|
||||
# You can add more processing logic here, such as storing in a database,
|
||||
# triggering notifications, etc.
|
||||
# Example usage
|
||||
# async def main():
|
||||
# account_address = "Vote111111111111111111111111111111111111111"
|
||||
@ -258,11 +262,7 @@ async def get_last_transactions(account_address, check_interval=300, limit=1000)
|
||||
|
||||
await asyncio.sleep(1) # Sleep for 1 second before checking again
|
||||
|
||||
async def process_transaction(signature):
|
||||
# Implement your logic to process each transaction
|
||||
print(f"Processing transaction: {signature['signature']}")
|
||||
# You can add more processing logic here, such as storing in a database,
|
||||
# triggering notifications, etc.
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(wallet_watch_loop())
|
Loading…
x
Reference in New Issue
Block a user