# Eth.getBalance

Returns the balance of an account in wei, at the latest block or at a block you name.

## Usage

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const program = Eth.getBalance(ADDRESS, "latest");

const balance = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// 10000000000000000000000n
```

The call works over every [transport](https://ethect.dev/guides/transports/). The examples below leave the
transport out.

### At a past block

Pass a block number to read the balance the account had at that block.

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const before = Eth.getBalance(ADDRESS, 19_000_000n);
```

A node keeps old state for a limited number of blocks unless it is an archive node.

### In ether

The result is a `bigint` of wei. ethect has no unit helpers: Effect's `BigDecimal` turns wei
into text, as [Amounts and decimals](https://ethect.dev/guides/amounts/) shows. By hand, it is a division.

```ts
import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const inEther = Effect.gen(function* () {
  const wei = yield* Eth.getBalance(ADDRESS, "latest");
  const whole = wei / 10n ** 18n; // 10000n
  const rest = wei % 10n ** 18n; // 0n
  return `${whole}.${rest.toString().padStart(18, "0")}`;
});
```

## Returns

[`Effect<bigint, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

The balance in wei. An address that was never used has a balance of `0n`.

## Parameters

### address

- **Type:** [`Address.Address`](https://ethect.dev/reference/primitives/address/)

The account. A plain string is not accepted: build it with
[`Address.makeUnsafe`](https://ethect.dev/reference/primitives/address/#makeunsafe), or with `Address.make` when it
comes from outside.

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const balance = Eth.getBalance(ADDRESS, "latest");
```

### block (optional)

- **Type:** [`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)

The block to read at: a number, a tag (`"latest"`, `"earliest"`, `"pending"`, `"safe"`,
`"finalized"`), or an object that names the block by hash.

```ts
import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const balances = Effect.all([
  Eth.getBalance(ADDRESS, "finalized"),
  Eth.getBalance(ADDRESS, 19_000_000n),
  Eth.getBalance(ADDRESS, { blockHash: `0x${"ab".repeat(32)}` }),
]);
```

## Behavior

- **No default block.** Left out, the param is not sent and the node chooses the block:
  see [Naming a block](https://ethect.dev/rpc/guides/calling-methods/#naming-a-block).
- **Wei as `bigint`.** The node answers `"0x21e19e0c9bab2400000"` and you get
  `10000000000000000000000n`. See [values on the wire](https://ethect.dev/rpc/guides/values/).
- **EIP-55 address.** The address is sent in its EIP-55 form, whatever case you wrote it in.
  A node reads either.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The cases specific to this
method, as anvil 1.9 reports them:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `InvalidParams` | the block number is above the chain's head |
| `RpcError` | `ResourceNotFound` | no block has the given `blockHash` |
| `CodecError` | `on: "params"` | the block number is negative: nothing is sent |

## JSON-RPC method

`eth_getBalance`
