# Abi.encode

Encodes one value, or several, to the bytes `abi.encode` gives.

## Usage

```ts
import { Abi } from "@ethect/core";

// one type and its value, or a list of types and one value for each
const one = Abi.encode(Abi.uint256, 1n); // abi.encode(x)
const several = Abi.encode([Abi.uint256, Abi.string], [1n, "one"]); // abi.encode(a, b)
```

Both are effects. Run one, or `yield*` it inside another.

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

const Transfer = Abi.struct({ to: Abi.address, amount: Abi.uint256 });

const TO = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const program = Abi.encode(Transfer, { to: TO, amount: 100n });

const data = await Effect.runPromise(program);
// 0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266
//   0000000000000000000000000000000000000000000000000000000000000064
```

### Several values, or one tuple

A list of types and a tuple of the same types are two things, as they are in Solidity.

```ts
import { Abi } from "@ethect/core";

// abi.encode(a, b): two values
const several = Abi.encode([Abi.uint256, Abi.string], [1n, "one"]);
// abi.encode(pair), for a struct `pair`: one value
const pair = Abi.encode(Abi.tuple([Abi.uint256, Abi.string]), [1n, "one"]);
```

| The types are | `Abi.encode([A, B], …)` next to `Abi.encode(Abi.tuple([A, B]), …)` |
| --- | --- |
| static only | the same bytes |
| one of them dynamic: `bytes`, `string`, `T[]` | the tuple is one word longer: it starts with `0x20`, its own offset |

A tuple with a dynamic component is one dynamic value, and a dynamic value is written by its
offset. Hook data, callback data and the keys you hash are most often several values: use the
list.

### A value that does not fit

A failure is a value. Handle it with `Effect.catchTag`.

```ts
import { Effect } from "effect";
import { Abi } from "@ethect/core";

const data = Abi.encode(Abi.uint8, 256).pipe(
  Effect.catchTag("AbiError", (error) => Effect.succeed(error.message)),
);
// succeeds with "Cannot encode uint8: 256 does not fit in uint8"
```

## Returns

`Effect<Hex.Hex, AbiError>`

The encoding, as lowercase [hex](https://ethect.dev/reference/primitives/hex/). It needs no service: no node is
asked.

## Parameters

### type

- **Type:** `Abi.Type<T>`, or a list of them

The Solidity type: one of [`Abi`'s](https://ethect.dev/reference/abi/types/), or a
[type of your own](https://ethect.dev/reference/abi/#custom-types). A list of types encodes several values. An
empty list encodes to `0x`.

### value

- **Type:** `T`, or one value for each type of the list

A value of the type. What `T` is for each type is in
[this table](https://ethect.dev/reference/abi/types/#what-each-type-is).

```ts
import { Abi } from "@ethect/core";

const fee = Abi.encode([Abi.uint24, Abi.bool], [3000, true]);
// @ts-expect-error the fee is a number, not a bigint
const wrong = Abi.encode([Abi.uint24, Abi.bool], [3000n, true]);
// @ts-expect-error one value for each type
const short = Abi.encode([Abi.uint24, Abi.bool], [3000]);
```

## Behavior

- **Two steps.** The type's schema turns the value into the plain ABI value, checking it. Then
  the layout writes the bytes: heads first, then tails, each dynamic value by its offset.
- **Every value is checked before a byte is written.** What each type checks is in
  [this table](https://ethect.dev/reference/abi/types/#what-each-type-is).
- **The message says where.** A value deep in a struct is named by its path:
  `at ["orders"][1]["amount"]`. In a list it says which value: `value 1 of (address,uint256)`.
- **One type is a list of one.** `Abi.encode(Abi.string, "one")` and
  `Abi.encode([Abi.string], ["one"])` are the same bytes. A dynamic value starts with its
  offset, `0x20`.
- **A type is told from a list with `Array.isArray`.** A type is a schema, never an array.
- **This is not the calldata of a call.** Calldata is a selector followed by the encoding of
  the arguments.

## Errors

| Error | When |
| --- | --- |
| [`AbiError`](https://ethect.dev/reference/errors/abi-error/) | a value is not one its type takes: out of range, of the wrong length, a field missing |
| `AbiError` | a list of types is given another number of values |
| `AbiError` | a type is not a Solidity type: a schema that was not built from `Abi` |
| `AbiError` | a type is a definition Solidity has no type for: `T[0]`, a tuple of nothing, a field named `0` |
