various fixes and improvements
This commit is contained in:
parent
7bb73ae3b0
commit
7bc36ddf44
@ -315,9 +315,9 @@ from spl.token.async_client import AsyncToken
|
||||
from spl.token.constants import TOKEN_PROGRAM_ID
|
||||
from borsh_construct import String, CStruct
|
||||
|
||||
async def get_token_name(mint_address):
|
||||
async def get_token_metadata_symbol(mint_address):
|
||||
global TOKENS_INFO
|
||||
if mint_address in TOKENS_INFO:
|
||||
if mint_address in TOKENS_INFO and 'symbol' in TOKENS_INFO[mint_address]:
|
||||
return TOKENS_INFO[mint_address].get('symbol')
|
||||
try:
|
||||
account_data_result = await solana_jsonrpc("getAccountInfo", mint_address)
|
||||
@ -325,14 +325,16 @@ async def get_token_name(mint_address):
|
||||
account_data_data = account_data_result['value']['data']
|
||||
if 'parsed' in account_data_data and 'info' in account_data_data['parsed']:
|
||||
account_data_info = account_data_data['parsed']['info']
|
||||
if 'tokenName' in account_data_info:
|
||||
TOKENS_INFO[mint_address] = account_data_info
|
||||
return account_data_info['tokenName']
|
||||
if 'decimals' in account_data_info:
|
||||
if mint_address in TOKENS_INFO:
|
||||
TOKENS_INFO[mint_address]['decimals'] = account_data_info['decimals']
|
||||
else:
|
||||
TOKENS_INFO[mint_address] = {'decimals': account_data_info['decimals']}
|
||||
if 'tokenName' in account_data_info:
|
||||
if mint_address in TOKENS_INFO:
|
||||
TOKENS_INFO[mint_address]['name'] = account_data_info['tokenName']
|
||||
else:
|
||||
TOKENS_INFO[mint_address] = {'name': account_data_info['tokenName']}
|
||||
# token_address = Pubkey.from_string(mint_address)
|
||||
# # token = Token(solana_client, token_address, TOKEN_PROGRAM_ID, Keypair())
|
||||
# token = AsyncToken(solana_client, token_address, TOKEN_PROGRAM_ID, Keypair())
|
||||
@ -346,10 +348,10 @@ async def get_token_name(mint_address):
|
||||
TOKENS_INFO[mint_address].update(metadata)
|
||||
else:
|
||||
TOKENS_INFO[mint_address] = metadata
|
||||
return metadata.get('symbol') or metadata.get('name')
|
||||
|
||||
# TOKENS_INFO[mint_address] = metadata
|
||||
# return metadata.get('symbol') or metadata.get('name')
|
||||
|
||||
return TOKENS_INFO[mint_address].get('symbol')
|
||||
except Exception as e:
|
||||
logging.error(f"Error fetching token name for {mint_address}: {str(e)}")
|
||||
return None
|
||||
@ -453,7 +455,7 @@ async def get_token_metadata(mint_address):
|
||||
return None
|
||||
|
||||
|
||||
async def get_wallet_balances(wallet_address):
|
||||
async def get_wallet_balances(wallet_address, doGetTokenName=True):
|
||||
balances = {}
|
||||
logging.info(f"Getting balances for wallet: {wallet_address}")
|
||||
global TOKENS_INFO
|
||||
@ -479,11 +481,10 @@ async def get_wallet_balances(wallet_address):
|
||||
if amount > 0:
|
||||
if mint in TOKENS_INFO:
|
||||
token_name = TOKENS_INFO[mint].get('symbol')
|
||||
else:
|
||||
token_name = await get_token_name(mint) or 'N/A'
|
||||
elif doGetTokenName:
|
||||
token_name = await get_token_metadata_symbol(mint) or 'N/A'
|
||||
# sleep for 1 second to avoid rate limiting
|
||||
await asyncio.sleep(2)
|
||||
|
||||
|
||||
TOKENS_INFO[mint]['holdedAmount'] = amount
|
||||
TOKENS_INFO[mint]['decimals'] = decimals
|
||||
@ -561,12 +562,6 @@ async def get_swap_transaction_details(tx_signature_str):
|
||||
parsed_result["token_out"] = mint
|
||||
parsed_result["amount_out"] = amount
|
||||
|
||||
# Calculate USD values if token is USDC
|
||||
if parsed_result["token_in"] == "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v":
|
||||
parsed_result["amount_in_USD"] = parsed_result["amount_in"]
|
||||
if parsed_result["token_out"] == "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v":
|
||||
parsed_result["amount_out_USD"] = parsed_result["amount_out"]
|
||||
|
||||
# Calculate percentage swapped
|
||||
if parsed_result["amount_in"] > 0 and parsed_result["amount_out"] > 0:
|
||||
parsed_result["percentage_swapped"] = (parsed_result["amount_out"] / parsed_result["amount_in"]) * 100
|
||||
@ -799,12 +794,13 @@ async def list_initial_wallet_states():
|
||||
logging.info(message)
|
||||
await send_telegram_message(message)
|
||||
# save token info to file
|
||||
await save_token_info()
|
||||
|
||||
async def save_token_info():
|
||||
with open('./logs/token_info.json', 'w') as f:
|
||||
json.dump(TOKENS_INFO, f, indent=2)
|
||||
|
||||
|
||||
|
||||
async def get_transaction_details_with_retry(transaction_id, retry_delay = 5, max_retries = 20):
|
||||
async def get_transaction_details_with_retry(transaction_id, retry_delay = 5, max_retries = 12):
|
||||
# wait for the transaction to be confirmed
|
||||
# await async_client.wait_for_confirmation(Signature.from_string(transaction_id))
|
||||
# qwery every 5 seconds for the transaction details untill not None or 30 seconds
|
||||
@ -912,10 +908,10 @@ async def process_log(log_result):
|
||||
tr_details["percentage_swapped"] = tr_details["percentage_swapped"] / 1000
|
||||
|
||||
try:
|
||||
tr_details["symbol_in"] = TOKENS_INFO[tr_details["token_in"]].get('symbol') or await get_token_name(tr_details["token_in"])
|
||||
tr_details["symbol_out"] = TOKENS_INFO[tr_details["token_out"]].get('symbol') or await get_token_name(tr_details["token_out"])
|
||||
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)
|
||||
# tr_details['amount_out_USD'] = tr_details['amount_out'] * TOKEN_PRICES.get(tr_details['token_out'], 0)
|
||||
except Exception as e:
|
||||
logging.error(f"Error fetching token prices: {e}")
|
||||
|
||||
@ -923,11 +919,12 @@ async def process_log(log_result):
|
||||
f"<b>Swap detected:</b>\n"
|
||||
f"Token In: {tr_details['symbol_in']}\n"
|
||||
f"Token Out: {tr_details['symbol_out']}\n"
|
||||
f"Amount In USD: {tr_details['amount_out_USD']:.2f}\n"
|
||||
f"Amount In USD: {tr_details['amount_in_USD']:.2f}\n"
|
||||
f"Percentage Swapped: {tr_details['percentage_swapped']:.2f}%"
|
||||
)
|
||||
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
|
||||
@ -947,25 +944,20 @@ async def process_log(log_result):
|
||||
|
||||
async def get_transaction_details_info(tx_signature_str: str, logs: List[str]) -> Dict[str, Any]:
|
||||
|
||||
try:
|
||||
tr_info = await get_swap_transaction_details(tx_signature_str)
|
||||
except Exception as e:
|
||||
logging.error(f"Error fetching swap transaction details: {e}")
|
||||
|
||||
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 price is None:
|
||||
if token in TOKENS_INFO:
|
||||
TOKENS_INFO[token]['price'] = price
|
||||
else:
|
||||
TOKENS_INFO[token] = {'price': price}
|
||||
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}
|
||||
|
||||
# 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)
|
||||
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
|
||||
tr_info['percentage_swapped'] = (tr_info['amount_in'] / tr_info['before_source_balance']) * 100 if tr_info['before_source_balance'] > 0 else 50
|
||||
@ -980,19 +972,21 @@ def _get_pre_balance(transaction_details: Dict[str, Any], token: str) -> float:
|
||||
|
||||
|
||||
async def follow_move(move):
|
||||
your_balances = await get_wallet_balances(YOUR_WALLET)
|
||||
your_balances = await 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 not your_balance_info:
|
||||
msg = f"<b>Move Failed:</b>\nNo balance found for token {move['token_in']}"
|
||||
your_balance = your_balance_info['amount']
|
||||
|
||||
|
||||
token_info = TOKENS_INFO.get(move['token_in'])
|
||||
token_name_in = token_info.get('symbol') or await get_token_metadata(move['token_in'])
|
||||
token_name_out = TOKENS_INFO[move['token_out']].get('symbol') or await get_token_metadata_symbol(move['token_out'])
|
||||
|
||||
if not your_balance:
|
||||
msg = f"<b>Move Failed:</b>\nNo balance found for token {move['token_in']}. Cannot follow move."
|
||||
logging.warning(msg)
|
||||
await send_telegram_message(msg)
|
||||
return
|
||||
|
||||
your_balance = TOKENS_INFO[move['token_in']].get('holdedAmount') or your_balance_info['amount']
|
||||
token_name_in = TOKENS_INFO[move['token_in']].get('symbol') or await get_token_name(move['token_in'])
|
||||
token_name_out = TOKENS_INFO[move['token_out']].get('symbol') or await get_token_name(move['token_out'])
|
||||
|
||||
# move["percentage_swapped"] = (move["amount_out"] / move["amount_in"]) * 100
|
||||
# Calculate the amount to swap based on the same percentage as the followed move
|
||||
amount_to_swap = your_balance * (move['percentage_swapped'] / 100)
|
||||
@ -1002,59 +996,73 @@ async def follow_move(move):
|
||||
|
||||
if your_balance >= amount_to_swap:
|
||||
try:
|
||||
private_key = Keypair.from_bytes(base58.b58decode(pk))
|
||||
async_client = AsyncClient(SOLANA_WS_URL)
|
||||
jupiter = Jupiter(async_client, private_key)
|
||||
# Convert to lamports
|
||||
# if decimals is 6, then amount = amount * 1e6; if 9, then amount = amount * 1e9
|
||||
amount = int(amount_to_swap * 10**your_balance_info['decimals'])
|
||||
amount = int(amount_to_swap * 10**token_info.get('decimals') )
|
||||
|
||||
transaction_data = await jupiter.swap(
|
||||
input_mint=move['token_in'],
|
||||
output_mint=move['token_out'],
|
||||
amount=amount,
|
||||
slippage_bps=100, # Increased to 1%
|
||||
)
|
||||
|
||||
notification = (
|
||||
f"<b>Initiating move:</b>\n (decimals: {your_balance_info['decimals']})\n"
|
||||
f"Swapping {move['percentage_swapped']:.2f}% ({amount_to_swap:.2f}) {token_name_in} for {token_name_out}"
|
||||
)
|
||||
logging.info(notification)
|
||||
error_logger.info(notification)
|
||||
await send_telegram_message(notification)
|
||||
|
||||
raw_transaction = VersionedTransaction.from_bytes(base64.b64decode(transaction_data))
|
||||
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)
|
||||
# send the transaction
|
||||
result = await async_client.send_raw_transaction(txn=bytes(signed_txn), opts=opts)
|
||||
|
||||
transaction_id = json.loads(result.to_json())['result']
|
||||
print(f"Follow Transaction Sent: https://solscan.io/tx/{transaction_id}")
|
||||
tx_details = await get_transaction_details_with_retry(transaction_id)
|
||||
|
||||
if tx_details is None:
|
||||
logging.info(f"Failed to get transaction details for {transaction_id}")
|
||||
try:
|
||||
notification = (
|
||||
f"<b>Move Followed:</b>\n"
|
||||
f"Swapped {amount_to_swap:.6f} {token_name_in} ({move['token_in']}) "
|
||||
f"(same {move['percentage_swapped']:.2f}% as followed wallet)\n"
|
||||
f"\n\n<b>Transaction:</b> <a href='https://solscan.io/tx/{transaction_id}'>{transaction_id}</a>"
|
||||
f"<b>Initiating move:</b>\n (decimals: {token_info.get('decimals')})\n"
|
||||
f"Swapping {move['percentage_swapped']:.2f}% ({amount_to_swap:.2f}) {token_name_in} for {token_name_out}"
|
||||
)
|
||||
logging.info(notification)
|
||||
error_logger.info(notification)
|
||||
await send_telegram_message(notification)
|
||||
except Exception as e:
|
||||
logging.error(f"Error sending notification: {e}")
|
||||
|
||||
for retry in range(2):
|
||||
private_key = Keypair.from_bytes(base58.b58decode(pk))
|
||||
async_client = AsyncClient(SOLANA_WS_URL)
|
||||
jupiter = Jupiter(async_client, private_key)
|
||||
transaction_data = await jupiter.swap(
|
||||
input_mint=move['token_in'],
|
||||
output_mint=move['token_out'],
|
||||
amount=amount,
|
||||
slippage_bps=100, # Increased to 1%
|
||||
)
|
||||
raw_transaction = VersionedTransaction.from_bytes(base64.b64decode(transaction_data))
|
||||
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)
|
||||
|
||||
# send the transaction
|
||||
result = await async_client.send_raw_transaction(txn=bytes(signed_txn), opts=opts)
|
||||
|
||||
transaction_id = json.loads(result.to_json())['result']
|
||||
print(f"Follow Transaction Sent: https://solscan.io/tx/{transaction_id}")
|
||||
tx_details = await get_transaction_details_with_retry(transaction_id)
|
||||
|
||||
if tx_details is not None:
|
||||
break
|
||||
else:
|
||||
logging.warning(f"Failed to get transaction details for {transaction_id}. Probably transaction failed. Retrying again...")
|
||||
await asyncio.sleep(5)
|
||||
await get_wallet_balances(YOUR_WALLET, doGetTokenName=False)
|
||||
|
||||
else:
|
||||
notification = (
|
||||
f"<b>Move Followed:</b>\n"
|
||||
f"Swapped {amount_to_swap:.6f} {token_name_in} ({move['symbol_in']}) "
|
||||
f"(same {move['percentage_swapped']:.2f}% as followed wallet)\n"
|
||||
f"for {tx_details['amount_out']:.6f} {token_name_out} ({move['symbol_out']})"
|
||||
# f"Amount In USD: {tr_details['amount_in_USD']}\n"
|
||||
f"\n\n<b>Transaction:</b> <a href='https://solscan.io/tx/{transaction_id}'>{transaction_id}</a>"
|
||||
)
|
||||
logging.info(notification)
|
||||
await send_telegram_message(notification)
|
||||
try:
|
||||
if tx_details is None:
|
||||
logging.info(f"Failed to get transaction details for {transaction_id}")
|
||||
notification = (
|
||||
f"<b>Move Followed:</b>\n"
|
||||
f"Swapped {amount_to_swap:.6f} {token_name_in} ({move['token_in']}) "
|
||||
f"(same {move['percentage_swapped']:.2f}% as followed wallet)\n"
|
||||
f"\n\n<b>Transaction:</b> <a href='https://solscan.io/tx/{transaction_id}'>{transaction_id}</a>"
|
||||
)
|
||||
|
||||
else:
|
||||
notification = (
|
||||
f"<b>Move Followed:</b>\n"
|
||||
f"Swapped {amount_to_swap:.6f} {token_name_in} ({move['symbol_in']}) "
|
||||
f"(same {move['percentage_swapped']:.2f}% as followed wallet)\n"
|
||||
f"for {tx_details['amount_out']:.2f} {token_name_out}"
|
||||
# f"Amount In USD: {tr_details['amount_in_USD']}\n"
|
||||
f"\n\n<b>Transaction:</b> <a href='https://solscan.io/tx/{transaction_id}'>{transaction_id}</a>"
|
||||
)
|
||||
logging.info(notification)
|
||||
await send_telegram_message(notification)
|
||||
except Exception as e:
|
||||
logging.error(f"Error sending notification: {e}")
|
||||
|
||||
except Exception as e:
|
||||
error_message = f"<b>Swap Follow Error:</b>\n{str(e)}"
|
||||
|
Loading…
x
Reference in New Issue
Block a user