What a Wallet Needs to Support This Proposal
What a Wallet Needs to Support This Proposal
This file accompanies the draft ERC “Serial-Numbered ERC-20 Tokens”. Its content was the section “What a wallet needs to support this proposal” of the draft. Section names in quotation marks refer to sections of the draft.
A wallet that does not know this proposal can use a token that conforms to this proposal as an ERC-20 token as usual. Its shortcoming is that the balance it displays can include units that cannot be sent, which the user discovers only when a send fails. On failure, a contract that follows the recommendations reverts with the errors this proposal defines (see clauses 3 and 5 of “Transferable balance” and clause 3 of “Acceptance check”). A wallet that can decode these errors can tell the user how much can be transferred to this payee address, which slice of serials cannot be sent, or that the incoming transfer was refused by the payee (ERC8527TransferRefused); but the errors do not state the cause, for example which reason the units are frozen for, or whether they are restricted by the flow rule of a class instead, or why the payee refused. To support this proposal fully, a wallet needs to do the following. All of the queries involved can be made with read-only calls and need no indexer.
- Detection. Query the identifier of the core interface with ERC-165, then the identifiers of the freezing extension and the classification extension, and the identifier of the acceptance check.
- Displaying the balance.
balanceOfis the total. When the token implements the freezing extension, callfrozenBalanceOf(account)to get the amount of it that is frozen, and callfreezeReasonsOf(account)andfreezeReasonInfoto explain why it is frozen. The amounts under the individual reasons cannot be added up, because the same unit can be frozen for several reasons. When the token implements the classification extension, also callclassesOf(account)to get the breakdown of the balance by class, and callclassInfofor every non-zero class and display the name it returns. The description documents of reasons and classes are only a supplement: a wallet can use them to display longer descriptions, images and properties, but displaying the balance does not depend on them. When thepropertiesof a reason’s description document containauthority,release,disposal,contestandreference, a wallet should display them in the details of a freeze: they tell the holder who can freeze and release, and how to contest a freeze. A wallet can also display which address initiated a freeze, which means reading theoperatorof theSliceFrozenevent and requires event logs; a further call tocanFreeze(reason, operator)tells the user whether that address can now freeze for the reason. Whenauthorityin the description document gives the address of the party that unfreezes, a wallet can check it withcanUnfreeze. The logs also show which address initiated a forced transfer or a destruction, namely theoperatorof theSliceForciblyTransferredevent. - Sending. After the user enters the payee address, call
transferableBalanceOf(from, to), use it as the upper limit of the amount that can be sent, and validate the amount against it. To explain why some part cannot be sent: on a token that implements the freezing extension, the frozen amount comes from the previous step; on a token that implements the classification extension, callclassAllowsfor every class in the breakdown to find the classes that are not allowed to be transferred to this address. The two parts can overlap: a frozen unit can also belong to a class that is not allowed, so their amounts cannot be added up. A wallet can display them separately and note that the two can overlap. To know how many units the two block together, the wallet has to callfrozenSpanAtandclassSpanAtfor each slice of the holding, find the serials that are frozen or whose class is not allowed, and take their union.balanceOf(from)minustransferableBalanceOf(from, to), minus the size of this union, leaves a difference that comes from restrictions the implementation defines itself and attaches to serials; the wallet can only report its amount. These queries have to be made against the state of the same block. The payee might also refuse the incoming transfer (see “Acceptance check”): when the token supports the acceptance check and the payee address has code, the wallet should simulate the transfer before sending; if it is refused, the wallet can try again withtransferSlices, naming longer slices. Other restrictions that the implementation itself provides (for example the account’s receive limit suggested in “The acceptance check and failed payments”) can likewise be discovered by the wallet only through simulation. - Estimating gas. The cost of a transfer depends on the number of slices it moves, so a wallet should estimate each transaction individually and not use a fixed value.
- Optionally, display the slices of the holding, using
sliceCountandsliceAt. When the token implements the freezing extension, callfrozenSpanAtfor each slice of the holding to mark the frozen parts of it. When the token implements the classification extension, callclassSpanAtfor each slice of the holding to get the class of each part. A span can extend beyond the boundary of the holding’s slice, and the wallet has to truncate it to that slice. These calls have to be read at the same block. - Optionally, let the user choose which units to send. Use the result of the previous step to exclude in advance the frozen parts and the parts whose class is not allowed to be transferred to the payee address (determined with
classAllows). Sort the slices the user selected in ascending order of start and send them with a singletransferSlicescall. If any one of the serials is frozen, or its class is not allowed to be transferred to the payee address, the whole call reverts. - Optionally, offer defragmentation. When
sliceCountreturns a large number of slices, the wallet can suggest that the user defragment: use the result of step 5 to exclude the frozen parts, sort the slices of one class in ascending order of their start, and calldefragmentSlices; when there are very many slices, use several transactions. A token can leave defragmentation closed, or set further conditions for it, for example a minimum number of slices or a limit on the consumption of serials (see “Finite serial space”), in which case the call reverts; the draft defines no function for querying these conditions, so the wallet should simulate the call first. After a defragmentation the serials of these units have changed, and slices the wallet read earlier have to be read again. To translate an old serial into the serial the unit has now, callconsolidatedSpanAtrepeatedly until it reports that the serial is not consolidated.