log processor

This commit is contained in:
Dobromir Popov 2024-11-06 11:08:59 +02:00
parent fb09daf983
commit 3dd7e315c7
6 changed files with 94 additions and 8 deletions

View File

@ -20,8 +20,10 @@ FOLLOW_AMOUNT=percentage
LIQUIDITY_TOKENS=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v,So11111111111111111111111111111111111111112
PRIORITY=1 # 0-10, 5 = market cap, 10 twice market cap
DO_WATCH_WALLET=True
# Niki's to Sync: [PROD]
FOLLOWED_WALLET="7keSmTZozjmuX66gd9GBSJYEHnMqsyutWpvuuKtXZKDH"
FOLLOWED_WALLET="3EZkyU9zQRrHPnrNovDiRCA9Yg3wLK35u9cdrcGcszi1"
YOUR_WALLET="7QXGLRjvyFAmxdRaP9Wk18KwWTMfspF4Na2sr3o3PzxV"
PK=3FxXjNrtEqwAKYj4BpkuLAJPzuKRWykkvjeBYQEVuFqRFWRm9eVcWrrYKbns2M31ESMoASG2WV39w9Dpx532sPUH

View File

@ -5,9 +5,7 @@ import websockets
import json
import datetime
import base64
import os
import base58
from dotenv import load_dotenv
@ -22,6 +20,7 @@ from solana.rpc.commitment import Processed
from modules.webui import init_app
from modules.storage import init_db, store_transaction
from modules.utils import telegram_utils, logging, get_pk
from modules.log_processor import watch_for_new_logs
from modules.SolanaAPI import SAPI
@ -31,7 +30,7 @@ load_dotenv('.env.secret')
# Configuration
from config import (FOLLOWED_WALLET, YOUR_WALLET, SOLANA_WS_URL, SOLANA_HTTP_URL, FOLLOW_AMOUNT, SOLANA_ENDPOINTS, logging, error_logger, logger)
from config import (DO_WATCH_WALLET, logging, logger)
@ -412,8 +411,13 @@ async def main():
pk = await get_pk()
await telegram_utils.initialize()
await telegram_utils.send_telegram_message("Solana Agent Started. Connecting to mainnet...")
# Start the log processor
asyncio.create_task(watch_for_new_logs())
# process_transaction
await SAPI.wallet_watch_loop()
if DO_WATCH_WALLET:
await SAPI.wallet_watch_loop()
def run_asyncio_tasks():
asyncio.run(main())
@ -430,7 +434,7 @@ if __name__ == '__main__':
"app:asgi_app",
host="0.0.0.0",
port=3001,
log_level="debug",
log_level="info", # "debug"
reload=True
)

View File

@ -22,6 +22,9 @@ DISPLAY_CURRENCY = os.getenv('DISPLAY_CURRENCY', 'USD')
BOT_NAME = os.getenv("BOT_NAME")
FOLLOW_AMOUNT = os.getenv('FOLLOW_AMOUNT', 'percentage')
LIQUIDITY_TOKENS = os.getenv('LIQUIDITY_TOKENS', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v').split(',')
DO_WATCH_WALLET = os.getenv('DO_WATCH_WALLET', 'False').lower() == 'true'
# 5 is current market cap priority
PRIORITY = int(os.getenv('PRIORITY', 5))
SOLANA_ENDPOINTS = [
"wss://api.mainnet-beta.solana.com",

View File

@ -767,6 +767,16 @@ class SolanaAPI:
slippage_bps=300, # Increased to 3%
)
logging.info(f"Initiating move. Transaction data:\n {transaction_data}")
fee = async_client.get_fee_for_message(transaction_data.message)
# priority_fee = 0
# if PRIORITY:
# priority_fee = 100 * PRIORITY # defalt if we can't get current rate
# try:
# priority_fee = await calculate_priority_fee(async_client, PRIORITY)
# except:
# logging.warning(f"Failed to get priority fee. Using default value: {priority_fee}")
# error_logger.info(f"Initiating move. Transaction data:\n {transaction_data}")
raw_transaction = VersionedTransaction.from_bytes(base64.b64decode(transaction_data))
# message = raw_transaction.message
@ -774,8 +784,12 @@ class SolanaAPI:
# signature = private_key.sign_message( bytes(message) )
signature = private_key.sign_message(message.to_bytes_versioned(raw_transaction.message))
signed_txn = VersionedTransaction.populate(raw_transaction.message, [signature])
opts = TxOpts(skip_preflight=False, preflight_commitment=Processed)
opts = TxOpts(
skip_preflight=False,
preflight_commitment=Processed,
# max_retries=10,
# priority_fee =priority_fee
)
# send the transaction
result = await async_client.send_raw_transaction(txn=bytes(signed_txn), opts=opts)
@ -842,6 +856,30 @@ class SolanaAPI:
logging.error(f"Error following move: {e}")
async def calculate_priority_fee(async_client, priority_level=5):
recent_fees = await async_client.get_recent_prioritization_fees()
if not recent_fees:
return 1000 # fallback value in microlamports
# Calculate average and max fees
fees = [fee.prioritization_fee for fee in recent_fees]
avg_fee = sum(fees) / len(fees)
max_fee = max(fees)
# Calculate base fee (weighted average between mean and max)
base_fee = (2 * avg_fee + max_fee) / 3 # You can adjust this weighting
# Calculate scaling factor (priority_level / 5)
# priority 5 = 1x base_fee
# priority 10 = 2x base_fee
# priority 1 = 0.2x base_fee
scaling_factor = priority_level / 5
final_fee = int(base_fee * scaling_factor)
# Set minimum fee to avoid too low values
return max(final_fee, 100) # minimum 100 microlamports
class SolanaDEX:
def __init__(self, DISPLAY_CURRENCY: str):

View File

@ -0,0 +1,36 @@
import os
import asyncio
from pathlib import Path
from .storage import store_transaction
LOG_DIRECTORY = "path/to/log/directory"
FILE_MASK = "*.log"
async def process_log_file(file_path):
# Read the file and extract transaction data
with open(file_path, 'r') as file:
data = file.read()
# Assume data is parsed into these variables
wallet_id = "extracted_wallet_id"
transaction_type = "extracted_transaction_type"
sell_currency = "extracted_sell_currency"
sell_amount = 0.0
sell_value = 0.0
buy_currency = "extracted_buy_currency"
buy_amount = 0.0
buy_value = 0.0
solana_signature = "extracted_solana_signature"
details = {}
# Store the transaction
await store_transaction(wallet_id, transaction_type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, solana_signature, details)
# Rename the file to append '_saved'
new_file_path = file_path.with_name(file_path.stem + "_saved" + file_path.suffix)
os.rename(file_path, new_file_path)
async def watch_for_new_logs():
while True:
for file_path in Path(LOG_DIRECTORY).glob(FILE_MASK):
await process_log_file(file_path)
await asyncio.sleep(10) # Check every 10 seconds

View File

@ -14,6 +14,9 @@ async def init_db():
await prisma_client.connect()
async def store_transaction(wallet_id, transaction_type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, solana_signature, details=None):
"""
Store a transaction record in the database.
"""
await prisma_client.transaction.create(
data={
'wallet_id': wallet_id,