-
Notifications
You must be signed in to change notification settings - Fork 9
/
node_calls.py
101 lines (92 loc) · 3.61 KB
/
node_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
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
"""
{cli_name} utility functions
- query bank balance
- query tx
- node status
- tx bank send
"""
import json
import subprocess
import logging
OUTPUT_TYPE_FLAG = '--output=json'
async def check_address(address: str, node_home: str = '~/.gaia', cli_name: str = 'gaiad'):
"""
{cli_name} keys parse <address>
"""
check = subprocess.run([cli_name, "keys", "parse",
f"{address}",
f'--home={node_home}',
OUTPUT_TYPE_FLAG],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
try:
check.check_returncode()
response = json.loads(check.stdout[:-1])
return response
except subprocess.CalledProcessError as cpe:
output = str(check.stderr).split('\n', maxsplit=1)[0]
logging.error("Called Process Error: %s, stderr: %s", cpe, output)
raise cpe
except IndexError as index_error:
logging.error('Parsing error on address check: %s', index_error)
raise index_error
async def get_balance_list(address: str, node: str, node_home: str = '~/.gaia', cli_name: str = 'gaiad'):
"""
{cli_name} query bank balances <address> <node> <chain-id>
"""
balance = subprocess.run([cli_name, "query", "bank", "balances",
f"{address}",
f'--node={node}',
f'--home={node_home}',
OUTPUT_TYPE_FLAG],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
try:
balance.check_returncode()
response = json.loads(balance.stdout)
return response['balances']
except subprocess.CalledProcessError as cpe:
output = str(balance.stderr).split('\n', maxsplit=1)[0]
logging.error("Called Process Error: %s, stderr: %s", cpe, output)
raise cpe
except IndexError as index_error:
logging.error('Parsing error on balance request: %s', index_error)
raise index_error
async def tx_send(request: dict, cli_name: str = 'gaiad'):
"""
The request dictionary must include these keys:
- "sender"
- "recipient"
- "amount"
- "fees"
- "node"
- "chain_id"
{cli_name} tx bank send <from address> <to address> <amount>
<fees> <node> <chain-id>
--keyring-backend=test -y
"""
tx_node = subprocess.run([cli_name, 'tx', 'bank', 'send',
f'{request["sender"]}',
f'{request["recipient"]}',
f'{request["amount"]}',
f'--fees={request["fees"]}',
f'--node={request["node"]}',
f'--chain-id={request["chain_id"]}',
f'--home={request["node_home"]}',
'--keyring-backend=test',
OUTPUT_TYPE_FLAG,
'-y'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
try:
tx_node.check_returncode()
response = json.loads(tx_node.stdout)
return response['txhash']
except subprocess.CalledProcessError as cpe:
output = str(tx_node.stderr).split('\n', maxsplit=1)[0]
logging.error("%s[%s]", cpe, output)
raise cpe
except (TypeError, KeyError) as err:
output = tx_node.stderr
logging.critical(
'Could not read %s in tx response: %s', err, output)
raise err