# fn.at

Gives a function at a contract. Calling the result with the arguments builds a call: a plain
value that nothing has encoded yet.

## Usage

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

const balanceOf = Fn.view<[owner: _]>()("balanceOf", {
  inputs: [Abi.address],
  output: Abi.uint256,
});
const transfer = Fn.nonpayable<[to: _, amount: _]>()("transfer", {
  inputs: [Abi.address, Abi.uint256],
  output: Abi.bool,
});

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

const program = Effect.gen(function* () {
  // a view: the call is also the effect of reading it
  const balance = yield* balanceOf.at(USDC)(USER);

  // a write: the call is plain data, to encode or to send
  const call = transfer.at(USDC)(USER, balance);
  return yield* Call.encode(call); // { to, data, value }
});

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

### Bound once

`at(address)` returns a function. Keep it to build several calls of the same contract.

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

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

const balances = (token: Address.Address, owners: ReadonlyArray<Address.Address>) => {
  const balance = balanceOf.at(token);
  return owners.map((owner) => balance(owner));
};
```

A [`Contract`](https://ethect.dev/reference/contract/) binds all its functions at once.

### With a value

A call of a payable function takes one more argument, `{ value }`, in wei.

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

const deposit = Fn.payable()("deposit", { inputs: [] });

const wrap = (weth: Address.Address) => deposit.at(weth)({ value: 10n ** 18n });
const none = (weth: Address.Address) => deposit.at(weth)(); // value: 0n
```

## Returns

`(...args) => Call.View<A, E>` for a `view` or a `pure` function, `(...args) => Call.Call<A, E>`
for the others.

A function that takes the arguments of the function, under the names its definition gave, and
returns a [call](https://ethect.dev/reference/call/).

| A call holds | Type | Is |
| --- | --- | --- |
| `fn` | `Fn.Any` | the function |
| `to` | `Address.Address` | the address given to `at` |
| `args` | `ReadonlyArray<unknown>` | the arguments, as they were given |
| `value` | `bigint` | the `value` of a payable call, `0n` otherwise |

## Parameters

### to

- **Type:** [`Address.Address`](https://ethect.dev/reference/primitives/address/)

The address of the contract. A plain string is not accepted: build it with
[`Address.makeUnsafe`](https://ethect.dev/reference/primitives/address/#makeunsafe), or with `Address.make` when it
comes from outside.

### args

- **Type:** the arguments of the function

One value for each input, of the TypeScript type of its
[Solidity type](https://ethect.dev/reference/abi/types/#what-each-type-is).

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

const transfer = Fn.nonpayable<[to: _, amount: _]>()("transfer", {
  inputs: [Abi.address, Abi.uint256],
});
declare const to: Address.Address;
const call = transfer.at(to)(to, 100n);
// @ts-expect-error an amount is a bigint
const wrong = () => transfer.at(to)(to, 100);
// @ts-expect-error both arguments are needed
const short = () => transfer.at(to)(to);
```

### options.value (optional)

- **Type:** `bigint`

The ether the call carries, in wei. Only a payable function has this argument.

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

const approve = Fn.nonpayable<[spender: _]>()("approve", { inputs: [Abi.address] });
// @ts-expect-error a function that is not payable takes no value
const paid = (token: Address.Address) => approve.at(token)(token, { value: 1n });
```

## Behavior

- **It never fails and never encodes.** The arguments are kept as you gave them. A value its
  type refuses is found when the call is read, encoded or sent, as an
  [`AbiError`](https://ethect.dev/reference/errors/abi-error/).
- **A call is a value.** Build it where you like, keep it, put it in a list. Nothing reaches a
  node until it is read or sent.
- **A view is read by running it.** `yield* call` is [`Call.read(call)`](https://ethect.dev/reference/call/read/).
  Pass options with `Call.read(call, options)`.
- **A write is not an effect.** `yield*` on it does not compile. It is sent with
  [`Tx`](https://ethect.dev/reference/tx/).
- **The value is there when there are more arguments than inputs.** That is how a payable call
  with no input, `deposit({ value })`, is told from a call without a value.

## Under the hood

Building a call sends nothing. Running a call of a view is one read:

- **Call.Reader** `eth_call({ to, data }, "latest")`

[`Call.read`](https://ethect.dev/reference/call/read/#under-the-hood) says what is sent with each option.
