Reference implementation of ERC-8527: Serial-Numbered ERC-20 Tokens

This directory holds the reference implementation of ERC-8527, “Serial-Numbered ERC-20 Tokens”, as a self-contained Foundry package, next to the other assets of the draft. It has no dependency other than forge-std, which only the tests use.

The implementation is written to check that the specification can be implemented and to be read alongside it. It is not audited and not optimized. The parts of the freezing extension, the classification extension and defragmentation that transfers do not use are in four libraries, which are deployed separately and linked into a token, so that every combination of modules on the slice stack is within the contract code size limit (see “Code size”).

Defragmentation is called consolidation in the code, which predates the draft’s term: the module that opens it is ERC8527Consolidation, its records are kept by ERC8527Consolidations, and the old serials it renumbers are reported by consolidatedSpanAt, as in the draft.

Every contract, library, function and error of the implementation that carries the name of the proposal uses its number, 8527 (IERC8527, ERC8527Freezable, ERC8527HoldingsStack, acceptsERC8527Transfer, ERC8527TransferRefused, …).

What the implementation does and where it falls short of the draft’s recommendations is described in reference-implementation.md. The sequences that test/conformance/DraftSequences.t.sol follows are in test-cases.md.

Layout

Path Content
contracts/spec/ The five interfaces of the draft: IERC8527 (core), IERC8527Freezable, IERC8527Classified, IERC8527DefragmentationFor (on-behalf defragmentation extension), and IERC8527Acceptance, which a receiving contract implements. From interface on, each file is identical to the code block in the draft. The on-behalf defragmentation extension is not implemented: only its interface is here, and its identifier is asserted by test/InterfaceIds.t.sol.
contracts/ERC8527.sol The core: ERC-20 and the core interface, holdings divided into piles, selection by amount. Defragmentation is closed by default: defragmentSlices always reverts. Abstract.
contracts/extensions/ One abstract contract per module: ERC8527Freezable (freezing extension, with the forced operations that take a position hint), ERC8527Classified (classification extension), ERC8527Allowlist (the example class that restricts destinations by payee address), ERC8527Acceptance (acceptance check), ERC8527Consolidation (opens defragmentation), ERC8527Burnable (burn, burnFrom, burnSlices), ERC8527Tidy (tidySlices). The files of the freezing and the classification extensions also hold their libraries, ERC8527FreezeOps and ERC8527ClassOps. Who may freeze and unfreeze is decided by canFreeze and canUnfreeze, which the token implements and which the freezing extension checks against the caller of every freeze and release.
contracts/holdings/ The storage of holdings: the abstract layer ERC8527Holdings, and its two implementations, the slice stack ERC8527HoldingsStack (with the library ERC8527StackBatch, which defragmentation uses) and the sorted array ERC8527HoldingsSorted.
contracts/utils/ The ERC-20 and ERC-165 interfaces and the ERC-6093 errors; the step function library ERC8527Steps; ERC8527Bounds; the defragmentation record library ERC8527Consolidations. ERC8527Steps and ERC8527Bounds are compiled into the token; ERC8527Consolidations is deployed separately.
contracts/examples/ ERC8527ExampleToken: one concrete token with every module except burning, on the slice stack, with a single admin. It is there to show how the modules are combined; with its libraries linked, it is within the code size limit.
test/presets/ The concrete tokens the tests run against: combinations of the modules on both storage backends, with a single test admin (Presets.sol), and the list of targets (Targets.sol).
test/conformance/ Run against every combination: the sequences of the draft’s test cases (DraftSequences), the errors and description queries of this implementation (Errors), and random walks and invariant tests against a shadow model that is rebuilt from events only (Invariant).
test/extensions/ Tests of the acceptance check, defragmentation, the forced operations that take a position hint and tidySlices, canFreeze and canUnfreeze against every freezing and unfreezing function and the operator of the freeze and forced events, the second selection rule, and the bounds kept for restrictions.
test/holdings/, test/utils/ Fuzz tests of the two storage backends against a bitmap model, and of the defragmentation record library against an oracle.
test/mocks/ Receiving contracts for the acceptance check tests.
test/examples/ A short test of the example token.
test/InterfaceIds.t.sol Asserts the ERC-165 identifiers written in the draft against the values the compiler computes.

Running the tests

Install forge-std into lib/ from this directory. --no-git keeps it from being registered as a submodule of the surrounding repository:

forge install foundry-rs/forge-std --no-git

Then:

forge test

Expected result: 893 tests passed, 0 failed, 155 skipped, 1,048 in total, in 92 test suites.

A skipped test is one that does not apply to the combination it runs against, for example a freezing test on a token without the freezing extension. The invariant tests take most of the time; the whole run takes one to two minutes. forge deploys and links the libraries by itself.

These results were obtained with forge 1.8.0 and forge-std 1.17.0, with the settings of foundry.toml: solc 0.8.36, optimizer on, 200 runs.

Snapshot

