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 variantsconst 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 hashesgetBlock("latest", true); // Effect<Block.Full, …>: transactions in fullEach 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.
Returns
Section titled “Returns”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; // 3Eth.getBlockByNumber.variants[0].result; // the schema of Block | nullAn overloaded method has no params and no result of its own: read them on a variant.
Parameters
Section titled “Parameters”variants
Section titled “variants”- 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>, …>Behavior
Section titled “Behavior”- First match wins. At run time the arguments are tested against each variant’s
paramsschema 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
CodecErrorthat names the params that did not fit. - Trailing
undefinedis ignored before matching, as in a plain call. - A value typed
booleanmatches neither atruenor afalsevariant. Add a last variant that takesSchema.Booleanand 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.