# Method.make

Defines a JSON-RPC method with positional params, from the names of its params, its wire
name, the schemas of its params and the schema of its result.

## Usage

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

/** Sets an account's balance, in wei. */
const setBalance = Method.make<[address: unknown, balance: unknown]>()(
  "anvil_setBalance",
  [Address.wireSchema, Quantity.schema],
  Schema.Null,
);

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

const program = Effect.gen(function* () {
  yield* setBalance(ADDRESS, 1n);
  return yield* 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,
);
// 1n
```

The definition is a function: `setBalance(address, 1n)` is typed from the two schemas and works
over every transport. The examples below leave the transport out.

`Method.make` is called twice: first with the [names](#names) of the params, as a type, then
with the definition.

### An optional param

Wrap the schema of a trailing param in `Schema.optionalKey` to let the caller leave it out,
and write its name with a `?`.

```ts
import { Schema } from "effect";
import { Address, Quantity } from "@ethect/core";
import { BlockId, Method } from "@ethect/core/rpc";

/** The number of transactions an account has sent. */
const getNonce = Method.make<[address: unknown, block?: unknown]>()(
  "eth_getTransactionCount",
  [Address.wireSchema, Schema.optionalKey(BlockId.schema)],
  Quantity.numberSchema,
);

getNonce(Address.zero); // sends one param
getNonce(Address.zero, "pending"); // sends two
```

Optional params come last. A required param after an optional one does not compile.

```ts
import { Schema } from "effect";
import { Quantity } from "@ethect/core";
import { BlockId, Method } from "@ethect/core/rpc";

const misordered = Method.make()(
  "eth_getBlockTransactionCountByNumber",
  // "A required param cannot follow an optional one"
  // @ts-expect-error
  [Schema.optionalKey(BlockId.numberOrTagSchema), Quantity.schema],
  Schema.NullOr(Quantity.numberSchema),
);
```

### Without names

Leave the type argument out to define a method without naming its params. It takes the same
arguments, which an editor then shows as `args_0`, `args_1`.

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

/** Sets the code of an account. */
const setCode = Method.make()("anvil_setCode", [Address.wireSchema, Hex.schema], Schema.Null);

setCode(Address.zero, "0x00"); // (args_0: Address, args_1: Hex) => Effect<null, …>
```

### A result of your own shape

Any schema works as a result. Reuse the fields of a wire type to add what your chain adds.

```ts
import { Schema } from "effect";
import { Hash, Quantity } from "@ethect/core";
import { Method, Receipt } from "@ethect/core/rpc";

// A rollup receipt that also reports what the transaction cost on L1.
const RollupReceipt = Schema.Struct({
  ...Receipt.schema.fields,
  l1Fee: Quantity.schema,
});

const getRollupReceipt = Method.make<[hash: unknown]>()(
  "eth_getTransactionReceipt",
  [Hash.schema],
  Schema.NullOr(RollupReceipt),
);
```

## Returns

[`Method.Method<Name, Schema.Tuple<Params>, Result, Names>`](https://ethect.dev/rpc/method/)

A method: call it with the params in order to get an
[`Effect<Result, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method). Its `method`,
`params` and `result` properties hold what you passed.

## Type parameters

### Names

- **Type:** a tuple type with a label per param (optional)

The names of the params, in order. They are what an editor shows for the arguments of the
method, in place of `args_0` and `args_1`.

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

const setCode = Method.make<[address: unknown, code: unknown]>()(
  "anvil_setCode",
  [Address.wireSchema, Hex.schema],
  Schema.Null,
);
// setCode(address: Address, code: Hex)
```

- **The types in the tuple are not read.** Write `unknown`: the schemas type the arguments.
- **An optional param has an optional name**: `[address: unknown, block?: unknown]`.
- **The names are checked against the params.** One name per param, optional where the param
  is. Anything else does not compile.

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

const setCode = Method.make<[address: unknown]>()(
  "anvil_setCode",
  // "The names are not those of the params"
  // @ts-expect-error
  [Address.wireSchema, Hex.schema],
  Schema.Null,
);
```

- **The names have a call of their own.** TypeScript infers the schemas only when no type
  argument is written next to them, so the names are given first and the definition after.
  State them once for several definitions that share them, as the variants of an
  [overload](https://ethect.dev/rpc/method/overload/) do.

## Parameters

### method

- **Type:** `string`

The name on the wire. It is kept as a literal type.

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

const getAutomine = Method.make()(
  "anvil_getAutomine",
  [],
  Schema.Boolean,
);

getAutomine.method; // "anvil_getAutomine"
```

### params

- **Type:** `ReadonlyArray<Schema>`

One schema per positional param, in order. An empty array defines a method without params.

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

const setCode = Method.make<[address: unknown, code: unknown]>()(
  "anvil_setCode",
  [Address.wireSchema, Hex.schema],
  Schema.Null,
);
```

The schemas decide the argument types of the function: here `(address: Address, code: Hex)`.

### result

- **Type:** `Schema`

The schema of the response's `result`. Its decoded type is the success type of the call.

```ts
import { Schema } from "effect";
import { Quantity } from "@ethect/core";
import { Method } from "@ethect/core/rpc";

const blockNumber = Method.make()(
  "eth_blockNumber",
  [],
  Quantity.schema,
);

blockNumber(); // Effect<bigint, Method.Error, Transport>
```

## Behavior

- **Pure.** Defining a method sends nothing and has no side effect. A module of definitions can
  be imported anywhere.
- **Names are types.** They are gone once compiled: a named definition and an unnamed one run
  the same code.
- **Both directions are checked.** Arguments that do not fit the `params` schemas fail with a
  [`CodecError`](https://ethect.dev/reference/errors/codec-error/) before anything is sent, and an answer that
  does not fit `result` fails with one after.
- **Trailing `undefined` is dropped.** An optional param left out is absent from the request,
  not sent as `null`.
- **Optional params come last.** The order is checked by the types. A definition that gets
  past them, through a cast, fails every call with a `CodecError` on the params.
- **`Schema.Null` is strict.** A method defined with it fails with a `CodecError` when the node
  answers anything but `null`. Use `Schema.Void` to ignore the answer.
- **Schemas need no services.** A param or result schema whose decoding requires a service
  from the Effect context is not accepted.

See [Custom methods](https://ethect.dev/rpc/guides/custom-methods/) for when to define one and how to choose the
schemas.
