-
Notifications
You must be signed in to change notification settings - Fork 1
/
solana_token_balance.py
79 lines (69 loc) · 2.26 KB
/
solana_token_balance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import requests
import json
import pandas as pd
from dotenv import load_dotenv
import os
from tqdm import tqdm
# Load environment variables from .env file
load_dotenv()
# Solana RPC URL and token contract address from .env file
RPC_URL = os.getenv("RPC_URL")
TOKEN_CONTRACT = os.getenv("TOKEN_CONTRACT")
# Load the wallet addresses from the JSON file
with open('solana_wallet.json', 'r') as file:
wallets = json.load(file)
def get_token_balance(wallet_address):
headers = {
"Content-Type": "application/json"
}
if TOKEN_CONTRACT == '0x0':
body = json.dumps({
"jsonrpc": "2.0",
"id": 1,
"method": "getBalance",
"params": [
wallet_address
]
})
else:
body = json.dumps({
"jsonrpc": "2.0",
"id": 1,
"method": "getTokenAccountsByOwner",
"params": [
wallet_address,
{
"mint": TOKEN_CONTRACT
},
{
"encoding": "jsonParsed"
}
]
})
response = requests.post(RPC_URL, headers=headers, data=body)
result = response.json()
if 'result' in result:
if TOKEN_CONTRACT == '0x0':
balance = result['result']['value'] / 1e9 # Convert lamports to SOL
return balance
else:
accounts = result['result']['value']
total_balance = sum(float(account['account']['data']['parsed']['info']['tokenAmount']['uiAmount']) for account in accounts)
return total_balance
else:
return 0
# List to hold the wallet balance data
wallet_data = []
# Iterate over wallets and get token balances with a progress bar
for wallet in tqdm(wallets, desc="Fetching balances", unit="wallet"):
if wallet['isConnected'] and not wallet['isDisabled']:
address = wallet['address']
balance = get_token_balance(address)
wallet_data.append({
"Wallet Address": address,
"Balance": balance
})
# Save the wallet balances to an Excel file
df = pd.DataFrame(wallet_data)
df.to_excel(f"solana_{TOKEN_CONTRACT}.xlsx", index=False)
print(f"Balances have been saved to solana_{TOKEN_CONTRACT}.xlsx")