wip get ballances

This commit is contained in:
Dobromir Popov 2024-10-02 17:15:06 +03:00
parent c3009fd358
commit 8c83ea6c87

View File

@ -13,6 +13,9 @@ import logging
from solana.rpc.websocket_api import connect from solana.rpc.websocket_api import connect
from solana.rpc.async_api import AsyncClient from solana.rpc.async_api import AsyncClient
from solana.rpc.commitment import Confirmed from solana.rpc.commitment import Confirmed
from solana.rpc.types import TokenAccountOpts
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
@ -71,13 +74,35 @@ async def get_token_balance(wallet_address, token_address):
logging.error(f"Error getting balance for {token_address} in {wallet_address}: {str(e)}") logging.error(f"Error getting balance for {token_address} in {wallet_address}: {str(e)}")
return 0 return 0
async def get_wallet_balances(wallet_address): async def get_wallet_balances(wallet_address):
balances = {} balances = {}
logging.info(f"Getting balances for wallet: {wallet_address}") logging.info(f"Getting balances for wallet: {wallet_address}")
for token, address in TOKEN_ADDRESSES.items():
balance = await get_token_balance(wallet_address, address) opts = TokenAccountOpts(
balances[token] = balance program_id=Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
logging.debug(f"Balance for {token}: {balance}") )
# Get all token accounts for the wallet
response = await solana_client.get_token_accounts_by_owner(
Pubkey.from_string(wallet_address),
opts,
commitment=Confirmed
)
if response.value:
for account in response.value:
mint = account.account.data.parsed.info.mint
balance_response = await solana_client.get_token_account_balance(account.pubkey)
if balance_response.value:
amount = float(balance_response.value.uiAmount)
balances[mint] = amount
logging.debug(f"Balance for {mint}: {amount}")
sol_balance = await solana_client.get_balance(Pubkey.from_string(wallet_address))
if sol_balance.value:
balances['SOL'] = sol_balance.value / 1e9
return balances return balances
async def get_non_zero_token_balances(wallet_address): async def get_non_zero_token_balances(wallet_address):
@ -153,43 +178,47 @@ async def follow_move(move):
logging.warning(message) logging.warning(message)
await send_telegram_message(message) await send_telegram_message(message)
async def on_logs(logs): async def on_logs(log):
print(f"Received logs: {logs}") print(f"Received log: {log}")
if logs.value.err: if 'err' in log and log['err']:
return return
tx = logs.value.signature tx = log['signature']
tx_result = await solana_client.get_transaction(tx) tx_result = await solana_client.get_transaction(tx)
if tx_result and tx_result.value: if tx_result and 'result' in tx_result and tx_result['result']:
for instruction in tx_result.value.transaction.message.instructions: transaction = tx_result['result']['transaction']
if instruction.program_id == Pubkey.from_string(TOKEN_ADDRESSES['SOL']): message = transaction['message']
for instruction in message['instructions']:
if instruction['programId'] == TOKEN_ADDRESSES['SOL']:
# This is a token transfer # This is a token transfer
from_pubkey = instruction.accounts[0] from_pubkey = instruction['accounts'][0]
to_pubkey = instruction.accounts[1] to_pubkey = instruction['accounts'][1]
amount = instruction.data[8:] # The amount is stored in the last 8 bytes amount = int(instruction['data'], 16) / 1e9 # Convert lamports to SOL
amount = int.from_bytes(amount, 'little') / 1e9 # Convert lamports to SOL
if from_pubkey == Pubkey.from_string(FOLLOWED_WALLET): if from_pubkey == FOLLOWED_WALLET:
move = { move = {
'token': 'SOL', 'token': 'SOL',
'amount': amount, 'amount': amount,
'to_token': 'Unknown' # You might want to determine this based on the receiving address 'to_token': 'Unknown' # You might want to determine this based on the receiving address
} }
await follow_move(move) await follow_move(move)
async def subscribe_to_wallet(): async def subscribe_to_wallet():
uri = SOLANA_URL uri = SOLANA_URL
async with websockets.connect(uri) as websocket: async with websockets.connect(uri) as websocket:
# Correct the `params` format to be an object # Correct the `params` format to be an array
request = { request = {
"jsonrpc": "2.0", "jsonrpc": "2.0",
"id": 1, "id": 1,
"method": "logsSubscribe", "method": "logsSubscribe",
"params": { "params": [
"mentions": [YOUR_WALLET], {
"commitment": "confirmed" "mentions": [FOLLOWED_WALLET] # Changed from YOUR_WALLET to FOLLOWED_WALLET
} },
{
"commitment": "confirmed"
}
]
} }
await websocket.send(json.dumps(request)) await websocket.send(json.dumps(request))
@ -197,7 +226,13 @@ async def subscribe_to_wallet():
# Listen for messages # Listen for messages
while True: while True:
response = await websocket.recv() response = await websocket.recv()
print(response) response_data = json.loads(response)
if 'result' in response_data:
print(f"Subscription successful. Subscription id: {response_data['result']}")
elif 'params' in response_data:
await on_logs(response_data['params']['result'])
else:
print(f"Unexpected response: {response}")
async def main(): async def main():
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)