# Functions (Fn)

A function of a contract as a definition: its name, the types it takes and returns, and the
errors it reverts with. A definition at an address builds [calls](https://ethect.dev/reference/call/), and
encodes nothing until a call is read or sent. There is one definer per mutability, and only a
`view` or a `pure` function gives a call that is also the effect of reading it.

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

// function balanceOf(address owner) view returns (uint256)
const balanceOf = Fn.view<[owner: _]>()("balanceOf", {
  inputs: [Abi.address],
  output: Abi.uint256,
});

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

const program = Effect.gen(function* () {
  const call = balanceOf.at(USDC)(USER); // a call: plain data, nothing is sent
  return yield* call; // reading it: one `eth_call`
});
```

| Export | Kind | Description |
| --- | --- | --- |
| [`Fn.view<Names>()(name, definition)`](https://ethect.dev/reference/fn/view/#view) | function | Defines a function that reads the chain. |
| [`Fn.pure<Names>()(name, definition)`](https://ethect.dev/reference/fn/view/#pure) | function | Defines a function that reads nothing. |
| [`Fn.nonpayable<Names>()(name, definition)`](https://ethect.dev/reference/fn/view/#nonpayable) | function | Defines a function that writes. |
| [`Fn.payable<Names>()(name, definition)`](https://ethect.dev/reference/fn/view/#payable) | function | Defines a function that writes and takes ether: its calls take `{ value }` as a last argument. |
| [`Fn.parse(signature)`](https://ethect.dev/reference/fn/parse/) | function | A function from its human-readable signature. |
| [`fn.at(address)(...args)`](https://ethect.dev/reference/fn/at/) | member | The function at a contract: builds a call. |
| [`Fn.encodeInput(fn, ...args)`](https://ethect.dev/reference/fn/encode-input/) | function | The calldata of a call: the selector, then the arguments. |
| [`Fn.decodeOutput(fn, data)`](https://ethect.dev/reference/fn/decode-output/) | function | What the function returned, from the bytes a node answered. |
| [`Fn.Fn<Name, Args, A, E, M>`](#what-a-function-holds) | type | A function: `name`, `signature`, `selector`, `mutability` and `at(address)`. |
| `Fn.Any` | type | Any function, whatever it takes and returns. |
| [`Fn.Mutability`](#mutability) | type | `"pure" \| "view" \| "nonpayable" \| "payable"`. |
| [`Fn.Payment`](https://ethect.dev/reference/fn/view/#payable) | type | What a call of a payable function can carry: `{ value? }`. |
| [`_`](#names) | type | Stands for the type of an input whose name alone is said, as in `Fn.view<[owner: _]>()`. Exported by `@ethect/core` itself. |

## What a function holds

A definition is a plain object. Its signature and its selector are computed once, where it is
defined.

```ts
import { Abi, 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,
});

deposit.name; // "deposit"
deposit.signature; // "deposit(uint256,address)"
deposit.selector; // "0x6e553f65"
deposit.mutability; // "nonpayable"
```

| Member | Type | Holds |
| --- | --- | --- |
| `name` | the name you gave, as a literal | the name of the function |
| `signature` | `string` | the name and the types of the inputs, as the selector is hashed from |
| `selector` | [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/) | the first four bytes of the hash of the signature: what calldata starts with |
| `mutability` | `Fn.Mutability` | which definer built it |
| [`at(address)`](https://ethect.dev/reference/fn/at/) | function | the function at a contract |

A function also holds the types of its inputs and outputs and its errors, under a key the
library reads. You do not build a function by hand: a definer does.

## Mutability

The definer says what a call of the function is.

| Definer | Solidity | A call is | Takes ether |
| --- | --- | --- | --- |
| `Fn.view` | `view` | a [`Call.View`](https://ethect.dev/reference/call/): also the effect of reading it | no |
| `Fn.pure` | `pure` | a `Call.View` | no |
| `Fn.nonpayable` | neither | a [`Call.Call`](https://ethect.dev/reference/call/): plain data, to send | no |
| `Fn.payable` | `payable` | a `Call.Call` | yes, with `{ value }` |

## Names

A definer takes the names of the inputs as a type argument: a tuple whose labels are the names.
The types of that tuple are not read, since `inputs` gives them. `_` is what to write in their
place.

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

// transfer(to: Address, amount: bigint)
const transfer = Fn.nonpayable<[to: _, amount: _]>()("transfer", {
  inputs: [Abi.address, Abi.uint256],
});
// approve(args_0: Address, args_1: bigint)
const approve = Fn.nonpayable()("approve", { inputs: [Abi.address, Abi.uint256] });
```

Without names a function takes the same arguments, shown as `args_0`, `args_1`. The names must
be one for each input: any other number does not compile.
