-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcosmos_like_utils.py
137 lines (116 loc) · 4.35 KB
/
cosmos_like_utils.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
import emoji
import os
import requests
from dotenv import load_dotenv
load_dotenv()
# region public functions
def get_validator_status(project_name: str):
if project_name == 'Desmos':
endpoint = os.getenv('DESMOS_LCD_ENDPOINT')
validator_address = os.getenv('DESMOS_VALIDATOR_ADDRESS')
return _get_validator_status_rest(endpoint, validator_address)
if project_name == 'Bluzelle':
endpoint = os.getenv('BLUZELLE_GRAPHQL_ENDPOINT')
validator_address = os.getenv('BLUZELLE_VALIDATOR_ADDRESS')
return _get_validator_status_graphql(endpoint, validator_address)
else:
return 'Not valid project name.'
def is_validator_active(project_name: str):
if project_name == 'Desmos':
endpoint = os.getenv('DESMOS_LCD_ENDPOINT')
validator_address = os.getenv('DESMOS_VALIDATOR_ADDRESS')
return _is_validator_active_rest(endpoint, validator_address)
if project_name == 'Bluzelle':
endpoint = os.getenv('BLUZELLE_GRAPHQL_ENDPOINT')
validator_address = os.getenv('BLUZELLE_VALIDATOR_ADDRESS')
return _is_validator_active_graphql(endpoint, validator_address)
# endregion
# region private functions
def _is_validator_active_rest(endpoint: str, validator_address: str):
url = endpoint + '/staking/validators/' + validator_address
r = requests.get(url)
r_json = r.json()
jailed = r_json['result']['jailed']
status_code = r_json['result']['status']
return _is_validator_active(jailed, status_code)
def _is_validator_active(jailed: bool, status: int):
if jailed is False and status == 2:
return True
else:
return False
def _get_validator_status_rest(endpoint: str, validator_address: str):
url = endpoint + '/staking/validators/' + validator_address
r = requests.get(url)
r_json = r.json()
jailed = r_json['result']['jailed']
status_code = r_json['result']['status']
status = map_status(status_code)
if _is_validator_active(jailed, status_code):
return emoji.emojize('Validator is active. :white_check_mark:')
else:
return emoji.emojize('Validator is inactive, please check. :x: ') \
+ 'Jailed: {}, Status: {}'.format(jailed, status)
def _is_validator_active_graphql(endpoint: str, validator_address: str):
query = """
{
validators {
valoper
moniker
jailed
}
}
"""
r = requests.post(endpoint, json={'query': query})
r_json = r.json()
validators = r_json['data']['validators']
for validator in validators:
if validator['valoper'] == validator_address:
if not validator['jailed']:
return True
else:
return False
return False
def _get_validator_status_graphql(endpoint: str, validator_address: str):
query = """
{
validators {
valoper
moniker
jailed
}
}
"""
r = requests.post(endpoint, json={'query': query})
r_json = r.json()
validators = r_json['data']['validators']
for validator in validators:
if validator['valoper'] == validator_address:
if not validator['jailed']:
return emoji.emojize('Validator is active. :white_check_mark:')
else:
return emoji.emojize('Validator is inactive, please check. :x: ') \
+ 'Jailed: {}'.format(validator['jailed'])
return emoji.emojize('Validator not found. :cry:')
# endregion
# Todo: reorganize the following functions.
def request_faucet(project_name: str):
if project_name == 'Desmos':
endpoint = os.getenv('DESMOS_FAUCET_ENDPOINT')
validator_address = os.getenv('DESMOS_ACCOUNT_ADDRESS')
else:
return 'Not valid project name.'
url = endpoint + '/airdrop'
r = requests.post(url, json={'address': validator_address})
if is_faucet_request_success(r.json()):
return emoji.emojize('Request faucet succeed. :moneybag:') \
+ 'Tx Hash: {}'.format(r.json()['txhash'])
else:
return emoji.emojize('Request faucet failed, try it later. :cry:')
def map_status(status: int):
return {
0: 'Unbonded',
1: 'Unbonding',
2: 'Active'
}.get(status, 'Unbonded')
def is_faucet_request_success(response_json):
return response_json.get('txhash') is not None