forked from ssvlabs/awesome-ssv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stake.py
140 lines (131 loc) · 10.2 KB
/
stake.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
import json
import sys
import time
import traceback
from collections import namedtuple
from eth_typing import HexAddress, HexStr
from ssv.ssv_cli import SSV, OperatorData, Operator
from staking_deposit.settings import GOERLI
from staking_deposit.utils.constants import WORD_LISTS_PATH
from staking_deposit.key_handling.key_derivation.mnemonic import get_mnemonic
from staking_deposit.validator_key import ValidatorKey
from utils.eth_connector import EthNode
from utils.ssv_network import SSVNetwork, SSVToken
from utils.stakepool import StakingPool
def read_file(file_path):
"""
This is used to read params from json file and convert them to python Namespaces
:param file_path: takes the json filepath
:returns: it returns data in form of python Namespace
"""
with open(file_path, "r") as file:
data = json.load(file, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
file.close()
return data
def start_staking(config_file):
"""
This is an entry function for command line argument stake.
Following are the actions performed by it:
[1] It is used to run the backend for stakepool contract.
[2] It regularly monitors the balances of the stakepool contract and submits keys and keyshares.
[3] It also monitors the balance of SSV token in the stakepool contract and sends some SSV token from whitelist address
[4] In the case of script failure it creates a fallabck file which stores the state of the system. Upon resuming the
service it
:param config_file: Refer sample_config(stake-config.json) folder for config file params
:return: Null
"""
config = read_file(config_file)
with open("fallback.json", "r") as file:
fallback = json.load(file) # used to store the state of the system
file.close()
try:
while True:
mnemonic = get_mnemonic(language="english", words_path=WORD_LISTS_PATH) # mnemonic
web3_eth = EthNode(config.eth.rpc, config.eth.priv_key)
if web3_eth.get_balance(config.contract_address.stakepool) >= 32 or len(fallback) > 0:
stake_pool = StakingPool(config.contract_address.stakepool, web3_eth.eth_node)
print("balance of staking pool:" + str(web3_eth.get_balance(config.contract_address.stakepool)))
num_validators = int(web3_eth.get_balance(config.contract_address.stakepool) / 32)
print("creating validators")
validators = ValidatorKey()
keystores, deposit_file = validators.generate_keys(mnemonic=mnemonic, validator_start_index=1,
num_validators=num_validators, folder="",
chain=GOERLI,
keystore_password=config.keystore_pass,
eth1_withdrawal_address=HexAddress(
HexStr(stake_pool.get_withdrawal_address())))
print("keys created are:\n")
print(keystores)
print("submitting validators")
for index, cred in enumerate(validators.get_deposit_data(deposit_file)):
tx = stake_pool.deposit_validator(cred.pubkey,
cred.withdrawal_credentials,
cred.signature,
cred.deposit_data_root,
web3_eth.account.address)
web3_eth.make_tx(tx)
fallback[cred.pubkey] = {"keystore": keystores[index], "ssv_share": ""}
print("deposit the key" + str(cred.pubkey))
print("submitted validators\n")
operator_id = stake_pool.get_operator_ids()
print("operator ids are:\n")
print(operator_id)
ssv_contract = SSVNetwork(config.contract_address.ssv_network, web3_eth.eth_node)
ssv_token = SSVToken(config.contract_address.ssv_token, web3_eth.eth_node)
network_fees = 0 if ssv_contract.get_network_fee() is None else ssv_contract.get_network_fee()
print("network fee is:\n")
print(network_fees)
pubkeys = list(fallback.keys())
for pubkey in pubkeys:
ssv = SSV(fallback[pubkey]["keystore"], config.keystore_pass)
if fallback[pubkey]["ssv_share"] == "":
operator_data = [Operator(1,"LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tCk1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBMVg2MUFXY001QUNLaGN5MTlUaEIKby9HMWlhN1ByOVUralJ5aWY5ZjAyRG9sd091V2ZLLzdSVUlhOEhEbHBvQlVERDkwRTVQUGdJSy9sTXB4RytXbwpwQ2N5bTBpWk9UT0Jz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bHg3RFE1WnVBNDNCSGNGMWtxMAo3ZHU0ejFUQ2tFN0ZIZlZRMTdFUnpwUHlmS2l5YlQ4UXdnb3VVV2hGUjJqK3ExbHZGbHJQR0U2OWpIWE9MVWM0CnV3SURBUUFCCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0K",0, "Bloxstaking4")]
file = ssv.generate_shares(operator_data, network_fees)
fallback[pubkey]["ssv_share"] = file
shares = ssv.get_keyshare(file)
else:
shares = ssv.get_keyshare(fallback[pubkey]["ssv_share"])
if ssv_token.get_balance(web3_eth.eth_node.toChecksumAddress(config.contract_address.stakepool)) < int(
shares["ssvAmount"]):
print("ssv token balance of stakepool is less than the required amount. Sending some tokens")
if ssv_token.get_balance(
web3_eth.eth_node.toChecksumAddress(web3_eth.account.address)) > 2 * int(
shares["ssvAmount"]):
tx = ssv_token.transfer_token(web3_eth.eth_node.toChecksumAddress(config.contract_address.stakepool),
2 * int(shares["ssvAmount"]), web3_eth.account.address)
web3_eth.make_tx(tx)
print("Added SSV tokens to stakepool account")
elif ssv_token.get_balance(web3_eth.eth_node.toChecksumAddress(web3_eth.account.address)) > int(
shares["ssvAmount"]):
tx = ssv_token.transfer_token(web3_eth.eth_node.toChecksumAddress(config.contract_address.stakepool),
int(shares["ssvAmount"]), web3_eth.account.address)
web3_eth.make_tx(tx)
print(
"WARNING!!!! Balance too low for account and stakepool for SSV tokens. Please add some")
else:
raise Exception(
"ERROR!!!! keys shares not added as your account doesn't have enough SSV tokens")
tx = stake_pool.send_key_shares(shares["validatorPublicKey"], operator_id,
shares["sharePublicKeys"], shares["sharePrivateKey"],
int(shares["ssvAmount"]),
web3_eth.account.address)
web3_eth.make_tx(tx)
print("ssv shares submitted to the contract")
fallback.pop(pubkey)
fallback = {}
else:
print("pool balance less than 32")
time.sleep(10)
print("trying again")
except Exception as err:
print(traceback.format_exc())
print(fallback)
with open("fallback.json", "w") as file:
json.dump(fallback, file)
file.close()
if __name__ == '__main__':
config_file = sys.argv[1]
start_staking(config_file)