-
Notifications
You must be signed in to change notification settings - Fork 6
/
awapi.py
153 lines (132 loc) · 5.1 KB
/
awapi.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
import logging
import requests
from requests.exceptions import ReadTimeout
class Account:
HTTP_TIMEOUT = 10
def __init__(self, account_name) -> None:
self.account = account_name
self._session = requests.Session()
self._session.headers.update(
{
'origin': 'https://wax.bloks.io',
'referer': 'https://wax.bloks.io',
}
)
def get_actions(self, pos:int=-1, offset:int=-100) -> dict:
"""
Get list of actions related to the account in the wax blockchain.
Returns dictionary with the list of actions, the current head block, and the last permanent block.
"""
try:
url = 'https://wax.greymass.com/v1/history/get_actions'
json_payload = {
'account_name': self.account,
'pos': pos,
'offset': offset,
}
reply = self._session.post(url, json=json_payload, timeout=Account.HTTP_TIMEOUT)
except ReadTimeout:
url= f'https://wax.eosrio.io/v2/history/get_actions?account={self.account}&skip=0&limit={abs(offset)}&sort=desc'
reply = self._session.get(url, timeout=Account.HTTP_TIMEOUT)
assert reply.status_code == 200
return reply.json()
def get_balance(self, contract:str, currency:str) -> float:
"""
Gets the account balance for the given contract and currency.
Returns the current balance as a float.
"""
json_payload = {
'code': contract,
'account': self.account,
'symbol': currency,
}
try:
url = 'https://wax.greymass.com/v1/chain/get_currency_balance'
reply = self._session.post(url, json=json_payload, timeout=Account.HTTP_TIMEOUT)
except ReadTimeout:
url = 'https://chain.wax.io/v1/chain/get_currency_balance'
reply = self._session.post(url, json=json_payload, timeout=Account.HTTP_TIMEOUT)
assert reply.status_code == 200
balance = float(reply.json()[0].split()[0])
return balance
def get_account(self) -> dict:
"""
Gets the instance account details and returns the data as a dictionary.
"""
json_payload = {
'account_name': self.account,
}
try:
url = 'https://wax.greymass.com/v1/chain/get_account'
reply = self._session.post(url, json=json_payload, timeout=Account.HTTP_TIMEOUT)
except ReadTimeout:
url = 'https://wax.eosrio.io/v1/chain/get_account'
reply = self._session.post(url, json=json_payload, timeout=Account.HTTP_TIMEOUT)
assert reply.status_code == 200
return reply.json()
def get_tokens(self) -> list:
"""
Gets and returns the list of tokens held by the instance account.
"""
try:
url = f'https://wax.eosrio.io/v2/state/get_tokens?account={self.account}'
reply = self._session.get(url, timeout=Account.HTTP_TIMEOUT)
assert reply.status_code == 200
except AssertionError:
return []
else:
data = reply.json()
assert data['account'] == self.account
return data['tokens']
def get_chain_info(self) -> dict:
"""
Gets the current block info and returns it as a dictionary.
"""
try:
url = 'https://chain.wax.io/v1/chain/get_info'
reply = self._session.get(url, timeout=Account.HTTP_TIMEOUT)
assert reply.status_code == 200
except AssertionError:
url = 'https://wax.eosrio.io/v1/chain/get_info'
reply = self._session.get(url, timeout=Account.HTTP_TIMEOUT)
assert reply.status_code == 200
return reply.json()
@property
def last_action(self) -> dict:
return self.get_actions(-1, -1)
@property
def wax_balance(self) -> float:
return self.get_balance(contract='eosio.token', currency='wax')
@property
def tlm_balance(self) -> float:
return self.get_balance(contract='alien.worlds', currency='tlm')
@property
def cpu_usage(self) -> dict:
return self.get_account()['cpu_limit']
@property
def net_usage(self) -> dict:
return self.get_account()['net_limit']
@property
def ram_usage(self) -> dict:
account = self.get_account()
max_ram = account['ram_quota']
use_ram = account['ram_usage']
free_ram = max_ram - use_ram
return {
'used': use_ram,
'available': free_ram,
'max': max_ram,
}
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
aw = Account(input("Enter wax account name: "))
print("ACCOUNT:", aw.account)
print("WAX:", aw.wax_balance)
print("TLM:", aw.tlm_balance)
print("CPU:", aw.cpu_usage)
print("NET:", aw.net_usage)
print("RAM:", aw.ram_usage)
print("\nAccount Data:", aw.get_account())
print("\nLast action:", aw.last_action)
print("\nTokens:", aw.get_tokens())
print("\nChain Info:", aw.get_chain_info())