# ERC-1155 token (Erc1155)

The ERC-1155 multi-token, built in. Its functions and its events at any address, with no ABI to write, and what a token adds to them.

The ERC-1155 multi-token, built in: the functions and the events of
[EIP-1155](https://eips.ethereum.org/EIPS/eip-1155), with no ABI to write. It is a
[contract](https://ethect.dev/reference/contract/): at an address it gives its functions, and each one builds
[calls](https://ethect.dev/reference/call/).

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

const ITEMS = Address.makeUnsafe("0x76BE3b62873462d2142405439777e971754E8E77");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const items = Erc1155.at(ITEMS);

const give = (ids: ReadonlyArray<bigint>) =>
  Effect.gen(function* () {
    const me = yield* Signer.address;
    // one read for the balances of several ids
    const balances = yield* items.balanceOfBatch(ids.map(() => me), ids);
    // and one transaction that moves them all
    return yield* Tx.send(items.safeBatchTransferFrom(me, TO, ids, balances, "0x"));
  });
```

| Export | Kind | Description |
| --- | --- | --- |
| [`Erc1155.at(address)`](#at) | function | The functions at a token: each one builds calls. |
| [`Erc1155.fn`](#fn-ev-and-err) | record | The functions, unbound: `Erc1155.fn.balanceOfBatch`. |
| [`Erc1155.ev`](#fn-ev-and-err) | record | The events: `TransferSingle`, `TransferBatch`, `ApprovalForAll`, `URI`. |
| [`Erc1155.err`](#fn-ev-and-err) | record | The errors: none. |
| [`Erc1155.extend(more)`](#extend) | function | ERC-1155 and what a token adds to it, in one contract. |

## What it declares

The six functions and the four events of the EIP, the `supportsInterface` of the
[ERC-165](https://eips.ethereum.org/EIPS/eip-165) it requires, and the `uri` of its optional
metadata extension.

```solidity
function balanceOf(address owner, uint256 id) view returns (uint256)
function balanceOfBatch(address[] owners, uint256[] ids) view returns (uint256[])
function setApprovalForAll(address operator, bool approved)
function isApprovedForAll(address owner, address operator) view returns (bool)
function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes data)
function safeBatchTransferFrom(address from, address to, uint256[] ids, uint256[] values, bytes data)

// ERC-165
function supportsInterface(bytes4 interfaceId) view returns (bool)

// optional: metadata
function uri(uint256 id) view returns (string)

event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value)
event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values)
event ApprovalForAll(address indexed owner, address indexed operator, bool approved)
event URI(string value, uint256 indexed id)
```

- **The metadata extension is optional.** On a token without it, reading `uri` fails.
- **It declares no error.** The EIP has none: each token reverts with its own, and
  [`extend`](#extend) adds them.

## at

Gives the functions at a token. Each takes the arguments of the function, by the names the
EIP gives them. An array is a `ReadonlyArray`.

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

const items = Erc1155.at(Address.makeUnsafe("0x76BE3b62873462d2142405439777e971754E8E77"));

// a view: Call.View<bigint>, also the effect of reading it
const balance = (owner: Address.Address) => items.balanceOf(owner, 1n);
// a write: Call.Call<void>, to give to `Tx.send`
const approval = (operator: Address.Address) => items.setApprovalForAll(operator, true);
```

### Returns

