forked from Larsenvini/EazyChains
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_api_calls.py
64 lines (52 loc) · 1.88 KB
/
clean_api_calls.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
import os
import requests
def clean_account_balance(data: dict) -> dict:
if data.get("status") != "1":
return {"error": data.get("message", "Unexpected error.")}
try:
wei_balance = int(data["result"])
ether_balance = wei_balance / 10**18 # Converter Wei para Ether
return {
"status": "success",
"balance_wei": wei_balance,
"balance_ether": ether_balance
}
except (ValueError, KeyError) as e:
return {"error": f"Failed to parse balance: {str(e)}"}
def clean_last_block(data: dict) -> dict:
if "result" not in data:
return {"error": "Unexpected format in block data."}
try:
block_number = int(data["result"], 16) # Converter hexadecimal para int
return {
"status": "success",
"block_number": block_number
}
except ValueError as e:
return {"error": f"Failed to parse block number: {str(e)}"}
def clean_transactions(data: dict) -> dict:
if data.get("status") != "1":
return {"error": data.get("message", "Unexpected error.")}
try:
transactions = data["result"]
return {
"status": "success",
"transactions": transactions
}
except KeyError as e:
return {"error": f"Failed to parse transactions: {str(e)}"}
def clean_TransactionbyHash(data: dict) -> dict:
if "result" not in data:
return {"error": "Unexpected format in transaction hash."}
try:
result = data["result"]
return {
"Block Number": result["blockNumber"],
"From": result["from"],
"To": result["to"],
"Value": result["value"],
"Gas": result["gas"],
"Gas Price": result["gasPrice"]
}
except (ValueError, KeyError) as e:
return {"error": f"Failed to parse balance: {str(e)}"}