Skip to content
Lattice

ERC1155SupplyLib

Git Source

Title: ERC1155SupplyLib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/extensions/ERC1155Supply.sol)

Library implementing per-id and all-ids total supply tracking for ERC-1155 tokens.

OpenZeppelin v5.6.1 overrides _update, so every mint and burn updates the counters. Lattice's base ERC1155Lib has no hook (decision D25(a) on #234), so this library carries its own mint and burn paths: _update runs ERC1155Lib._update, then the OpenZeppelin supply arithmetic, and _updateWithAcceptanceCheck runs the receiver check after both, in OpenZeppelin's order. Differences from OpenZeppelin v5.6.1:

  • Only mints and burns that go through this library are counted. A facet that mints or burns through ERC1155Lib directly (including ERC1155Burnable's burn) leaves the counters behind, and a later burn through this library then wraps the unchecked subtraction. A mint facet on a supply-tracked diamond must call _mint/_mintBatch, and the ERC1155Supply facet serves burn/burnBatch in place of ERC1155Burnable's.
  • OpenZeppelin warns against adding this extension in an upgrade to a deployed token. On a diamond that means: never cut ERC1155Supply into a diamond that already holds balances, since those balances are not in the counters and burning them wraps.
  • The supply views and burns are exported by a facet (OpenZeppelin's burn/burnBatch live in ERC1155Burnable), and the views are advertised through ERC-165 as IERC1155Supply. Overflow is as in OpenZeppelin: the mint arithmetic is checked, per id and across all ids (a global limit of type(uint256).max tokens), and the burn arithmetic is unchecked. All state lives in its own ERC-7201 slot.

Functions

erc1155SupplyStorage

function erc1155SupplyStorage() internal pure returns (ERC1155SupplyStorage storage $);

Returns

NameTypeDescription
$ERC1155SupplyStorage

__ERC1155Supply_init

Initializes the ERC-1155 supply module: registers IERC1155Supply via ERC-165.

Must be called inside a pre/postInitializer block. The counters start at zero, so the diamond must not hold balances yet.

function __ERC1155Supply_init() internal;

registerInterface

Registers support for the IERC1155Supply interface via ERC-165.

function registerInterface() internal;

totalSupply

Total value of tokens of type id.

function totalSupply(uint256 id) internal view returns (uint256);

Parameters

NameTypeDescription
iduint256

Returns

NameTypeDescription
<none>uint256

totalSupply

Total value of tokens across every id.

function totalSupply() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

exists

Whether any token of type id exists.

function exists(uint256 id) internal view returns (bool);

Parameters

NameTypeDescription
iduint256

Returns

NameTypeDescription
<none>bool

burn

Destroys value tokens of type id from account, lowering the supply. The caller must be account or an approved operator of account (as ERC1155BurnableLib.burn).

function burn(address account, uint256 id, uint256 value) internal;

Parameters

NameTypeDescription
accountaddress
iduint256
valueuint256

burnBatch

Batched version of burn.

function burnBatch(address account, uint256[] memory ids, uint256[] memory values) internal;

Parameters

NameTypeDescription
accountaddress
idsuint256[]
valuesuint256[]

_mint

Mints value of token id to to, raising the supply. Supply-tracking ERC1155Lib._mint.

function _mint(address to, uint256 id, uint256 value, bytes memory data) internal;

Parameters

NameTypeDescription
toaddress
iduint256
valueuint256
databytes

_mintBatch

Batch mints tokens to to, raising the supply. Supply-tracking ERC1155Lib._mintBatch.

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

Parameters

NameTypeDescription
toaddress
idsuint256[]
valuesuint256[]
databytes

_burn

Burns value of token id from from, lowering the supply. Supply-tracking ERC1155Lib._burn.

function _burn(address from, uint256 id, uint256 value) internal;

Parameters

NameTypeDescription
fromaddress
iduint256
valueuint256

_burnBatch

Batch burns tokens from from, lowering the supply. Supply-tracking ERC1155Lib._burnBatch.

function _burnBatch(address from, uint256[] memory ids, uint256[] memory values) internal;

Parameters

NameTypeDescription
fromaddress
idsuint256[]
valuesuint256[]

_updateWithAcceptanceCheck

_update, then the receiver acceptance check when to is not the zero address. Mirrors ERC1155Lib._updateWithAcceptanceCheck, so the receiver sees the updated supply.

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

Parameters

NameTypeDescription
fromaddress
toaddress
idsuint256[]
valuesuint256[]
databytes
batchboolTrue for a batch operation: picks onERC1155BatchReceived over onERC1155Received.

_update

ERC1155Lib._update, then the supply counters: a mint (from == 0) raises them with checked arithmetic, a burn (to == 0) lowers them unchecked. Port of OpenZeppelin v5.6.1 ERC1155Supply._update.

function _update(address from, address to, uint256[] memory ids, uint256[] memory values) internal;

Parameters

NameTypeDescription
fromaddress
toaddress
idsuint256[]
valuesuint256[]