# Eth.getAccount

Returns the nonce, the balance, the storage root and the code hash of an account in one call.
Not served by every client.

## 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("0x5FbDB2315678afecb367f032d93F642f64180aa3");

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

const account = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// {
//   nonce: 1,
//   balance: 0n,
//   storageRoot: "0xf258b1c6d5ee7f6f3549117fb0ac79118d5ad19b9c027f9b8e1471ca519b3b6c",
//   codeHash: "0x56a1235d0e575670832f9fb685f7146d7d9d6d7e4cbc28bfe94e39beb717a7cf"
// }
```

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

### Nonce and balance together

One request replaces [`Eth.getTransactionCount`](https://ethect.dev/rpc/eth/get-transaction-count/) and
[`Eth.getBalance`](https://ethect.dev/rpc/eth/get-balance/).

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

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

const sender = Effect.gen(function* () {
  const account = yield* Eth.getAccount(ADDRESS, "latest");
  if (account === null) return { nonce: 0, balance: 0n };
  return { nonce: account.nonce, balance: account.balance };
});
```

### Tell a contract from a plain account

An account without code has the hash of empty code. `Hash.keccak256` of no bytes gives it.

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

const emptyCodeHash = Hash.keccak256(new Uint8Array());

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

const isContract = Effect.gen(function* () {
  const account = yield* Eth.getAccount(ADDRESS, "latest");
  return account !== null && account.codeHash !== emptyCodeHash; // true
});
```

### When the node does not serve it

A node without the method answers with an `RpcError` whose reason is `MethodNotFound`. Fall
back to the two calls every node serves.

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

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

const sender = Eth.getAccount(ADDRESS, "latest").pipe(
  Effect.map((account) => ({ nonce: account?.nonce, balance: account?.balance })),
  Effect.catchReason("RpcError", "MethodNotFound", () =>
    Effect.all({
      nonce: Eth.getTransactionCount(ADDRESS, "latest"),
      balance: Eth.getBalance(ADDRESS, "latest"),
    }),
  ),
);
```

## Returns

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

An [`Account`](https://ethect.dev/rpc/types/account/): the four fields an account has in the state.

## 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 account = Eth.getAccount(ADDRESS, "latest");
```

### block

- **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. Unlike the other state methods, it
is required.

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

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

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

## Behavior

- **An unused address is an empty account on anvil.** anvil 1.9 answers a nonce of `0`, a
  balance of `0n`, and the hashes of empty storage and empty code. The result type also allows
  `null`: handle it.
- **The same values as a proof.** `nonce`, `balance` and `codeHash` are the ones
  [`Eth.getProof`](https://ethect.dev/rpc/eth/get-proof/) returns, without the trie nodes.
- **No code, no storage.** The account holds two hashes. Read the code with
  [`Eth.getCode`](https://ethect.dev/rpc/eth/get-code/) and a slot with
  [`Eth.getStorageAt`](https://ethect.dev/rpc/eth/get-storage-at/).

## 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` |

## JSON-RPC method

`eth_getAccount`
