> ## Documentation Index
> Fetch the complete documentation index at: https://www.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# depositToEarnVault

> Prepares and executes a deposit into an Earn vault with an EVM wallet

## Function Signature

```typescript theme={"system"}
depositToEarnVault(params: {
  vaultId: string;
  amount: string;
  receiverAddress?: string;
  walletMetadata: WalletMetadata;
  password?: string;
  externalServerKeyShares?: ServerKeyShare[];
  sponsored?: boolean;
  chain?: Chain;
  rpcUrl?: string;
  onStepChange?: (step: EarnExecutionStep) => void;
  traceContext?: TraceContext;
}): Promise<EarnTransactionResult>
```

## Description

Prepares and executes a deposit into an [Earn](https://www.dynamic.xyz/docs/earn/overview.md) vault in one call: it builds the payload with [`prepareDeposit`](https://www.dynamic.xyz/docs/node/reference/earn/prepare-deposit) and executes it with [`executeEarnTransaction`](https://www.dynamic.xyz/docs/node/reference/evm/execute-earn-transaction), sending the ERC-20 approval first and waiting for it to confirm whenever the vault's allowance is short. Shares are credited to `receiverAddress` when given, otherwise to the depositing wallet.

## Parameters

### Required Parameters

* **`vaultId`** (`string`): The vault identifier from [`listVaults`](https://www.dynamic.xyz/docs/node/reference/earn/list-vaults).
* **`amount`** (`string`): How much of the vault's underlying asset to deposit, in the asset's human-readable units (`'12.5'` USDC, not `'12500000'`). Needs a leading zero (`'0.01'`, not `'.01'`).
* **`walletMetadata`** ([`WalletMetadata`](https://www.dynamic.xyz/docs/node/reference/types/wallet-metadata)): Non-sensitive wallet metadata persisted from `createWalletAccount()` / `importPrivateKey()`. Its `chainName` must be `'EVM'`.

### Optional Parameters

* **`receiverAddress`** (`string`): The address that receives the vault shares. Defaults to the depositing wallet.
* **`password`** (`string`): Required if the wallet was created with `backUpToDynamic: true`. Used for backup decryption when `externalServerKeyShares` is not provided.
* **`externalServerKeyShares`** ([`ServerKeyShare[]`](https://www.dynamic.xyz/docs/node/reference/types/server-key-share)): Caller-supplied plaintext shares.
* **`sponsored`** (`boolean`): When `true`, relays the approval and vault call as one gasless batch. See [EVM Gas Sponsorship](https://www.dynamic.xyz/docs/node/wallets/server-wallets/gas-sponsorship-evm.md).
* **`chain`** (`Chain`): The viem chain for the vault. Its `id` must match the vault's `chainId`. When omitted, the chain is built from `rpcUrl`, so one of `chain` or `rpcUrl` is required.
* **`rpcUrl`** (`string`): RPC endpoint for the vault's chain.
* **`onStepChange`** (`(step: EarnExecutionStep) => void`): Reports `approval` then `transaction` as execution proceeds.
* **`traceContext`** (`TraceContext`): Trace context forwarded to the Dynamic API.

## Returns

* **`Promise<EarnTransactionResult>`**: An object with `transactionHash`, the hash of the vault transaction.

## Example

```typescript theme={"system"}
import { authenticatedEvmClient } from './client';

const evmClient = await authenticatedEvmClient();

const { transactionHash } = await evmClient.depositToEarnVault({
  vaultId: 'steakhouse-prime-usdc',
  amount: '12.5',
  walletMetadata,
  externalServerKeyShares,
  password: 'user-password',
  rpcUrl: 'https://mainnet.base.org',
  onStepChange: (step) => console.log(`Step: ${step}`),
});
```

## Error Handling

Fails with an HTTP 403 error when Earn is not enabled for your environment or an HTTP 400 error when the amount or address is invalid. Throws when `walletMetadata.chainName` is not `'EVM'`, when `chain.id` mismatches the payload's `chainId`, when neither `chain` nor `rpcUrl` is given, or when the approval or vault transaction reverts.

## See Also

* [Earn](https://www.dynamic.xyz/docs/node/earn.md)
* [withdrawFromEarnVault](https://www.dynamic.xyz/docs/node/reference/evm/withdraw-from-earn-vault.md)
* [executeEarnTransaction](https://www.dynamic.xyz/docs/node/reference/evm/execute-earn-transaction.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.