# Calls and reads (Call)

A call is a message to a contract, as a plain value: building one does nothing. A call of a
view function is also the effect of reading it, and any call can be sent as a transaction with
[`Tx`](https://ethect.dev/reference/tx/).

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

const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
const USER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const program = Effect.gen(function* () {
  // a call: the function, the address, the arguments. Nothing is encoded or sent
  const call = Erc20.at(USDC).balanceOf(USER);
  // a call of a view is the effect of reading it
  const balance = yield* call;
  // the same, with options
  const before = yield* Call.read(call, { block: 19_000_000n });
  return balance - before;
});
```

| Export | Kind | Description |
| --- | --- | --- |
| [`Call.read(call, options?)`](https://ethect.dev/reference/call/read/) | function | Reads a call: what its function returns, or the error it reverts with. |
| [`Call.all(calls, options?)`](https://ethect.dev/reference/call/all/) | function | Reads several calls together, at one block: a tuple or a record of them. |
| [`Call.make(call)`](https://ethect.dev/reference/call/make/) | function | A raw call: no ABI, the bytes as they are. Without `to`, a deployment. |
| [`Call.encode(call)`](https://ethect.dev/reference/call/encode/) | function | The call as a transaction carries it. The only place a call is encoded. |
| [`Call.key(call)`](https://ethect.dev/reference/call/key/) | function | A stable name for a call: a cache key. It cannot fail. |
| [`Call.withBlock(block)`](https://ethect.dev/reference/call/with-block/) | function | Makes every read inside an effect at one block. |
| [`Call.batching(options)`](https://ethect.dev/reference/call/batching/) | function | A layer that tunes how reads are merged into multicalls, or turns it off with `false`. |
| [`Call.blockNumber`, `Call.timestamp`, `Call.chainId`, `Call.baseFee`, `Call.gasLimit`](https://ethect.dev/reference/call/state/) | call | The state of the chain, as calls to read beside others. |
| [`Call.balance(account)`, `Call.blockHash(block)`](https://ethect.dev/reference/call/state/#balance) | function | The same, for an account and for a block. |
| [`Call.Reader`](https://ethect.dev/reference/call/reader/) | reference | What runs `eth_call`s. By default, it merges reads into multicalls. |
| [`Call.direct`](https://ethect.dev/reference/call/reader/#direct) | reader | One `eth_call` per read. |
| `Call.TypeId` | constant | The key under which a call holds what reading it gives and fails with. |
| [`Call.Call<A, E>`](#what-a-call-holds) | type | A call: `fn`, `to`, `args`, `value`, and `data` for a raw call. |
| [`Call.View<A, E>`](#what-a-call-holds) | type | A call of a view or pure function: also an `Effect`. |
| [`Call.Encoded`](https://ethect.dev/reference/call/encode/#returns) | type | A call as a transaction carries it: `to`, `data`, `value`. |
| [`Call.ReadOptions`](https://ethect.dev/reference/call/read/#parameters) | type | How a read is made: `block`, `from`, `gas`, `stateOverride`, `blockOverride`, `batch`. |
| [`Call.Error`](#what-a-read-fails-with) | type, Schema | What any read can fail with, beyond the errors its function declares. |
| [`Call.Request`](https://ethect.dev/reference/call/reader/#request) | type | One read as a reader gets it: encoded, with its block decided. |
| [`Call.Batching`](https://ethect.dev/reference/call/batching/#parameters) | type | The options of `Call.batching`: `window`, `maxCalls`, `maxBytes`, `multicall`. |

## What a call holds

A call is built by [`fn.at(address)(...args)`](https://ethect.dev/reference/fn/at/), or by
[`Call.make`](https://ethect.dev/reference/call/make/) for raw bytes. It is plain data.

| Field | Type | Is |
| --- | --- | --- |
| `fn` | `Fn.Any \| undefined` | the function called. Absent for a raw call |
| `to` | `Address.Address \| undefined` | the address called. Absent for a deployment |
| `args` | `ReadonlyArray<unknown>` | the arguments, as they were given. Not encoded |
| `value` | `bigint` | the ether the call carries, in wei |
| `data` | `Hex.Hex`, optional | the bytes of a raw call |

| Built from | The call is | You can |
| --- | --- | --- |
| a `view` or a `pure` function | a `Call.View<A, E>`: also an `Effect` | read it, by running it or with `Call.read` |
| a `nonpayable` or a `payable` function | a `Call.Call<A, E>` | send it with [`Tx`](https://ethect.dev/reference/tx/) |
| [`Call.make`](https://ethect.dev/reference/call/make/) | a `Call.Call<Hex, never>` | send it with `Tx` |

`A` is what the function returns, and `E` the errors it declares. Any call can be
[encoded](https://ethect.dev/reference/call/encode/) and [named](https://ethect.dev/reference/call/key/).

## What a read fails with

| Failure | When |
| --- | --- |
| an error the function declares | the call reverted with it |
| [`Revert.Revert`](https://ethect.dev/reference/revert/) | the call reverted with something else |
| [`AbiError`](https://ethect.dev/reference/errors/abi-error/) | the arguments could not be encoded, or the answer could not be decoded |
| [`Transport.Error`](https://ethect.dev/reference/transport/#types) | the node answered with an error, or did not answer |

All but the first are `Call.Error`. It is also a value: the Schema of that type, for the failure
of a query that reads.

```ts
import { Schema } from "effect";
import { Call } from "@ethect/core";

// a revert, an ABI error, or the node's: each is read back as its class
const failure = Schema.decodeUnknownSync(Call.Error)({ _tag: "RevertPanic", code: 17 });
// a Revert.Panic: "Panic 0x11: an arithmetic operation overflowed or underflowed"
```

## Under the hood

Only the node is asked. Each function has its requests on its page.

**`Call.read(call)`, and a view call that is yielded**

- **your program**: the selector and the arguments are encoded. No request
- **your node** `eth_call({ to, data }, block)`
- **your program**: what the function returns is decoded, or its revert with the errors it declares. No request

**`Call.all([a, b, c])`, and reads issued together**

- **your node** `eth_call({ to: Multicall3, data: aggregate3([a, b, c]) }, block)`: all of them, in one request

See [`Call.read`](https://ethect.dev/reference/call/read/#under-the-hood), [`Call.all`](https://ethect.dev/reference/call/all/#under-the-hood)
and [`Call.batching`](https://ethect.dev/reference/call/batching/).
