Skip to content

TestSigner

A Signer over the accounts a node has unlocked, for tests of sending. It holds no key and has no signing code: the node signs. What it says it can do is what the test asks, so each path of Tx can be taken.

import { TestSigner } from "@ethect/core/testing";
Export Kind Description
TestSigner.layer(options?) layer A signer that reports the atomic and unattended it is given.
TestSigner.Options type atomic, unattended and accounts, all optional.

A transfer, sent with Tx.send: the signer in context signs it, the node mines it, and the test gets its receipt.

transfer.test.ts
import { NodeServices } from "@effect/platform-node";
import { assert, layer } from "@effect/vitest";
import { Effect, Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Call, Transport, Tx } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
import { AnvilNode, TestSigner } from "@ethect/core/testing";
// A fresh anvil per test, and an HTTP transport that reaches it.
const Node = AnvilNode.layer().pipe(Layer.provide(NodeServices.layer));
const Http = Layer.unwrap(
Effect.map(AnvilNode.AnvilNode, (node) => Transport.http(node.httpUrl)),
).pipe(Layer.provide(FetchHttpClient.layer));
const Chain = Layer.provideMerge(Http, Node);
// A signer over the accounts of that node: no key anywhere.
const Dev = Layer.merge(Chain, TestSigner.layer({ unattended: true }));
layer(Dev, { buildPerTest: true, excludeTestServices: true })("a transfer", (it) => {
it.effect("is sent from the signer's first account", () =>
Effect.gen(function* () {
const [alice, bob] = yield* Eth.accounts();
// sent from the signer's first account, and waited for
const receipt = yield* Tx.send(Call.make({ to: bob!, value: 1n }));
assert.strictEqual(receipt.status, "success");
assert.strictEqual(receipt.from, alice);
}),
);
});

The examples below leave the node out.

capabilities answers what the options say. That answer is what Tx.plan reads to decide how many signatures calls take.

Options It says Several calls
none atomic: "unsupported", unattended: false one transaction each: a batch as one is refused
{ unattended: true } signs without asking anyone, as a local key does one transaction each, sent in a row
{ atomic: "supported" } runs several calls as one one transaction for all, all or nothing
{ atomic: "ready" } could, after an upgrade as "supported": there is no user to ask

With atomic: "supported", calls submitted as one go out as one transaction. The answer is a handle, which only the signer can resolve: its id is the hash of that transaction.

import { Effect } from "effect";
import { type Call, Tx } from "@ethect/core";
import { TestSigner } from "@ethect/core/testing";
const Batching = TestSigner.layer({ atomic: "supported" });
const together = (calls: ReadonlyArray<Call.Call<unknown, unknown>>) =>
Effect.gen(function* () {
const pending = yield* Tx.submit(calls);
// { _tag: "Handle", kind: "batch", id: the hash, from, chainId }
return yield* Tx.wait(pending); // asks the signer its `status`: first, then at each block
// one receipt: the calls ran as one transaction
}).pipe(Effect.provide(Batching));
status When
pending the node holds the transaction
included it is mined, and did not revert
reverted it is mined, and a call reverted: nothing of the batch is kept
dropped the node no longer knows it

The signer asks the node for the gas of each transaction first. A revert found there fails with Signer.Estimate.Reverted, which holds the revert data, and nothing is sent.

import { Effect } from "effect";
import { type Call, Tx } from "@ethect/core";
const careful = (call: Call.Call<unknown, unknown>) =>
Tx.submit(call).pipe(
Effect.map((pending) => `sent: ${pending._tag}`),
// `Tx.submit` reads the revert with the errors of the call: nothing was sent
Effect.catchTag("TxReverted", (reverted) =>
Effect.succeed(`call ${reverted.call} would revert: ${reverted.message}`),
),
);

Set transaction.gas to send a call that reverts anyway: what is set is not estimated, and anvil mines the transaction as reverted.

The default accounts are the ten of anvil. To send as any other account, have the node unlock it and give it to the signer.

