How to make a lot of eth2 deposits?

Warning: Please do not try this with real ether, this blogpost is purely meant
for testnet use

Let’s assume for a second you have a lot of testnet ether lying around and you
want to test out your staking setup. Sounds great!…until you have to make the
dozens of deposits. Thankfully we can script this and perform it in stages!

Pre-requisites:
We will use the following tools in this blogpost:

Deposit steps:

  1. We start by obtaining the Goerli ether, either from faucets or from someone
    who has a lot. For the sake of this blog, let’s assume the funds are held on
    metamask.

  2. Create a new folder and a file inside that folder called secrets.env. The
    secrets.env file will contain our secrets to avoid having to store them in our
    script directly. You should add this file to your .gitignore to avoid committing
    it by mistake.

  3. Fill secrets.env with the following variable names:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ACC_START_INDEX=
    ACC_END_INDEX=
    DEPOSIT_AMOUNT=
    FORK_VERSION=
    VALIDATORS_MNEMONIC=
    WITHDRAWALS_MNEMONIC=
    DEPOSIT_DATAS_FILE_LOCATION=

    DEPOSIT_CONTRACT_ADDRESS=
    ETH1_FROM_ADDR=
    ETH1_FROM_PRIV=
    FORCE_DEPOSIT=false
    ETH1_NETWORK=goerli

Find the deposit contract address, deposit amount and fork version
for your required scenario. For example, we have these values for the Prater
testnet:

1
2
3
4
DEPOSIT_AMOUNT=32000000000
FORK_VERSION="0x00001020"

DEPOSIT_CONTRACT_ADDRESS="0xff50ed3d0ec03aC01D4C79aAd74928BFF48a7b2b"
  1. Our Mnemonic is the source of the private keys of the validators. Loosing
    this mnemonic means the loss of all funds, so please store the mnemonic safely.
    We can generate the validator and withdrawal mnemonic using the tool
    eth2-val-tools. In your terminal, type out:
    1
    eth2-val-tools mnemonic
    The output should contain a list of 24 words. Copy those words into the
    secrets.env file under the variable VALIDATORS_MNEMONIC. Don’t forget to wrap
    the 24 words inside “”!

Repeat the process for WITHDRAWALS_MNEMONIC.

Please backup these mnemonics safely! Ideally inside a password manager as a
secure note.

  1. Decide on how many deposits you would like to perform. Generally this would
    be (amount of testnet ether you possess)/32. Set the ACC_START_INDEX and
    ACC_END_INDEX in secrets.env accordingly.

  2. Now we can finally add our private key and address from which the deposits
    will be made. This will be the in metamask, go to the account with the goerli
    ether > click on the three dots > click on Account details > click on export
    private key. We will need the private key in the script to access the funds and
    perform the deposits, so save this somewhere safely. Fill out ETH1_FROM_ADDR and
    ETH1_FROM_PRIV in secrets.env accordingly. Don’t forget to wrap both inside “”
    and they should start with 0x.

  3. We are finally ready to start generating our deposit data! Instead of
    directly performing the deposits, we will split our scripts into two halves- One
    to generate the deposit data and one to execute the deposits.

We will store the deposit data temporarily to allow us to manually inspect it,
enter the full path of where the file should be stored as the variable
DEPOSIT_DATAS_FILE_LOCATION in secrets.env.

Create a file called build_deposits.sh with the following contents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

echo "USE AT YOUR OWN RISK"
read -p "Are you sure? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

source secrets.env

eth2-val-tools deposit-data \
--source-min=$ACC_START_INDEX \
--source-max=$ACC_END_INDEX \
--amount=$DEPOSIT_AMOUNT \
--fork-version=$FORK_VERSION \
--withdrawals-mnemonic="$WITHDRAWALS_MNEMONIC" \
--validators-mnemonic="$VALIDATORS_MNEMONIC" > $DEPOSIT_DATAS_FILE_LOCATION.txt

The script will read the variables inside secrets.env and build the deposit data
for you in a text file stored at $DEPOSIT_DATAS_FILE_LOCATION.

