-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup_evm.py
More file actions
executable file
·225 lines (205 loc) · 7.2 KB
/
setup_evm.py
File metadata and controls
executable file
·225 lines (205 loc) · 7.2 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
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
import argparse
import json
import os
import pathlib
import subprocess
import sys
import bittensor
import bittensor.utils
import bittensor_wallet
from address_conversion import h160_to_ss58
from generate_keypair import generate_and_save_keypair
from subtensor import associate_evm_key, publish_contract_address as publish_contract_address_onchain
DENY_TIMEOUT = 5 * 24 * 60 * 60 # 5 days
MIN_COLLATERAL_INCREASE = 10000000000000 # 0.01 TAO
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--amount-tao",
required=True,
help="Amount of TAO to transfer to the EVM wallet",
type=float,
)
parser.add_argument(
"--deploy",
action="store_true",
help="Deploy the Contract to the Network",
)
parser.add_argument(
"--verify",
action="store_true",
help="Verify the deployed contract with TaoStats (mainnet only)",
)
parser.add_argument(
"--netuid",
help="Netuid of the Subnet in the Network.",
required=True,
type=int,
)
parser.add_argument(
"--network",
default="finney",
help="The Subtensor Network to connect to.",
)
parser.add_argument(
"--wallet-hotkey",
default="default",
help="Hotkey of the Wallet",
)
parser.add_argument(
"--wallet-name",
required=True,
help="Name of the Wallet.",
)
parser.add_argument(
"--wallet-path",
help="Path where the Wallets are located.",
)
parser.add_argument(
"--deny-timeout",
type=int,
default=DENY_TIMEOUT,
help="Timeout for validators to deny a reclaim request in seconds. Default is 3 days.",
)
parser.add_argument(
"--min-collateral-increase",
type=int,
default=MIN_COLLATERAL_INCREASE,
help="Minimum collateral increase for miners for deposits in Wei. Default is 10000000000000, which is 0.01 TAO.",
)
parser.add_argument(
"--skip-association",
action="store_true",
help="Don't try associating the EVM key with your hotkey"
)
override_or_reuse = parser.add_mutually_exclusive_group()
override_or_reuse.add_argument(
"--overwrite",
action="store_true",
help="Overwrite the existing H160 file with the new one.",
)
override_or_reuse.add_argument(
"--reuse",
action="store_true",
help="Reuse the existing H160 file if it already exists.",
)
args = parser.parse_args()
wallet = bittensor_wallet.Wallet(
name=args.wallet_name,
hotkey=args.wallet_hotkey,
path=args.wallet_path,
)
_, network_url = bittensor.utils.determine_chain_endpoint_and_network(
args.network,
)
keypair_path = (
pathlib.Path(wallet.path).expanduser().joinpath(wallet.name, "h160", wallet.hotkey_str)
)
if args.reuse:
try:
keypair = json.loads(keypair_path.read_text())
except FileNotFoundError:
print(
f"File {keypair_path} does not exists. Run the script without --reuse to generate a new keypair.",
file=sys.stderr,
)
sys.exit(1)
else:
try:
keypair = generate_and_save_keypair(output_path=keypair_path, overwrite=args.overwrite)
except FileExistsError as e:
print(f"File {e.filename} already exists. Use --overwrite or --reuse.", file=sys.stderr)
sys.exit(1)
with bittensor.Subtensor(
network=network_url,
) as subtensor:
if not args.skip_association:
print(f"Associating {keypair['address']} with your hotkey.", flush=True)
success, error = associate_evm_key(
subtensor,
wallet,
keypair["private_key"],
args.netuid,
)
if not success:
print(f"Unable to Associate EVM Key. {error}", file=sys.stderr)
sys.exit(1)
if args.amount_tao > 0:
print(f"Transfering {args.amount_tao} to {keypair['address']}.", flush=True)
success = subtensor.transfer(
wallet,
dest=h160_to_ss58(keypair["address"]),
amount=bittensor.Balance.from_tao(args.amount_tao),
wait_for_inclusion=True,
wait_for_finalization=True,
)
if not success:
print(f"Unable to Transfer TAO to generated EVM wallet.", file=sys.stderr)
sys.exit(1)
else:
print(f"Transfer Tao to `{h160_to_ss58(keypair['address'])}` in order to top up {keypair['address']}.", flush=True)
if not args.deploy:
return
print(f"Deploying new collateral contract(netuid={args.netuid}, min_collateral_increase={args.min_collateral_increase}, deny_timeout={args.deny_timeout}).\n"
f"Using RPC_URL={network_url}", flush=True
)
try:
contract = subprocess.run(
[
"bash",
"./deploy.sh",
str(args.netuid),
keypair["address"],
str(args.min_collateral_increase),
str(args.deny_timeout),
],
capture_output=True,
check=True,
cwd=pathlib.Path(__file__).parents[1],
env={
**os.environ,
"RPC_URL": network_url,
"DEPLOYER_PRIVATE_KEY": keypair["private_key"],
},
text=True,
)
except subprocess.CalledProcessError as e:
print(f"Unable to Deploy Contract. {e.stderr}", file=sys.stderr)
sys.exit(2)
else:
contract_address = next(
line.removeprefix("Deployed to: ")
for line in contract.stdout.split("\n")
if line.startswith("Deployed to: ")
)
print("Collateral smart contract deployed.")
print(f"Contract address: {contract_address}")
if args.verify:
try:
print("Verifying deployed contract with evm.taostats.io.")
subprocess.run(
[
"bash",
"./verify-with-taostats.sh",
contract_address,
],
check=True,
cwd=pathlib.Path(__file__).parents[1],
)
except subprocess.CalledProcessError as e:
print("Failed to verify deployed contract", file=sys.stderr)
print(f"Error: {e.stderr}", file=sys.stderr)
print("", file=sys.stderr)
try:
print("Publishing contract address as knowledge commitment.", flush=True)
publish_contract_address_onchain(
subtensor,
wallet,
args.netuid,
contract_address,
)
except bittensor.MetadataError as e:
print(f"Unable to Publish Contract Address. {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()