Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ERC1155Upgradeable

Inherits: Initializable, ContextUpgradeable, ERC165Upgradeable, IERC1155, IERC1155MetadataURI, IERC1155Errors

Implementation of the basic standard multi-token. See https://eips.ethereum.org/EIPS/eip-1155 Originally based on code by Enjin: https://github.com/enjin/erc-1155

State Variables

ERC1155StorageLocation

bytes32 private constant ERC1155StorageLocation = 0x88be536d5240c274a3b1d3a1be54482fd9caa294f08c62a7cde569f49a3c4500;

Functions

_getERC1155Storage

function _getERC1155Storage() private pure returns (ERC1155Storage storage $);

__ERC1155_init

See _setURI.

function __ERC1155_init(string memory uri_) internal onlyInitializing;

__ERC1155_init_unchained

function __ERC1155_init_unchained(string memory uri_) internal onlyInitializing;

supportsInterface

Query if a contract implements an interface

Interface identification is specified in ERC-165. This function uses less than 30,000 gas.

function supportsInterface(bytes4 interfaceId)
    public
    view
    virtual
    override(ERC165Upgradeable, IERC165)
    returns (bool);

Parameters

NameTypeDescription
interfaceIdbytes4

Returns

NameTypeDescription
<none>booltrue if the contract implements interfaceID and interfaceID is not 0xffffffff, false otherwise

uri

See IERC1155MetadataURI-uri. This implementation returns the same URI for all token types. It relies on the token type ID substitution mechanism https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the ERC]. Clients calling this function must replace the \{id\} substring with the actual token type ID.

function uri(uint256) public view virtual returns (string memory);

balanceOf

Get the balance of an account's tokens.

function balanceOf(address account, uint256 id) public view virtual returns (uint256);

Parameters

NameTypeDescription
accountaddress
iduint256

Returns

NameTypeDescription
<none>uint256The _owner's balance of the token type requested

balanceOfBatch

*See IERC1155-balanceOfBatch. Requirements:

  • accounts and ids must have the same length.*
function balanceOfBatch(address[] memory accounts, uint256[] memory ids)
    public
    view
    virtual
    returns (uint256[] memory);

setApprovalForAll

Enable or disable approval for a third party ("operator") to manage all of the caller's tokens.

MUST emit the ApprovalForAll event on success.

function setApprovalForAll(address operator, bool approved) public virtual;

Parameters

NameTypeDescription
operatoraddress
approvedbool

isApprovedForAll

Queries the approval status of an operator for a given owner.

function isApprovedForAll(address account, address operator) public view virtual returns (bool);

Parameters

NameTypeDescription
accountaddress
operatoraddress

Returns

NameTypeDescription
<none>boolTrue if the operator is approved, false if not

safeTransferFrom

Transfers _value amount of an _id from the _from address to the _to address specified (with safety call).

*Caller must be approved to manage the tokens being transferred out of the _from account (see "Approval" section of the standard).

  • MUST revert if _to is the zero address.
  • MUST revert if balance of holder for token _id is lower than the _value sent.
  • MUST revert on any other error.
  • MUST emit the TransferSingle event to reflect the balance change (see "Safe Transfer Rules" section of the standard).
  • After the above conditions are met, this function MUST check if _to is a smart contract (e.g. code size > 0). If so, it MUST call onERC1155Received on _to and act appropriately (see "Safe Transfer Rules" section of the standard).*
function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes memory data) public virtual;

Parameters

NameTypeDescription
fromaddress
toaddress
iduint256
valueuint256
databytes

safeBatchTransferFrom

Transfers _values amount(s) of _ids from the _from address to the _to address specified (with safety call).

*Caller must be approved to manage the tokens being transferred out of the _from account (see "Approval" section of the standard).

  • MUST revert if _to is the zero address.
  • MUST revert if length of _ids is not the same as length of _values.
  • MUST revert if 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.
  • MUST revert on any other error.
  • MUST emit TransferSingle or TransferBatch event(s) such that all the balance changes are reflected (see "Safe Transfer Rules" section of the standard).
  • Balance changes and events MUST follow the ordering of the arrays (_ids[0]/_values[0] before _ids[1]/_values[1], etc).
  • After the above conditions for the transfer(s) in the batch are met, this function MUST check if _to is a smart contract (e.g. code size > 0). If so, it MUST call the relevant ERC1155TokenReceiver hook(s) on _to and act appropriately (see "Safe Transfer Rules" section of the standard).*
function safeBatchTransferFrom(
    address from,
    address to,
    uint256[] memory ids,
    uint256[] memory values,
    bytes memory data
) public virtual;

Parameters

