# Method.makeByName

Defines a JSON-RPC method whose params are one object sent by name, such as
`wallet_watchAsset`. Most methods take their params by position: use
[`Method.make`](https://ethect.dev/rpc/method/make/) for those.

## Usage

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

/** A method of a private API that reads its params by name. */
const getQuote = Method.makeByName<[request: unknown]>()(
  "acme_getQuote",
  Schema.Struct({ pair: Schema.String, depth: Schema.optionalKey(Schema.Number) }),
  Schema.Struct({ bid: Schema.String, ask: Schema.String }),
);

const quote = getQuote({ pair: "ETH/USD" });
// Effect<{ bid: string; ask: string }, Method.Error, Transport>
```

The request carries the object itself as `params`, not an array:

```json
{ "jsonrpc": "2.0", "id": 0, "method": "acme_getQuote", "params": { "pair": "ETH/USD" } }
```

The one method of the library defined this way is
[`Wallet.watchAsset`](https://ethect.dev/rpc/wallet/watch-asset/).

## Returns

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

A method that takes one argument, the params object, and returns an
[`Effect<Result, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method).

## Type parameters

### Names

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

The name of the one argument: what an editor shows for it. As in
[`Method.make`](https://ethect.dev/rpc/method/make/#names), the type in the tuple is not read, and the names
have a call of their own.

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

const getAllowance = Method.makeByName<[allowance: unknown]>()(
  "acme_getAllowance",
  Schema.Struct({ owner: Address.wireSchema, spender: Address.wireSchema }),
  Quantity.schema,
);
// getAllowance(allowance: { owner: Address; spender: Address })
```

Without it, `Method.makeByName()(…)`, the argument is named `params`.

## Parameters

### method

- **Type:** `string`

The name on the wire.

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

const ping = Method.makeByName()(
  "acme_ping",
  Schema.Struct({ echo: Schema.String }),
  Schema.String,
);
```

### params

- **Type:** `Schema`

The schema of the whole `params` object, usually a `Schema.Struct`.

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

const getAllowance = Method.makeByName()(
  "acme_getAllowance",
  Schema.Struct({ owner: Address.wireSchema, spender: Address.wireSchema }),
  Quantity.schema,
);
```

### result

- **Type:** `Schema`

The schema of the response's `result`.

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

const getAllowance = Method.makeByName()(
  "acme_getAllowance",
  Schema.Struct({ owner: Address.wireSchema, spender: Address.wireSchema }),
  Quantity.schema,
);

// (params: { owner: Address; spender: Address }) => Effect<bigint, …>
```

## Behavior

- **One argument.** The function takes the object, whatever the number of fields.
- **Sent as given.** The object is encoded through the schema and sent as `params`. No field is
  added or reordered.
- **Same checks as `make`.** Params that do not fit fail with a
  [`CodecError`](https://ethect.dev/reference/errors/codec-error/) before anything is sent.
