forked from casper-network/casper-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhow_to_delegate.py
More file actions
159 lines (127 loc) · 4.3 KB
/
how_to_delegate.py
File metadata and controls
159 lines (127 loc) · 4.3 KB
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
import argparse
import os
import pathlib
import typing
from pycspr import factory
from pycspr.api import NodeConnectionInfo
from pycspr.client import NodeClient
from pycspr.crypto import KeyAlgorithm
from pycspr.types import PrivateKey
from pycspr.types import Deploy
from pycspr.types import PublicKey
USER_1_SECRET = "assets/net-1/users/user-1/secret_key.pem"
NODE_1_PUB_KEY_HEX = "assets/net-1/nodes/node-1/keys/public_key_hex",
DELEGATE_WASM = "assets/net-1/bin/auction/delegate.wasm",
# CLI argument parser.
_ARGS = argparse.ArgumentParser("Demo illustrating how to delegate CSPR "
"tokens to a validator.")
# CLI argument: path to delegator secret key - defaults to NCTL user 1.
_ARGS.add_argument(
"--delegator-secret-key-path",
default=pathlib.Path(os.getenv("NCTL")) / USER_1_SECRET,
dest="path_to_delegator_secret_key",
help="Path to delegator's secret_key.pem file.",
type=str,
)
# CLI argument: type of delegator secret key - defaults to ED25519.
_ARGS.add_argument(
"--delegator-secret-key-type",
default=KeyAlgorithm.ED25519.name,
dest="type_of_delegator_secret_key",
help="Type of delegator's secret key.",
type=str,
)
# CLI argument: path to validator's account key - defaults to NCTL node 1.
_ARGS.add_argument(
"--validator-account-key-path",
default=pathlib.Path(os.getenv("NCTL")) / NODE_1_PUB_KEY_HEX,
dest="path_to_validator_account_key",
help="Path to validator's public_key_hex file.",
type=str,
)
# CLI argument: path to session code wasm binary - defaults to
# NCTL bin/eco/delegate.wasm.
_ARGS.add_argument(
"--path-to-wasm",
default=pathlib.Path(os.getenv("NCTL")) / DELEGATE_WASM,
dest="path_to_wasm",
help="Path to delegate.wasm file.",
type=str,
)
# CLI argument: name of target chain - defaults to NCTL chain.
_ARGS.add_argument(
"--chain",
default="casper-net-1",
dest="chain_name",
help="Name of target chain.",
type=str,
)
# CLI argument: host address of target node - defaults to NCTL 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 @ NCTL 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,
)
def _main(args: argparse.Namespace):
""" Main entry point.
:param args: Parsed command line arguments.
"""
# Set node client.
client = _get_client(args)
# Set counter-parties.
delegator, validator = _get_counter_parties(args)
# Set deploy.
deploy: Deploy = _get_deploy(args, delegator, validator)
# Approve deploy.
deploy.approve(delegator)
# Dispatch deploy to a node.
client.deploys.send(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. """
connection = NodeConnectionInfo(
host=args.node_host,
port_rpc=args.node_port_rpc,
)
return NodeClient(connection)
def _get_counter_parties(args: argparse.Namespace
) -> typing.Tuple[PrivateKey, PublicKey]:
""" Returns the 2 counter-parties participating in the delegation. """
delegator = factory.parse_private_key(
args.path_to_delegator_secret_key,
args.type_of_delegator_secret_key,
)
validator = factory.parse_public_key(
args.path_to_validator_account_key
)
return delegator, validator
def _get_deploy(args: argparse.Namespace, delegator: PrivateKey,
validator: PublicKey) -> Deploy:
""" Returns delegation deploy to be dispatched to a node. """
# Set standard deploy parameters.
deploy_params = factory.create_deploy_parameters(
account=delegator,
chain_name=args.chain_name
)
# Set deploy.
deploy = factory.create_validator_delegation(
params=deploy_params,
amount=int(1e9),
public_key_of_delegator=delegator,
public_key_of_validator=validator,
path_to_wasm=args.path_to_wasm
)
return deploy
# Entry point.
if __name__ == '__main__':
_main(_ARGS.parse_args())