# ethect

A viem alternative built on Effect, with a near zero-dependency state of mind. Contracts, calls, transactions, events and wallets, with errors as values, reads merged into one request, and every JSON-RPC method as a typed function.

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.

- [Quick start](https://ethect.dev/getting-started/quick-start/)
- [Why ethect?](https://ethect.dev/getting-started/why-ethect/)
- [GitHub](https://github.com/maxencerb/ethect)

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

`Effect<Receipt, Failure<E>, Signer | Transport>`

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

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

**Three reads, one request** (`Call.all`): Reads issued together leave as one multicall, at one block.

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

**One line sends** (`Tx.send`): Signed, sent and waited for. The node and the signer are provided where it runs.

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

**A failure is a value** (`Effect.catchTag`): The wallet says no. The failure has a tag, and catching it takes it out of the type.

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

**Retry a read, not a send** (`Effect.retry`): The node drops two requests. An exponential schedule tries again, four times at most.

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

## 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](https://ethect.dev/getting-started/coming-from-viem/)
maps them all.

```ts title="viem.ts"
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

```ts title="ethect.ts"
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

## Four entry points, two dependencies

`@noble/curves` for the secp256k1 signatures and `@noble/hashes` for keccak-256. `effect` is its
one peer dependency. Nothing else is installed with it.

- [`@ethect/core`](https://ethect.dev/reference/contract/), **The main APIs**: Contracts (Abi, Fn, Err, Ev, Contract and the four standards), reads and sends (Call, Tx, Signer), wallets, transports, errors and primitives.
- [`@ethect/core/rpc`](https://ethect.dev/rpc/), **The JSON-RPC layer**: Every method as a typed function that returns an Effect, with the wire types it takes and gives.
- [`@ethect/core/ens`](https://ethect.dev/reference/ens/), **Names**: A name to an address and back, its text records and its avatar. An empty Option where there is none.
- [`@ethect/core/testing`](https://ethect.dev/reference/testing/), **Test tools**: A local anvil node, a mock provider, a wallet and a signer for tests.

## 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](https://ethect.dev/getting-started/why-ethect/#what-it-does-not-have) has the whole list of
what is missing.

Tested against anvil. The reads of the JSON-RPC layer also run against Geth, reth, Erigon, Nethermind.

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.

```sh
npm install @ethect/core@alpha effect
```

- [Quick start](https://ethect.dev/getting-started/quick-start/)