NameTypeDescription
fromaddress
toaddress
idsuint256[]
valuesuint256[]
databytes

_update

*Transfers a value amount of tokens of type id from from to to. Will mint (or burn) if from (or to) is the zero address. Emits a {TransferSingle} event if the arrays contain one element, and {TransferBatch} otherwise. Requirements:

  • If to refers to a smart contract, it must implement either {IERC1155Receiver-onERC1155Received} or {IERC1155Receiver-onERC1155BatchReceived} and return the acceptance magic value.
  • ids and values must have the same length. NOTE: The ERC-1155 acceptance check is not performed in this function. See {_updateWithAcceptanceCheck} instead.*
function _update(address from, address to, uint256[] memory ids, uint256[] memory values) internal virtual;

_updateWithAcceptanceCheck

Version of _update that performs the token acceptance check by calling {IERC1155Receiver-onERC1155Received} or {IERC1155Receiver-onERC1155BatchReceived} on the receiver address if it contains code (eg. is a smart contract at the moment of execution). IMPORTANT: Overriding this function is discouraged because it poses a reentrancy risk from the receiver. So any update to the contract state after this function would break the check-effect-interaction pattern. Consider overriding {_update} instead.

function _updateWithAcceptanceCheck(
    address from,
    address to,
    uint256[] memory ids,
    uint256[] memory values,
    bytes memory data
) internal virtual;

_safeTransferFrom

*Transfers a value tokens of token type id from from to to. Emits a {TransferSingle} event. Requirements:

  • to cannot be the zero address.
  • from must have a balance of tokens of type id of at least value amount.
  • If to refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the acceptance magic value.*
function _safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes memory data) internal;

_safeBatchTransferFrom

*xref:ROOT:erc1155.adoc#batch-operations[Batched] version of _safeTransferFrom. Emits a {TransferBatch} event. Requirements:

  • If to refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the acceptance magic value.
  • ids and values must have the same length.*
function _safeBatchTransferFrom(
    address from,
    address to,
    uint256[] memory ids,
    uint256[] memory values,
    bytes memory data
) internal;

_setURI

Sets a new URI for all token types, by relying on the token type ID substitution mechanism https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the ERC]. By this mechanism, any occurrence of the \{id\} substring in either the URI or any of the values in the JSON file at said URI will be replaced by clients with the token type ID. For example, the https://token-cdn-domain/\{id\}.json URI would be interpreted by clients as https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json for token type ID 0x4cce0. See uri. Because these URIs cannot be meaningfully represented by the {URI} event, this function emits no events.

function _setURI(string memory newuri) internal virtual;

_mint

*Creates a value amount of tokens of type id, and assigns them to to. Emits a {TransferSingle} event. Requirements:

  • to cannot be the zero address.
  • If to refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the acceptance magic value.*
function _mint(address to, uint256 id, uint256 value, bytes memory data) internal;

_mintBatch

*xref:ROOT:erc1155.adoc#batch-operations[Batched] version of _mint. Emits a {TransferBatch} event. Requirements:

  • ids and values must have the same length.
  • to cannot be the zero address.
  • If to refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the acceptance magic value.*
function _mintBatch(address to, uint256[] memory ids, uint256[] memory values, bytes memory data) internal;

_burn

*Destroys a value amount of tokens of type id from from Emits a {TransferSingle} event. Requirements:

  • from cannot be the zero address.
  • from must have at least value amount of tokens of type id.*
function _burn(address from, uint256 id, uint256 value) internal;

_burnBatch

*xref:ROOT:erc1155.adoc#batch-operations[Batched] version of _burn. Emits a {TransferBatch} event. Requirements:

  • from cannot be the zero address.
  • from must have at least value amount of tokens of type id.
  • ids and values must have the same length.*
function _burnBatch(address from, uint256[] memory ids, uint256[] memory values) internal;

_setApprovalForAll

*Approve operator to operate on all of owner tokens Emits an {ApprovalForAll} event. Requirements:

  • operator cannot be the zero address.*
function _setApprovalForAll(address owner, address operator, bool approved) internal virtual;

_asSingletonArrays

Creates an array in memory with only one value for each of the elements provided.

function _asSingletonArrays(uint256 element1, uint256 element2)
    private
    pure
    returns (uint256[] memory array1, uint256[] memory array2);

Structs

ERC1155Storage

Note: storage-location: erc7201:openzeppelin.storage.ERC1155

struct ERC1155Storage {
    mapping(uint256 id => mapping(address account => uint256)) _balances;
    mapping(address account => mapping(address operator => bool)) _operatorApprovals;
    string _uri;
}