[`Contract.Bound<Fns>`](https://ethect.dev/reference/contract/#at): an object with one key per function.

### Parameters

#### to

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

The address of the token.

## fn, ev and err

The definitions themselves, as a [contract](https://ethect.dev/reference/contract/) holds them.

```ts
import { Erc1155, Ev } from "@ethect/core";
import type { Log } from "@ethect/core/rpc";

Erc1155.fn.uri.selector; // "0x0e89341c"
// the transfers among logs, of one id or of several: told apart by `_tag`
const moved = (logs: ReadonlyArray<Log.Log>) =>
  Ev.decode([Erc1155.ev.TransferSingle, Erc1155.ev.TransferBatch], logs);
```

`Erc1155.err` is an empty record.

## extend

Gives ERC-1155 and what a token adds to it, in one contract. It is the
[`extend` of any contract](https://ethect.dev/reference/contract/#extend), and takes a contract.

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

const Items = Erc1155.extend(
  Abi.parse([
    "function totalSupply(uint256 id) view returns (uint256)",
    "error ERC1155InsufficientBalance(address sender, uint256 balance, uint256 needed, uint256 tokenId)",
    "error ERC1155MissingApprovalForAll(address operator, address owner)",
  ]),
);

const balance = (items: Address.Address, owner: Address.Address) =>
  Items.at(items).balanceOf(owner, 1n).pipe(
    Effect.catchTag("ERC1155MissingApprovalForAll", () => Effect.succeed(0n)),
  );
```

The errors are failures of every function. A function, an event or an error of a name the
standard has takes its place: see [`contract.extend`](https://ethect.dev/reference/contract/#extend) for every
case, and [`Erc20.extend`](https://ethect.dev/reference/standards/erc20/#extend) for a JSON ABI and a contract
made by hand.

### Returns

[`Contract.Contract<Fns, Evs, Errs>`](https://ethect.dev/reference/contract/#extend)

### Parameters

#### more

- **Type:** [`Contract.Contract`](https://ethect.dev/reference/contract/)

The contract to add: one that [`Abi.parse`](https://ethect.dev/reference/abi/parse/),
[`Abi.fromJson`](https://ethect.dev/reference/abi/from-json/) or [`Contract.make`](https://ethect.dev/reference/contract/#make)
gave, or another built-in one.

## Compared with viem's `erc1155Abi`

The same eight functions and four events, by signature and by mutability. Two things differ:

| Item | Here | viem's `erc1155Abi` |
| --- | --- | --- |
| errors | none: the EIP declares none | seven, those of [ERC-6093](https://eips.ethereum.org/EIPS/eip-6093) |
| the first field of `ApprovalForAll` | `owner`, as the EIP names it | `account` |

A token that reverts with the errors of ERC-6093 takes them through `extend`, as above. The
ABI you gave viem works as it is: `Abi.fromJson(erc1155Abi)`.

## Behavior

- **Nothing is encoded or sent.** `Erc1155` is definitions. A call is built when you call a
  function, and reaches a node when it is read or sent.
- **The arguments have names.** An editor shows `balanceOf(owner, id)`.
- **The lengths of the arrays are not checked.** `ids` and `values` of two lengths build a
  call, and the token reverts.
- **`extend` changes nothing of `Erc1155`.** It gives a new contract.
- **A program ships what it uses.** One that does not import `Erc1155` holds nothing of it.

## Errors

`Erc1155` declares no error, and none of its exports fails.

| Error | When |
| --- | --- |
| [`Call.Error`](https://ethect.dev/reference/call/#what-a-read-fails-with) | reading a call: the node cannot be reached, the call reverts, or what came back is not what the function returns |
| [`AbiError`](https://ethect.dev/reference/errors/abi-error/) | using a function of a contract given to `extend`, when its signature did not parse |

## Under the hood

`Erc1155` asks no node. Its definitions are built once, when its module is loaded, with the
[definers](https://ethect.dev/reference/fn/view/) a contract made by hand uses. A request is sent when a call
is read or sent.

**`Erc1155.at(ITEMS).balanceOfBatch(owners, ids)`**

- **your program**: no request: the call is plain data, `{ to: ITEMS, fn: balanceOfBatch, args: [owners, ids] }`

**`yield*` that call**

- **your node** `eth_call({ to: ITEMS, data: 0x4e1273f4 + owners + ids })`: the read

A read goes through [`Call.Reader`](https://ethect.dev/reference/call/reader/), and a call that is sent through
[`Tx.send`](https://ethect.dev/reference/tx/send/#under-the-hood).
