ERC-1132 ERC20 token locking extension (Extending ERC20 with token locking capability)
Stagnant · Tokens · created 2018-06-03
An extension standard that lets ERC20 tokens be locked for a set period within the token contract itself, without moving them to an external escrow, while still allowing the locked tokens to be managed and utilized.
Why it is needed
To lock tokens using the staking (#900) approach, the tokens must be moved to an escrow or stake manager contract. In this case, the user must additionally trust that contract, an approve step and gas cost are needed for the transfer, and the user must claim the tokens directly once the lock period ends. In addition, balanceOf() queries do not show the moved tokens, making it hard for the user to see their actual holdings or activity, and once locked, tokens are tied up for that single purpose and cannot be used for other activities at the same time. At Nexus Mutual, claims assessors needed to be able to vote multiple times during the period after locking their tokens, which was difficult to achieve with the staking approach.
What to implement
function lock(bytes32 _reason, uint256 _amount, uint256 _time) public returns (bool): Locks _amount tokens for _time seconds under the specified reason (_reason).tokensLocked(address _of, bytes32 _reason) view returns (uint256 amount): Queries the amount of tokens a given address has locked for a given reason.function tokensLockedAtTime(address _of, bytes32 _reason, uint256 _time) public view returns (uint256 amount): Queries the amount of tokens that will be locked for a given address and reason at a specific future time.function totalBalanceOf(address _of) view returns (uint256 amount): Queries the total holdings, combining locked and transferable tokens.function extendLock(bytes32 _reason, uint256 _time) public returns (bool): Extends the lock period for tokens locked under a specific reason.function increaseLockAmount(bytes32 _reason, uint256 _amount) public returns (bool): Increases the amount of tokens locked under a specific reason.function tokensUnlockable(address _of, bytes32 _reason) public view returns (uint256 amount): Queries how many of the tokens an address has locked under a specific reason are now unlocked and retrievable.function getUnlockableTokens(address _of) public view returns (uint256 unlockableTokens): Queries the total amount of tokens an address can retrieve (that are unlocked).function unlock(address _of) public returns (uint256 unlockableTokens): Actually unlocks a given address's unlocked tokens, returning them to a transferable state.event Locked(address indexed _of, uint256 indexed _reason, uint256 _amount, uint256 _validity): An event recorded when tokens are locked.event Unlocked(address indexed _of, uint256 indexed _reason, uint256 _amount): An event recorded when a token lock is released.
In depth
lock() and per-reason locking (_reason)
lock(bytes32 _reason, uint256 _amount, uint256 _time) is a function that locks _amount tokens for _time seconds under the specified reason (_reason). Because the reason serves as a discriminator, the same address can simultaneously lock multiple separate amounts for different purposes, such as vesting and governance participation. The tokens are not moved to an external contract; they are simply marked as locked within the same token contract and the same account.
Querying lock status: tokensLocked / tokensLockedAtTime / totalBalanceOf
tokensLocked(_of, _reason) returns the amount of tokens a given address currently has locked under a given reason. tokensLockedAtTime(_of, _reason, _time) performs the same query as of a specific future time (_time). totalBalanceOf(_of) returns the total holdings, locked plus transferable tokens combined, revealing actual holdings that balanceOf() alone does not show.
Changing lock conditions: extendLock / increaseLockAmount
extendLock(_reason, _time) extends the lock period of tokens already locked under a given reason by _time. increaseLockAmount(_reason, _amount) increases the amount of tokens locked under the same reason by _amount. Both functions modify an existing lock rather than requiring a fresh call to lock().
Querying unlockable tokens and performing the actual unlock
tokensUnlockable(_of, _reason) returns the amount of tokens a given address has locked under a given reason that have passed their lock period and are now unlockable. getUnlockableTokens(_of) returns the total amount of tokens that address can unlock across all reasons. unlock(_of) actually releases the unlockable tokens from lock, returning them to a transferable state, and returns the amount released (unlockableTokens).
The Locked / Unlocked events
Locked(address indexed _of, uint256 indexed _reason, uint256 _amount, uint256 _validity) is recorded when tokens are locked, and Unlocked(address indexed _of, uint256 indexed _reason, uint256 _amount) is recorded when a lock is released. Like ERC20's Transfer() event, these let lock and unlock history be tracked. Note that in the source, _reason is bytes32 on the function side but is written as uint256 in the event declaration, an inconsistency present in the source itself.
Advantages claimed over staking (#900)
The source lists six problems with the staking approach: the need to additionally trust an escrow or stake manager, a transfer approval step, increased gas cost from the transfer, a user experience that requires claiming tokens directly upon release, holdings not being reflected in balanceOf() and so untrackable, and locked tokens being unusable for other activity in the ecosystem at the same time. This standard aims to solve these problems by locking and managing tokens within the same contract without moving them.
Proposal status: Stagnant
This proposal is in Stagnant status, with no further discussion since it was written in 2018. Since it has not been finalized as an official standard, anyone actually wanting to adopt it would need to separately review the reference implementations linked in the source (lockable-token, GovBlocks implementation).