# Deposits and gas

> For the complete documentation index, see [llms.txt](https://ensforge.com/llms.txt).

Fund an HCA, read and top up its EntryPoint deposit, and learn who pays gas for each executor, with or without a paymaster.

Source: https://ensforge.com/docs/hca/deposits-and-gas

<Callout type="info" title="Experimental">
`@ensforge/hca` is experimental for 1.0. Its API can change in a minor release.
</Callout>

An [HCA](/docs/reference/glossary#hca) is its own account, so it can hold funds and pay for things. By the end of this guide you will know who pays gas for each [executor](/docs/reference/glossary#executor), how to fund an HCA before it exists, and how to manage its ERC-4337 deposit.

## Who pays what [#who-pays-what]

Two costs exist. The **gas** pays for running the execution. The **spend** is what the calls themselves pay, such as ETH sent with a call or a registration fee in tokens.

| Executor                                       | Pays the gas                                                             | Pays ETH the calls send                        | Pays tokens the calls spend |
| ---------------------------------------------- | ------------------------------------------------------------------------ | ---------------------------------------------- | --------------------------- |
| `owner()`                                      | The owner's wallet.                                                      | The owner's wallet, sent with the transaction. | The HCA.                    |
| `bundler()` or `pimlico()` without a paymaster | The HCA.                                                                 | The HCA.                                       | The HCA.                    |
| `bundler()` or `pimlico()` with a paymaster    | The [paymaster](/docs/reference/glossary#paymaster).                     | The HCA.                                       | The HCA.                    |
| `rhinestone()`                                 | Sponsored by default. With `sponsored: false`, a session's refund token. | The HCA.                                       | The HCA.                    |

The HCA is `msg.sender` of the calls it runs, so tokens a call spends come from the HCA's balance, not the wallet's. A registration through `via: hca()` fails with [`INSUFFICIENT_TOKEN_BALANCE`](/docs/reference/errors/INSUFFICIENT_TOKEN_BALANCE) before it is sent when the HCA cannot pay. Fund the HCA first.

## Fund an HCA before it exists [#fund-an-hca-before-it-exists]

The address of an HCA is known before deployment. Predict it and send funds there.

**Example** (Reading the Address to Fund)

```ts title="fund.ts"
import { hcaActions } from "@ensforge/hca";
import { Ensforge, ensActions } from "@ensforge/sdk";
import { createPublicClient, createWalletClient, http, parseEther } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { sepolia } from "viem/chains";

const account = privateKeyToAccount(
  "0x0123456789012345678901234567890123456789012345678901234567890123",
);
const publicClient = createPublicClient({ chain: sepolia, transport: http() });
const walletClient = createWalletClient({ account, chain: sepolia, transport: http() });
const ens = Ensforge.create({ publicClient, walletClient })
  .extend(ensActions())
  .extend(hcaActions());
const address = await ens.hca.predictAddress({});

await walletClient.sendTransaction({ to: address, value: parseEther("0.01") });
```

[`hca.predictAddress`](/docs/reference/hca/hca/predict-address) takes an optional `owner`, which defaults to the client's account. The address never changes, even after upgrades.

## Read and top up the EntryPoint deposit [#read-and-top-up-the-entrypoint-deposit]

With a bundler and no paymaster, the HCA pays for its UserOperations. The ERC-4337 **EntryPoint** keeps a deposit for each account and charges that gas to it. Read the deposit, and add to it ahead of time, with these actions.

| Action                                                            | What it does                                                      |
| ----------------------------------------------------------------- | ----------------------------------------------------------------- |
| [`hca.getDeposit`](/docs/reference/hca/hca/get-deposit)           | Reads the deposit in wei. Takes an optional `address` or `owner`. |
| [`hca.addDeposit`](/docs/reference/hca/hca/add-deposit)           | Adds `amount` wei from your wallet to the deposit.                |
| [`hca.withdrawDeposit`](/docs/reference/hca/hca/withdraw-deposit) | Withdraws `amount` wei to `to`.                                   |

**Example** (Topping Up and Withdrawing)

```ts title="deposit.ts"
import { hcaActions } from "@ensforge/hca";
import { Ensforge, ensActions } from "@ensforge/sdk";
import { createPublicClient, createWalletClient, http, parseEther } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { sepolia } from "viem/chains";

const account = privateKeyToAccount(
  "0x0123456789012345678901234567890123456789012345678901234567890123",
);
const ens = Ensforge.create({
  publicClient: createPublicClient({ chain: sepolia, transport: http() }),
  walletClient: createWalletClient({ account, chain: sepolia, transport: http() }),
})
  .extend(ensActions())
  .extend(hcaActions());
const before = await ens.hca.getDeposit({});

await ens.hca.addDepositSync({ amount: parseEther("0.01") });

const after = await ens.hca.getDeposit({});

await ens.hca.withdrawDepositSync({ to: account.address, amount: after - before });
```

`withdrawDeposit` runs from the HCA through `executeByOwner`, because only the account may withdraw its deposit. Each action has a `…Sync` form that waits for the receipt and a form that resolves once the transaction is submitted. See [Ways to call actions](/docs/sdk/ways-to-call-actions).

## Check the cost before you send [#check-the-cost-before-you-send]

Every write has `prepare`, `simulate` and `estimateGas`. With an executor that quotes fees, the [plan](/docs/reference/glossary#plan) shows them in `plan.fees` before anything is sent. See [Preview before sending](/docs/sdk/preview-before-sending).

## Troubleshooting [#troubleshooting]

### `INSUFFICIENT_TOKEN_BALANCE` [#insufficient_token_balance]

The HCA does not hold enough of the payment token for the registration. Send the token to the HCA's address and run the write again. See [`INSUFFICIENT_TOKEN_BALANCE`](/docs/reference/errors/INSUFFICIENT_TOKEN_BALANCE).

### `HCA_BUNDLER_REJECTED` [#hca_bundler_rejected]

With a bundler and no paymaster, neither the HCA nor a paymaster could pay. Fund the HCA or its deposit, or add a paymaster. See [Bundlers](/docs/hca/bundlers).