Method.make
Defines a JSON-RPC method with positional params, from the names of its params, its wire name, the schemas of its params and the schema of its result.
import { Effect, Schema } from "effect";import { FetchHttpClient } from "effect/http";import { Address, Quantity, Transport } from "@ethect/core";import { Eth, Method } from "@ethect/core/rpc";
/** Sets an account's balance, in wei. */const setBalance = Method.make<[address: unknown, balance: unknown]>()( "anvil_setBalance", [Address.wireSchema, Quantity.schema], Schema.Null,);
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Effect.gen(function* () { yield* setBalance(ADDRESS, 1n); return yield* Eth.getBalance(ADDRESS, "latest");});
const balance = await program.pipe( Effect.provide(Transport.http("http://127.0.0.1:8545")), Effect.provide(FetchHttpClient.layer), Effect.runPromise,);// 1nThe definition is a function: setBalance(address, 1n) is typed from the two schemas and works
over every transport. The examples below leave the transport out.
Method.make is called twice: first with the names of the params, as a type, then
with the definition.
An optional param
Section titled “An optional param”Wrap the schema of a trailing param in Schema.optionalKey to let the caller leave it out,
and write its name with a ?.
import { Schema } from "effect";import { Address, Quantity } from "@ethect/core";import { BlockId, Method } from "@ethect/core/rpc";
/** The number of transactions an account has sent. */const getNonce = Method.make<[address: unknown, block?: unknown]>()( "eth_getTransactionCount", [Address.wireSchema, Schema.optionalKey(BlockId.schema)], Quantity.numberSchema,);
getNonce(Address.zero); // sends one paramgetNonce(Address.zero, "pending"); // sends twoOptional params come last. A required param after an optional one does not compile.
import { Schema } from "effect";import { Quantity } from "@ethect/core";import { BlockId, Method } from "@ethect/core/rpc";
const misordered = Method.make()( "eth_getBlockTransactionCountByNumber", // "A required param cannot follow an optional one" // @ts-expect-error [Schema.optionalKey(BlockId.numberOrTagSchema), Quantity.schema], Schema.NullOr(Quantity.numberSchema),);Without names
Section titled “Without names”Leave the type argument out to define a method without naming its params. It takes the same
arguments, which an editor then shows as args_0, args_1.
import { Schema } from "effect";import { Address, Hex } from "@ethect/core";import { Method } from "@ethect/core/rpc";
/** Sets the code of an account. */const setCode = Method.make()("anvil_setCode", [Address.wireSchema, Hex.schema], Schema.Null);
setCode(Address.zero, "0x00"); // (args_0: Address, args_1: Hex) => Effect<null, …>A result of your own shape
Section titled “A result of your own shape”Any schema works as a result. Reuse the fields of a wire type to add what your chain adds.
import { Schema } from "effect";import { Hash, Quantity } from "@ethect/core";import { Method, Receipt } from "@ethect/core/rpc";
// A rollup receipt that also reports what the transaction cost on L1.const RollupReceipt = Schema.Struct({ ...Receipt.schema.fields, l1Fee: Quantity.schema,});
const getRollupReceipt = Method.make<[hash: unknown]>()( "eth_getTransactionReceipt", [Hash.schema], Schema.NullOr(RollupReceipt),);Returns
Section titled “Returns”Method.Method<Name, Schema.Tuple<Params>, Result, Names>
A method: call it with the params in order to get an
Effect<Result, Method.Error, Transport>. Its method,
params and result properties hold what you passed.
Type parameters
Section titled “Type parameters”- Type: a tuple type with a label per param (optional)
The names of the params, in order. They are what an editor shows for the arguments of the
method, in place of args_0 and args_1.
import { Schema } from "effect";import { Address, Hex } from "@ethect/core";import { Method } from "@ethect/core/rpc";
const setCode = Method.make<[address: unknown, code: unknown]>()( "anvil_setCode", [Address.wireSchema, Hex.schema], Schema.Null,);// setCode(address: Address, code: Hex)- The types in the tuple are not read. Write
unknown: the schemas type the arguments. - An optional param has an optional name:
[address: unknown, block?: unknown]. - The names are checked against the params. One name per param, optional where the param is. Anything else does not compile.
import { Schema } from "effect";import { Address, Hex } from "@ethect/core";import { Method } from "@ethect/core/rpc";
const setCode = Method.make<[address: unknown]>()( "anvil_setCode", // "The names are not those of the params" // @ts-expect-error [Address.wireSchema, Hex.schema], Schema.Null,);- The names have a call of their own. TypeScript infers the schemas only when no type argument is written next to them, so the names are given first and the definition after. State them once for several definitions that share them, as the variants of an overload do.
Parameters
Section titled “Parameters”method
Section titled “method”- Type:
string
The name on the wire. It is kept as a literal type.
import { Schema } from "effect";import { Method } from "@ethect/core/rpc";
const getAutomine = Method.make()( "anvil_getAutomine", [], Schema.Boolean,);
getAutomine.method; // "anvil_getAutomine"params
Section titled “params”- Type:
ReadonlyArray<Schema>
One schema per positional param, in order. An empty array defines a method without params.
import { Schema } from "effect";import { Address, Hex } from "@ethect/core";import { Method } from "@ethect/core/rpc";
const setCode = Method.make<[address: unknown, code: unknown]>()( "anvil_setCode", [Address.wireSchema, Hex.schema], Schema.Null,);The schemas decide the argument types of the function: here (address: Address, code: Hex).
result
Section titled “result”- Type:
Schema
The schema of the response’s result. Its decoded type is the success type of the call.
import { Schema } from "effect";import { Quantity } from "@ethect/core";import { Method } from "@ethect/core/rpc";
const blockNumber = Method.make()( "eth_blockNumber", [], Quantity.schema,);
blockNumber(); // Effect<bigint, Method.Error, Transport>Behavior
Section titled “Behavior”- Pure. Defining a method sends nothing and has no side effect. A module of definitions can be imported anywhere.
- Names are types. They are gone once compiled: a named definition and an unnamed one run the same code.
- Both directions are checked. Arguments that do not fit the
paramsschemas fail with aCodecErrorbefore anything is sent, and an answer that does not fitresultfails with one after. - Trailing
undefinedis dropped. An optional param left out is absent from the request, not sent asnull. - Optional params come last. The order is checked by the types. A definition that gets
past them, through a cast, fails every call with a
CodecErroron the params. Schema.Nullis strict. A method defined with it fails with aCodecErrorwhen the node answers anything butnull. UseSchema.Voidto ignore the answer.- Schemas need no services. A param or result schema whose decoding requires a service from the Effect context is not accepted.
See Custom methods for when to define one and how to choose the schemas.