# Why ethect

ethect is a viem alternative built on Effect, with a near zero-dependency state of mind. What you get from it, what it holds today, and what it does not have.

ethect is a TypeScript library for Ethereum built on [Effect](https://effect.website): an
alternative to [viem](https://viem.sh), with a near zero-dependency state of mind. It reads and
writes contracts, sends transactions, follows events and connects wallets. This page says what
you get, what the package holds, and what it does not have.

## What it looks like

```ts
import { Effect } from "effect";
import { Address, Erc20, Signer, Tx } from "@ethect/core";

const token = Erc20.at(Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"));
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const program = Effect.gen(function* () {
  const balance = yield* token.balanceOf(yield* Signer.address); // bigint
  return yield* Tx.send(token.transfer(TO, balance / 2n)); // the receipt, once in a block
});
// Effect<Receipt, …, Signer | Transport>
```

The type of the program 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: see the
[quick start](https://ethect.dev/getting-started/quick-start/).

## What you get

- **Tokens and vaults, with no ABI to write.** [`Erc20`](https://ethect.dev/reference/standards/erc20/),
  [`Erc721`](https://ethect.dev/reference/standards/erc721/), [`Erc1155`](https://ethect.dev/reference/standards/erc1155/) and
  [`Erc4626`](https://ethect.dev/reference/standards/erc4626/) are built in, and `extend` adds what a contract
  has more.
- **Any other contract from its signatures.** [`Abi.parse`](https://ethect.dev/reference/abi/parse/) reads them
  as text, [`Abi.fromJson`](https://ethect.dev/reference/abi/from-json/) takes the ABI you already have. A call
  is a value: it is built, passed around, and sent or read later.
- **Reads merge by themselves.** Reads issued together leave as
  [one multicall](https://ethect.dev/guides/reading/), at one block. Nothing has to be gathered by hand.
- **One line sends, with a key or with a wallet.** [`Tx.send`](https://ethect.dev/reference/tx/send/) signs,
  sends and waits. Several calls are one prompt on a wallet that batches, and the same code
  elsewhere.
- **Errors are values.** No effect throws. The custom error of a contract is a
  [typed failure caught by its tag](https://ethect.dev/guides/errors/), and the compiler knows which failures
  you have not handled.
- **Events are decoded, and watched as a stream.** [`Ev.watch`](https://ethect.dev/reference/ev/watch/) is
  pushed over a WebSocket and polled over HTTP: the line is the same.
- **Wallets are found and followed.** The [installed wallets](https://ethect.dev/reference/wallets/) announce
  themselves, and a [connection](https://ethect.dev/reference/connection/) follows the account and the chain
  the user picks.
- **A secret is never a string.** A private key is a `Redacted`, and so is a URL with a key in
  it: neither can end up in a log or in an error.
- **Tests run against a real node.** [`AnvilNode`](https://ethect.dev/guides/testing/) gives each test its own
  anvil, and `TestWallet` is a wallet whose user is the test.
- **Plain JSON-RPC 2.0 on the wire.** Every method a node serves is also a
  [typed function](https://ethect.dev/rpc/), for when you want one request and nothing around it.

## What it holds

The package is `@ethect/core`, at an early version: its API can still change. It has one entry
point per part.

It depends on two packages, both for cryptography: `@noble/curves` for the secp256k1
signatures and `@noble/hashes` for keccak-256. `effect` is its one peer dependency. Nothing
else is installed with it.

| Part | Import from | Holds |
| --- | --- | --- |
| Contracts | `@ethect/core` | [`Abi`](https://ethect.dev/reference/abi/), [`Fn`](https://ethect.dev/reference/fn/), [`Err`](https://ethect.dev/reference/err/), [`Ev`](https://ethect.dev/reference/ev/), [`Contract`](https://ethect.dev/reference/contract/), and the standards: [`Erc20`](https://ethect.dev/reference/standards/erc20/), [`Erc721`](https://ethect.dev/reference/standards/erc721/), [`Erc1155`](https://ethect.dev/reference/standards/erc1155/), [`Erc4626`](https://ethect.dev/reference/standards/erc4626/) |
| Reads and sends | `@ethect/core` | [`Call`](https://ethect.dev/reference/call/), [`Tx`](https://ethect.dev/reference/tx/), [`Signer`](https://ethect.dev/reference/signer/), [`Block`](https://ethect.dev/reference/block/) |
| Wallets | `@ethect/core` | [`Wallets`](https://ethect.dev/reference/wallets/), [`Connection`](https://ethect.dev/reference/connection/), [`Chain`](https://ethect.dev/reference/chain/) |
| The base | `@ethect/core` | [transports](https://ethect.dev/reference/transport/), [errors](https://ethect.dev/reference/errors/), [`Address`](https://ethect.dev/reference/primitives/address/), [`Hex`](https://ethect.dev/reference/primitives/hex/), [`Hash`](https://ethect.dev/reference/primitives/hash/), [`Quantity`](https://ethect.dev/reference/primitives/quantity/) |
| Names | `@ethect/core/ens` | [`Ens`](https://ethect.dev/reference/ens/) |
| JSON-RPC | `@ethect/core/rpc` | 161 [methods](https://ethect.dev/rpc/) and 5 subscription kinds, with the [wire types](https://ethect.dev/rpc/types/) |
| Testing | `@ethect/core/testing` | [`AnvilNode`](https://ethect.dev/reference/testing/anvil-node/), [`MockProvider`](https://ethect.dev/reference/testing/mock-provider/), [`TestWallet`](https://ethect.dev/reference/testing/test-wallet/), [`TestSigner`](https://ethect.dev/reference/testing/test-signer/) |

Everything is tested against anvil. The reads of the JSON-RPC layer have also been run
against Geth, reth, Erigon, Nethermind and the clients of several other chains: see
[the list](https://ethect.dev/rpc/#clients-it-has-been-run-against). No real wallet and no browser are part of a
test: wallets are tested with `TestWallet` and with providers that answer as the EIPs say.

## What it does not have

| Not there | So |
| --- | --- |
| a React package | the hooks are a few lines you keep: see [the setup](https://ethect.dev/guides/wallet-selection/#the-setup) of the wallet picker |
| WalletConnect, and other connectors | [`Wallets.of`](https://ethect.dev/reference/wallets/#of) takes a provider you built |
| SIWE | [`Signature.verifyMessage`](https://ethect.dev/reference/signature/verify-message/) checks the signature, and the message is yours to write and to read |
| unit helpers such as `parseEther` | by decision: Effect's `BigDecimal` does it. See [Amounts and decimals](https://ethect.dev/guides/amounts/) |
| a list of chains | a [`Chain`](https://ethect.dev/reference/chain/) is data you write, and you give the URL of the node |
| mnemonics and HD accounts | [`Signer.privateKey`](https://ethect.dev/reference/signer/private-key/) takes one key |
| blob and EIP-7702 transactions signed with a key | `TxUnsupported`, with the reason `"signing"` |
| a fallback transport, automatic retries | [a wrapper you write](https://ethect.dev/guides/custom-transports/), `Effect.retry` |
| account abstraction, OP Stack and ZKsync extensions | none |

## Compared to viem

| | viem | ethect |
| --- | --- | --- |
| A read | `await client.readContract({ address, abi, functionName, args })` | `yield* token.balanceOf(owner)` |
| A send | `writeContract`, then `waitForTransactionReceipt` | `yield* Tx.send(token.transfer(to, amount))` |
| When it runs | when you call it | when you run the program |
| Where it goes | the client you created | the transport and the signer you provide |
| Several reads | `multicall`, or `batch.multicall` on the client | merged without asking |
| A failure | a thrown error, found with `instanceof` or `error.walk` | a value in the error channel, matched by tag |
| What can fail | documented per action | in the type of the call |
| Watching | a callback and an `unwatch` function | a `Stream` that stops when you stop reading it |
| Results | `bigint`, `number` and named types and statuses | the same choices |
| An address | a `` `0x${string}` `` string | a branded lowercase string, sent in its EIP-55 form |

[Coming from viem](https://ethect.dev/getting-started/coming-from-viem/) maps each function you know to its
counterpart.

## When not to use ethect

- **You do not use Effect and do not want to.** Every call is an `Effect`. The
  [essentials](https://ethect.dev/guides/effect-essentials/) fit on one page, but they are not optional.
- **You need something of the table above.** WalletConnect and a React package are the ones
  an application misses first.
- **You need an API that no longer moves.** The version is early.
- **You need Windows for tests.** `AnvilNode` needs a POSIX shell.

## Next steps

- [Installation](https://ethect.dev/getting-started/installation/): the package, its peer dependency and what
  each transport needs.
- [Quick start](https://ethect.dev/getting-started/quick-start/): a read and a send against a local node.
- [Read and write a contract](https://ethect.dev/guides/contracts/): the first guide.
- [How it works](https://ethect.dev/concepts/how-it-works/): the two services, and who is asked what.
