# Fn.encodeInput

Encodes the arguments of a function to the calldata of a call: the selector, then the
arguments.

## Usage

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

// function deposit(uint256 assets, address receiver) returns (uint256)
const deposit = Fn.nonpayable<[assets: _, receiver: _]>()("deposit", {
  inputs: [Abi.uint256, Abi.address],
  output: Abi.uint256,
});

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

const program = Fn.encodeInput(deposit, 100n, RECEIVER);

const data = await Effect.runPromise(program);
// 0x6e553f65
//   0000000000000000000000000000000000000000000000000000000000000064
//   000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266
```

You rarely call it yourself: reading or sending a call encodes it.
[`Call.encode`](https://ethect.dev/reference/call/encode/) gives the same bytes with the address and the value
of a call.

### An argument that does not fit

A failure is a value. It names the function, the argument and why.

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

const deposit = Fn.nonpayable<[assets: _, receiver: _]>()("deposit", {
  inputs: [Abi.uint256, Abi.address],
});

const message = (receiver: Address.Address) =>
  Fn.encodeInput(deposit, 2n ** 256n, receiver).pipe(
    Effect.catchTag("AbiError", (error) => Effect.succeed(error.message)),
  );
// "deposit: Cannot encode uint256, value 0 of (uint256,address):
//  1157…9936 does not fit in uint256"
```

## Returns

`Effect<Hex.Hex, AbiError>`

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

## Parameters

### fn

- **Type:** `Fn.Fn<string, Args, any, any, any>`

The function, of any mutability.

### args

- **Type:** `Args`, the arguments of the function

One value for each input. A payable function takes no `{ value }` here: the value of a call is
not in its calldata.

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

const deposit = Fn.payable()("deposit", { inputs: [] });
const data = Fn.encodeInput(deposit); // "0xd0e30db0": the selector alone
// @ts-expect-error a payment is not an argument
const wrong = () => Fn.encodeInput(deposit, { value: 1n });
```

## Behavior

- **The selector comes first**, four bytes. The arguments follow as
  [`Abi.encode`](https://ethect.dev/reference/abi/encode/) writes a list of values.
- **Every argument 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).

## Errors

| Error | When |
| --- | --- |
| [`AbiError`](https://ethect.dev/reference/errors/abi-error/) | an argument is not a value its type takes |
| `AbiError` | the number of arguments is not the number of inputs |
| `AbiError` | an input of the function is not a Solidity type |
