-
Notifications
You must be signed in to change notification settings - Fork 19
/
how_to_unstake.py
157 lines (125 loc) · 4.13 KB
/
how_to_unstake.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
import argparse
import asyncio
import os
import pathlib
import pycspr
from pycspr import NodeRpcClient as NodeClient
from pycspr import NodeRpcConnectionInfo as NodeConnectionInfo
from pycspr.types.crypto import KeyAlgorithm
from pycspr.types.crypto import PrivateKey
from pycspr.types.node import Deploy
from pycspr.types.cl import CLV_URef
# Path to CCTL assets.
_PATH_TO_CCTL_ASSETS = pathlib.Path(os.getenv("CCTL")) / "assets"
# CLI argument parser.
_ARGS = argparse.ArgumentParser("Demo illustrating how to unstake CSPR tokens as a validator.")
# CLI argument: path to validator secret key - defaults to CCTL user 1.
_ARGS.add_argument(
"--validator-secret-key-path",
default=_PATH_TO_CCTL_ASSETS / "nodes" / "node-1" / "keys" / "secret_key.pem",
dest="path_to_validator_secret_key",
help="Path to validator's secret_key.pem file.",
type=str,
)
# CLI argument: type of validator secret key - defaults to ED25519.
_ARGS.add_argument(
"--validator-secret-key-type",
default=KeyAlgorithm.ED25519.name,
dest="type_of_validator_secret_key",
help="Type of validator's secret key.",
type=str,
)
# CLI argument: path to session code wasm binary - defaults to CCTL bin/wasm/withdraw_bid.wasm.
_ARGS.add_argument(
"--path-to-wasm",
default=_PATH_TO_CCTL_ASSETS / "bin" / "withdraw_bid.wasm",
dest="path_to_wasm",
help="Path to withdraw_bid.wasm file.",
type=str,
)
# CLI argument: amount to unstake, i.e. unbond, from the network.
_ARGS.add_argument(
"--amount",
default=1000000000000001,
dest="amount",
help="Amount to unbond.",
type=int,
)
# CLI argument: name of target chain - defaults to CCTL chain.
_ARGS.add_argument(
"--chain",
default="cspr-dev-cctl",
dest="chain_name",
help="Name of target chain.",
type=str,
)
# CLI argument: host address of target node - defaults to CCTL node 1.
_ARGS.add_argument(
"--node-host",
default="localhost",
dest="node_host",
help="Host address of target node.",
type=str,
)
# CLI argument: Node API JSON-RPC port - defaults to 11101 @ CCTL node 1.
_ARGS.add_argument(
"--node-port-rpc",
default=11101,
dest="node_port_rpc",
help="Node API JSON-RPC port. Typically 7777 on most nodes.",
type=int,
)
async def _main(args: argparse.Namespace):
"""Main entry point.
:param args: Parsed command line arguments.
"""
print("-" * 74)
print("PYCSPR :: How To Unstake")
print("")
print("Illustrates usage of pycspr.create_validator_auction_bid_withdrawal function.")
print("-" * 74)
# Set node client.
client: NodeClient = _get_client(args)
# Set validator key.
validator: PrivateKey = pycspr.parse_private_key(
args.path_to_validator_secret_key,
args.type_of_validator_secret_key,
)
# Set validator unbond purse.
validator_purse_uref: CLV_URef = \
await client.get_account_main_purse_uref(validator.account_key)
# Set deploy.
deploy: Deploy = _get_deploy(args, validator, validator_purse_uref)
# Approve deploy.
deploy.approve(validator)
# Dispatch deploy.
await client.send_deploy(deploy)
print(f"Deploy dispatched to node [{args.node_host}]: {deploy.hash.hex()}")
def _get_client(args: argparse.Namespace) -> NodeClient:
"""Returns a pycspr client instance.
"""
return NodeClient(NodeConnectionInfo(args.node_host, args.node_port_rpc))
def _get_deploy(
args: argparse.Namespace,
validator: PrivateKey,
validator_purse_uref: CLV_URef
) -> Deploy:
"""Returns delegation deploy to be dispatched to a node.
"""
# Set standard deploy parameters.
deploy_params = pycspr.create_deploy_parameters(
account=validator,
chain_name=args.chain_name
)
# Set deploy.
deploy = pycspr.create_validator_auction_bid_withdrawal(
params=deploy_params,
amount=args.amount,
public_key=validator.to_public_key(),
path_to_wasm=args.path_to_wasm,
unbond_purse_ref=validator_purse_uref,
)
return deploy
# Entry point.
if __name__ == "__main__":
asyncio.run(_main(_ARGS.parse_args()))