· 5 years ago · May 11, 2020, 12:02 PM
1
2from flask import Flask
3import requests
4from requests.utils import requote_uri
5import json
6import ast
7from flask import jsonify
8import urllib3
9from flask_cors import CORS
10
11
12http = urllib3.PoolManager()
13
14app = Flask(__name__)
15CORS(app)
16
17
18
19base_url = "http://54.243.16.45:4001"
20headers = {
21 'accept': 'application/json',
22}
23
24def get_current_ethereum_price():
25 api = 'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,JPY,EUR?1eef1b3afe86c87ccb9b43f6a7ed8c4483fc5076e99b0f8cecfcae04f054ec75'
26 response = requests.get(api)
27 if response.status_code == 200:
28 data = parse_dictionary(response.content.decode('UTF-8'))
29 price = data.get('USD')
30 return price
31 else:
32 return ('error in parsing eth price')
33
34def get_current_epoch():
35 try:
36 uri = '/eth/v1alpha1/beacon/chainhead'
37 url = base_url+uri
38 response = requests.get(url)
39 if response.status_code == 200:
40 data = response.content.decode('UTF-8')
41 data = ast.literal_eval(data)
42 return int(data.get('finalizedEpoch'))
43 except Exception as e:
44 print (e)
45 return send_error_msg()
46
47
48def send_error_msg():
49 return jsonify({'error_msg':'no response from Prysm api'}), 505
50
51def parse_dictionary(data):
52 return (ast.literal_eval(data))
53
54
55
56def send_sucess_msg(response,**kwargs):
57 if not type(response) == dict:
58 response = parse_dictionary(response)
59 response['message'] = 'Sucess'
60 for k,v in kwargs.items():
61 response[k] = v
62
63 return jsonify(response), 200
64
65@app.route('/attestations')
66def get_attestations():
67 try:
68
69
70 '''
71 Retrieve attestations by block root, slot, or epoch.
72 An attestation is a validator’s vote, weighted by the validator’s balance.
73 Attestations are broadcasted by validators in addition to blocks.
74 '''
75
76 url = base_url+"/eth/v1alpha1/beacon/attestations"
77 current_epoch = str(get_current_epoch())
78 pageSize = 8
79 page = 2
80 list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
81 # pagesize = int(input("ENTER PAGE SIZE: "))
82 # page = int(input("ENTER A PAGE NUMBER: "))
83 printList = [1]
84 for y in range((page - 1) * pageSize, page * pageSize):
85 printList.append(list1[y])
86 print(printList)
87
88 attestations = http.request(
89 'GET',
90 url,
91 fields={
92 'epoch' : current_epoch,
93 'pageSize' : pageSize
94 }
95 )
96
97 if attestations.status == 200:
98 response = attestations.data.decode('UTF-8')
99 additional_data = {
100 'defination' :'An attestation is a validator’s vote, weighted by the validator’s balance. Attestations are broadcasted by validators in addition to blocks.'
101 }
102 return send_sucess_msg(response, **additional_data)
103 else:
104 return send_error_msg()
105 except Exception as e :
106 print(e)
107 return send_error_msg()
108
109
110@app.route('/get_block_stream')
111def get_block_stream():
112 try:
113 api = '/eth/v1alpha1/beacon/blocks/stream'
114 url = base_url+api
115 block_stream = requests.get(url,headers=headers)
116 print (block_stream)
117 except Exception as e :
118 print(e)
119 return send_error_msg()
120
121@app.route('/validators_queue')
122def get_validator_queue():
123 try:
124 uri = '/eth/v1alpha1/validators/queue'
125 url = base_url+uri
126 response = requests.get(url)
127 if response.status_code == 200:
128 data = response.content.decode('UTF-8')
129 data = parse_dictionary(data)
130 return_data = {
131 'public_keys' : data.get('activationPublicKeys'),
132 'count' : len(data)
133 }
134 return send_sucess_msg(return_data)
135 else:
136 return send_error_msg()
137 except Exception as e :
138 print(e)
139 return send_error_msg()
140
141
142#not working
143@app.route('/beacon/beacon_configuration')
144def get_beacon_configuration():
145 try:
146
147 uri = '/eth/v1alpha1/beacon/config'
148 url = base_url+uri
149 response = requests.get(url, headers=headers)
150
151 if response.status_code == 200:
152 data = response.content.decode('UTF-8')
153 return send_sucess_msg(data)
154 else:
155 return send_error_msg()
156
157 except Exception as e :
158 print(e)
159 return send_error_msg()
160
161@app.route('/beacon/get_current_beacon_state')
162def get_current_beacon_state():
163 try:
164 uri = '/eth/v1alpha1/beacon/chainhead'
165 url = base_url+uri
166 response = requests.get(url)
167 if response.status_code == 200:
168 data = response.content.decode('UTF-8')
169 return send_sucess_msg(data)
170 else:
171 return send_error_msg()
172
173 except Exception as e :
174 print(e)
175 return send_error_msg()
176
177
178
179@app.route('/beacon/get_current_chain_state')
180def get_current_chain_state():
181 try:
182 uri = '/eth/v1alpha1/beacon/chainhead'
183 url = base_url+uri
184 response = requests.get(url)
185 if response.status_code == 200:
186 data = response.content.decode('UTF-8')
187 data = parse_dictionary(data)
188 return_data = {
189 'finalizedEpoch' : data.get('finalizedEpoch'),
190 'finalizedSlot' : data.get('finalizedSlot')
191 }
192 price = get_current_ethereum_price()
193 peers_data = node_peers()
194
195
196 additional_data = {
197 'slot_defination' : 'A slot is a chance for a block to be added to the Beacon Chain and shards. A slot is like the block time, but slots can be empty as well',
198 'epoch_defination' : 'Epoch is collection of slots , basically 32 slots i.e 6.4 minutes form one epoch',
199 'price' : price,
200 'peers_defination' : 'Peers are a fundamental element of the network who host ledgers and smart contracts',
201 'peers_count' : len(peers_data.get('peers')),
202 'peers' : peers_data.get('peers')
203 }
204
205 return send_sucess_msg(return_data, **additional_data)
206 else:
207 return send_error_msg()
208 except Exception as e:
209 print (e)
210
211
212@app.route('/validators/validators_list')
213def get_validators():
214 uri = '/eth/v1alpha1/validators'
215 url = base_url+uri
216 validators = http.request(
217 'GET',
218 url,
219 fields={
220 'epoch' : get_current_epoch()
221 }
222 )
223
224 if validators.status == 200:
225 validators = json.loads(validators.data.decode('UTF-8'))
226 additional_data = {
227 'count' : len(validators.get('validatorList'))
228 }
229 return send_sucess_msg(validators, **additional_data)
230
231
232def node_peers():
233 uri = '/eth/v1alpha1/node/peers'
234 url = base_url+uri
235 response = requests.get(url)
236 if response.status_code == 200:
237 data = response.content.decode('UTF-8')
238 return parse_dictionary(data)
239
240
241@app.route('/node/genesis')
242def node_genesis():
243 uri = '/eth/v1alpha1/node/genesis'
244 url = base_url+uri
245 response = requests.get(url)
246 if response.status_code == 200:
247 data = response.content.decode('UTF-8')
248 return send_sucess_msg(data)
249 else:
250 return send_error_msg()
251
252
253@app.route('/node/version')
254def node_version():
255 # uri = 'eth/v1alpha1/node/version'
256 url = 'https://api.prylabs.net/eth/v1alpha1/node/version'
257 response = requests.get(url)
258 if response.status_code == 200:
259 data = response.content.decode('UTF-8')
260 return send_sucess_msg(data)
261 else:
262 return send_error_msg()
263
264
265
266
267
268# Validator info by Publick Key
269
270@app.route('/validator/info/<publicKey>')
271def get_validators_info(publicKey):
272 url = 'https://api.prylabs.net/eth/v1alpha1/validator/status'
273 validators = http.request(
274 'GET',
275 url,
276 fields={
277 'publicKey' : publicKey
278 }
279 )
280
281 if validators.status == 200:
282 status_data = validators.data.decode('UTF-8')
283 status_data = parse_dictionary(status_data)
284 return_data = {
285 'status' : status_data.get('status'),
286 'activationEpoch' : status_data.get('activationEpoch')
287 }
288
289 url = 'https://api.prylabs.net/eth/v1alpha1/validators/balances'
290
291 response = http.request(
292 'GET',
293 url,
294 fields={
295 'publicKeys' : publicKey
296 }
297 )
298
299 if response.status == 200:
300 balance_data = response.data.decode('UTF-8')
301 balance_data = parse_dictionary(balance_data).get('balances')
302 balance_data = balance_data[0]
303
304 balance = int(balance_data.get('balance'))/9000000000
305 balance = str(round(balance, 2)) +" ETH"
306
307 index = balance_data.get('index')
308
309
310 return_data['balance'] = balance
311 return_data['index'] = index
312
313 return send_sucess_msg(return_data)
314 else:
315 return send_error_msg()
316
317
318
319
320
321
322if __name__ == "__main__":
323 app.run(debug=True,host= '0.0.0.0')