# Quick start

Read three values from a local node in one typed program, then sign and send a transaction to it.

Read from a local node, then send it a transaction. You need
[`@ethect/core` and `effect`](https://ethect.dev/getting-started/installation/), and
[Foundry](https://getfoundry.sh) for the node.

## 1. Start a node

```sh
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.

## 2. Describe what to read

```ts title="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`](https://ethect.dev/reference/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`](https://ethect.dev/reference/primitives/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`](https://ethect.dev/reference/primitives/address/#make) | a failure of an effect: an answer to give |

## 3. Give it a node and run

```ts title="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);
```

```txt
{ chainId: 31337, block: 0n, balance: 10000000000000000000000n }
```

[`Transport.http`](https://ethect.dev/reference/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

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:

```sh
export PRIVATE_KEY=<the first private key anvil printed>
```

```ts title="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));
```

```txt
{ 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`](https://ethect.dev/reference/tx/send/) needs a [`Signer`](https://ethect.dev/reference/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

No effect throws. A failure is a value with a tag, and TypeScript knows which ones are
possible.

```ts
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

- [Read and write a contract](https://ethect.dev/guides/contracts/): the same two steps with an ERC-20.
- [Read many things at once](https://ethect.dev/guides/reading/) and
  [Send transactions](https://ethect.dev/guides/transactions/): what each does, and what it sends.
- [Effect essentials](https://ethect.dev/guides/effect-essentials/): the five things to know about Effect.
- [Coming from viem](https://ethect.dev/getting-started/coming-from-viem/): each function you know, and its
  counterpart.
- [The JSON-RPC layer](https://ethect.dev/rpc/): 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.
