-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidators.py
286 lines (208 loc) Β· 11.9 KB
/
validators.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
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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from datetime import datetime
from constants.constants import (
MAX_VALIDATOR_COUNT,
UBASE,
ULUNA,
USER_ACTION_QUIT,
USER_ACTION_VALIDATOR_DELEGATE,
USER_ACTION_VALIDATOR_LIST_UNDELEGATIONS,
USER_ACTION_VALIDATOR_UNDELEGATE,
USER_ACTION_VALIDATOR_SWITCH,
WITHDRAWAL_REMAINDER
)
from classes.common import (
check_version,
get_user_choice,
)
from classes.delegation_transaction import delegate_to_validator, undelegate_from_validator, switch_validator
from classes.transaction_core import TransactionResult
from classes.wallet import UserParameters
from classes.wallets import UserWallets
from classes.validators import Validators
from terra_classic_sdk.core.coin import Coin
def main():
# Check if there is a new version we should be using
check_version()
# Get the user wallets
wallets = UserWallets()
user_wallets = wallets.loadUserWallets(get_delegations = True)
if len(user_wallets) > 0:
print (f'You have these wallets available:')
wallet, answer = wallets.getUserSinglechoice("Select a wallet number 1 - " + str(len(user_wallets)) + ", 'X' to continue, or 'Q' to quit: ", True)
if answer == USER_ACTION_QUIT:
print (' π Exiting...\n')
exit()
else:
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()
# Get the desired actions
print ('\nWhat action do you want to take?\n')
print (' (D) Delegate to a validator')
print (' (U) Undelegate coins from a validator')
print (' (S) Switch validators')
print (' (L) List undelegations in progress')
print (' (Q) Quit')
print ('')
user_action = get_user_choice(' β Pick an option: ', [
USER_ACTION_VALIDATOR_DELEGATE,
USER_ACTION_VALIDATOR_LIST_UNDELEGATIONS,
USER_ACTION_VALIDATOR_UNDELEGATE,
USER_ACTION_VALIDATOR_SWITCH,
USER_ACTION_QUIT
])
if user_action == USER_ACTION_QUIT:
print (' π Exiting...\n')
exit()
print ('\nGetting available validators - please wait...')
validators = Validators()
validators.create()
sorted_validators:dict = validators.sorted_validators
if len(sorted_validators) == 0:
print (' π No validators could be retrieved - perhaps there are network issues?')
exit()
# Not required for listing undelegations, but no harm having it here:
delegations:dict = wallet.delegations
# Set up the basic user params object
user_params:UserParameters = UserParameters()
user_params.percentages_allowed = True
if ULUNA in wallet.balances:
user_params.target_amount = wallet.formatUluna(wallet.balances[ULUNA], ULUNA)
user_params.target_denom = ULUNA
if user_action == USER_ACTION_VALIDATOR_DELEGATE:
if ULUNA not in wallet.balances or wallet.balances[ULUNA] == 0:
print (' π This wallet has no LUNC available to delegate.\n')
print (' π Exiting...\n')
exit()
print (f'Select a validator to delegate to:')
max_number:int = len(sorted_validators)
if max_number > MAX_VALIDATOR_COUNT:
max_number = MAX_VALIDATOR_COUNT
user_validator, answer = validators.getValidatorSingleChoice("Select a validator number 1 - " + str(max_number) + ", 'X' to continue, or 'Q' to quit: ", sorted_validators, [], delegations)
if answer == USER_ACTION_QUIT:
print (' π Exiting...\n')
exit()
print (f"The {wallet.name} wallet holds {wallet.formatUluna(wallet.balances[ULUNA], ULUNA, True)}")
print (f"NOTE: A minimum amount of {WITHDRAWAL_REMAINDER} LUNC will be retained for future transactions.")
# Update the user params object before we use it
user_params.max_number = float(wallet.formatUluna(wallet.balances[ULUNA], ULUNA))
delegated_uluna:int = int(float(wallet.getUserNumber('How much are you delegating? ', user_params)))
if delegated_uluna == 0:
print (' π Delegated amount is zero, exiting...\n')
print (' π Exiting...\n')
exit()
print (f"You are about to delegate {wallet.formatUluna(delegated_uluna, ULUNA, True)} to {user_validator['moniker']}.")
complete_transaction = get_user_choice(' β Do you want to continue? (y/n) ', [])
if complete_transaction == False:
print (' π Exiting...\n')
exit()
print (f'Delegating {wallet.formatUluna(delegated_uluna, ULUNA, True)}...')
delegated_coin:Coin = wallet.createCoin(delegated_uluna, ULUNA)
transaction_result:TransactionResult = delegate_to_validator(wallet, user_validator['operator_address'], delegated_coin)
transaction_result.showResults()
if user_action == USER_ACTION_VALIDATOR_UNDELEGATE:
print (f'Select a validator to undelegate from:')
# Get the validators currently being used
filter_list:list = []
for validator in delegations:
filter_list.append(validator)
if len(filter_list) == 0:
print (' π This wallet has no active validators with delegations.\n')
print (' π Exiting...\n')
exit()
if ULUNA not in wallet.balances or wallet.balances[ULUNA] == 0:
print (' π This wallet has no LUNC available to cover the undelegation fees.\n')
print (' π Exiting...\n')
exit()
user_validator, answer = validators.getValidatorSingleChoice("Select a validator number 1 - " + str(len(filter_list)) + ", 'X' to continue, or 'Q' to quit: ", sorted_validators, filter_list, delegations)
if answer == USER_ACTION_QUIT:
print (' π Exiting...\n')
exit()
available_undelegation_uluna:int = delegations[user_validator['moniker']]['balance_amount']
print (f"The {wallet.name} wallet has {wallet.formatUluna(available_undelegation_uluna, ULUNA, True)} available to be undelegated.")
print (f"NOTE: You can send the entire value of this delegation by typing '100%' - no minimum amount will be retained.")
# Update the user params object before we use it
user_params.max_number = float(wallet.formatUluna(available_undelegation_uluna, ULUNA, False))
user_params.target_amount = user_params.max_number
undelegated_uluna:str = wallet.getUserNumber('How much are you undelegating? ', user_params)
print (f"You are about to undelegate {wallet.formatUluna(undelegated_uluna, ULUNA, True)} from {user_validator['moniker']}.")
print (' ποΈ Undelegated funds will not be available for 21 days.')
complete_transaction = get_user_choice(' β Do you want to continue? (y/n) ', [])
if complete_transaction == False:
print (' π Exiting...\n')
exit()
print (f'Undelegating {wallet.formatUluna(undelegated_uluna, ULUNA, True)}...')
undelegated_coin:Coin = wallet.createCoin(undelegated_uluna, ULUNA)
transaction_result:TransactionResult = undelegate_from_validator(wallet, user_validator['operator_address'], undelegated_coin)
transaction_result.showResults()
if user_action == USER_ACTION_VALIDATOR_SWITCH:
# Get the validators currently being used
filter_list:list = []
for validator in delegations:
filter_list.append(validator)
if len(filter_list) == 0:
print (' π This wallet has no active validators with delegations.\n')
print (' π Exiting...\n')
exit()
if ULUNA not in wallet.balances or wallet.balances[ULUNA] == 0:
print (' π This wallet has no LUNC available to cover the delegation switch fees.\n')
print (' π Exiting...\n')
exit()
print (f'Select a validator to delegate switch FROM:')
from_validator, answer = validators.getValidatorSingleChoice("Select a validator number 1 - " + str(len(filter_list)) + ", 'X' to continue, or 'Q' to quit: ", sorted_validators, filter_list, delegations)
if answer == USER_ACTION_QUIT:
print (' π Exiting...\n')
exit()
print (f'Select a validator to delegate switch TO:')
max_number:int = len(sorted_validators)
if max_number > MAX_VALIDATOR_COUNT:
max_number = MAX_VALIDATOR_COUNT
to_validator, answer = validators.getValidatorSingleChoice("Select a validator number 1 - " + str(max_number) + ", 'X' to continue, or 'Q' to quit: ", sorted_validators, [], delegations)
if answer == USER_ACTION_QUIT:
print (' π Exiting...\n')
exit()
total_delegated_uluna = delegations[from_validator['moniker']]['balance_amount']
print (f"The {from_validator['moniker']} wallet holds {wallet.formatUluna(total_delegated_uluna, ULUNA, True)}")
print (f"NOTE: You can switch the entire value of this delegation by typing '100%' - no minimum amount will be retained.")
# Update the user params object before we use it
user_params.max_number = float(wallet.formatUluna(total_delegated_uluna, ULUNA, False))
user_params.target_amount = user_params.max_number
switched_uluna:float = wallet.getUserNumber('How much are you switching? ', user_params)
print (f"You are about to switch {wallet.formatUluna(switched_uluna, ULUNA, True)} from {from_validator['moniker']} and move it to {to_validator['moniker']}.")
complete_transaction = get_user_choice(' β Do you want to continue? (y/n) ', [])
if complete_transaction == False:
print (' π Exiting...\n')
exit()
print (f'Redelegating {wallet.formatUluna(switched_uluna, ULUNA, True)}...')
delegated_coin:Coin = wallet.createCoin(switched_uluna, ULUNA)
transaction_result:TransactionResult = switch_validator(wallet, to_validator['operator_address'], from_validator['operator_address'], delegated_coin)
transaction_result.showResults()
if user_action == USER_ACTION_VALIDATOR_LIST_UNDELEGATIONS:
# Get the validator list and the undelegations in progress
validator_list:dict = validators.validators_by_address
undelegations:dict = wallet.undelegations
today:datetime = datetime.now().astimezone()
print ('')
if len (undelegations) > 0:
for undelegation in undelegations:
if undelegation == UBASE:
print ('BASE')
for entry in undelegations[undelegation]['entries']:
# At 9:10pm 21st Feb, I undelegated 2 BASE
#Tx hash: 3061B90D40749DB73DB4DC735BDDDD5F5A1680E2100D5DEDD319FB7F23DD5875
finish_day = datetime.strptime(entry['completion_time'], '%d/%m/%Y').astimezone()
days_until = (finish_day - today).days + 1
print (f"{wallet.formatUluna(entry['balance'], UBASE, True)} becomes available in {days_until} days (midnight UTC on {finish_day.year}-{finish_day.month}-{finish_day.day})")
else:
print (validator_list[undelegations[undelegation]['validator_address']]['moniker'])
for entry in undelegations[undelegation]['entries']:
finish_day = entry['completion_time']
days_until = (finish_day - today).days
print (f"{wallet.formatUluna(entry['balance'], ULUNA, True)} becomes available in {days_until} days ({finish_day})")
else:
print ('No undelegations are currently in progress')
print ('')
if __name__ == "__main__":
""" This is executed when run from the command line """
main()