Skip to content

Method.overload

Groups several definitions of one JSON-RPC method into a single function whose result type depends on its params. A call goes to the first variant whose params accept the arguments.

import { Schema } from "effect";
import { Block, BlockId, Method } from "@ethect/core/rpc";
const tag = BlockId.numberOrTagSchema;
// the names of the params, said once for both variants
const define = Method.make<[block: unknown, full: unknown]>();
const getBlock = Method.overload(
define("eth_getBlockByNumber", [tag, Schema.Literal(false)], Block.schema),
define("eth_getBlockByNumber", [tag, Schema.Literal(true)], Block.fullSchema),
);
getBlock("latest", false); // Effect<Block.Block, …>: transactions as hashes
getBlock("latest", true); // Effect<Block.Full, …>: transactions in full

Each variant is a complete definition. TypeScript sees one function with one overload per variant, in the order given, each under the names of its own definition.

Method.Overloaded<Variants, "method">

A function with the call signatures of every variant, plus two properties:

Property Type Holds
method string the wire name of the first variant
variants a tuple of Method the definitions, in order
import { Eth } from "@ethect/core/rpc";
Eth.getBlockByNumber.method; // "eth_getBlockByNumber"
Eth.getBlockByNumber.variants.length; // 3
Eth.getBlockByNumber.variants[0].result; // the schema of Block | null

An overloaded method has no params and no result of its own: read them on a variant.

  • Type: one or more Method, as separate arguments

The definitions to group. They normally share a wire name and differ by a literal in their params.

import { Schema } from "effect";
import { Hash } from "@ethect/core";
import { Method, Transaction } from "@ethect/core/rpc";
const full = Schema.Literal(true);
const hashes = Schema.optionalKey(Schema.Literal(false));
const pending = Method.overload(
Method.make<[full: unknown]>()("acme_pending", [full], Schema.Array(Transaction.schema)),
Method.make<[full?: unknown]>()("acme_pending", [hashes], Schema.Array(Hash.schema)),
);
pending(); // Effect<ReadonlyArray<Hash>, …>
pending(true); // Effect<ReadonlyArray<Transaction>, …>
  • First match wins. At run time the arguments are tested against each variant’s params schema in order, and the first that accepts them makes the call. Put the most specific variant first.
  • No match falls back to the first variant, whose encoding then fails with a CodecError that names the params that did not fit.
  • Trailing undefined is ignored before matching, as in a plain call.
  • A value typed boolean matches neither a true nor a false variant. Add a last variant that takes Schema.Boolean and returns the union, as the two block methods do. At run time the literal variants before it still make the call.

The library’s overloaded methods are Eth.getBlockByNumber, Eth.getBlockByHash, Eth.simulateV1 and the Debug.trace* methods, which have one variant per tracer.