Test Cases

This file accompanies the draft ERC “Serial-Numbered ERC-20 Tokens”. Its content was the section “Test Cases” of the draft. Section names in quotation marks refer to sections of the draft.

The sequences of operations in this section hold for every implementation that conforms to this proposal and meets the following assumptions, whatever its selection rule:

  • Apart from the refusal defined in “Acceptance check”, the implementation does not reject any of the operations on grounds other than state attached to serials (see clause 6 of “Transferable balance”); apart from the extensions of this proposal, it also places no other restriction on serials.
  • None of the addresses denoted by letters in the sequences has code, unless stated otherwise.
  • “Mint” and “issue” refer to the issuance operation that the implementation provides. One issuance emits only one Transfer event; unless stated otherwise, the units issued are not frozen and their class is the default class.
  • Apart from what the steps state, no operation changes the frozen state or the class of any serial.
  • Freezing, unfreezing and forced operations are performed by a caller the implementation permits, with a reason the implementation accepts. The operator of every SliceFrozen, SliceUnfrozen and SliceForciblyTransferred event is the caller of the operation that emits it. Flow rules do not change while a sequence runs.

In the first sequence below, A, B, C and D are distinct non-zero addresses and the token starts with no units issued.

  1. Mint 100 units to A. One or more SliceTransferred events from the zero address to A are emitted, together covering the serials 0 through 99, followed by Transfer(0x0, A, 100). serialsIssued() is 100.
  2. A transfers 30 units to B. One or more SliceTransferred(A, B, start, amount) events are emitted, with slices inside (0, 100) and amounts summing to 30, followed by Transfer(A, B, 30). The call returns true. A transferring 1 unit to the zero address reverts.
  3. B transfers 10 units to C. The slices reported lie inside the slices B received in step 2.
  4. Mint 50 units to C. The SliceTransferred events emitted cover the serials 100 through 149. serialsIssued() is 150, and balanceOf is 70, 20 and 60 for A, B and C respectively.

The following steps continue from the state above and apply to contracts that implement the freezing extension. r1 and r2 are two different reasons for which freezeReasonInfo returns a non-empty name. freezeSlice is called by a caller the implementation permits, called F below; unfreezeSlice is called by a caller the implementation permits, called U below, which can be the same address as F.

  1. Before the call, canFreeze(r1, F) returns true; for any address X for which canFreeze(r1, X) returns false, X calling freezeSlice(0, 100, r1) reverts. F calls freezeSlice(0, 100, r1). The SliceFrozen events the contract emits with reason equal to r1 together cover the serials 0 through 99, and the operator of each is F. balanceOf is unchanged. For any to, transferableBalanceOf returns 0 for A and B and 50 for C. frozenBalanceOf is 70, 20 and 10 for A, B and C respectively. frozenSpanAt(0) returns frozen equal to true and length between 1 and 100; frozenSpanAt(100) returns frozen equal to false and length between 1 and 50; frozenSpanAt(150) returns (false, 0). freezeReasonsOf(C) has only one entry, 10 under r1. A calling approve(D, 5) succeeds and allowance(A, D) is 5; D calling transferFrom(A, D, 5) reverts, because A has no transferable units.
  2. B transferring 1 unit reverts. C transferring 51 units to D reverts; an implementation that follows the recommended error reverts with ERC8527InsufficientTransferableBalance(C, D, 51, 50). C transferring 50 units to D succeeds, and every slice reported lies inside (100, 50).
  3. freezeSlice(0, 150, r2) is called. The serials 0 through 99 are now frozen for both r1 and r2, and the serials 100 through 149 are frozen for r2 only. frozenBalanceOf(A) is still 70, while freezeReasonsOf(A) has two entries, 70 under r1 and 70 under r2. frozenBalanceOf(D) is 50, and freezeReasonsOf(D) has only one entry, 50 under r2.
  4. Before the call, canUnfreeze(r1, U) returns true; for any address X for which canUnfreeze(r1, X) returns false, X calling unfreezeSlice(0, 100, r1) reverts. U calls unfreezeSlice(0, 100, r1), and the operator of each SliceUnfrozen event the contract emits for it is U. The serials 0 through 99 are still frozen for r2: frozenSpanAt(0) still returns frozen equal to true; for any to, transferableBalanceOf returns 0 for each of A, B, C and D; and B transferring 1 unit still reverts. freezeReasonsOf(A) has only one entry, 70 under r2.
  5. unfreezeSlice(0, 100, r2) is called. The serials 0 through 99 are no longer frozen, and the serials 100 through 149 are still frozen for r2: frozenSpanAt(0) returns frozen equal to false and length between 1 and 100; frozenSpanAt(100) returns frozen equal to true and length between 1 and 50. frozenBalanceOf(A) is 0. For any to, transferableBalanceOf returns 70, 20 and 10 for A, B and C respectively, and 0 for D. B transferring 1 unit to D succeeds. D calling transferSlices(A, [100], [1]) reverts, because serial 100 is frozen; an implementation that follows the recommended error reverts with ERC8527NotTransferable(D, A, 100, 1). On a contract that provides burnSlices, D calling burnSlices([100], [1]) reverts as well, and the recommended error is ERC8527NotTransferable(D, 0x0, 100, 1).
  6. unfreezeSlice(100, 50, r2) is called. frozenBalanceOf is 0 for each of A, B, C and D, and freezeReasonsOf(D) returns two empty arrays. Afterwards balanceOf is 70, 19, 10 and 51 for A, B, C and D respectively, and totalSupply() and serialsIssued() are both 150. allowance(A, D) is still 5: neither freezing nor unfreezing changed it.

