Skip to content
v0.1.0-alpha.1

Ethereum, on Effect

ethect is a viem alternative built on Effect, with a near zero-dependency state of mind. Read and write contracts, send transactions, follow events and connect wallets. A node is a Layer, a failure is a value, and reads merge into one request.
bun add @ethect/core@alpha effect
A contract from its signatures
Functions, events and errors, typed. A call is a value: it is read, or sent.
Reads merge by themselves
Reads issued together leave as one multicall, at one block.
One line sends
With a key or a wallet: signed, sent and waited for. One prompt where the wallet batches.
Errors are values
Tagged failures, the custom errors of a contract among them. No effect throws.

// one type

A program is a description

A call to a contract is a value. Its type says what it returns, how it can fail, and what it needs: a node, and someone who signs. Which node and which signer is decided where the program runs.

What it returns
The receipt of the block it landed in, once the node has it.
How it can fail
Rejected, TxReverted with the contract's own error, TxDropped: each by its tag.
What it needs
Someone who signs, and a node. Which ones is decided where it runs.

// see it run

Four programs, on one clock

Each lane is a request: to your node, or to the signer. What a lane gives back is a value, or a failure with a tag.

// Call.all

Three reads, one request

Reads issued together leave as one multicall, at one block.

const holding = Call.all({
symbol: usdc.symbol(),
decimals: usdc.decimals(),
balance: usdc.balanceOf(OWNER),
});
idle0 ms

// Tx.send

One line sends

Signed, sent and waited for. The node and the signer are provided where it runs.

const receipt = yield* Tx.send(usdc.transfer(TO, balance / 2n));
// Effect<Receipt, Failure<E>, Signer | Transport>
idle0 ms

// Effect.catchTag

A failure is a value

The wallet says no. The failure has a tag, and catching it takes it out of the type.

Tx.send(vault.deposit(amount, ME)).pipe(
Effect.catchTag("Rejected", () =>
Effect.succeed("You refused to sign")),
);
idle0 ms

// Effect.retry

Retry a read, not a send

The node drops two requests. An exponential schedule tries again, four times at most.

ownerOf(nft, tokenId).pipe(
Effect.retry({
while: (error) => error._tag === "TransportError",
schedule: Schedule.exponential("250 millis"),
times: 4,
}),
);
idle0 ms

// coming from viem

The same read, with the failure in the type

Each viem function has a counterpart. What changes is where the node is chosen, and what you know about a failure before it happens. Coming from viem maps them all.

viem.ts
6 collapsed lines
import { createPublicClient, erc20Abi, http } from "viem";
import { mainnet } from "viem/chains";
declare const url: string;
const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const OWNER = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
const client = createPublicClient({
chain: mainnet,
transport: http(url),
batch: { multicall: true },
});
const usdc = { address: USDC, abi: erc20Abi } as const;
try {
const [symbol, balance] = await Promise.all([
client.readContract({ ...usdc, functionName: "symbol" }),
client.readContract({ ...usdc, functionName: "balanceOf", args: [OWNER] }),
]);
} catch (error) {
// error: unknown
}
  • The node is chosen where the client is made
  • A failure is thrown, and typed unknown
  • Batching is an option of the client
ethect.ts
5 collapsed lines
import { Effect } from "effect";
import { Address, Call, Erc20 } from "@ethect/core";
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
const OWNER = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const usdc = Erc20.at(USDC);
export const holding = Call.all({
symbol: usdc.symbol(),
balance: usdc.balanceOf(OWNER),
}).pipe(
// the node is down: a value, not an exception
Effect.catchReason("TransportError", "Unreachable", () =>
Effect.succeed({ symbol: "?", balance: 0n }),
),
);
  • The node is a Layer, provided where it runs
  • Each failure is in the type, caught by its tag
  • Reads issued together merge by themselves

// in the box

Four entry points, two dependencies

// status

Alpha, and tested against real nodes

Versions are published under the alpha tag of npm, and the API can still change from one to the next. Why ethect has the whole list of what is missing.

Tested againstanvil· the reads of the JSON-RPC layer also run againstGethrethErigonNethermind

What it holds

  • Contracts from their signatures · or from the ABI you have
  • Reads that merge into one multicall
  • Sends with a key or a wallet · one prompt where it batches
  • Secrets as Redacted · never in a log

What it does not

  • A React package
  • A WalletConnect connector
  • A list of chains
  • Unit helpers · Effect's BigDecimal does that

Install it, and run a first program

The quick start reads from a local anvil node, then sends it a transaction.

bun add @ethect/core@alpha effect