---
# System prepended metadata

title: Deterministic deposits in typescript

---

# Deterministic deposits in typescript

## TL:DR; SLOW CODE, DON'T HAVE THE ANSWER THAT IT CAN RUN IN FRONTEND
## IT CAN RUN A NETWORK!


TS:
@Chainsafe/lodestar
packages/beacon-node/src/node/utils/interop/deposits.ts
```typescript=
// deterministicDeposits assume that dataRootList will be committed within a function body
export function deterministicDeposits(
    config: IChainConfig,
    depositDataRootList: DepositTree,
    deposits: phase0.DepositData[],
): phase0.Deposit[] {
  depositDataRootList.commit();
  const depositTreeDepth = depositDataRootList.type.depth;

  return deposits.map((depositData, i) => {
    depositDataRootList.push(ssz.phase0.DepositData.hashTreeRoot(depositData));
    depositDataRootList.commit();

    return {
      proof: new Tree(depositDataRootList.node).getSingleProof(toGindex(depositTreeDepth, BigInt(i))),
      data: depositData,
    };
  });
}
```

```typescript=
// Apply deposits
  applyDeposits(config, state, deposits, fullDepositDataRootList);


export function applyDeposits(
  config: IChainForkConfig,
  state: CachedBeaconStateAllForks,
  newDeposits: phase0.Deposit[],
  fullDepositDataRootList?: DepositDataRootViewDU
): {activatedValidatorCount: number} {
  const initDepositCount = depositDataRootList.length;
  const depositDatas = fullDepositDataRootList ? null : newDeposits.map((deposit) => deposit.data);
  const {DepositData, DepositDataRootList} = ssz.phase0;

  for (const [index, deposit] of newDeposits.entries()) {
    if (fullDepositDataRootArr) {
      depositDataRootList.push(fullDepositDataRootArr[index + initDepositCount]);
      state.eth1Data.depositRoot = DepositDataRootList.hashTreeRoot(depositDataRootList);
    } else if (depositDatas) {
      const depositDataList = depositDatas.slice(0, index + 1);
      state.eth1Data.depositRoot = DepositDataRootList.hashTreeRoot(
        depositDataList.map((d) => DepositData.hashTreeRoot(d))
      );
    }

    // THIS MUST BE 0 IF THE POS CONFIG APPLIES FROM THE START (BLOCK 0)!
    state.eth1Data.depositCount = 0;

    const fork = config.getForkSeq(GENESIS_SLOT);
    processDeposit(fork, state, deposit);
  }
    
    // Set genesis validators root for domain separation and chain versioning
  // .hashTreeRoot() automatically commits()
  state.genesisValidatorsRoot = state.validators.hashTreeRoot();
}

```