forked from ref-finance/indexer-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnear_rpc_provider.py
100 lines (76 loc) · 3.55 KB
/
near_rpc_provider.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
import requests
import base64
import json
class JsonProviderError(Exception):
pass
class JsonProvider(object):
def __init__(self, rpc_addr):
if isinstance(rpc_addr, tuple):
self._rpc_addr = "http://%s:%s" % rpc_addr
else:
self._rpc_addr = rpc_addr
def rpc_addr(self):
return self._rpc_addr
def json_rpc(self, method, params, timeout=2):
j = {
'method': method,
'params': params,
'id': 'dontcare',
'jsonrpc': '2.0'
}
r = requests.post(self.rpc_addr(), json=j, timeout=timeout)
r.raise_for_status()
content = json.loads(r.content)
if "error" in content:
raise JsonProviderError(content["error"])
return content["result"]
def send_tx(self, signed_tx):
return self.json_rpc('broadcast_tx_async', [base64.b64encode(signed_tx).decode('utf8')])
def send_tx_and_wait(self, signed_tx, timeout):
return self.json_rpc('broadcast_tx_commit', [base64.b64encode(signed_tx).decode('utf8')], timeout=timeout)
def get_status(self):
r = requests.get("%s/status" % self.rpc_addr(), timeout=2)
r.raise_for_status()
return json.loads(r.content)
def get_validators(self):
return self.json_rpc('validators', [None])
def query(self, query_object):
return self.json_rpc('query', query_object)
def get_account(self, account_id, finality='optimistic'):
return self.json_rpc('query', {"request_type": "view_account", "account_id": account_id, "finality": finality})
def get_access_key_list(self, account_id, finality='optimistic'):
return self.json_rpc('query', {"request_type": "view_access_key_list", "account_id": account_id, "finality": finality})
def get_access_key(self, account_id, public_key, finality='optimistic'):
return self.json_rpc('query', {"request_type": "view_access_key", "account_id": account_id,
"public_key": public_key, "finality": finality})
def view_call(self, account_id, method_name, args, finality='optimistic'):
return self.json_rpc('query', {"request_type": "call_function", "account_id": account_id,
"method_name": method_name, "args_base64": base64.b64encode(args).decode('utf8'), "finality": finality})
def get_block(self, block_id):
return self.json_rpc('block', [block_id])
def get_chunk(self, chunk_id):
return self.json_rpc('chunk', [chunk_id])
def get_tx(self, tx_hash, tx_recipient_id):
return self.json_rpc('tx', [tx_hash, tx_recipient_id])
def get_changes_in_block(self, changes_in_block_request):
return self.json_rpc('EXPERIMENTAL_changes_in_block', changes_in_block_request)
if __name__ == "__main__":
# conn = JsonProvider("https://rpc.testnet.near.org")
# status = conn.get_status()
# # print(status)
# ret = conn.view_call("ref-farming.testnet", "get_number_of_farms", b"")
# # print(ret["result"])
# a = "".join([chr(x) for x in ret["result"]])
# print(a)
# print()
# ret = conn.view_call("ref-farming.testnet", "list_farms", b'{"from_index": 0, "limit": 100}')
# # print(ret["result"])
# b = "".join([chr(x) for x in ret["result"]])
# # print(b)
# c = json.loads(b)
# for item in c:
# print(item)
conn = JsonProvider("https://rpc.mainnet.near.org")
ret = conn.view_call("ref-finance.near", "get_number_of_pools", b'')
b = "".join([chr(x) for x in ret["result"]])
print(b)