This commit is contained in:
Dobromir Popov
2024-10-28 16:57:19 +02:00
parent 800cbede4d
commit 885eb523f6
6 changed files with 482 additions and 136 deletions

View File

@ -3,24 +3,21 @@ import uvicorn
from asgiref.wsgi import WsgiToAsgi
import websockets
import json
from flask import Flask, render_template, request, jsonify
import datetime
import base64
import os
import base58
from dotenv import load_dotenv, set_key
import aiohttp
import requests
import re
import random
from dotenv import load_dotenv
from threading import Thread
from solana.rpc.async_api import AsyncClient
from solders.transaction import VersionedTransaction
from solana.rpc.types import TxOpts
from solana.rpc.commitment import Confirmed, Finalized, Processed
from solders.keypair import Keypair
from jupiter_python_sdk.jupiter import Jupiter
from solana.rpc.commitment import Processed
from modules.webui import init_app
from modules.storage import init_db, store_transaction
@ -192,7 +189,7 @@ async def process_log(log_result):
f"{tr_details['symbol_out']} \n"
)
await telegram_utils.send_telegram_message(message_text)
await follow_move(tr_details)
await SAPI.follow_move(tr_details)
await SAPI.save_token_info()
except Exception as e:
@ -210,10 +207,11 @@ async def process_log(log_result):
async def follow_move(move):
your_balances = await SAPI.get_wallet_balances(YOUR_WALLET, doGetTokenName=False)
async def follow_move_legacy(move):
global pk
if pk is None:
pk = await get_pk()
your_balances = await SAPI.dex.get_wallet_balances(YOUR_WALLET, doGetTokenName=False)
your_balance_info = next((balance for balance in your_balances.values() if balance['address'] == move['token_in']), None)
if your_balance_info is not None:
# Use the balance
@ -243,8 +241,9 @@ async def follow_move(move):
amount_to_swap = move['amount_in']
else:
try:
fixed_amount = float(FOLLOW_AMOUNT)
amount_to_swap = min(fixed_amount, your_balance)
fixed_amount = float(FOLLOW_AMOUNT) # un USD
fixed_amount_in_token = fixed_amount / move["token_in_price"]
amount_to_swap = min(fixed_amount_in_token, your_balance)
except ValueError:
msg = f"<b>Move not followed:</b>\nInvalid FOLLOW_AMOUNT '{FOLLOW_AMOUNT}'. Must be 'percentage' or a number."
logging.warning(msg)
@ -272,11 +271,12 @@ async def follow_move(move):
try:
notification = (
f"<b>Initiating move:</b>\n"
f"Swapping {move['percentage_swapped']:.2f}% ({amount_to_swap:.2f}) {token_name_in} for {token_name_out}"
f"Swapping {amount_to_swap:.2f} {token_name_in} for {token_name_out}"
+ (f" ({move['percentage_swapped']:.2f}%)" if 'percentage_swapped' in move else "")
)
# logging.info(notification)
# error_logger.info(notification)
# await telegram_utils.send_telegram_message(notification)
await telegram_utils.send_telegram_message(notification)
except Exception as e:
logging.error(f"Error sending notification: {e}")
@ -324,7 +324,7 @@ async def follow_move(move):
await telegram_utils.send_telegram_message(error_message)
amount = amount * 0.75
await SAPI.get_wallet_balances(YOUR_WALLET, doGetTokenName=False)
await SAPI.dex.get_wallet_balances(YOUR_WALLET, doGetTokenName=False)
try:
if tx_details is None:
@ -401,7 +401,7 @@ async def process_messages(websocket):
pk = None
app = init_app()
app = init_app(follow_move_legacy)
# Convert Flask app to ASGI
asgi_app = WsgiToAsgi(app)
@ -427,7 +427,7 @@ if __name__ == '__main__':
# Run the ASGI server
uvicorn.run(
"app:asgi_app",
host="127.0.0.1",
host="0.0.0.0",
port=3001,
log_level="debug",
reload=True