# Fn.parse

Builds one function from its human-readable signature.

## Usage

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Fn, Transport } from "@ethect/core";

const balanceOf = Fn.parse("function balanceOf(address owner) view returns (uint256)");

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

// balanceOf(args_0: Address): one `eth_call`
const program = balanceOf.at(USDC)(USER);

const balance = await program.pipe(
  Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
```

Arguments are positional and have no names: `balanceOf(args_0: Address)`. For named
arguments, define the function with [`Fn.view`](https://ethect.dev/reference/fn/view/).

The signature is read with the grammar of [`Abi.parse`](https://ethect.dev/reference/abi/parse/#the-grammar),
and the function is the one [`Abi.parse`](https://ethect.dev/reference/abi/parse/) gives for it. It declares no
error: the errors of a contract come with the contract.

### Structs

A signature that uses a struct by its name takes the struct beside it, as its own signature.

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

const fill = Fn.parse("function fill(Order order) payable returns (bool)", {
  structs: [
    "struct Order { address maker; uint256 amount; Leg[] legs; }",
    "struct Leg { uint8 kind; bytes data; }",
  ],
});

const call = (exchange: Address.Address, maker: Address.Address) =>
  // a struct is an object of its members, and a payable function takes a value
  fill.at(exchange)({ maker, amount: 100n, legs: [{ kind: 1, data: "0x" }] }, { value: 1n });
```

### A signature that does not parse

It does not compile, and neither does a signature that is not one of a function.

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

// @ts-expect-error adress is not a type: it is no Solidity type, and no struct of that name is declared
const transfer = Fn.parse("function transfer(adress to, uint256 amount)");
// @ts-expect-error Order is not a type: it is no Solidity type, and no struct of that name is declared
const fill = Fn.parse("function fill(Order order)");
// @ts-expect-error Fn.parse takes the signature of a function
const Transfer = Fn.parse("event Transfer(address indexed from, address indexed to, uint256 value)");
```

## Returns

[`Fn.Fn<Name, Args, A, never, M>`](https://ethect.dev/reference/fn/#what-a-function-holds)

The function: its `name`, `signature`, `selector` and `mutability`, and
[`at(address)`](https://ethect.dev/reference/fn/at/), which builds its calls.

| Type parameter | Is |
| --- | --- |
| `Name` | the name in the signature, as a literal |
| `Args` | the arguments of a call, by position |
| `A` | what a call returns: nothing, the one value, an object of several values that all have a name, or a tuple |
| `M` | `view`, `pure`, `payable` or `nonpayable`, as the signature says. Without a word, `nonpayable` |

A signature that is not a literal, such as a `string` read from a file, gives a function that
takes any arguments and returns `unknown`.

## Parameters

### signature

- **Type:** `string`

The signature of the function, as Solidity declares it:
`function name(type [location] [name], …) [visibility] [mutability] [returns (type [name], …)]`.

### options.structs (optional)

- **Type:** `ReadonlyArray<string>`

The structs the signature uses by name, each as `struct Name { type name; … }`. A struct can
use another of the list.

## Behavior

- **A signature that does not parse does not compile**, and the message says why. When the
  program runs it gives a function whose every use fails with an
  [`AbiError`](https://ethect.dev/reference/errors/abi-error/) that quotes the signature and says where it
  stops. It is a view, so a call of it is an effect that fails. Nothing throws.
- **The function is the one a definer builds by hand**: same signature, same selector, same
  bytes.
- **Names are read and not kept**, except those of several returned values and of the members
  of a struct.

## Errors

`Fn.parse` returns a function and has no failure of its own.

| Error | When |
| --- | --- |
| [`AbiError`](https://ethect.dev/reference/errors/abi-error/) | encoding a call of, or decoding what came back from, a function whose signature does not parse |