import { Effect } from "effect";
import { Address, Signer } from "@ethect/core";
import { Anvil } from "@ethect/core/rpc";
import { TestSigner } from "@ethect/core/testing";
const WHALE = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const asWhale = Effect.gen(function* () {
yield* Anvil.setBalance(WHALE, 10n ** 18n);
yield* Anvil.impersonateAccount(WHALE);
// the signer's first account is the whale: what it sends, the node signs for it
return yield* Signer.address.pipe(Effect.provide(TestSigner.layer({ accounts: [WHALE] })));
});

Layer<Signer>

A layer that provides the Signer service. It needs nothing: it sends on the Transport of whoever calls it, so one signer serves several chains.

  • Type: "supported" | "ready" | "unsupported"
  • Default: "unsupported"

What it says of several calls as one, and what it does with them: see the table above.

  • Type: boolean
  • Default: false

Whether it says it signs without asking anyone, as a local key does. It changes what it reports and nothing of what it does: it never asks anyone.

  • Type: ReadonlyArray<Address.Address>
  • Default: the ten accounts of anvil

The accounts it holds, the first being the sender when no from is given. The node must have them unlocked. Pass them for a node started with other accounts, or for an account unlocked with Anvil.impersonateAccount.

  • No key. The node signs what it is sent, with the key of an account it has unlocked. The signer signs nothing that is not sent: signMessage, signTypedData and signTransaction fail.
  • Gas is the node’s estimate, unless set. The other fields of transaction are passed on as they are. With one set of fields and several calls sent as one, the set is for that transaction. With a list, each call has its own.
  • Several calls as one really are one transaction. The signer puts a small contract at the account’s own address, has the account send itself one transaction that carries the calls, and puts the account’s own code back: on the first status that finds the transaction mined or dropped, and at the latest when the layer is released. See TestWallet for what an account with that code does.
  • A reorg does not run the calls again. Once their transaction is mined and status has seen it, the account has its own code back: the same transaction mined again in another block does nothing. A test of a reorg uses a plain transaction, one call at a time.
  • A handle is not stored. Its id is the hash of the transaction: status asks the node, and any TestSigner answers for it. The id given to submit is not used.
  • It has no capability. A capability asked for without optional is refused.
  • The accounts are not read from the node. A signer has no transport of its own to ask with. An account the node has not unlocked is refused by the node, when sending.
Error Reason When
Signer.Estimate.Reverted a call would revert: nothing was sent. call is its index, data what it reverted with
Tx.Unsupported "atomic" several calls as one, with atomic: "unsupported", or with a deployment among them
Tx.Unsupported "capability" a capability asked for without optional
Tx.Unsupported "signing" signTransaction
Tx.Partial several calls, not as one: a call after the first was refused. sent holds what went out
Signer.Error "Unknown" signMessage, signTypedData, or status for a handle of another kind
Signer.Error "Disconnected" status could not reach the node
RpcError, TransportError the node refused a transaction, or did not answer

Every request goes to the node of the Transport in context, when submit or status runs.

  1. accounts, capabilities

    1. your programno request: what the options say
  2. submit, one call

    1. your nodeeth_estimateGas(call)unless transaction.gas is set
    2. your nodeeth_sendTransaction(call)gives its hash
  3. submit, several, not as one

    1. your nodethe same two for each call, in ordergives one hash per call
  4. submit, several as one

    1. your nodeeth_estimateGas(batch)the batch code on the account as a state override
    2. your nodeeth_getCode(account)its own code, kept
    3. your nodeanvil_setCode(account, batch)the code that runs the calls
    4. your nodeeth_sendTransaction({ from: account, to: account, data: calls })gives a handle: its id is that hash
  5. status(handle)

    1. your nodeeth_getTransactionReceipt(id)
    2. none

      1. your nodeeth_getTransactionByHash(id)pending or dropped
    3. mined or dropped

      1. your nodeanvil_setCode(account, its own code)
  6. when the layer is released

    1. your nodeanvil_setCode(account, its own code)for each account left