From 55c8fc6d7148ec466cabea3a06ab54363a6dff4f Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Wed, 2 Oct 2024 15:13:09 +0300 Subject: [PATCH] telegram working --- crypto/sol/app.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/crypto/sol/app.py b/crypto/sol/app.py index 82433ff..c83ae08 100644 --- a/crypto/sol/app.py +++ b/crypto/sol/app.py @@ -7,6 +7,7 @@ import asyncio from telegram import Bot from telegram.constants import ParseMode import datetime +import logging app = Flask(__name__) @@ -23,20 +24,6 @@ TELEGRAM_BOT_TOKEN = "6805059978:AAHNJKuOeazMSJHc3-BXRCsFfEVyFHeFnjw" # Initialize Telegram Bot bot = Bot(token=TELEGRAM_BOT_TOKEN) -# Token addresses (to be populated dynamically) -TOKEN_ADDRESSES = {} - -async def get_token_balance(wallet_address, token_address): - try: - balance = await solana_client.get_token_account_balance( - Pubkey.from_string(token_address), - commitment=Confirmed - ) - return float(balance['result']['value']['uiAmount']) - except Exception as e: - print(f"Error getting balance for {token_address}: {str(e)}") - return 0 - async def send_telegram_message(message): try: await bot.send_message(chat_id=DEVELOPER_CHAT_ID, text=message, parse_mode=ParseMode.HTML) @@ -99,18 +86,40 @@ async def follow_move(): 'message': f"Insufficient balance to swap {amount_to_swap:.6f} {move['token']}" }) + + +# Token addresses (to be populated dynamically) +TOKEN_ADDRESSES = {} + +async def get_token_balance(wallet_address, token_address): + try: + balance = await solana_client.get_token_account_balance( + Pubkey.from_string(token_address), + commitment=Confirmed + ) + logging.debug(f"Balance for {token_address}: {balance}") + return float(balance['result']['value']['uiAmount']) + except Exception as e: + logging.error(f"Error getting balance for {token_address}: {str(e)}") + return 0 + async def get_wallet_balances(wallet_address): balances = {} + logging.info(f"Getting balances for wallet: {wallet_address}") for token, address in TOKEN_ADDRESSES.items(): - balances[token] = await get_token_balance(wallet_address, address) + balance = await get_token_balance(wallet_address, address) + balances[token] = balance + logging.debug(f"Balance for {token}: {balance}") return balances async def get_non_zero_token_balances(wallet_address): non_zero_balances = {} + logging.info(f"Getting non-zero balances for wallet: {wallet_address}") for token, address in TOKEN_ADDRESSES.items(): balance = await get_token_balance(wallet_address, address) if balance > 0: non_zero_balances[token] = address + logging.debug(f"Non-zero balance for {token}: {balance}") return non_zero_balances async def list_initial_wallet_states():