Test Cases
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
Transferevent; 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
operatorof everySliceFrozen,SliceUnfrozenandSliceForciblyTransferredevent 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.
- Mint 100 units to
A. One or moreSliceTransferredevents from the zero address toAare emitted, together covering the serials 0 through 99, followed byTransfer(0x0, A, 100).serialsIssued()is 100. Atransfers 30 units toB. One or moreSliceTransferred(A, B, start, amount)events are emitted, with slices inside(0, 100)and amounts summing to 30, followed byTransfer(A, B, 30). The call returnstrue.Atransferring 1 unit to the zero address reverts.Btransfers 10 units toC. The slices reported lie inside the slicesBreceived in step 2.- Mint 50 units to
C. TheSliceTransferredevents emitted cover the serials 100 through 149.serialsIssued()is 150, andbalanceOfis 70, 20 and 60 forA,BandCrespectively.
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.
- Before the call,
canFreeze(r1, F)returnstrue; for any addressXfor whichcanFreeze(r1, X)returnsfalse,XcallingfreezeSlice(0, 100, r1)reverts.FcallsfreezeSlice(0, 100, r1). TheSliceFrozenevents the contract emits withreasonequal tor1together cover the serials 0 through 99, and theoperatorof each isF.balanceOfis unchanged. For anyto,transferableBalanceOfreturns 0 forAandBand 50 forC.frozenBalanceOfis 70, 20 and 10 forA,BandCrespectively.frozenSpanAt(0)returnsfrozenequal totrueandlengthbetween 1 and 100;frozenSpanAt(100)returnsfrozenequal tofalseandlengthbetween 1 and 50;frozenSpanAt(150)returns(false, 0).freezeReasonsOf(C)has only one entry, 10 underr1.Acallingapprove(D, 5)succeeds andallowance(A, D)is 5;DcallingtransferFrom(A, D, 5)reverts, becauseAhas no transferable units. Btransferring 1 unit reverts.Ctransferring 51 units toDreverts; an implementation that follows the recommended error reverts withERC8527InsufficientTransferableBalance(C, D, 51, 50).Ctransferring 50 units toDsucceeds, and every slice reported lies inside(100, 50).freezeSlice(0, 150, r2)is called. The serials 0 through 99 are now frozen for bothr1andr2, and the serials 100 through 149 are frozen forr2only.frozenBalanceOf(A)is still 70, whilefreezeReasonsOf(A)has two entries, 70 underr1and 70 underr2.frozenBalanceOf(D)is 50, andfreezeReasonsOf(D)has only one entry, 50 underr2.- Before the call,
canUnfreeze(r1, U)returnstrue; for any addressXfor whichcanUnfreeze(r1, X)returnsfalse,XcallingunfreezeSlice(0, 100, r1)reverts.UcallsunfreezeSlice(0, 100, r1), and theoperatorof eachSliceUnfrozenevent the contract emits for it isU. The serials 0 through 99 are still frozen forr2:frozenSpanAt(0)still returnsfrozenequal totrue; for anyto,transferableBalanceOfreturns 0 for each ofA,B,CandD; andBtransferring 1 unit still reverts.freezeReasonsOf(A)has only one entry, 70 underr2. unfreezeSlice(0, 100, r2)is called. The serials 0 through 99 are no longer frozen, and the serials 100 through 149 are still frozen forr2:frozenSpanAt(0)returnsfrozenequal tofalseandlengthbetween 1 and 100;frozenSpanAt(100)returnsfrozenequal totrueandlengthbetween 1 and 50.frozenBalanceOf(A)is 0. For anyto,transferableBalanceOfreturns 70, 20 and 10 forA,BandCrespectively, and 0 forD.Btransferring 1 unit toDsucceeds.DcallingtransferSlices(A, [100], [1])reverts, because serial 100 is frozen; an implementation that follows the recommended error reverts withERC8527NotTransferable(D, A, 100, 1). On a contract that providesburnSlices,DcallingburnSlices([100], [1])reverts as well, and the recommended error isERC8527NotTransferable(D, 0x0, 100, 1).unfreezeSlice(100, 50, r2)is called.frozenBalanceOfis 0 for each ofA,B,CandD, andfreezeReasonsOf(D)returns two empty arrays. AfterwardsbalanceOfis 70, 19, 10 and 51 forA,B,CandDrespectively, andtotalSupply()andserialsIssued()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.
- Mint 100 units to
A, with the serials 0 through 99. AcallingtransferSlices(B, [40], [20])succeeds and returnstrue. TheSliceTransferredevents the contract emits fromAtoBtogether cover exactly the serials 40 through 59, followed byTransfer(A, B, 20).balanceOfis 80 and 20 forAandBrespectively. The slicessliceAtreturns forBtogether are the serials 40 through 59, and the slices it returns forAtogether are the serials 0 through 39 and 60 through 99.AcallingtransferSlices(B, [0, 70], [10, 5])succeeds. TheSliceTransferredevents the contract emits fromAtoBtogether cover exactly the serials 0 through 9 and 70 through 74, followed by only oneTransfer(A, B, 15).balanceOfis 65 and 35 forAandBrespectively.- Each of the following calls made by
Areverts:transferSlices(B, [30], [20]), because the serials 40 through 49 are not held byA;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]), becausetois the zero address. An implementation that follows the recommended errors reverts withERC8527NotHeld(A, 30, 20)andERC8527NotHeld(A, 90, 20)for the first two calls respectively. BcallingtransferSlices(B, [40], [20])succeeds. The contract emitsTransfer(B, B, 20)and noSliceTransferredevent, and no serial changes holder.BcallingtransferSlices(B, [40, 45], [10, 5])reverts, because the two slices overlap; this rule applies equally whentois the caller.BcallingtransferSlices(A, [45], [5])succeeds. AfterwardsAholds the serials 10 through 39, 45 through 49, 60 through 69 and 75 through 99,Bholds the serials 0 through 9, 40 through 44, 50 through 59 and 70 through 74, andbalanceOfis 70 and 30 forAandBrespectively.- This step applies only to contracts that provide
burnSlices.AcallingburnSlices([0], [5])reverts, because the serials 0 through 4 are not held byA; the recommended error isERC8527NotHeld(A, 0, 5).AcallingburnSlices([10, 60], [5, 10])succeeds. TheSliceTransferredevents the contract emits fromAto the zero address together cover exactly the serials 10 through 14 and 60 through 69, followed by only oneTransfer(A, 0x0, 15).balanceOf(A)is 55,totalSupply()is 85, andserialsIssued()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.
- Issue 100 units of class
wtoA. TheSliceClassifiedevents the contract emits together cover the serials 0 through 99 and haveclassIdequal tow; all of them are emitted after theTransferevent that issues these serials. - Issue 50 units of the default class to
A, with the serials 100 through 149.balanceOf(A)is 150,transferableBalanceOf(A, B)is 50, andtransferableBalanceOf(A, M)is 150.classesOf(A)has two entries, 100 underwand 50 under zero.classSpanAt(0)returnsclassIdequal towandlengthbetween 1 and 100;classSpanAt(100)returnsclassIdequal to zero andlengthbetween 1 and 50;classSpanAt(150)returns zero for bothclassIdandlength. Atransferring 51 units toBreverts; an implementation that follows the recommended error reverts withERC8527InsufficientTransferableBalance(A, B, 51, 50).Atransferring 50 units toBsucceeds, and every slice reported lies inside(100, 50).Atransferring 30 units toMsucceeds, and every slice reported lies inside(0, 100). The class of these serials is stillw: for any serialsamong them,classSpanAt(s)returnsclassIdequal tow.transferableBalanceOf(M, B)andtransferableBalanceOf(M, A)are both 0, andMtransferring 1 unit toBreverts.balanceOfis 70, 50 and 30 forA,BandMrespectively.- 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 underw: 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.
- Mint 100 units to
A, with the serials 0 through 99.AcallstransferSlices(B, [20], [30]), after whichBholds the serials 20 through 49. - 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. freezeSlice(20, 30, r)is called.forcedTransferSlice(A, T, 20, 10)reverts, because these serials are not held byA; the recommended error isERC8527NotHeld(A, 20, 10).EcallingforcedTransferSlice(B, T, 20, 10)succeeds. TheSliceTransferredevents the contract emits fromBtoTtogether cover exactly the serials 20 through 29, followed byTransfer(B, T, 10)and then bySliceForciblyTransferred(B, T, E, 20, 10).balanceOf(T)andfrozenBalanceOf(T)are both 10, and for anyto,transferableBalanceOf(T, to)is 0: in the hands ofTthese serials are still frozen forr.frozenSpanAt(20)returnsfrozenequal totrueandlengthbetween 1 and 30.EcallingdestroySlice(B, 30, 20)succeeds. TheSliceTransferredevents the contract emits fromBto the zero address together cover exactly the serials 30 through 49, followed byTransfer(B, 0x0, 20)and then bySliceForciblyTransferred(B, 0x0, E, 30, 20).balanceOf(B)is 0,totalSupply()is 80, andserialsIssued()is still 100.unfreezeSlice(20, 10, r)is called. For anyto,transferableBalanceOf(T, to)is 10.frozenSpanAt(20)returnsfrozenequal tofalseandlengthbetween 1 and 10;frozenSpanAt(30)returnsfrozenequal totrueandlengthbetween 1 and 20: the destroyed serials 30 through 49 are still frozen forr.
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.
- Mint 100 units to
A, with the serials 0 through 99.AcallstransferSlices(B, [10, 30, 50], [10, 10, 10]), after whichBholds the serials 10 through 19, 30 through 39 and 50 through 59. BcallingdefragmentSlices([10, 30, 50], [10, 10, 5])succeeds and returns 100. The contract emitsSliceConsolidated(B, B, 10, 100, 10),SliceConsolidated(B, B, 30, 110, 10)andSliceConsolidated(B, B, 50, 120, 5), in this order, and emits neitherTransfernorSliceTransferred.serialsIssued()is 125,totalSupply()is still 100, andbalanceOfis still 70 and 30 forAandB. The slicessliceAtreturns forBtogether are the serials 55 through 59 and 100 through 124, and those it returns forAtogether are still the serials 0 through 9, 20 through 29, 40 through 49 and 60 through 99.consolidatedSpanAt(10)returnsconsolidatedequal totrue,successorequal to 100 andlengthbetween 1 and 10;consolidatedSpanAt(15)returnsconsolidatedequal totrue,successorequal to 105 andlengthbetween 1 and 5;consolidatedSpanAt(20)returnsconsolidatedequal tofalse,successorequal to zero andlengthbetween 1 and 10;consolidatedSpanAt(55)returnsconsolidatedequal tofalseandlengthbetween 1 and 70;consolidatedSpanAt(125)returns(false, 0, 0).- The following calls all revert.
BcallingtransferSlices(A, [10], [1])anddefragmentSlices([10], [1]), because the serial 10 is consolidated and has no holder; the recommended error isERC8527NotHeld(B, 10, 1).BcallingdefragmentSlices([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.AcallingdefragmentSlices([55], [1]), because the serial 55 is not held byA; the recommended error isERC8527NotHeld(A, 55, 1). BcallingdefragmentSlices([55, 100], [5, 10])succeeds and returns 125. The contract emitsSliceConsolidated(B, B, 55, 125, 5)andSliceConsolidated(B, B, 100, 130, 10), in this order.serialsIssued()is 140, and the slicessliceAtreturns forBtogether are the serials 110 through 139.consolidatedSpanAt(10)still returnssuccessorequal to 100: one query takes one step.consolidatedSpanAt(100)returnsconsolidatedequal totrue,successorequal to 130 andlengthbetween 1 and 10;consolidatedSpanAt(130)returnsconsolidatedequal tofalseandlengthbetween 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.
freezeSlice(100, 5, r1)is called. The serials 100 through 104 are consolidated, and their successors are the serials 130 through 134. TheSliceFrozenevents withreasonequal tor1that the contract emits together cover the serials 100 through 104 and 130 through 134.frozenSpanAt(130)returnsfrozenequal totrueandlengthbetween 1 and 5;frozenSpanAt(10)returnsfrozenequal tofalse: a freeze does not propagate to earlier serials.frozenBalanceOf(B)is 5.freezeSlice(10, 10, r2)is called. TheSliceFrozenevents withreasonequal tor2that 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, andfreezeReasonsOf(B)has two entries, 5 underr1and 10 underr2. For anyto,transferableBalanceOf(B, to)is 20.BcallingtransferSlices(A, [135], [1])reverts; the recommended error isERC8527NotTransferable(B, A, 135, 1).BcallingdefragmentSlices([130], [10])reverts, because these serials are frozen; the recommended error isERC8527NotConsolidatable(B, 130, 10).unfreezeSlice(10, 10, r2)is called. TheSliceUnfrozenevents withreasonequal tor2that the contract emits together cover the serials 10 through 19, 100 through 109 and 130 through 139.frozenBalanceOf(B)is 5, andfreezeReasonsOf(B)has only one entry, 5 underr1.frozenSpanAt(130)returnsfrozenequal totrueandlengthbetween 1 and 5;frozenSpanAt(135)returnsfrozenequal tofalseandlengthbetween 1 and 5.unfreezeSlice(100, 5, r1)is called.frozenBalanceOf(B)is 0.BcallingdefragmentSlices([130], [10])succeeds and returns 140. AfterwardsbalanceOfis 70 and 30 forAandBrespectively,totalSupply()is 100, andserialsIssued()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.
- Issue 10 units of class
wtoA, with the serials 0 through 9. Mint 1 unit toB, with the serial 10. Mint 10 units of the default class toA, with the serials 11 through 20. Issue another 10 units of classwtoA, with the serials 21 through 30. - The following calls by
Aall revert, because the serials named are not all of the same class:defragmentSlices([0, 11], [10, 5]), for which the recommended error isERC8527NotConsolidatable(A, 11, 5);defragmentSlices([11, 21], [5, 5]), for which the recommended error isERC8527NotConsolidatable(A, 21, 5);defragmentSlices([20], [2]), for which the recommended error isERC8527NotConsolidatable(A, 20, 2). AcallingdefragmentSlices([0, 21], [10, 10])succeeds and returns 31. The contract emitsSliceConsolidated(A, A, 0, 31, 10)andSliceConsolidated(A, A, 21, 41, 10), in this order, and theSliceClassifiedevents withclassIdequal towthat it emits after them together cover the serials 31 through 50.classSpanAt(31)returnsclassIdequal towandlengthbetween 1 and 20.classesOf(A)has two entries, 20 underwand 10 under zero.balanceOf(A)is still 30.AcallingdefragmentSlices([11], [10])succeeds and returns 51.classSpanAt(51)returnsclassIdequal to zero andlengthbetween 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.
- Mint 100 units to
A, with the serials 0 through 99.AcallingtransferSlices(P, [0], [1])succeeds:Pdoes not implement the acceptance interface.AcallingtransferSlices(C, [1], [1])succeeds:Chas no code. AcallingtransferSlices(R, [10], [1])succeeds: a single serial can be reported by only oneSliceTransferredevent, soslicesis 1.AcallingtransferSlices(R, [20, 22], [1, 1])reverts: two non-adjacent serials need at least twoSliceTransferredevents to report them, soslicesis at least 2; an implementation that follows the recommended error reverts withERC8527TransferRefused(A, R, 2).Atransferring 0 units toRsucceeds, and the contract emitsTransfer(A, R, 0): a zero-value incoming transfer is exempt.transferableBalanceOf(A, R)is 97, equal tobalanceOf(A): it does not reflect refusal.AcallingtransferSlices(S, [30], [1])reverts.Acallsapprove(S, 5)andapprove(C, 5), and both calls succeed.CcallingtransferFrom(A, S, 5)reverts. Anyone callingpull(A, 5)onSsucceeds: an incoming transfer that the recipient pulls itself is exempt. AfterwardsbalanceOfis 92, 1, 1, 1 and 5 forA,P,C,RandSrespectively,totalSupply()is 100, andallowance(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.