# JSON-RPC layer

The low-level layer of ethect: one typed definition per JSON-RPC method and per subscription
kind, in the namespaces nodes and wallets serve. It is the `@ethect/core/rpc` entry point.

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

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

const account = Effect.gen(function* () {
  const balance = yield* Eth.getBalance(ADDRESS, "latest"); // bigint
  const nonce = yield* Eth.getTransactionCount(ADDRESS, "latest"); // number
  return { balance, nonce };
});
```

A function of this layer is one request to a node: `Eth.getBalance` sends `eth_getBalance` and
decodes its answer. Nothing is added, combined or retried.

## Where it sits

| Layer | Entry point | Holds | Documented |
| --- | --- | --- | --- |
| The main APIs | `@ethect/core` | contracts, reads, transactions, events, wallets | in the [guides](https://ethect.dev/guides/contracts/) and under Reference |
| JSON-RPC | `@ethect/core/rpc` | a function per method, a stream per subscription kind, the wire types | in this section |
| The base | `@ethect/core` | [transports](https://ethect.dev/reference/transport/), [errors](https://ethect.dev/reference/errors/), [primitives](https://ethect.dev/reference/primitives/address/) | under Reference |

- **It is the layer underneath.** The main APIs are built from these definitions: a read of a
  contract ends as `Eth.call`, a send as `Eth.sendRawTransaction` or `Eth.sendTransaction`.
  See [How it works](https://ethect.dev/concepts/how-it-works/#from-a-function-to-a-request).
- **Most programs do not call it.** [Read and write a contract](https://ethect.dev/guides/contracts/) is where
  to start.
- **Call it for what the main APIs do not cover**: a method of one client, a test node, exact
  control over one request.

## What it holds

| Module | Is | Pages |
| --- | --- | --- |
| [`Eth`](https://ethect.dev/rpc/eth/) | the 60 `eth_` methods of an execution client, and the 5 [subscription kinds](https://ethect.dev/rpc/eth/subscribe/) | state, blocks, transactions, logs, fees, sending |
| [`Wallet`](https://ethect.dev/rpc/wallet/) | the 29 methods a wallet serves: `wallet_`, and the `eth_` and `personal_` ones that prompt the user | accounts, chains, signing, call batches |
| [`Anvil`](https://ethect.dev/rpc/anvil/), [`Evm`](https://ethect.dev/rpc/evm/) | the 51 `anvil_` and 11 `evm_` methods of a local test node | state, mining, time, forks |
| [`Debug`](https://ethect.dev/rpc/debug/) | the 12 `debug_` methods | raw data and tracing |
| [`Txpool`](https://ethect.dev/rpc/txpool/), [`Net`](https://ethect.dev/rpc/net/), [`Web3`](https://ethect.dev/rpc/web3/) | the 4 `txpool_`, 3 `net_` and 2 `web3_` methods | one page each |
| [`Method`](https://ethect.dev/rpc/method/), [`Subscription`](https://ethect.dev/rpc/subscription/) | how a method and a subscription kind are defined | [defining your own](https://ethect.dev/rpc/guides/custom-methods/) |
| [the wire types](https://ethect.dev/rpc/types/) | 38 modules, such as `Block`, `Transaction`, `Receipt` and `Log`: a schema and the type it decodes to | one page each |

That is 161 methods. `Wallet` shares 11 of its 29 with `Eth`.

```ts
import { type Block, Eth, Method, type Receipt, Wallet } from "@ethect/core/rpc";
```

Everything on these pages is imported from `@ethect/core/rpc`. Rolldown keeps the methods a
program calls and drops the rest: see [Installation](https://ethect.dev/getting-started/installation/#bundle-size).

## What it needs

A method returns an `Effect` that needs a [transport](https://ethect.dev/guides/transports/), and fails with one
of the three [errors](https://ethect.dev/reference/errors/). Both come from `@ethect/core`, and they are the same
whichever layer makes the call.

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

const chainId = await Eth.chainId().pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
```

## Guides

| Guide | Covers |
| --- | --- |
| [Calling methods](https://ethect.dev/rpc/guides/calling-methods/) | params in order, native results, naming a block, several calls |
| [Batching](https://ethect.dev/rpc/guides/batching/) | several calls in one HTTP request |
| [Subscriptions](https://ethect.dev/rpc/guides/subscriptions/) | `eth_subscribe` as a `Stream` |
| [Custom methods](https://ethect.dev/rpc/guides/custom-methods/) | a method the library does not define, or one your chain shapes differently |
| [Values on the wire](https://ethect.dev/rpc/guides/values/) | how hex becomes `bigint`, `number`, addresses and names |

## Clients it has been run against

The package's tests run the definitions against anvil 1.9. A second, opt-in suite reads from a
real node: the `eth_` read methods (blocks, transactions, receipts, state, logs, fees, a call,
a simulation) and a `newHeads` subscription. It makes a few dozen calls, sends nothing, and
fails when an answer does not decode.

| Client | Chain |
| --- | --- |
| Geth 1.17 | Ethereum mainnet, Hoodi |
| reth 2.11 | Ethereum mainnet, Sepolia |
| Erigon 3.8 | Ethereum mainnet |
| Nethermind 2.2 | Gnosis |
| op-reth 2.8 | OP Mainnet |
| reth 2.5, as Base runs it | Base |
| Arbitrum nitro 3.13 | Arbitrum One |
| zkSync Era | zkSync Era |
| RskJ 9.0 | Rootstock |
| bor 2.11 | Polygon |
| BSC's Geth | BNB Smart Chain |
| Scroll's Geth | Scroll |

What those nodes send differently is in the types: a `null` blob base fee on Arbitrum, a proof
without `codeHash` on Scroll, forks that start at a block on Polygon. Each type page says so
where it applies.

- **Not run: Besu.** No public endpoint was found for it.
- **Not run: a real wallet.** The [`Wallet`](https://ethect.dev/rpc/wallet/) methods are checked against their
  EIPs and a [`MockProvider`](https://ethect.dev/reference/testing/mock-provider/), not against a wallet.
- **Not run: a browser.** The tests run on Node.
- **Per method, the errors are anvil's.** A table that says "as anvil 1.9 reports them" was
  observed on anvil. Another client can answer the same mistake with another code.

## Next steps

- [Calling methods](https://ethect.dev/rpc/guides/calling-methods/): the first guide of this layer.
- [`Eth`](https://ethect.dev/rpc/eth/): every `eth_` method, by group.
- [Types](https://ethect.dev/rpc/types/): the shape of what the methods take and return.
- [How it works](https://ethect.dev/concepts/how-it-works/): what a call does between your code and the node.
