Spring til hovedindholdet

Hjælp med at oversætte denne side


Du ser denne side på engelsk, fordi vi ikke har oversat den endnu. Hjælp os med at oversætte dette indhold.

Oversæt siden

Ingen fejl her!🐛

Denne side oversættes ikke. Siden er bevidst bibeholdt på engelsk for nu.

ERC-1155 Multi-Token Standard

Seneste redigering: , Invalid DateTime
Redigér side


A standard interface for contracts that manage multiple token types. A single deployed contract may include any combination of fungible tokens, non-fungible tokens or other configurations (e.g. semi-fungible tokens).

What is meant by Multi-Token Standard?

The idea is simple and seeks to create a smart contract interface that can represent and control any number of fungible and non-fungible token types. In this way, the ERC-1155 token can do the same functions as an ERC-20 and ERC-721 token, and even both at the same time. And best of all, improving the functionality of both standards, making it more efficient, and correcting obvious implementation errors on the ERC-20 and ERC-721 standards.

The ERC-1155 token is described fully in EIP-1155.


To better understand this page, we recommend you first read about token standards, ERC-20, and ERC-721.

ERC-1155 Functions and Features:

Batch Transfers

The batch transfer works very similar to regular ERC-20 transfers. Let's look at the regular ERC-20 transferFrom function:

1// ERC-20
2function transferFrom(address from, address to, uint256 value) external returns (bool);
4// ERC-1155
5function safeBatchTransferFrom(
6 address _from,
7 address _to,
8 uint256[] calldata _ids,
9 uint256[] calldata _values,
10 bytes calldata _data
11) external;
Vis alle
📋 Kopiér

The only difference in ERC-1155 is that we pass the values as an array and we also pass an array of id's. For example given ids=[3, 6, 13] and values=[100, 200, 5], the resulting transfers will be

  1. Transfer 100 tokens with id 3 from _from to _to.
  2. Transfer 200 tokens with id 6 from _from to _to.
  3. Transfer 5 tokens with id 13 from _from to _to.

In ERC-1155 we only have transferFrom, no transfer. To use it like a regular transfer, just set the from address to the address that's calling the function.

Batch Balance

The respective ERC-20 balanceOf call likewise has its partner function with batch support. As a reminder, this is the ERC-20 version:

1// ERC-20
2function balanceOf(address owner) external view returns (uint256);
4// ERC-1155
5function balanceOfBatch(
6 address[] calldata _owners,
7 uint256[] calldata _ids
8) external view returns (uint256[] memory);
📋 Kopiér

Even simpler for the balance call, we can retrieve multiple balances in a single call. We pass the array of owners, followed by the array of token ids.

For example given _ids=[3, 6, 13] and _owners=[0xbeef..., 0x1337..., 0x1111...], the return value will be

2 balanceOf(0xbeef...),
3 balanceOf(0x1337...),
4 balanceOf(0x1111...)
📋 Kopiér

Batch Approval

1// ERC-1155
2function setApprovalForAll(
3 address _operator,
4 bool _approved
5) external;
7function isApprovedForAll(
8 address _owner,
9 address _operator
10) external view returns (bool);
Vis alle
📋 Kopiér

The approvals are slightly different than ERC-20. Instead of approving specific amounts, you set an operator to approved or not approved via setApprovalForAll.

Reading the current status can be done via isApprovedForAll. As you can see, it's an all or nothing. You cannot define how many tokens to approve or even which token class.

This is intentionally designed with simplicity in mind. You can only approve everything for one address.

Receive Hook

1function onERC1155BatchReceived(
2 address _operator,
3 address _from,
4 uint256[] calldata _ids,
5 uint256[] calldata _values,
6 bytes calldata _data
7) external returns(bytes4);
📋 Kopiér

Given the EIP-165 support, ERC-1155 supports receive hooks for smart contracts only. The hook function must return a magic predefined bytes4 value which is given as:

📋 Kopiér

When the receiving contract returns this value, it is assumed the contract accepts the transfer and knows how to handle the ERC-1155 tokens. Great, no more stuck tokens in a contract!

NFT Support

When the supply is just one, the token is essentially a non-fungible token (NFT). And as is standard for ERC-721, you can define a metadata URL. The URL can be read and modified by clients, see here.

Safe Transfer Rule

We've touched on a few safe transfer rules already in the previous explanations. But let's look at the most important of the rules:

  1. The caller must be approved to spend the tokens for the _from address or the caller must equal _from.
  2. The transfer call must revert if
    1. _to address is 0.
    2. length of _ids is not the same as length of _values.
    3. any of the balance(s) of the holder(s) for token(s) in _ids is lower than the respective amount(s) in _values sent to the recipient.
    4. any other error occurs.

Note: All batch functions including the hook also exist as versions without batch. This is done for gas efficiency, considering transferring just one asset will likely still be the most commonly used way. We've left them out for simplicity in the explanations, including safe transfer rules. The names are identical, just remove the 'Batch'.

Further reading

Var denne artikel nyttig?