Skip to content

Quick start

Read from a local node, then send it a transaction. You need @ethect/core and effect, and Foundry for the node.

anvil

anvil 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.

read.ts
import { Address, Call } from "@ethect/core";
// anvil's first account
const 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
main.ts
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.

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>
send.ts
import { Config, Effect, Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Call, Signer, Transport, Tx } from "@ethect/core";
// anvil's second account
const 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.

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.