This commit is contained in:
Dobromir Popov 2024-10-09 16:57:17 +03:00
parent ee8aef06d7
commit ab2081a084

View File

@ -157,20 +157,33 @@ async def send_telegram_message(message):
# # # # # # # # # # CRYPTO PUBLIC # # # # # # # # # #
async def get_token_prices(token_addresses: List[str]) -> Dict[str, float]:
coingecko_prices = await get_prices_from_coingecko(token_addresses)
global TOKENS_INFO
prices = await get_prices_from_coingecko(token_addresses)
# For tokens not found in CoinGecko, use DexScreener
missing_tokens = set(token_addresses) - set(coingecko_prices.keys())
missing_tokens = set(token_addresses) - set(prices.keys())
if missing_tokens:
dexscreener_prices = await get_prices_from_dexscreener(list(missing_tokens))
coingecko_prices.update(dexscreener_prices)
prices.update(dexscreener_prices)
# If any tokens are still missing, set their prices to 0
for token in set(token_addresses) - set(coingecko_prices.keys()):
coingecko_prices[token] = 0.0
for token in set(token_addresses) - set(prices.keys()):
prices[token] = 0.0
logging.warning(f"Price not found for token {token}. Setting to 0.")
return coingecko_prices
# update token info with prices
# for token, price in prices.items():
# if token in TOKENS_INFO:
# TOKENS_INFO[token]['price'] = price
# else:
# TOKENS_INFO[token] = {'price': price}
for token, price in prices.items():
if not token in TOKENS_INFO or not TOKENS_INFO[token].get('symbol'):
token_name = await get_token_metadata_symbol(token)
TOKENS_INFO[token] = {'symbol': token_name}
TOKENS_INFO[token] = {'price': price}
return prices
async def get_prices_from_coingecko(token_addresses: List[str]) -> Dict[str, float]:
url = "https://api.coingecko.com/api/v3/simple/token_price/solana"
@ -473,7 +486,7 @@ async def get_wallet_balances(wallet_address, doGetTokenName=True):
TOKENS_INFO[mint]['holdedAmount'] = amount
TOKENS_INFO[mint]['decimals'] = decimals
balances[mint] = {
'name': token_name,
'name': token_name or 'N/A',
'address': mint,
'amount': amount,
'decimals': decimals
@ -901,11 +914,20 @@ async def process_log(log_result):
if tr_details["percentage_swapped"] > 100:
tr_details["percentage_swapped"] = tr_details["percentage_swapped"] / 1000
# update token info
all_token_addresses = list(set([tr_details["token_in"], tr_details["token_out"]]))
await get_token_prices(all_token_addresses)
try:
tr_details["symbol_in"] = TOKENS_INFO[tr_details["token_in"]].get('symbol') or await get_token_metadata_symbol(tr_details["token_in"])
tr_details["symbol_out"] = TOKENS_INFO[tr_details["token_out"]].get('symbol') or await get_token_metadata_symbol(tr_details["token_out"])
tr_details['amount_in_USD'] = tr_details['amount_in'] * TOKEN_PRICES.get(tr_details['token_in'], 0)
# tr_details['amount_out_USD'] = tr_details['amount_out'] * TOKEN_PRICES.get(tr_details['token_out'], 0)
token_in = TOKENS_INFO[tr_details["token_in"]]
token_out = TOKENS_INFO[tr_details["token_out"]]
tr_details["symbol_in"] = token_in.get('symbol')
tr_details["symbol_out"] = token_out.get('symbol')
tr_details['amount_in_USD'] = tr_details['amount_in'] * token_in.get('price', 0)
tr_details['amount_out_USD'] = tr_details['amount_out'] * token_out.get('price', 0)
except Exception as e:
logging.error(f"Error fetching token prices: {e}")
@ -919,6 +941,7 @@ async def process_log(log_result):
await send_telegram_message(message_text)
await follow_move(tr_details)
await save_token_info()
except Exception as e:
logging.error(f"Error aquiring log details and following: {e}")
return
@ -937,24 +960,27 @@ async def process_log(log_result):
# "Program log: source_token_change: 58730110139, destination_token_change: 270131294",
async def get_transaction_details_info(tx_signature_str: str, logs: List[str]) -> Dict[str, Any]:
global TOKENS_INFO
tr_info = await get_transaction_details_with_retry(tx_signature_str)
# Fetch token prices
token_prices = await get_token_prices([tr_info['token_in'], tr_info['token_out']])
for token, price in token_prices.items():
if not token in TOKENS_INFO:
token_name = await get_token_metadata_symbol(token)
TOKENS_INFO[token] = {'symbol': token_name}
TOKENS_INFO[token] = {'price': price}
# for token, price in token_prices.items():
# if not token in TOKENS_INFO or not TOKENS_INFO[token].get('symbol'):
# token_name = await get_token_metadata_symbol(token)
# TOKENS_INFO[token] = {'symbol': token_name}
# TOKENS_INFO[token] = {'price': price}
# Calculate USD values
tr_info['amount_in_USD'] = tr_info['amount_in'] * token_prices.get(tr_info['token_in'], 0)
tr_info['amount_out_USD'] = tr_info['amount_out'] * token_prices.get(tr_info['token_out'], 0)
# Calculate the percentage of the source balance that was swapped; ToDo: fix decimals for percentage
try:
tr_info['percentage_swapped'] = (tr_info['amount_in'] / tr_info['before_source_balance']) * 100 if tr_info['before_source_balance'] > 0 else 50
except Exception as e:
logging.error(f"Error calculating percentage swapped: {e}")
return tr_info
def _get_pre_balance(transaction_details: Dict[str, Any], token: str) -> float: