Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 47 ยท Tokens and Standards

ERC-1155, ERC-4626 and Friends

Learn the multi-token standard ERC-1155, the ERC-4626 vault share standard, ERC-165 interface detection and the permit idea, with runnable stand-ins.

ERC-20 and ERC-721 are the best-known token standards, but they are not the only ones. A game with 10,000 item types would need 10,000 ERC-20 contracts, or one ERC-721 entry per sword. A lending pool needs a way to say "your deposit is 3.2% of the pool". Contracts also need a way to ask another contract "do you speak this standard?" before they send it something valuable. This lesson covers four ideas that answer those needs: ERC-165, ERC-1155, ERC-4626 and permits.

All of them follow the same pattern you have seen before: a small agreed interface so that wallets, markets and other contracts can work with any implementation. You will want to know ERC-20 and ERC-721 first.

Four standards, four jobs

ERC-165 Ask a contract which interfaces it supports.
ERC-1155 Many token types in one contract.
ERC-4626 Vaults that issue shares for deposits.
Permit Approve with a signature, not a transaction.

Each one is a small agreed interface that any tool can rely on.

1 / 2

ERC-165: asking "do you support this?"

Contracts have no way to inspect each other's code in a safe, general way. ERC-165 gives them a simple convention: a contract implements one function, supportsInterface(bytes4 interfaceId), which returns true for each interface it supports.

An interface id is the XOR of the selectors of every function in the interface (see ABI Encoding and Low-Level Calls for selectors). XOR keeps it to four bytes, however large the interface is. Ids you will meet:

Interface Id
ERC-165 itself 0x01ffc9a7
ERC-721 0x80ac58cd
ERC-721 metadata extension 0x5b5e139f
ERC-1155 0xd9b67a26

Marketplaces and routers call supportsInterface(0x80ac58cd) to learn whether an address is an NFT contract. Standards such as ERC-721 and ERC-1155 build it in; ERC-20 predates ERC-165 and has no such function.

The stand-in below uses a toy hash for selectors, but the XOR rule is the real one: order does not matter, and the same set of functions always gives the same id.

JS Interface ids by XOR

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A caveat: ERC-165 is a promise, not a proof. A contract can return true and still behave badly, so it detects honest mistakes, not malice.

ERC-1155: many tokens, one contract

ERC-1155 is the multi-token standard. One contract holds any number of token types, identified by an id. Each id can be fungible (a million gold coins), non-fungible (a supply of one) or semi-fungible (100 identical tickets). The balance table is two-dimensional: balanceOf(account, id).

Its main advantage is batching. safeBatchTransferFrom(from, to, ids, amounts, data) moves many token types in one transaction, and balanceOfBatch(accounts, ids) reads many balances in one call. That saves a lot of gas compared with a transfer per token. Instead of one Transfer event per token it emits TransferSingle or TransferBatch.

Key differences from ERC-721:

  • There is no per-token approval, only setApprovalForAll(operator, approved).
  • Transfers are always safe-style: safeTransferFrom and safeBatchTransferFrom are the only transfer functions, and they call onERC1155Received or onERC1155BatchReceived on contract receivers (expecting 0xf23a6e61 and 0xbc197c81 respectively).
  • Metadata is one function, uri(id), which returns a URL template that may contain ; clients replace it with the id written as 64 lowercase hexadecimal digits.
JS Batch balances and batch transfers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In the real EVM the failed batch would revert the whole transaction, so no partial transfer remains. The stand-in gets the same effect by checking every line before changing anything.

ERC-4626: tokenized vaults

A vault takes a deposit of some token (the asset), gives the depositor shares, and later exchanges shares back for assets. If the vault earns yield (interest, trading profit, staking rewards), each share becomes worth more assets over time. Before ERC-4626 every project invented its own function names. The standard fixes them, so aggregators and wallets integrate once. A vault is itself an ERC-20: the shares are its token.

The main functions:

  • asset(), totalAssets(): which token, and how much of it the vault controls
  • convertToShares(assets) and convertToAssets(shares): the current exchange rate
  • deposit(assets, receiver) and mint(shares, receiver): put assets in
  • withdraw(assets, receiver, owner) and redeem(shares, receiver, owner): take assets out
  • preview... and max... helpers for UIs

The math is a proportion: shares = assets x totalSupply / totalAssets. When the vault is empty, shares and assets are exchanged 1 to 1. Rounding always favors the vault: deposits give slightly fewer shares, and withdrawals take slightly more, so rounding errors can never be farmed to drain it.

JS Vault shares and yield

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the last line: when one share is worth more than one asset, a tiny deposit can round down to zero shares, and the depositor loses it. This ties into the best-known vault attack, the inflation (donation) attack: the first depositor puts in 1 unit, then "donates" a huge amount directly to the vault to inflate the price per share, so later depositors' shares round down to nothing. Defenses are virtual shares and assets (a built-in offset, which OpenZeppelin's ERC-4626 supports), a minimum first deposit, or checks on the minted amount.

Permits: approving with a signature

Plain ERC-20 approve costs a transaction and ETH for gas, even if you only wanted to swap. EIP-2612 permit adds a function permit(owner, spender, value, deadline, v, r, s). The owner signs an off-chain message that says "spender may use value until deadline", and anyone (usually the app) submits it, along with the action, in a single transaction. The signature uses the EIP-712 typed-data format, includes a per-owner nonce so it cannot be replayed, and a deadline so it expires. You will see how signatures and replay protection work in Common Vulnerabilities.

Not every token supports it. Check for permit and DOMAIN_SEPARATOR before you build a flow around it, and note that signing a permit is just as powerful as sending an approve: read what you sign.

What is real: XOR interface ids, the ERC-1155 batch functions and their rules, the ERC-4626 share math with floor rounding, and the permit idea. What is the stand-in: selectors use a toy hash, balances are JavaScript maps, receiver hooks and events are simplified, and the vault has no real token underneath. The permit idea is described, not run, because it needs real signatures.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How is an ERC-165 interface id calculated?

    The id is the XOR of the four-byte selectors of every function the interface declares, so it stays four bytes whatever the interface size, and the order does not matter.
  2. 2. What is the main benefit of ERC-1155 over using many ERC-20 or ERC-721 contracts?

    A single contract can hold fungible, non-fungible and semi-fungible ids, and batch functions move or read many ids in one call, saving gas and deployments.
  3. 3. In an ERC-4626 vault with 2,000 assets and 1,000 shares, how many shares does a 400-asset deposit mint?

    shares = assets x totalSupply / totalAssets = 400 x 1000 / 2000 = 200. Each share is worth 2 assets, so 400 assets buy 200 shares.
  4. 4. What does a permit signature replace?

    The owner signs an off-chain approval that anyone can submit with the action that needs it, so the user saves one transaction. Nonce and deadline protect against replay and old signatures.

Summary

  • ERC-165 lets contracts ask supportsInterface(id); an id is the XOR of the interface's function selectors.
  • ERC-1155 keeps many token ids in one contract, with batch transfers, batch balance reads, setApprovalForAll only and mandatory receiver checks.
  • ERC-4626 standardizes vaults: assets in, ERC-20 shares out, with rounding that favors the vault.
  • Watch for the vault inflation attack and rounding to zero shares; use virtual shares or minimums.
  • EIP-2612 permit approves through an off-chain signature with a nonce and deadline, saving a transaction.
  • Interface detection and signatures are conventions: they help honest code and do not stop malicious code.

Next: Using OpenZeppelin.