ERC-1155 token (Erc1155)
The ERC-1155 multi-token, built in: the functions and the events of EIP-1155, with no ABI to write. It is a contract: at an address it gives its functions, and each one builds calls.
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) |
function | The functions at a token: each one builds calls. |
Erc1155.fn |
record | The functions, unbound: Erc1155.fn.balanceOfBatch. |
Erc1155.ev |
record | The events: TransferSingle, TransferBatch, ApprovalForAll, URI. |
Erc1155.err |
record | The errors: none. |
Erc1155.extend(more) |
function | ERC-1155 and what a token adds to it, in one contract. |
What it declares
Section titled “What it declares”The six functions and the four events of the EIP, the supportsInterface of the
ERC-165 it requires, and the uri of its optional
metadata extension.
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-165function supportsInterface(bytes4 interfaceId) view returns (bool)
// optional: metadatafunction 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
urifails. - It declares no error. The EIP has none: each token reverts with its own, and
extendadds them.
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.
import { Address, Erc1155 } from "@ethect/core";
const items = Erc1155.at(Address.makeUnsafe("0x76BE3b62873462d2142405439777e971754E8E77"));
// a view: Call.View<bigint>, also the effect of reading itconst 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
Section titled “Returns”Contract.Bound<Fns>: an object with one key per function.
Parameters
Section titled “Parameters”- Type:
Address.Address
The address of the token.
fn, ev and err
Section titled “fn, ev and err”The definitions themselves, as a contract holds them.
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
Section titled “extend”Gives ERC-1155 and what a token adds to it, in one contract. It is the
extend of any contract, and takes a contract.
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 for every
case, and Erc20.extend for a JSON ABI and a contract
made by hand.
Returns
Section titled “Returns”Contract.Contract<Fns, Evs, Errs>
Parameters
Section titled “Parameters”- Type:
Contract.Contract
The contract to add: one that Abi.parse,
Abi.fromJson or Contract.make
gave, or another built-in one.
Compared with viem’s erc1155Abi
Section titled “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 |
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
Section titled “Behavior”- Nothing is encoded or sent.
Erc1155is 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.
idsandvaluesof two lengths build a call, and the token reverts. extendchanges nothing ofErc1155. It gives a new contract.- A program ships what it uses. One that does not import
Erc1155holds nothing of it.
Errors
Section titled “Errors”Erc1155 declares no error, and none of its exports fails.
| Error | When |
|---|---|
Call.Error |
reading a call: the node cannot be reached, the call reverts, or what came back is not what the function returns |
AbiError |
using a function of a contract given to extend, when its signature did not parse |
Under the hood
Section titled “Under the hood”Erc1155 asks no node. Its definitions are built once, when its module is loaded, with the
definers a contract made by hand uses. A request is sent when a call
is read or sent.
Erc1155.at(ITEMS).balanceOfBatch(owners, ids)- your programno request: the call is plain data,
{ to: ITEMS, fn: balanceOfBatch, args: [owners, ids] }
- your programno request: the call is plain data,
yield*that call- your node
eth_call({ to: ITEMS, data: 0x4e1273f4 + owners + ids })the read
- your node
A read goes through Call.Reader, and a call that is sent through
Tx.send.