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.
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.
What it says it can do
Section titled “What it says it can do”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 |
Several calls as one
Section titled “Several calls as one”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 |
A call that would revert
Section titled “A call that would revert”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.
Another account
Section titled “Another account”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] })));});Returns
Section titled “Returns”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.
Parameters
Section titled “Parameters”options.atomic (optional)
Section titled “options.atomic (optional)”- Type:
"supported" | "ready" | "unsupported" - Default:
"unsupported"
What it says of several calls as one, and what it does with them: see the table above.
options.unattended (optional)
Section titled “options.unattended (optional)”- 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.
options.accounts (optional)
Section titled “options.accounts (optional)”- 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.
Behavior
Section titled “Behavior”- 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,signTypedDataandsignTransactionfail. - Gas is the node’s estimate, unless set. The other fields of
transactionare 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
statusthat finds the transaction mined or dropped, and at the latest when the layer is released. SeeTestWalletfor what an account with that code does. - A reorg does not run the calls again. Once their transaction is mined and
statushas 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:
statusasks the node, and anyTestSigneranswers for it. Theidgiven tosubmitis not used. - It has no capability. A capability asked for without
optionalis 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.
Errors
Section titled “Errors”| 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 |
Under the hood
Section titled “Under the hood”Every request goes to the node of the Transport in context, when submit or status runs.
accounts,capabilities- your programno request: what the options say
submit, one call- your node
eth_estimateGas(call)unlesstransaction.gasis set - your node
eth_sendTransaction(call)gives its hash
- your node
submit, several, not as one- your nodethe same two for each call, in ordergives one hash per call
submit, several as one- your node
eth_estimateGas(batch)the batch code on the account as a state override - your node
eth_getCode(account)its own code, kept - your node
anvil_setCode(account, batch)the code that runs the calls - your node
eth_sendTransaction({ from: account, to: account, data: calls })gives a handle: its id is that hash
- your node
status(handle)- your node
eth_getTransactionReceipt(id) none
- your node
eth_getTransactionByHash(id)pending or dropped
- your node
mined or dropped
- your node
anvil_setCode(account, its own code)
- your node
- your node
when the layer is released
- your node
anvil_setCode(account, its own code)for each account left
- your node