Method definitions (Method)
A Method is the wire name of a JSON-RPC method with the schemas of its params and result. It
is also the function that calls it, so every export of a namespace such as
Eth is one.
import { Eth } from "@ethect/core/rpc";
Eth.getBalance.method; // "eth_getBalance"Eth.getBalance.params; // the schema of [address, block?]Eth.getBalance.result; // the schema of a quantity, decoded to bigint
// (address, block?) => Effect<bigint, Method.Error, Transport>const call = Eth.getBalance;Functions
Section titled “Functions”| Function | Returns | Description |
|---|---|---|
Method.make<Names>()(method, params, result) |
Method |
Defines a method with positional params, under the names given. |
Method.makeByName<Names>()(method, params, result) |
Method |
Defines a method whose params are one object sent by name. |
Method.overload(...variants) |
overloaded function | One method whose result type depends on its params. |
Calling a method
Section titled “Calling a method”A call takes the params in order and returns an Effect. Nothing is sent until the effect
runs.
import { Effect } from "effect";import type { Transport } from "@ethect/core";import type { Method } from "@ethect/core/rpc";import { Eth } from "@ethect/core/rpc";
const chainId: Effect.Effect<number, Method.Error, Transport.Transport> = Eth.chainId();The three type parameters of the effect say everything about the call:
| Part | Type | Meaning |
|---|---|---|
| success | the decoded result, here number |
what the method’s result schema produces |
| error | Method.Error |
RpcError, TransportError or CodecError |
| requirement | Transport |
the service that carries the call, provided by a transport layer |
When the effect runs, the call goes through three steps:
- Encode. The arguments go through the
paramsschema: abigintbecomes a hex quantity, an address is written out. Arguments that do not fit fail with aCodecErroron"params", and nothing is sent. - Request. The transport sends the wire name and the encoded params. A JSON-RPC error
fails with
RpcError, no answer fails withTransportError. - Decode. The answer goes through the
resultschema. An answer of another shape fails with aCodecErroron"result".
- Optional params. An argument left
undefinedat the end of the list is not sent:Eth.getBalance(address)sends one param. paramsis always sent. A method without params sends"params": [].- A span per call. Each call runs inside an Effect span named after the wire method, of
kind
client. Nothing about tracing is added to the request.
Properties
Section titled “Properties”method
Section titled “method”- Type:
string(a literal, such as"eth_getBalance")
The name on the wire.
import { Anvil, Eth } from "@ethect/core/rpc";
Eth.signTypedDataV4.method; // "eth_signTypedData_v4"Anvil.sendUnsignedTransaction.method; // "eth_sendUnsignedTransaction"params
Section titled “params”- Type: an Effect
Schema
The schema of the request’s params: a tuple for positional params, a struct for a method
defined with makeByName.
import { Effect, Schema } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const wire = Effect.gen(function* () { const encode = Schema.encodeEffect(Eth.getBalance.params); return yield* encode([Address.zero, 255n]); // ["0x0000000000000000000000000000000000000000", "0xff"]});result
Section titled “result”- Type: an Effect
Schema
The schema of the response’s result.
import { Effect, Schema } from "effect";import { Eth } from "@ethect/core/rpc";
const decoded = Effect.gen(function* () { const decode = Schema.decodeUnknownEffect(Eth.getBalance.result); return yield* decode("0x21e19e0c9bab2400000"); // 10000000000000000000000n});| Type | Is |
|---|---|
Method.Method<Name, Params, Result, Names> |
a definition: the function plus method, params and result. Names holds the names of its params |
Method.Any |
any method, whatever its schemas: the bound of a generic that takes a method |
Method.Args<Params> |
the arguments of a call: the positional params spread out, or the one by-name object |
Method.Error |
what calling a method can fail with: RpcError | TransportError | CodecError |
Method.Overloaded<Variants, Key> |
what Method.overload and Subscription.overload return: the call signatures of the variants, the name they share ("method" or "kind") and the variants |
Method.optional<S> |
the schema of a field, or a trailing param, that may be absent. A key set to undefined, which only a provider in the page can send, decodes as an absent key, and nothing is encoded as undefined. The schemas of the wire types name such a field by it |
Method.nullish<S> |
the schema of a field some nodes leave out and others send as null: both decode to an absent key, as undefined does. The schemas of the wire types name such a field by it |
import type { Method } from "@ethect/core/rpc";
// A helper that accepts any method and reads its wire name.const nameOf = (method: Method.Any): string => method.method;Method.Overloaded, Method.optional and Method.nullish are names you read more than you
write. A hover on Eth.getBlockByNumber shows the first, and
a hover on the schema of a wire type shows the others on each field of that
kind.
import type { Quantity } from "@ethect/core";import { Eth, Log, type Method } from "@ethect/core/rpc";
// the timestamp of a log: absent from some nodes, `null` from othersconst timestamp: Method.nullish<Quantity.schema> = Log.schema.fields.blockTimestamp;
// any overloaded method: its variants, under the wire name they sharetype AnyOverloaded = Method.Overloaded<readonly [Method.Any, ...Array<Method.Any>], "method">;const count = (method: AnyOverloaded): number => method.variants.length;
count(Eth.getBlockByNumber); // 3