Run the script using ./build_deposits.sh.

  1. The deposit data will be a bunch of json with the following format:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    {
    "account": "m/12381/3600/1/0/0",
    "deposit_data_root": "",
    "pubkey": "",
    "signature": "",
    "value": 32000000000,
    "version": 1,
    "withdrawal_credentials": ""
    }

Manually check if it looks okay to begin with. The account format
would be m/12381/3600/key_number/0/0. The key_number should co-relate to the
ACC_START_INDEX and ACC_END_INDEX entered earlier.

  1. Manually generate the public keys using eth2-val-tools to cross check if the
    public keys generated are correct.

    1
    eth2-val-tools pubkeys --validators-mnemonic=$VALIDATORS_MNEMONIC --source-min=$ACC_START_INDEX --source-max=$ACC_END_INDEX

    You will get a list of public keys in order of the min and max entered. I
    normally check the first and last public key as a sanity check.

  2. Perform the deposits! Please be really sure before executing this step!

    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
    #!/bin/bash
    echo "USE AT YOUR OWN RISK"
    read -p "Are you sure? " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]
    then
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
    fi

    source secrets.env

    if [[ -z "${DEPOSIT_CONTRACT_ADDRESS}" ]]; then
    echo "need DEPOSIT_CONTRACT_ADDRESS environment var"
    exit 1 || return 1
    fi


    if [[ -z "${ETH1_FROM_ADDR}" ]]; then
    echo "need ETH1_FROM_ADDR environment var"
    exit 1 || return 1
    fi
    if [[ -z "${ETH1_FROM_PRIV}" ]]; then
    echo "need ETH1_FROM_PRIV environment var"
    exit 1 || return 1
    fi

    # Iterate through lines, each is a json of the deposit data and some metadata
    while read x; do
    # TODO: check validity of deposit before sending it
    account_name="$(echo "$x" | jq '.account')"
    pubkey="$(echo "$x" | jq '.pubkey')"
    echo "Sending deposit for validator $account_name $pubkey"
    ethereal beacon deposit \
    --allow-unknown-contract=$FORCE_DEPOSIT \
    --address="$DEPOSIT_CONTRACT_ADDRESS" \
    --network=$ETH1_NETWORK \
    --data="$x" \
    --value="$DEPOSIT_ACTUAL_VALUE" \
    --from="$ETH1_FROM_ADDR" \
    --privatekey="$ETH1_FROM_PRIV"
    echo "Sent deposit for validator $account_name $pubkey"
    sleep 2
    done < "$DEPOSIT_DATAS_FILE_LOCATION.txt"

This script will read the values in secrets.env and then go through the
temporary deposits file specified in $DEPOSIT_DATAS_FILE_LOCATION. It will
process the json line by line and perform the deposit using the private key.

Since we want to perform a large amount of deposits, we ideally want to avoid
dos-ing the network all at once. To avoid that, there is a sleep command between
each deposit to allow some time between deposits.

Note: It is possible that one or more deposits might fail if done too quickly or
due to the network state. To avoid this, its best to split up deposits into
smaller batches of 500/1000 or so - this would also make debugging nonce issues
easier. I have tested doing 2000 deposits in one go on Goerli without an issue
with the above mentioned parameters.

Finally,Run the script using exec_deposits.sh .

  1. Give the transactions some time to be included in the block and indexed.
    Then head over to a explorer such as Otterscan
    [https://github.com/wmitsuda/otterscan] or Etherscan
    [https://goerli.etherscan.io/] and enter either your own address or the deposit
    contract address. You should now be able to see a list of all your deposits,
    also the same as the output from exec_deposits.sh.

  2. Collect one or more public keys, either from the sanity check stage of 10.
    or from the deposit data and head over to Beaconchain
    [https://prater.beaconcha.in/], enter your Validator public key here to check
    its status. You would usually have to wait for the deposit to be processed and
    for the validator to be activated. Beaconchain will also show you if your
    deposit was valid or if there was some error.

  3. Setup your validator ahead of time! Do not wait for the validator to be
    activated to begin the setup process, there is no penalty for starting a
    validator before it is activated. To help with client diversity, please setup a
    non-majority [https://twitter.com/superphiz/status/1434857668783575040] client!

  4. Profit ???

Huge shoutout to Protolambda [https://protolambda.com/] and the tool developers
for creating both the scripts and the tools!