-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_balance.py
176 lines (154 loc) · 4.57 KB
/
show_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import sqlite3
from datetime import datetime
from tabulate import tabulate
from config import DB_NAME
# Chain settings
CHAINS = {
'ethereum': {
'name': 'Ethereum Mainnet',
'symbol': 'ETH'
},
'avalanche': {
'name': 'Avalanche C-Chain',
'symbol': 'AVAX'
},
'base': {
'name': 'Base Chain',
'symbol': 'ETH'
},
'bsc': {
'name': 'BNB Smart Chain',
'symbol': 'BNB'
},
'polygon': {
'name': 'Polygon',
'symbol': 'MATIC'
},
'arbitrum': {
'name': 'Arbitrum One',
'symbol': 'ETH'
}
}
def setup_database():
"""Set up database tables"""
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
# Balance history table
cursor.execute('''
CREATE TABLE IF NOT EXISTS balance_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
address TEXT,
chain_id TEXT,
balance REAL,
check_date TIMESTAMP,
UNIQUE(address, chain_id, check_date)
)
''')
conn.commit()
conn.close()
def format_datetime(dt):
"""Convert datetime to readable format"""
if isinstance(dt, str):
try:
dt = datetime.fromisoformat(dt)
except:
return dt
return dt.strftime('%Y-%m-%d %H:%M:%S')
def show_latest_balances():
"""Display latest balance status"""
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
# Get latest check date
cursor.execute('SELECT MAX(check_date) FROM balance_history')
latest_date = cursor.fetchone()[0]
if not latest_date:
print("No balance data found")
return
print(f"\n=== Latest Balance Status ({format_datetime(latest_date)}) ===")
# Get balances by address
cursor.execute('''
SELECT address, chain_id, balance
FROM balance_history
WHERE check_date = ?
ORDER BY address, chain_id
''', (latest_date,))
balances = cursor.fetchall()
if balances:
# Display by address
current_address = None
address_balances = []
for address, chain_id, balance in balances:
if balance > 0: # Only show non-zero balances
address_balances.append([
address,
CHAINS[chain_id]['name'],
f"{balance:.8f}",
CHAINS[chain_id]['symbol']
])
if address_balances:
print("\n=== Address Balances ===")
headers = ["Address", "Chain", "Balance", "Currency"]
print(tabulate(address_balances, headers=headers, tablefmt="grid"))
# Total balance by chain
cursor.execute('''
SELECT chain_id, SUM(balance) as total
FROM balance_history
WHERE check_date = ?
GROUP BY chain_id
HAVING total > 0
ORDER BY total DESC
''', (latest_date,))
totals = cursor.fetchall()
if totals:
print("\n=== Total Balance by Chain ===")
headers = ["Chain", "Total Balance", "Currency"]
table_data = [
[CHAINS[chain_id]['name'], f"{total:.8f}", CHAINS[chain_id]['symbol']]
for chain_id, total in totals
]
print(tabulate(table_data, headers=headers, tablefmt="grid"))
conn.close()
def show_balance_history():
"""Display balance history"""
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
# Get all balance history
cursor.execute('''
SELECT
address,
chain_id,
balance,
check_date
FROM balance_history
WHERE balance > 0
ORDER BY check_date DESC, address, chain_id
''')
history = cursor.fetchall()
if history:
print("\n=== Balance History ===")
headers = ["Check Date", "Address", "Chain", "Balance", "Currency"]
table_data = [
[
format_datetime(date),
addr,
CHAINS[chain_id]['name'],
f"{balance:.8f}",
CHAINS[chain_id]['symbol']
]
for addr, chain_id, balance, date in history
]
print(tabulate(table_data, headers=headers, tablefmt="grid"))
else:
print("\nNo balance history found")
conn.close()
def main():
"""Main execution function"""
try:
setup_database()
show_latest_balances()
print("\n" + "="*50)
show_balance_history()
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__ == "__main__":
main()