The code is a snapshot of commit 2d4b1b8e2311137033b6112941abe950edaab53c of the repository in which the draft and this implementation are developed. Against that commit, the snapshot has comments translated to English, gas benchmarks and experimental variants left out, the example token added, a few local variables renamed for the spelling check of the ERCs repository, and the wording of a few comments changed.

Code size

EIP-170 limits the runtime code of a contract to 24,576 bytes. To stay within it, the parts of the freezing extension, the classification extension and defragmentation that transfers do not use are in four libraries with external functions:

Library File Content Runtime size
ERC8527FreezeOps contracts/extensions/ERC8527Freezable.sol Freezing and unfreezing (rebuilding the step function, emitting the events), reason descriptions, the counting behind freezeReasonsOf, frozenSpanAt and frozenBalanceOf, the check that the units of a forced operation are frozen 11,304
ERC8527ClassOps contracts/extensions/ERC8527Classified.sol Setting classes and issuing with a class, carrying classes over after a defragmentation, class descriptions, the counting behind classesOf 5,609
ERC8527Consolidations contracts/utils/ERC8527Consolidations.sol Checking the arguments of a defragmentation, writing its records and emitting SliceConsolidated, consolidatedSpanAt, and the lookups that let a freeze follow consolidated serials 6,951
ERC8527StackBatch contracts/holdings/ERC8527HoldingsStack.sol Taking several slices out of the slice stack at once during a defragmentation 1,658

Each library is deployed once, and a token that uses it has the library’s address linked into its code when it is deployed; tokens on the same chain can share the same deployed libraries. forge test deploys and links them by itself. To deploy a token, deploy the libraries first, or use ones already deployed on the chain, and give their addresses to the linker, for example with forge’s --libraries option.

Transfers make no library calls. Freezing, unfreezing, setting a class, forced operations and defragmentation call a library, which costs somewhat more than the same code compiled into the token would.

To save code, classesOf, classInfo, freezeReasonsOf, freezeReasonInfo and frozenSpanAt are declared external and return the data that the library has already ABI-encoded. A contract that inherits them can override them, but cannot call them internally or through super.

The table gives the runtime size in bytes of the test presets in test/presets/Presets.sol and of the example token, without the libraries, as reported by forge build --sizes with solc 0.8.36, optimizer on, 200 runs, without via_ir. Every preset also contains the burn module and the admin functions of the tests, and every preset with the freezing or the classification extension contains tidySlices.

Combination Contract measured (stack / sorted array) Slice stack Sorted array Fits (stack / sorted array)
Core PresetCoreStack / PresetCoreSorted 12,454 14,761 yes / yes
Core, acceptance check PresetAcceptStack / PresetAcceptSorted 13,172 15,479 yes / yes
Core, defragmentation PresetCoreConsolStack / PresetCoreConsolSorted 14,797 17,895 yes / yes
Classes PresetClassStack / PresetClassSorted 16,985 19,293 yes / yes
Classes, defragmentation PresetClassConsolStack / PresetClassConsolSorted 19,378 22,535 yes / yes
Freezing PresetFreezeStack / PresetFreezeSorted 18,685 20,993 yes / yes
Freezing, defragmentation PresetFreezeConsolStack / PresetFreezeConsolSorted 21,217 24,374 yes / yes
Freezing, classes PresetBothStack / PresetBothSorted 21,660 23,968 yes / yes
Freezing, classes, acceptance check PresetAllStack / PresetAllSorted 22,444 24,752 yes / no
Freezing, classes, defragmentation PresetBothConsolStack / PresetBothConsolSorted 24,263 27,420 yes / no
Freezing, classes, acceptance check, defragmentation, no burning ERC8527ExampleToken 23,859   yes

With the burn module added to the example token, so that it has every module, its runtime code is 24,385 bytes. The functions that a token adds itself, for issuance and administration, count toward its size as well. So every combination on the slice stack can be deployed where EIP-170 applies, together with its libraries, as long as the deployment parameters of defragmentation keep their defaults. On the sorted array, which is there to compare data structures, the freezing and classification extensions together with the acceptance check or with defragmentation are over the limit. The margins are small: PresetBothConsolTunedStack, the same combination as PresetBothConsolStack with the deployment parameters of defragmentation overridden, is 24,697 bytes, 121 over the limit, and another compiler version or setting can take other combinations over it.

EIP-7954 raises the limit to 65,536 bytes. It is scheduled for the Glamsterdam upgrade, which was not active on mainnet when this was written (November 2026). Every contract in the tables is below that limit.

No compiler setting was tuned for size. The tests are not affected by the limit, because the test EVM of forge does not enforce it by default. forge build --sizes exits with an error because of the two rows over the limit on the sorted array and of PresetBothConsolTunedStack.

License

The code in contracts/ and test/ is licensed under the MIT License, as the header of each file states; the text of the license and the copyright notice are in LICENSE.md. The five interface files in contracts/spec/ are the exception: they are identical to the code blocks of the draft, and for them, as for the draft and the documents in this directory, copyright and related rights are waived via CC0.