-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbalances.py
More file actions
301 lines (219 loc) · 11.7 KB
/
balances.py
File metadata and controls
301 lines (219 loc) · 11.7 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import copy
from constants.constants import (
BASIC_COIN_LOOKUP,
FULL_COIN_LOOKUP,
ULUNA
)
from classes.common import (
check_database,
check_version,
divide_raw_balance,
get_user_choice
)
from classes.wallets import UserWallets
from classes.wallet import UserWallet
def widestWalletName(user_wallets:UserWallets) -> int:
longest_name: int = 0
for wallet_name in user_wallets:
if len(wallet_name) > longest_name:
longest_name = len(wallet_name)
return longest_name
def main():
# Check if there is a new version we should be using
check_version()
check_database()
# Get the user wallets. We'll be getting the balances futher on down.
user_wallets:dict = UserWallets().loadUserWallets(get_balances = False)
if len(user_wallets) == 0:
print (" 🛑 This password couldn't decrypt any wallets. Make sure it is correct, or rebuild the wallet list by running the configure_user_wallet.py script again.\n")
exit()
just_main_coins:bool = get_user_choice(' ❓ Show just LUNC and USTC? (y/n) ', [])
if just_main_coins == True:
coin_lookup = BASIC_COIN_LOOKUP
print ('\n 🕐 Getting the balances for just LUNC and USTC in your wallets, please wait...')
else:
coin_lookup = FULL_COIN_LOOKUP
print ('\n 🕐 Getting the balances for all coins in your wallets, please wait...')
balance_coins = {}
label_widths = []
# These are the default widths of the first 4 columns
# We need to go through each validator find its name and width
label_widths.append(len('Coin'))
label_widths.append(len('Wallet'))
label_widths.append(len('Value'))
label_widths.append(len('Available'))
label_widths.append(len('Delegated'))
# First, create a template of all the validators
validator_template:dict = {'Available': '', 'Delegated': ''}
wallet_count: int = 0
max_padding: int = widestWalletName(user_wallets)
for wallet_name in user_wallets:
wallet_count += 1
update_text: str = f'{wallet_name} {wallet_count}/{len(user_wallets)}' + (' ' * max_padding)
print (f'\r {update_text}', end='\r')
wallet:UserWallet = user_wallets[wallet_name]
delegations:dict = wallet.getDelegations()
if delegations is not None:
for validator in delegations:
if validator not in validator_template:
if int(delegations[validator]['balance_amount']) > 0 and len(delegations[validator]['rewards']) > 0:
validator_template.update({validator: ''})
# The default width is zero until we find out what the maximum width/value is:
label_widths.append(0)
# Then, get all the coins we'll be charting (column 1)
coin_denoms:list = []
for wallet_name in user_wallets:
wallet:UserWallet = user_wallets[wallet_name]
wallet.getBalances()
if len(wallet_name) > label_widths[1]:
label_widths[1] = len(wallet_name)
for denom in wallet.balances:
raw_amount:float = divide_raw_balance(wallet.balances[denom], denom)
denom:str = wallet.denomTrace(denom)
if just_main_coins == True and denom in BASIC_COIN_LOOKUP:
if denom not in coin_denoms:
coin_denoms.append(denom)
elif just_main_coins == False:
if denom not in coin_denoms:
coin_denoms.append(denom)
for denom in wallet.balances:
raw_amount:float = divide_raw_balance(wallet.balances[denom], denom)
denom:str = wallet.denomTrace(denom)
amount:str = ("%.6f" % (raw_amount)).rstrip('0').rstrip('.')
if float(amount) > 0:
if denom in coin_lookup:
coin_denom = copy.deepcopy(coin_lookup[denom])
if len(coin_denom) > label_widths[0]:
label_widths[0] = len(coin_denom)
if coin_denom not in balance_coins:
balance_coins[coin_denom] = {}
if wallet_name not in balance_coins[coin_denom]:
balance_coins[coin_denom].update({wallet_name: validator_template})
cur_vals:dict = copy.deepcopy(balance_coins[coin_denom][wallet_name])
cur_vals.update({'Available': amount})
if len(str(amount)) > label_widths[3]:
label_widths[3] = len(str(amount))
# Get the total number of delegations here and populate label_widths[3]
cur_wallets:dict = copy.deepcopy(balance_coins[coin_denom])
cur_wallets.update({wallet_name: cur_vals})
balance_coins.update({coin_denom: cur_wallets})
# Get the delegations on this wallet
delegations:dict = wallet.delegations
# Keep track of the current delegated amount
delegated_amount:float = 0
if delegations is not None:
for validator in delegations:
for denom in delegations[validator]['rewards']:
if denom == ULUNA:
raw_amount = divide_raw_balance(delegations[validator]['balance_amount'], denom)
delegated_amount += float(("%.6f" % (raw_amount)).rstrip('0').rstrip('.'))
raw_amount:float = divide_raw_balance(delegations[validator]['rewards'][denom], denom)
amount:str = ("%.6f" % (raw_amount)).rstrip('0').rstrip('.')
if denom in coin_lookup:
if float(amount) > 0:
coin_denom = copy.deepcopy(coin_lookup[denom])
if coin_denom not in balance_coins:
balance_coins[coin_denom] = {}
if wallet_name not in balance_coins[coin_denom]:
balance_coins[coin_denom].update({wallet_name: validator_template})
cur_vals:dict = copy.deepcopy(balance_coins[coin_denom][wallet_name])
if denom == ULUNA:
cur_vals.update({'Delegated': delegated_amount})
else:
cur_vals.update({'Delegated': ''})
cur_vals.update({validator: amount})
cur_wallets:dict = copy.deepcopy(balance_coins[coin_denom])
cur_wallets.update({wallet_name: cur_vals})
balance_coins.update({coin_denom: cur_wallets})
# Find the validator column that this applies to:
val_count = 0
for val_name in validator_template:
if val_name == validator:
if len(str(amount)) > label_widths[3 + val_count]:
label_widths[3 + val_count] = len(str(amount))
break
val_count += 1
# Update the delegation column width:
if len(str(delegated_amount)) > label_widths[4]:
label_widths[4] = len(str(delegated_amount))
# Go and get all the prices in one request:
coin_prices:dict = wallet.getCoinPrice(coin_denoms)
# Figure out the biggest total
for coin_type in balance_coins:
for wallet_name in balance_coins[coin_type]:
denom_total:float = 0
if balance_coins[coin_type][wallet_name]['Available'] != '':
denom_total = float(balance_coins[coin_type][wallet_name]['Available'])
if balance_coins[coin_type][wallet_name]['Delegated'] != '':
denom_total += float(balance_coins[coin_type][wallet_name]['Delegated'])
# Get this coin's technical name (ie, uluna)
this_coin:str = list(FULL_COIN_LOOKUP.keys())[list(FULL_COIN_LOOKUP.values()).index(coin_type)]
if this_coin in coin_prices:
# Get the formatted value
denom_value:str = "${:,.2f}".format((denom_total) * coin_prices[this_coin])
if len(denom_value) > label_widths[2]:
label_widths[2] = len(denom_value)
padding_str:str = ' ' * 100
if label_widths[0] > len('Coin'):
header_string = ' Coin' + padding_str[0:label_widths[0] - len('Coin')] + ' |'
else:
header_string = ' Coin |'
if label_widths[1] > len('Wallet'):
header_string += ' Wallet ' + padding_str[0:label_widths[1] - len('Wallet')] + '|'
else:
header_string += ' Wallet |'
if label_widths[2] > len('Value'):
header_string += ' Value ' + padding_str[0:label_widths[2] - len('Value')] + '|'
else:
header_string += ' Value |'
val_count:int = 1
for validator in validator_template:
if label_widths[2 + val_count] >= len(validator):
header_string += ' ' + validator + ' ' + padding_str[0:label_widths[2 + val_count] - len(validator)] + '|'
else:
header_string += ' ' + validator[0:label_widths[2 + val_count] - len(validator)] + ' |'
val_count += 1
horizontal_spacer:str = '-' * len(header_string)
# Sort the balance coins into alphabetical order
sorted_coins:dict = dict(sorted(balance_coins.items()))
body_string:str = ''
for coin_type in sorted_coins:
current_coin = ' ' + coin_type + padding_str[0:label_widths[0] - len(coin_type)] + ' |'
body_string += current_coin
first:bool = True
for wallet_name in balance_coins[coin_type]:
if first == True:
body_string += ' ' + ((wallet_name + padding_str)[0:label_widths[1]]) + ' |'
else:
body_string += padding_str[0:len(current_coin) - 1] + '| ' + ((wallet_name + padding_str)[0:label_widths[1]]) + ' |'
# Get this coin's technical name (ie, uluna)
this_coin:str = list(FULL_COIN_LOOKUP.keys())[list(FULL_COIN_LOOKUP.values()).index(coin_type)]
# Add up the total amount we have for this wallet
denom_total:float = 0
if balance_coins[coin_type][wallet_name]['Available'] != '':
denom_total = float(balance_coins[coin_type][wallet_name]['Available'])
if balance_coins[coin_type][wallet_name]['Delegated'] != '':
denom_total += float(balance_coins[coin_type][wallet_name]['Delegated'])
# Format it nicely
if this_coin in coin_prices:
denom_value:str = "${:,.2f}".format((denom_total) * coin_prices[this_coin])
else:
denom_value:str = '---'
body_string += ' ' + ((denom_value + padding_str)[0:label_widths[2]]) + ' |'
val_count:int = 1
for validator in balance_coins[coin_type][wallet_name]:
body_string += ' ' + (((str(balance_coins[coin_type][wallet_name][validator])) + padding_str)[0:label_widths[2 + val_count]]) + ' |'
val_count += 1
body_string += '\n'
first = False
body_string += horizontal_spacer + '\n'
print ('\n')
print (horizontal_spacer)
print (header_string)
print (horizontal_spacer)
print (body_string)
if __name__ == "__main__":
""" This is executed when run from the command line """
main()