# Method definitions (Method)

A method definition. The wire name of a JSON-RPC method with the schemas of its params and result, which is also the function that calls it.

A `Method` is the wire name of a JSON-RPC method with the schemas of its params and result. It
is also the function that calls it, so every export of a namespace such as
[`Eth`](https://ethect.dev/rpc/eth/) is one.

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

Eth.getBalance.method; // "eth_getBalance"
Eth.getBalance.params; // the schema of [address, block?]
Eth.getBalance.result; // the schema of a quantity, decoded to bigint

// (address, block?) => Effect<bigint, Method.Error, Transport>
const call = Eth.getBalance;
```

## Functions

| Function | Returns | Description |
| --- | --- | --- |
| [`Method.make<Names>()(method, params, result)`](https://ethect.dev/rpc/method/make/) | `Method` | Defines a method with positional params, under the names given. |
| [`Method.makeByName<Names>()(method, params, result)`](https://ethect.dev/rpc/method/make-by-name/) | `Method` | Defines a method whose params are one object sent by name. |
| [`Method.overload(...variants)`](https://ethect.dev/rpc/method/overload/) | overloaded function | One method whose result type depends on its params. |

## Calling a method

A call takes the params in order and returns an `Effect`. Nothing is sent until the effect
runs.

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

const chainId: Effect.Effect<number, Method.Error, Transport.Transport> = Eth.chainId();
```

The three type parameters of the effect say everything about the call:

| Part | Type | Meaning |
| --- | --- | --- |
| success | the decoded result, here `number` | what the method's `result` schema produces |
| error | `Method.Error` | [`RpcError`](https://ethect.dev/reference/errors/rpc-error/), [`TransportError`](https://ethect.dev/reference/errors/transport-error/) or [`CodecError`](https://ethect.dev/reference/errors/codec-error/) |
| requirement | [`Transport`](https://ethect.dev/reference/transport/#transport) | the service that carries the call, provided by a transport layer |

When the effect runs, the call goes through three steps:

1. **Encode.** The arguments go through the `params` schema: a `bigint` becomes a hex quantity,
   an address is written out. Arguments that do not fit fail with a `CodecError` on `"params"`,
   and nothing is sent.
2. **Request.** The transport sends the wire name and the encoded params. A JSON-RPC error
   fails with `RpcError`, no answer fails with `TransportError`.
3. **Decode.** The answer goes through the `result` schema. An answer of another shape fails
   with a `CodecError` on `"result"`.

- **Optional params.** An argument left `undefined` at the end of the list is not sent:
  `Eth.getBalance(address)` sends one param.
- **`params` is always sent.** A method without params sends `"params": []`.
- **A span per call.** Each call runs inside an Effect span named after the wire method, of
  kind `client`. Nothing about tracing is added to the request.

## Properties

### method

- **Type:** `string` (a literal, such as `"eth_getBalance"`)

The name on the wire.

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

Eth.signTypedDataV4.method; // "eth_signTypedData_v4"
Anvil.sendUnsignedTransaction.method; // "eth_sendUnsignedTransaction"
```

### params

- **Type:** an Effect `Schema`

The schema of the request's `params`: a tuple for positional params, a struct for a method
defined with [`makeByName`](https://ethect.dev/rpc/method/make-by-name/).

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

const wire = Effect.gen(function* () {
  const encode = Schema.encodeEffect(Eth.getBalance.params);
  return yield* encode([Address.zero, 255n]);
  // ["0x0000000000000000000000000000000000000000", "0xff"]
});
```

### result

- **Type:** an Effect `Schema`

The schema of the response's `result`.

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

const decoded = Effect.gen(function* () {
  const decode = Schema.decodeUnknownEffect(Eth.getBalance.result);
  return yield* decode("0x21e19e0c9bab2400000"); // 10000000000000000000000n
});
```

## Types

| Type | Is |
| --- | --- |
| `Method.Method<Name, Params, Result, Names>` | a definition: the function plus `method`, `params` and `result`. `Names` holds the [names of its params](https://ethect.dev/rpc/method/make/#names) |
| `Method.Any` | any method, whatever its schemas: the bound of a generic that takes a method |
| `Method.Args<Params>` | the arguments of a call: the positional params spread out, or the one by-name object |
| `Method.Error` | what calling a method can fail with: `RpcError \| TransportError \| CodecError` |
| `Method.Overloaded<Variants, Key>` | what [`Method.overload`](https://ethect.dev/rpc/method/overload/) and [`Subscription.overload`](https://ethect.dev/rpc/subscription/overload/) return: the call signatures of the variants, the name they share (`"method"` or `"kind"`) and the `variants` |
| `Method.optional<S>` | the schema of a field, or a trailing param, that may be absent. A key set to `undefined`, which only a provider in the page can send, decodes as an absent key, and nothing is encoded as `undefined`. The schemas of the wire types name such a field by it |
| `Method.nullish<S>` | the schema of a field some nodes leave out and others send as `null`: both decode to an absent key, as `undefined` does. The schemas of the wire types name such a field by it |

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

// A helper that accepts any method and reads its wire name.
const nameOf = (method: Method.Any): string => method.method;
```

`Method.Overloaded`, `Method.optional` and `Method.nullish` are names you read more than you
write. A hover on [`Eth.getBlockByNumber`](https://ethect.dev/rpc/eth/get-block-by-number/) shows the first, and
a hover on the schema of a [wire type](https://ethect.dev/rpc/types/) shows the others on each field of that
kind.

```ts
import type { Quantity } from "@ethect/core";
import { Eth, Log, type Method } from "@ethect/core/rpc";

// the timestamp of a log: absent from some nodes, `null` from others
const timestamp: Method.nullish<Quantity.schema> = Log.schema.fields.blockTimestamp;

// any overloaded method: its variants, under the wire name they share
type AnyOverloaded = Method.Overloaded<readonly [Method.Any, ...Array<Method.Any>], "method">;
const count = (method: AnyOverloaded): number => method.variants.length;

count(Eth.getBlockByNumber); // 3
```