The following sequence starts from a token with no units issued and checks transferSlices; it too holds for every implementation that conforms to this proposal and meets the assumptions above. A and B are distinct non-zero addresses.

  1. Mint 100 units to A, with the serials 0 through 99.
  2. A calling transferSlices(B, [40], [20]) succeeds and returns true. The SliceTransferred events the contract emits from A to B together cover exactly the serials 40 through 59, followed by Transfer(A, B, 20). balanceOf is 80 and 20 for A and B respectively. The slices sliceAt returns for B together are the serials 40 through 59, and the slices it returns for A together are the serials 0 through 39 and 60 through 99.
  3. A calling transferSlices(B, [0, 70], [10, 5]) succeeds. The SliceTransferred events the contract emits from A to B together cover exactly the serials 0 through 9 and 70 through 74, followed by only one Transfer(A, B, 15). balanceOf is 65 and 35 for A and B respectively.
  4. Each of the following calls made by A reverts: transferSlices(B, [30], [20]), because the serials 40 through 49 are not held by A; transferSlices(B, [90], [20]), because the serials 100 through 109 have not been issued yet; transferSlices(B, [10], [0]), because the amount is zero; transferSlices(B, [], []), because the arrays are empty; transferSlices(B, [10, 20], [5]), because the two arrays differ in length; transferSlices(B, [10, 12], [5, 5]), because the two slices overlap; transferSlices(B, [20, 10], [5, 5]), because the slices are not given in ascending order of start; transferSlices(0x0, [10], [5]), because to is the zero address. An implementation that follows the recommended errors reverts with ERC8527NotHeld(A, 30, 20) and ERC8527NotHeld(A, 90, 20) for the first two calls respectively.
  5. B calling transferSlices(B, [40], [20]) succeeds. The contract emits Transfer(B, B, 20) and no SliceTransferred event, and no serial changes holder. B calling transferSlices(B, [40, 45], [10, 5]) reverts, because the two slices overlap; this rule applies equally when to is the caller.
  6. B calling transferSlices(A, [45], [5]) succeeds. Afterwards A holds the serials 10 through 39, 45 through 49, 60 through 69 and 75 through 99, B holds the serials 0 through 9, 40 through 44, 50 through 59 and 70 through 74, and balanceOf is 70 and 30 for A and B respectively.
  7. This step applies only to contracts that provide burnSlices. A calling burnSlices([0], [5]) reverts, because the serials 0 through 4 are not held by A; the recommended error is ERC8527NotHeld(A, 0, 5). A calling burnSlices([10, 60], [5, 10]) succeeds. The SliceTransferred events the contract emits from A to the zero address together cover exactly the serials 10 through 14 and 60 through 69, followed by only one Transfer(A, 0x0, 15). balanceOf(A) is 55, totalSupply() is 85, and serialsIssued() is still 100.

