Skip to content

Instantly share code, notes, and snippets.

@banteg
Last active July 31, 2022 18:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save banteg/f6e48007e835376ba0bbb67354e64214 to your computer and use it in GitHub Desktop.
Save banteg/f6e48007e835376ba0bbb67354e64214 to your computer and use it in GitHub Desktop.
eth2 mass deposit for lighthouse
# 1. generate keys with `lighthouse account validator new random N`
# 2. run `python deposit.py`, it skips the ones already deposited and deposits the rest
import json
import sys
from pathlib import Path
from getpass import getpass
from eth_utils import encode_hex, decode_hex
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
DEPOSIT_DATA_BYTES = 420
DEPOSIT_CONTRACT_ADDRESS = '0x74A03685a1cbc279EfE4ea88b5A86d6Cb0C6Cedb'
DEPOSIT_CONTRACT_BLOCK = 1857278
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
assert w3.eth.syncing == False, 'wait for node to finish syncing'
assert w3.eth.chainId == 5, 'invalid network, switch to goerli'
account = w3.eth.account.from_key(getpass('priv: '))
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(account))
print('using', account.address)
abi = json.load(open('unsafe_validator_registration.json'))
contract = w3.eth.contract(DEPOSIT_CONTRACT_ADDRESS, abi=abi['abi'])
print('fetching deposit events')
logs = contract.events.DepositEvent.getLogs(fromBlock=DEPOSIT_CONTRACT_BLOCK)
existing_validators = [encode_hex(x.args.pubkey) for x in logs]
print(f'fetched {len(existing_validators)} events')
keys_dir = Path('~/.lighthouse/validators').expanduser()
pubkeys = [path.name for path in keys_dir.iterdir()]
print(f'loaded {len(pubkeys)} pubkeys')
for pub in pubkeys:
if pub in existing_validators:
print(f'skipping {pub}')
continue
print(f'depositing {pub}')
deposit_data = open(keys_dir / pub / 'eth1_deposit_data.rlp').read()
assert len(decode_hex(deposit_data)) == DEPOSIT_DATA_BYTES
tx = w3.eth.sendTransaction(
{
'from': account.address,
'to': contract.address,
'gas': 4_000_000,
'value': w3.toWei('3.2', 'ether'),
'data': deposit_data,
}
)
print(f'sent tx https://goerli.etherscan.io/tx/{encode_hex(tx)}')
receipt = w3.eth.waitForTransactionReceipt(tx)
if not receipt.status:
print('tx failed, exiting to safety')
break
balance = w3.fromWei(w3.eth.getBalance(account.address), "ether")
print(f'remaining balance: {balance} eth')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment