Quick start
Read from a local node, then send it a transaction. You need
@ethect/core and effect, and
Foundry for the node.
1. Start a node
Section titled “1. Start a node”anvilanvil listens on http://127.0.0.1:8545. It prints ten accounts, each with its private key
and 10,000 ether. Any node works the same way: only the URL changes.
2. Describe what to read
Section titled “2. Describe what to read”import { Address, Call } from "@ethect/core";
// anvil's first accountconst ACCOUNT = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
// Three values, read together at one block.export const read = Call.all({ chainId: Call.chainId, block: Call.blockNumber, balance: Call.balance(ACCOUNT),});Call.all reads several things in one request. Nothing has run yet:
read is a description, and it does not say where the node is.
An address has two ways in, and the rule is the same everywhere:
| The address is | Use | What is no address is |
|---|---|---|
| written in your source, as here | Address.makeUnsafe |
thrown when the file loads: a typo to fix |
| typed by a user, carried by a request, held in a file | Address.make |
a failure of an effect: an answer to give |
3. Give it a node and run
Section titled “3. Give it a node and run”import { Effect } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { read } from "./read";
const result = await read.pipe( Effect.provide(Transport.http("http://127.0.0.1:8545")), Effect.provide(FetchHttpClient.layer), Effect.runPromise,);
console.log(result);{ chainId: 31337, block: 0n, balance: 10000000000000000000000n }Transport.http is the node, and FetchHttpClient.layer is how
HTTP requests are made: the one built on fetch. Effect.runPromise runs the program and
gives a Promise of its result. A chain id is a number, a block number and a balance in wei
are bigint.
4. Send a transaction
Section titled “4. Send a transaction”A transaction needs someone to sign it. Put the first private key anvil printed in the environment, so it is never written in your code:
export PRIVATE_KEY=<the first private key anvil printed>import { Config, Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Address, Call, Signer, Transport, Tx } from "@ethect/core";
// anvil's second accountconst TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const send = Effect.gen(function* () { // One ether: signed with the key, sent, and waited for until it is in a block. const receipt = yield* Tx.send(Call.make({ to: TO, value: 10n ** 18n })); return { block: receipt.blockNumber, balance: yield* Call.balance(TO) };});
// A signer and a node. The key is read as a secret: it cannot end up in a log.const Local = Layer.unwrap( Effect.map(Config.Redacted("PRIVATE_KEY"), (key) => Layer.merge(Signer.privateKey(key), Transport.http("http://127.0.0.1:8545")), ),).pipe(Layer.provide(FetchHttpClient.layer));
console.log(await send.pipe(Effect.provide(Local), Effect.runPromise));{ block: 1n, balance: 10001000000000000000000n }Effect.gen is an async function, and yield* is its await: the two lines run one after
the other. Tx.send needs a Signer beside the
node, and the type of send says so: provide the node alone, and it does not compile. Local
is a Layer, which is what gives a program the services it needs.
When it fails
Section titled “When it fails”No effect throws. A failure is a value with a tag, and TypeScript knows which ones are possible.
import { Effect } from "effect";import { read } from "./read";
const careful = read.pipe( Effect.map(({ block }) => `block ${block}`), // the node is not running, or the URL is wrong Effect.catchReason("TransportError", "Unreachable", () => Effect.succeed("no node at this URL"), ),);Stop anvil and run careful in place of read: the result is "no node at this URL", and
not a rejected promise.
Next steps
Section titled “Next steps”- Read and write a contract: the same two steps with an ERC-20.
- Read many things at once and Send transactions: what each does, and what it sends.
- Effect essentials: the five things to know about Effect.
- Coming from viem: each function you know, and its counterpart.
- The JSON-RPC layer: every method a node serves as a typed function, for when you want one request and nothing around it. Everything on this page is built on it.