The following sequence starts from a token with no units issued and applies to contracts that implement the classification extension and have a class that restricts destinations by payee address. A, B and M are distinct non-zero addresses. Let w be a class for which classInfo(w) returns a non-empty name; M is a permitted recipient of this class, and A and B are not: when from is any one of A, B and M, classAllows(w, from, to) returns true when to is M and returns false when to is A or B. Transfers do not change the class of a unit. How classes are set is up to the implementation. r is a reason for which freezeReasonInfo(r) returns a non-empty name; freezeSlice is called by a caller the implementation permits.

  1. Issue 100 units of class w to A. The SliceClassified events the contract emits together cover the serials 0 through 99 and have classId equal to w; all of them are emitted after the Transfer event that issues these serials.
  2. Issue 50 units of the default class to A, with the serials 100 through 149. balanceOf(A) is 150, transferableBalanceOf(A, B) is 50, and transferableBalanceOf(A, M) is 150. classesOf(A) has two entries, 100 under w and 50 under zero. classSpanAt(0) returns classId equal to w and length between 1 and 100; classSpanAt(100) returns classId equal to zero and length between 1 and 50; classSpanAt(150) returns zero for both classId and length.
  3. A transferring 51 units to B reverts; an implementation that follows the recommended error reverts with ERC8527InsufficientTransferableBalance(A, B, 51, 50). A transferring 50 units to B succeeds, and every slice reported lies inside (100, 50).
  4. A transferring 30 units to M succeeds, and every slice reported lies inside (0, 100). The class of these serials is still w: for any serial s among them, classSpanAt(s) returns classId equal to w.
  5. transferableBalanceOf(M, B) and transferableBalanceOf(M, A) are both 0, and M transferring 1 unit to B reverts. balanceOf is 70, 50 and 30 for A, B and M respectively.
  6. When the contract also implements the freezing extension, freezeSlice(0, 100, r) is called. transferableBalanceOf(A, M) becomes 0. classesOf(A) still has only one entry, 70 under w: freezing does not change classes.

The following sequence starts from a token with no units issued and checks forced transfer and destruction; it applies to contracts that implement the freezing extension and provide both operations. A, B and T are distinct non-zero addresses, and r is a reason for which freezeReasonInfo(r) returns a non-empty name. freezeSlice, unfreezeSlice, forcedTransferSlice and destroySlice are called by a caller the implementation permits; the address that calls forcedTransferSlice and destroySlice is called E below.

  1. Mint 100 units to A, with the serials 0 through 99. A calls transferSlices(B, [20], [30]), after which B holds the serials 20 through 49.
  2. On an implementation that requires freezing first, forcedTransferSlice(B, T, 20, 10) reverts at this point, because these serials are not frozen. On an implementation that does not require it, this step is skipped and the call is not made. The remaining steps all freeze before they dispose of units, so they hold whether or not the implementation requires it.
  3. freezeSlice(20, 30, r) is called.
  4. forcedTransferSlice(A, T, 20, 10) reverts, because these serials are not held by A; the recommended error is ERC8527NotHeld(A, 20, 10). E calling forcedTransferSlice(B, T, 20, 10) succeeds. The SliceTransferred events the contract emits from B to T together cover exactly the serials 20 through 29, followed by Transfer(B, T, 10) and then by SliceForciblyTransferred(B, T, E, 20, 10). balanceOf(T) and frozenBalanceOf(T) are both 10, and for any to, transferableBalanceOf(T, to) is 0: in the hands of T these serials are still frozen for r. frozenSpanAt(20) returns frozen equal to true and length between 1 and 30.
  5. E calling destroySlice(B, 30, 20) succeeds. The SliceTransferred events the contract emits from B to the zero address together cover exactly the serials 30 through 49, followed by Transfer(B, 0x0, 20) and then by SliceForciblyTransferred(B, 0x0, E, 30, 20). balanceOf(B) is 0, totalSupply() is 80, and serialsIssued() is still 100.
  6. unfreezeSlice(20, 10, r) is called. For any to, transferableBalanceOf(T, to) is 10. frozenSpanAt(20) returns frozen equal to false and length between 1 and 10; frozenSpanAt(30) returns frozen equal to true and length between 1 and 20: the destroyed serials 30 through 49 are still frozen for r.

