Stake Your First Validator

In this guide, you'll learn how to stake your first Ethereum validator on the testnet, step by step. Whether you're new to staking or just looking for a streamlined process, this guide covers all the essentials to get you up and running in just a few minutes.

🚧

Important

This staking process is designed for institutions that want to hold custody over funds. It means you must have full control over the wallet from which deposits will be made. Never share the API keys discussed in this guide with third parties, particularly via web-based interfaces.

If your goal is to enable non-custodial staking for your customers (they will retain control of their wallet and funds), we recommend exploring our Retail Staking product, which provides access to public APIs and API keys tailored for customer-managed staking.

What's a Validator?

Validators are the heart of the Ethereum network. Before you proceed with staking, it’s essential to understand their role.

A validator is an entity that participates in the consensus layer by proposing and validating new blocks on the blockchain. Each validator must stake a minimum of 32 Ether (up to a maximum of 2048 Ether) as collateral to ensure honest behavior. Validators play a crucial role in maintaining the network's security and integrity. In return, they earn rewards in the form of Ether.

However, validators that misbehave can face penalties, including slashing, which can result in a significant loss of staked Ether.

Check Remaining Public Keys

While the Stakefish API simplifies the staking process to a single API call followed by a blockchain transaction, it's essential to verify that your organization has enough allocated public keys. By performing this check, you ensure that your staking operations are carried out efficiently and within the limits of your allocated resources.

Use the following API to check how many public keys are currently available for your organization:

curl -X GET "https://api.testnet.stake.fish/v1/eth/stake/v1/remaining-keys" \
     -H "Authorization: YOUR_SECURE_API_KEY"

This endpoint returns the number of remaining public keys as text/plain regardless of your requested Accept header. For example, a response of 716 indicates that 716 public keys are available for staking.

Create Validator

After confirming that your account is ready and you have enough public keys, the next step is to prepare your validator for staking. This involves creating a validator with the necessary deposit details.

To create a validator, send a POST request to the Stakefish API with the following payload:

curl -X POST "https://api.testnet.stake.fish/v1/eth/stake/v1/create-validators-admin" \
    -H "Content-Type: application/json" \
    -H "Authorization: YOUR_SECURE_API_KEY" \
    --data '{
        "count": "VALIDATOR_COUNT_UP_TO_100_AT_ONCE",
        "withdrawalCredentials": "YOUR_WALLET_ADDRESS"
    }'

Explanation of Fields

  • count: Specifies the number of validators you want to create. In this example, count is set to 1, indicating that we're creating a single validator. You can create up to 100 validators in one request.
  • withdrawalCredentials: This is the address where your rewards and staked Ether will be withdrawn. Ensure that you have full access to this wallet. Otherwise your assets will be lost.

Example Response

After sending the POST request, you’ll receive a response similar to the following:

{
  "pubkeys": "0xb078b58f3801a6afe94ba4233dd96a6ad61f56c4c3e8be69c4a89820efe14db47dc53ea84ae8751e2493a9efdfcd7d28",
  "withdrawalCredentials": "0x0100000000000000000000001ec7fa23a8468f1f3a135bfefe6d678e1657ee65",
  "signatures": "0x89a39b1e8c9488ba1f65af7f0f6fc50641bb2b50e7cb7133729fbc1ff1fa1c25c169a35a685c5ce9d53db42807979ef61689e860fa3001ff9d475c7171fa19fd96717ad3089f3006139076282d87fff1703db57b56934a055207fe8e9978a12a",
  "depositDataRoots": [
    "0xc01bd131d4307686b280a1d4631550c1d81d09d386582980ac29a657ade2521e"
  ]
}

Deposit Validator

Once your validator is prepared and you've generated the deposit details, the final step is to deposit your validator on the Ethereum blockchain. This process involves interacting with the Ethereum network to send your deposit transaction.

In this example, we're using the viem library to encode the deposit calldata and send the transaction. The process includes the following steps:

1. Encode the Batch Deposit Calldata:

Use the encodeBatchDepositCalldata function to encode the public keys, withdrawal credentials, signatures, and deposit data roots into a single calldata string. This calldata will be sent to the batch deposit contract.

2. Initialize Wallet Client:

Create a wallet client using your Ethereum account's private key. Ensure you're connected to an Ethereum RPC node (e.g., Holesky for testnet).

3. Send the Transaction:

Using the walletClient, send a transaction to the batch deposit contract address. The transaction should include the encoded calldata, the recipient address (batch deposit contract), and the value (amount of Ether to be staked, e.g., 32 ETH per validator).

4. Check the Transaction Status:

After sending the transaction, you'll receive a transaction hash. Use this hash to monitor the transaction status on Etherscan or directly via your client library.

Here’s the completed code snippet:

import { createWalletClient, custom } from 'viem'
import { holesky } from 'viem/chains'
 
export const walletClient = createWalletClient({
  chain: holesky,
  transport: custom(window.ethereum!),
})

function encodeBatchDepositCalldata(pubkeys, withdrawalCredentials, signatures, depositDataRoots) {
  return viem.encodeFunctionData({
    abi: [viem.parseAbiItem('function batchDeposit(bytes,bytes,bytes,bytes32[])')],
    functionName: 'batchDeposit',
    args: [pubkeys, withdrawalCredentials, signatures, depositDataRoots],
  })
}

// This is obtained from the create-validators-admin endpoint
const depositPayload = {
  pubkeys: '0xb22447d996887afec5631410abd417de030e659c6d6c33d99af5db930f50a05f7f983bbb52778dae1244484469b8d675',
  withdrawalCredentials: '0x0100000000000000000000001ec7fa23a8468f1f3a135bfefe6d678e1657ee65',
  signatures: '0x93c1c8305c66a3f9016e0dbd616038170e15a7984bbae7489f2ff476d8e4d910c1b1dcf4d30c547f2c415b32b16f36f3033d92751f32e922999352cb0f7b6a97574612e8c9cb8cdd983e49d2c59022f2fbadd42c3b4bcf098113b3a48e65b3f9',
  depositDataRoots: [
    '0x8ec08305d04cc0738a5bbc571494b06ea7f7c67be00723664da8d6d098e5e053'
  ]
}

const calldata = encodeBatchDepositCalldata(
  depositPayload.pubkeys,
  depositPayload.withdrawalCredentials,
  depositPayload.signatures,
  depositPayload.depositDataRoots,
)

const txHash = await walletClient.sendTransaction({
  data: calldata,
  to: BATCH_DEPOSIT_ADDRESS,
  value: 32n * (10n **18n),
  type: 'eip1559',
})

console.log("Transaction Hash: ", txHash);

Explanation of Constants:

  • BATCH_DEPOSIT_ADDRESS: This is the contract address used to perform batch deposits of validators. You'll receive this address from Stakefish once your account is set up.

Final Steps

After sending the transaction, your Ethereum client will return a transaction hash. You can use this hash to monitor the transaction's progress on Holesky Etherscan or your preferred block explorer.

Deposit transaction on Etherscan

Congratulations! You've successfully staked your first Ethereum validator using Stakefish API. You can now monitor your validator and watch it contribute to the security and stability of the Ethereum network.

Validator Activation: Once the transaction is confirmed, your validator is in the process of being activated. The deposit typically takes between 16 to 24 hours to be processed by the consensus layer. After processing, your validator will be queued for activation, which can take anywhere from a few minutes to several days, depending on the current validator queue on Ethereum.


What’s Next