Skip to content

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 for those.

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:

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

The one method of the library defined this way is Wallet.watchAsset.

Method.Method<Name, Params, Result, Names>

A method that takes one argument, the params object, and returns an Effect<Result, Method.Error, Transport>.

  • Type: a tuple type with one label (optional)

The name of the one argument: what an editor shows for it. As in Method.make, the type in the tuple is not read, and the names have a call of their own.

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.

  • Type: string

The name on the wire.

import { Schema } from "effect";
import { Method } from "@ethect/core/rpc";
const ping = Method.makeByName()(
"acme_ping",
Schema.Struct({ echo: Schema.String }),
Schema.String,
);
  • Type: Schema

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

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,
);
  • Type: Schema

The schema of the response’s result.

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, …>
  • 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 before anything is sent.