The following sequence starts from a token with no units issued and checks defragmentation; it applies to contracts that open defragmentation and allow each of the defragmentations in it (see clause 7 of “Defragmentation”). A and B are distinct non-zero addresses.

  1. Mint 100 units to A, with the serials 0 through 99. A calls transferSlices(B, [10, 30, 50], [10, 10, 10]), after which B holds the serials 10 through 19, 30 through 39 and 50 through 59.
  2. B calling defragmentSlices([10, 30, 50], [10, 10, 5]) succeeds and returns 100. The contract emits SliceConsolidated(B, B, 10, 100, 10), SliceConsolidated(B, B, 30, 110, 10) and SliceConsolidated(B, B, 50, 120, 5), in this order, and emits neither Transfer nor SliceTransferred. serialsIssued() is 125, totalSupply() is still 100, and balanceOf is still 70 and 30 for A and B. The slices sliceAt returns for B together are the serials 55 through 59 and 100 through 124, and those it returns for A together are still the serials 0 through 9, 20 through 29, 40 through 49 and 60 through 99. consolidatedSpanAt(10) returns consolidated equal to true, successor equal to 100 and length between 1 and 10; consolidatedSpanAt(15) returns consolidated equal to true, successor equal to 105 and length between 1 and 5; consolidatedSpanAt(20) returns consolidated equal to false, successor equal to zero and length between 1 and 10; consolidatedSpanAt(55) returns consolidated equal to false and length between 1 and 70; consolidatedSpanAt(125) returns (false, 0, 0).
  3. The following calls all revert. B calling transferSlices(A, [10], [1]) and defragmentSlices([10], [1]), because the serial 10 is consolidated and has no holder; the recommended error is ERC8527NotHeld(B, 10, 1). B calling defragmentSlices([120], [10]), because the serials 125 through 129 have not been issued; defragmentSlices([55], [0]), because the amount is zero; defragmentSlices([], []), because the arrays are empty; defragmentSlices([55, 100], [1]), because the two arrays differ in length; defragmentSlices([100, 55], [1, 1]), because the slices are not given in ascending order of their start; defragmentSlices([100, 105], [10, 1]), because the two slices overlap. A calling defragmentSlices([55], [1]), because the serial 55 is not held by A; the recommended error is ERC8527NotHeld(A, 55, 1).
  4. B calling defragmentSlices([55, 100], [5, 10]) succeeds and returns 125. The contract emits SliceConsolidated(B, B, 55, 125, 5) and SliceConsolidated(B, B, 100, 130, 10), in this order. serialsIssued() is 140, and the slices sliceAt returns for B together are the serials 110 through 139. consolidatedSpanAt(10) still returns successor equal to 100: one query takes one step. consolidatedSpanAt(100) returns consolidated equal to true, successor equal to 130 and length between 1 and 10; consolidatedSpanAt(130) returns consolidated equal to false and length between 1 and 10. The unit that had the serial 10 now has the serial 130.

The following steps continue from the state above and apply to contracts that also implement the freezing extension. r1 and r2 are two different reasons. freezeSlice and unfreezeSlice are called by a caller the implementation allows.

  1. freezeSlice(100, 5, r1) is called. The serials 100 through 104 are consolidated, and their successors are the serials 130 through 134. The SliceFrozen events with reason equal to r1 that the contract emits together cover the serials 100 through 104 and 130 through 134. frozenSpanAt(130) returns frozen equal to true and length between 1 and 5; frozenSpanAt(10) returns frozen equal to false: a freeze does not propagate to earlier serials. frozenBalanceOf(B) is 5.
  2. freezeSlice(10, 10, r2) is called. The SliceFrozen events with reason equal to r2 that the contract emits together cover the serials 10 through 19, 100 through 109 and 130 through 139: the freeze passed through two generations of successors. frozenBalanceOf(B) is 10, and freezeReasonsOf(B) has two entries, 5 under r1 and 10 under r2. For any to, transferableBalanceOf(B, to) is 20. B calling transferSlices(A, [135], [1]) reverts; the recommended error is ERC8527NotTransferable(B, A, 135, 1). B calling defragmentSlices([130], [10]) reverts, because these serials are frozen; the recommended error is ERC8527NotConsolidatable(B, 130, 10).
  3. unfreezeSlice(10, 10, r2) is called. The SliceUnfrozen events with reason equal to r2 that the contract emits together cover the serials 10 through 19, 100 through 109 and 130 through 139. frozenBalanceOf(B) is 5, and freezeReasonsOf(B) has only one entry, 5 under r1. frozenSpanAt(130) returns frozen equal to true and length between 1 and 5; frozenSpanAt(135) returns frozen equal to false and length between 1 and 5.
  4. unfreezeSlice(100, 5, r1) is called. frozenBalanceOf(B) is 0. B calling defragmentSlices([130], [10]) succeeds and returns 140. Afterwards balanceOf is 70 and 30 for A and B respectively, totalSupply() is 100, and serialsIssued() is 150.

