# Values on the wire

Ethereum JSON-RPC sends everything as hex strings. ethect decodes each value to a native type,
and encodes it back, by one set of rules that holds for every method and every wire type.

| On the wire | In code | Schema |
| --- | --- | --- |
| a quantity that can be large | `bigint` | [`Quantity.schema`](https://ethect.dev/reference/primitives/quantity/) |
| a quantity that stays small | `number` | [`Quantity.numberSchema`](https://ethect.dev/reference/primitives/quantity/) |
| an address, sent in its EIP-55 form | [`Address.Address`](https://ethect.dev/reference/primitives/address/): lowercase and branded | `Address.wireSchema` |
| a 32-byte hash | [`Hash.Hash`](https://ethect.dev/reference/primitives/hash/) | `Hash.schema` |
| any other bytes | [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/) | `Hex.schema` |
| a type or status code, such as `"0x2"` or `"0x1"` | a name, such as `"eip1559"` or `"success"` | per type |

## `bigint` or `number`

A quantity is a `bigint` when it can outgrow what a JavaScript `number` holds exactly, and a
`number` when it is a counter or an index that never will.

| `bigint` | `number` |
| --- | --- |
| wei: balances, values, fees, gas prices | chain ids |
| gas: limits, usage | nonces |
| block numbers | positions: `transactionIndex`, `logIndex` |
| timestamps | counts: transactions in a block, uncles, pending transactions |
| sizes | `yParity`, the block count of `Eth.feeHistory` |

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

const receiptOf = (hash: `0x${string}`) =>
  Effect.map(Eth.getTransactionReceipt(hash), (receipt) => ({
    block: receipt?.blockNumber, // bigint
    gasUsed: receipt?.gasUsed, // bigint
    index: receipt?.transactionIndex, // number
  }));
```

- **Never mixed.** A `bigint` field is a `bigint` on every type that has it. A block number is
  never a `number` anywhere.
- **Checked on the way out.** A negative `bigint`, or a `number` that is not a safe
  non-negative integer, fails with a `CodecError` on the params before anything is sent.
- **Written in the shortest form.** `0n` is sent as `"0x0"` and `255n` as `"0xff"`. Leading
  zeros in what a node sends are accepted, and `"0x"` is read as 0.
- **JSON numbers stay numbers.** A few fields are plain JSON numbers on the wire, not hex, such
  as the ratios of [`FeeHistory`](https://ethect.dev/rpc/types/fee-history/). They are `number`, and the
  type page of each says so.
- **Except block numbers and timestamps.** Where anvil sends one as a JSON number, it is
  still a `bigint`.

## Addresses

An address in code is always lowercase, and it is a branded type: a plain string is not
accepted where an address is expected.

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

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
// "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
const display = Address.checksum(ADDRESS); // "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
```

| Rule | Why |
| --- | --- |
| stored lowercase | two addresses are the same account exactly when they are `===` |
| branded | an unvalidated string cannot reach a method by mistake |
| any letter case is accepted when an address is built | nodes and users write addresses in every case |
| a mixed-case string must have a valid EIP-55 checksum | a typo in a checksummed address is caught |
| sent in its EIP-55 form | the one form every wallet takes, and nodes read any case |
| `Address.checksum(address)` for display | the EIP-55 form is for people and for the wire, not for comparison |

An address in a result is decoded to lowercase too, so it compares with one you made. Its
letter case is not checked against EIP-55: nobody typed it. The schema for those is
[`Address.wireSchema`](https://ethect.dev/reference/primitives/address/#wireschema), and `Address.schema` is the
one for an address a person wrote.

Every address a method sends is in its EIP-55 form: its params, the keys of a state override,
a log filter, the typed data a wallet signs. A wallet such as Ambire looks an account up by its
checksum, and does not find it in lowercase. The checksum costs a keccak the first time, and
the last 8,192 are kept, so an address sent again costs a lookup.

## Hashes and bytes

A hash and any other byte string are `` `0x${string}` `` in the type, so a literal works.

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

const transaction = Eth.getTransactionByHash(
  "0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b",
);
```

A hash is checked to be exactly 32 bytes when it is encoded. One of another length fails with
a `CodecError` on the params.

Hex in a result is decoded to lowercase, so two hashes read from a node compare with `===`.
What you write is sent in the case you wrote it.

## Names instead of codes

Where the wire uses a hex code for a small set of cases, the code is given a name.

| Field | On the wire | In code |
| --- | --- | --- |
| transaction `type` | `"0x0"`, `"0x1"`, `"0x2"`, `"0x3"`, `"0x4"` | `"legacy"`, `"eip2930"`, `"eip1559"`, `"eip4844"`, `"eip7702"` |
| receipt `status` | `"0x1"`, `"0x0"` | `"success"`, `"reverted"` |
| simulated call `status` | `"0x1"`, `"0x0"` | `"success"`, `"failure"` |

The names are what TypeScript narrows on: a transaction whose `type` is `"eip1559"` has
`maxFeePerGas`.

A code is matched by value, so `"0x02"` is `"eip1559"` too. A transaction a node sends without
a `type` is read as `"legacy"`.

A transaction of a type the library has no name for still decodes. Its `type` is the raw type
byte, and it carries the fields every transaction has.

## Fields that can be missing

A field is typed in one of three ways, and the type tells you which check to write.

| Typed | Means | Test it with |
| --- | --- | --- |
| `T` | always there | nothing |
| `T \| null` | always there, and `null` in a known situation: a pending transaction has no `blockNumber`, a contract creation has no `to` | `=== null` |
| `T` (optional) | absent when the node leaves it out or sends `null`: a field a later fork added, or one only some clients send | `=== undefined` |

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

const describe = Effect.gen(function* () {
  const block = yield* Eth.getBlockByNumber("pending", false);
  if (block === null) return "no pending block";
  // number is `bigint | null`, baseFeePerGas is `bigint | undefined`
  const fee = block.baseFeePerGas ?? "no base fee";
  return `${block.number ?? "pending"}: ${fee}`;
});
```

An optional field that the node sent as `null` is absent from the decoded object, not `null`.
There is one way to be missing.

## Unknown fields

A response may hold fields the library does not know: an L2 adds its own to blocks, receipts
and transactions.

- **They are dropped.** The result holds the fields of the schema and nothing else, so the
  types never lie about what is there.
- **They are not an error.** Only a missing or mistyped field of the schema fails a decode.
- **To read them**, define the method with a schema that names them: see
  [A wider result](https://ethect.dev/rpc/guides/custom-methods/#a-wider-result).

## The schemas are yours to use

Every wire type exports its schema, so you can decode data that did not come through a method:
a webhook payload, a file, a message from another service.

```ts
import { Effect, Schema } from "effect";
import { Log } from "@ethect/core/rpc";

const fromWebhook = (payload: unknown) =>
  Schema.decodeUnknownEffect(Schema.Array(Log.schema))(payload);
// Effect<ReadonlyArray<Log.Log>, Schema.SchemaError>
```

`Schema.encodeEffect` goes the other way, from native values back to the wire.

## Next steps

- [Types](https://ethect.dev/rpc/types/): every wire type, with its fields.
- [`Address`](https://ethect.dev/reference/primitives/address/), [`Quantity`](https://ethect.dev/reference/primitives/quantity/),
  [`Hex`](https://ethect.dev/reference/primitives/hex/), [`Hash`](https://ethect.dev/reference/primitives/hash/): the primitives.
- [Custom methods](https://ethect.dev/rpc/guides/custom-methods/): choosing schemas for a method of your own.
