forked from crypto-org-chain/chain-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosmoscli.py
301 lines (279 loc) · 8.94 KB
/
cosmoscli.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import json
import tempfile
import requests
from pystarport import cluster, cosmoscli
class CosmosCLI(cosmoscli.CosmosCLI):
def submit_gov_proposal(self, proposal, **kwargs):
rsp = json.loads(
self.raw(
"tx",
"gov",
"submit-proposal",
proposal,
"-y",
home=self.data_dir,
**kwargs,
)
)
if rsp["code"] == 0:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp
def gov_propose_legacy(
self,
proposer,
kind,
proposal,
no_validate=False,
event_query_tx=True,
**kwargs,
):
if kind == "software-upgrade":
rsp = json.loads(
self.raw(
"tx",
"gov",
"submit-legacy-proposal",
kind,
proposal["name"],
"-y",
"--no-validate" if no_validate else None,
from_=proposer,
# content
title=proposal.get("title"),
description=proposal.get("description"),
upgrade_height=proposal.get("upgrade-height"),
upgrade_time=proposal.get("upgrade-time"),
upgrade_info=proposal.get("upgrade-info", "info"),
deposit=proposal.get("deposit"),
# basic
home=self.data_dir,
node=self.node_rpc,
keyring_backend="test",
chain_id=self.chain_id,
**kwargs,
)
)
if rsp["code"] == 0 and event_query_tx:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp
elif kind == "cancel-software-upgrade":
rsp = json.loads(
self.raw(
"tx",
"gov",
"submit-legacy-proposal",
kind,
"-y",
from_=proposer,
# content
title=proposal.get("title"),
description=proposal.get("description"),
deposit=proposal.get("deposit"),
# basic
home=self.data_dir,
node=self.node_rpc,
keyring_backend="test",
chain_id=self.chain_id,
**kwargs,
)
)
if rsp["code"] == 0 and event_query_tx:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp
else:
with tempfile.NamedTemporaryFile("w") as fp:
json.dump(proposal, fp)
fp.flush()
rsp = json.loads(
self.raw(
"tx",
"gov",
"submit-legacy-proposal",
kind,
fp.name,
"-y",
from_=proposer,
# basic
home=self.data_dir,
node=self.node_rpc,
keyring_backend="test",
chain_id=self.chain_id,
**kwargs,
)
)
if rsp["code"] == 0 and event_query_tx:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp
def transfer(
self,
from_,
to,
coins,
generate_only=False,
event_query_tx=True,
**kwargs,
):
default_kwargs = {
"home": self.data_dir,
"keyring_backend": "test",
"chain_id": self.chain_id,
"node": self.node_rpc,
}
rsp = json.loads(
self.raw(
"tx",
"bank",
"send",
from_,
to,
coins,
"-y",
"--generate-only" if generate_only else None,
**(default_kwargs | kwargs),
)
)
if not generate_only and rsp["code"] == 0 and event_query_tx:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp
def sign_tx(self, tx_file, signer):
return json.loads(
self.raw(
"tx",
"sign",
tx_file,
from_=signer,
home=self.data_dir,
keyring_backend="test",
chain_id=self.chain_id,
node=self.node_rpc,
)
)
def sign_tx_json(self, tx, signer, max_priority_price=None):
if max_priority_price is not None:
tx["body"]["extension_options"].append(
{
"@type": "/ethermint.types.v1.ExtensionOptionDynamicFeeTx",
"max_priority_price": str(max_priority_price),
}
)
with tempfile.NamedTemporaryFile("w") as fp:
json.dump(tx, fp)
fp.flush()
return self.sign_tx(fp.name, signer)
def broadcast_tx(self, tx_file, event_query_tx=True, **kwargs):
kwargs.setdefault("broadcast_mode", "sync")
kwargs.setdefault("output", "json")
rsp = json.loads(
self.raw("tx", "broadcast", tx_file, node=self.node_rpc, **kwargs)
)
if event_query_tx and rsp["code"] == 0:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp
def broadcast_tx_json(self, tx, event_query_tx=True, **kwargs):
with tempfile.NamedTemporaryFile("w") as fp:
json.dump(tx, fp)
fp.flush()
return self.broadcast_tx(fp.name, event_query_tx, **kwargs)
def tx_search_rpc(self, events: str):
node_rpc_http = "http" + self.node_rpc.removeprefix("tcp")
rsp = requests.get(
f"{node_rpc_http}/tx_search",
params={
"query": f'"{events}"',
},
).json()
assert "error" not in rsp, rsp["error"]
return rsp["result"]["txs"]
def sign_batch_multisig_tx(
self,
tx_file,
multi_addr,
signer_name,
account_number,
sequence_number,
sigonly=True,
):
r = self.raw(
"tx",
"sign-batch",
"--offline",
"--signature-only" if sigonly else None,
tx_file,
account_number=account_number,
sequence=sequence_number,
from_=signer_name,
multisig=multi_addr,
home=self.data_dir,
keyring_backend="test",
chain_id=self.chain_id,
node=self.node_rpc,
)
return r.decode("utf-8")
def query_host_params(self):
kwargs = {
"node": self.node_rpc,
"output": "json",
}
return json.loads(
self.raw(
"q",
"interchain-accounts",
"host",
"params",
**kwargs,
)
)
def query_params(self, mod):
kwargs = {
"node": self.node_rpc,
"output": "json",
}
return json.loads(
self.raw(
"q",
mod,
"params",
**kwargs,
)
)
class ClusterCLI(cluster.ClusterCLI):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cmd = self.cmd or self.config.get("cmd") or "chain-maind"
def cosmos_cli(self, i=0):
return CosmosCLI(
self.home(i),
self.node_rpc(i),
chain_id=self.chain_id,
cmd=self.cmd,
zemu_address=self.zemu_address,
zemu_button_port=self.zemu_button_port,
)
def submit_gov_proposal(self, proposer, i=0, **kwargs):
return self.cosmos_cli(i).submit_gov_proposal(proposer, **kwargs)
def gov_propose_legacy(
self,
proposer,
kind,
proposal,
i=0,
no_validate=False,
event_query_tx=True,
**kwargs,
):
return self.cosmos_cli(i).gov_propose_legacy(
proposer,
kind,
proposal,
no_validate,
event_query_tx,
**kwargs,
)
def transfer(self, from_, to, coins, i=0, generate_only=False, **kwargs):
return self.cosmos_cli(i).transfer(from_, to, coins, generate_only, **kwargs)
def sign_batch_multisig_tx(self, *args, i=0, **kwargs):
return self.cosmos_cli(i).sign_batch_multisig_tx(*args, **kwargs)
def query_host_params(self, i=0):
return self.cosmos_cli(i).query_host_params()
def query_params(self, mod, i=0):
return self.cosmos_cli(i).query_params(mod)