The following sequence starts from a token with no units issued and checks defragmentation together with classes; it applies to contracts that implement the classification extension, open defragmentation, and allow each of the defragmentations in it. A and B are distinct non-zero addresses. w is a class for which classInfo(w) returns a non-empty name.

  1. Issue 10 units of class w to A, with the serials 0 through 9. Mint 1 unit to B, with the serial 10. Mint 10 units of the default class to A, with the serials 11 through 20. Issue another 10 units of class w to A, with the serials 21 through 30.
  2. The following calls by A all revert, because the serials named are not all of the same class: defragmentSlices([0, 11], [10, 5]), for which the recommended error is ERC8527NotConsolidatable(A, 11, 5); defragmentSlices([11, 21], [5, 5]), for which the recommended error is ERC8527NotConsolidatable(A, 21, 5); defragmentSlices([20], [2]), for which the recommended error is ERC8527NotConsolidatable(A, 20, 2).
  3. A calling defragmentSlices([0, 21], [10, 10]) succeeds and returns 31. The contract emits SliceConsolidated(A, A, 0, 31, 10) and SliceConsolidated(A, A, 21, 41, 10), in this order, and the SliceClassified events with classId equal to w that it emits after them together cover the serials 31 through 50. classSpanAt(31) returns classId equal to w and length between 1 and 20. classesOf(A) has two entries, 20 under w and 10 under zero. balanceOf(A) is still 30.
  4. A calling defragmentSlices([11], [10]) succeeds and returns 51. classSpanAt(51) returns classId equal to zero and length between 1 and 10.

On a contract that does not open defragmentation, every call to defragmentSlices reverts, and for every issued serial s, consolidatedSpanAt(s) returns consolidated equal to false, successor equal to zero and length between 1 and serialsIssued() - s.

The following sequence starts from a token with no units issued and concerns the acceptance check; it applies only to contracts that support the acceptance check, that is, whose supportsInterface(0xde7278e5) returns true. A and C are distinct non-zero addresses without code. P, R and S are three contracts. P does not implement ERC-165. R implements ERC-165 and IERC8527Acceptance: its acceptsERC8527Transfer returns 0x3fbf9755 when slices is not greater than 1 and returns 0x00000000 otherwise. S also implements these two interfaces, and its acceptsERC8527Transfer always reverts; S has in addition a function pull(from, value) that anyone can call, which calls transferFrom(from, S, value) on the token. Issuance is performed by a restricted caller and does not query receiving contracts.

  1. Mint 100 units to A, with the serials 0 through 99. A calling transferSlices(P, [0], [1]) succeeds: P does not implement the acceptance interface. A calling transferSlices(C, [1], [1]) succeeds: C has no code.
  2. A calling transferSlices(R, [10], [1]) succeeds: a single serial can be reported by only one SliceTransferred event, so slices is 1. A calling transferSlices(R, [20, 22], [1, 1]) reverts: two non-adjacent serials need at least two SliceTransferred events to report them, so slices is at least 2; an implementation that follows the recommended error reverts with ERC8527TransferRefused(A, R, 2). A transferring 0 units to R succeeds, and the contract emits Transfer(A, R, 0): a zero-value incoming transfer is exempt. transferableBalanceOf(A, R) is 97, equal to balanceOf(A): it does not reflect refusal.
  3. A calling transferSlices(S, [30], [1]) reverts. A calls approve(S, 5) and approve(C, 5), and both calls succeed. C calling transferFrom(A, S, 5) reverts. Anyone calling pull(A, 5) on S succeeds: an incoming transfer that the recipient pulls itself is exempt. Afterwards balanceOf is 92, 1, 1, 1 and 5 for A, P, C, R and S respectively, totalSupply() is 100, and allowance(A, C) is still 5.

On a contract that does not support the acceptance check, supportsInterface(0xde7278e5) returns false. Under the assumptions of this section, none of the calls above that transfer units to R or S is refused on such a contract.