Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article examines ERC5058, a proposal for adding a locking mechanism to NFTs.
This article translates, summarizes, and expands on the proposal below.
I have also covered a range of other EIPs.
Overview
ERC5058 proposes a locking mechanism as an extension to the ERC721 NFT standard. An NFT owner grants locking permission to a designated operator, who can then lock the NFT. A locked NFT cannot be transferred until the configured lock period expires.
This feature allows NFTs to interact with other contracts while remaining in their owners' wallets, without being deposited into a separate contract.
Motivation
The proposal adds a native locking mechanism to ERC721, the NFT standard. With ERC5058, an NFT can be made temporarily non-transferable while remaining in the owner's wallet.
Adding a locking mechanism to NFTs
ERC5058 allows an NFT owner to delegate locking authority to a third party (an operator). The mechanism works as follows.
- The owner uses
setLockApprovalForAll()orlockApprove()to authorize an operator to lock NFTs. - The authorized operator calls
lock()to lock an NFT. - The locked NFT cannot be transferred until the predefined period expires.
Even during the lock period, however, the NFT remains in the owner's wallet.
Why a locking mechanism is needed
The NFT market has grown rapidly, with many well-known collections commonly referred to as blue-chip NFTs. One major challenge for NFTs, however, is their limited liquidity.
Projects that enable borrowing against NFTs, such as NFTFi and BendDAO, have existed for some time, but have generally required transferring ownership of the NFT to a contract. This approach carries several risks.
-
Smart contract risk
A vulnerability or bug in the contract may result in the loss of the NFT. -
Loss of utility
If the NFT no longer appears in the owner's wallet, uses such as displaying it as a profile picture may be lost. -
Inability to receive airdrops
Token distributions (airdrops) intended for NFT owners may no longer reach the original owner correctly.
These issues degrade the user experience and discourage participation in NFT-based financial services (NFTFi).
Improvements enabled by locking
The locking mechanism proposed by ERC5058 allows the following without transferring ownership of an NFT.
- The NFT remains in the wallet, eliminating the need to deposit it into a contract.
- Transfers are prohibited while the NFT is locked, but other functionality, such as displaying the NFT and using its metadata, remains available.
- Owners can continue receiving airdrops themselves.
Locking an NFT in this way allows its value as a collectible and its utility in other projects to coexist.
Potential use cases and benefits
The locking mechanism provides a foundation for a range of use cases, particularly in NFTFi. Applications include the following.
- Staking
- Lending
- Participation in crowdfunding
Managing an NFT's locked state without moving it out of the wallet allows owners to use NFTs as financial assets with greater confidence. The mechanism is expected to encourage NFTFi participation and support the development of the broader NFT ecosystem.
Specification
Interface
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.8;
/**
* @dev EIP-721 Non-Fungible Token Standard, optional lockable extension
* ERC721 Token that can be locked for a certain period and cannot be transferred.
* This is designed for a non-escrow staking contract that comes later to lock a user's NFT
* while still letting them keep it in their wallet.
* This extension can ensure the security of user tokens during the staking period.
* If the nft lending protocol is compatible with this extension, the trouble caused by the NFT
* airdrop can be avoided, because the airdrop is still in the user's wallet
*/
interface IERC5058 {
/**
* @dev Emitted when `tokenId` token is locked by `operator` from `from`.
*/
event Locked(address indexed operator, address indexed from, uint256 indexed tokenId, uint256 expired);
/**
* @dev Emitted when `tokenId` token is unlocked by `operator` from `from`.
*/
event Unlocked(address indexed operator, address indexed from, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to lock the `tokenId` token.
*/
event LockApproval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to lock all of its tokens.
*/
event LockApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the locker who is locking the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function lockerOf(uint256 tokenId) external view returns (address locker);
/**
* @dev Lock `tokenId` token until the block number is greater than `expired` to be unlocked.
*
* Requirements:
*
* - `tokenId` token must be owned by `owner`.
* - `expired` must be greater than block.number
* - If the caller is not `owner`, it must be approved to lock this token
* by either {lockApprove} or {setLockApprovalForAll}.
*
* Emits a {Locked} event.
*/
function lock(uint256 tokenId, uint256 expired) external;
/**
* @dev Unlock `tokenId` token.
*
* Requirements:
*
* - `tokenId` token must be owned by `owner`.
* - the caller must be the operator who locks the token by {lock}
*
* Emits a {Unlocked} event.
*/
function unlock(uint256 tokenId) external;
/**
* @dev Gives permission to `to` to lock `tokenId` token.
*
* Requirements:
*
* - The caller must own the token or be an approved lock operator.
* - `tokenId` must exist.
*
* Emits an {LockApproval} event.
*/
function lockApprove(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an lock operator for the caller.
* Operators can call {lock} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the caller.
*
* Emits an {LockApprovalForAll} event.
*/
function setLockApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns the account lock approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getLockApproved(uint256 tokenId) external view returns (address operator);
/**
* @dev Returns if the `operator` is allowed to lock all of the assets of `owner`.
*
* See {setLockApprovalForAll}
*/
function isLockApprovedForAll(address owner, address operator) external view returns (bool);
/**
* @dev Returns if the `tokenId` token is locked.
*/
function isLocked(uint256 tokenId) external view returns (bool);
/**
* @dev Returns the `tokenId` token lock expired time.
*/
function lockExpiredTime(uint256 tokenId) external view returns (uint256);
}ERC5058 extends ERC721 with a locking mechanism that temporarily prevents transfers.
Implementing the proposed IERC5058 interface allows external operations to be restricted while the NFT remains in the user's wallet.
The design targets use cases such as non-escrow staking and lending.
Users can participate in other projects by locking their NFTs in their own wallets while retaining the ability to receive airdrops and display their NFTs.
Events
IERC5058 defines the following events for locking, unlocking, and approval operations.
Locked
Emitted when an NFT is locked. Records the locking operator, owner, token ID, and block number at which the lock expires.
Unlocked
Emitted when an NFT is unlocked.
LockApproval
Emitted when locking approval is granted for a particular NFT.
LockApprovalForAll
Emitted when an owner grants or revokes an operator's locking authority over all of the owner's tokens.
Functions
lock
Locks an NFT. The lock expires at a specified block number and prevents the token from being transferred until then. The following conditions must be met.
- The caller must be the NFT owner or an operator previously authorized through
lockApproveorsetLockApprovalForAll. - The block number specified in
expiredmust be greater than the currentblock.number.
unlock
Unlocks an NFT. The operator who performed the locking operation must perform the unlock. Even the owner cannot unlock the NFT unless they are the operator who locked it.
lockApprove
Grants a specified address locking authority over a particular NFT. Only the NFT owner or an authorized lock operator can execute this function.
setLockApprovalForAll
Grants or revokes an address's authority to lock all NFTs owned by the caller in a single operation.
It is the locking counterpart to setApprovalForAll.
lockerOf
Returns the address of the operator currently locking the specified NFT. Returns the zero address if the NFT is not locked.
getLockApproved
Returns the address authorized to lock a particular NFT.
isLockApprovedForAll
Returns whether the specified operator can lock all NFTs belonging to the owner.
isLocked
Checks whether an NFT is currently locked.
Returns true if the NFT is locked.
lockExpiredTime
Returns the expiration block number of the lock if the NFT is locked.
Security and use cases
A key feature of ERC5058 is the ability to temporarily restrict NFT transfers through locking while preserving self-custody. NFTs used for staking or lending therefore do not need to be deposited into another contract, preserving access to airdrops and evidence of ownership in the wallet.
Restricting unlocking authority to the operator who applied the lock also helps prevent unauthorized unlocking and protects the NFT.
Additional considerations
Granting locking authority
NFT owners can delegate the authority to lock their NFTs to trusted external contracts or accounts.
Two functions are provided for this purpose: lockApprove() and setLockApprovalForAll().
lockApprove()grants locking authority over an individual NFT.setLockApprovalForAll()grants locking authority over all NFTs owned by the caller within the token contract.
In NFTFi projects, these functions are used to obtain locking authority in advance, after which the project calls lock() to lock the NFT.
The locked NFT remains in the owner's wallet, with only transfers prohibited.
Controlling locks and unlocks
A locked NFT cannot be transferred until the specified block number is reached.
The operator who applied the lock (typically the project's contract) can call unlock() to release it early, even during the lock period.
Only the address (operator) that performed the locking operation can unlock the NFT.
The owner and other accounts cannot release it unless they are that operator.
Lock duration
Locking an NFT requires specifying an expiration block number (expired).
This block number must be greater than the current block.number.
When the lock period ends, the transfer restriction expires automatically and the NFT becomes transferable again.
The NFT returns to its original transferable state without an explicit unlock transaction.
The Bound NFT extension concept
One proposed application of the ERC5058 locking mechanism is the Bound NFT concept. A Bound NFT is a temporary NFT issued only while the original NFT is locked.
- It has the same metadata as the original NFT and can also be transferred.
- It is automatically invalidated or destroyed when the lock period ends and the original NFT is unlocked, as envisioned by the extension.
A Bound NFT can serve as a rental NFT or as a staking credential in another contract. For example, a user could lock their NFT, receive a Bound NFT in return, and use it to interact with another project.
The role of the Bound NFT Factory
The Bound NFT Factory is a factory contract for creating a Bound NFT contract corresponding to an original NFT contract.
Similar to Uniswap's token pair creation mechanism, it uses create2 so that the address of the NFT-associated contract can be determined in advance.
A Bound NFT contract created by this factory can only be controlled by the corresponding original NFT contract. This protects Bound NFT creation and management against unauthorized operations.
Backwards compatibility
ERC5058 is compatible with ERC721.
Reference implementation
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.0;
import "./IERC5058.sol";
/**
* @dev Implementation ERC721 Lockable Token
*/
abstract contract ERC5058 is ERC721, IERC5058 {
// Mapping from token ID to unlock time
mapping(uint256 => uint256) public lockedTokens;
// Mapping from token ID to lock approved address
mapping(uint256 => address) private _lockApprovals;
// Mapping from owner to lock operator approvals
mapping(address => mapping(address => bool)) private _lockOperatorApprovals;
/**
* @dev See {IERC5058-lockApprove}.
*/
function lockApprove(address to, uint256 tokenId) public virtual override {
require(!isLocked(tokenId), "ERC5058: token is locked");
address owner = ERC721.ownerOf(tokenId);
require(to != owner, "ERC5058: lock approval to current owner");
require(
_msgSender() == owner || isLockApprovedForAll(owner, _msgSender()),
"ERC5058: lock approve caller is not owner nor approved for all"
);
_lockApprove(owner, to, tokenId);
}
/**
* @dev See {IERC5058-getLockApproved}.
*/
function getLockApproved(uint256 tokenId) public view virtual override returns (address) {
require(_exists(tokenId), "ERC5058: lock approved query for nonexistent token");
return _lockApprovals[tokenId];
}
/**
* @dev See {IERC5058-lockerOf}.
*/
function lockerOf(uint256 tokenId) public view virtual override returns (address) {
require(_exists(tokenId), "ERC5058: locker query for nonexistent token");
require(isLocked(tokenId), "ERC5058: locker query for non-locked token");
return _lockApprovals[tokenId];
}
/**
* @dev See {IERC5058-setLockApprovalForAll}.
*/
function setLockApprovalForAll(address operator, bool approved) public virtual override {
_setLockApprovalForAll(_msgSender(), operator, approved);
}
/**
* @dev See {IERC5058-isLockApprovedForAll}.
*/
function isLockApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
return _lockOperatorApprovals[owner][operator];
}
/**
* @dev See {IERC5058-isLocked}.
*/
function isLocked(uint256 tokenId) public view virtual override returns (bool) {
return lockedTokens[tokenId] > block.number;
}
/**
* @dev See {IERC5058-lockExpiredTime}.
*/
function lockExpiredTime(uint256 tokenId) public view virtual override returns (uint256) {
return lockedTokens[tokenId];
}
/**
* @dev See {IERC5058-lock}.
*/
function lock(uint256 tokenId, uint256 expired) public virtual override {
//solhint-disable-next-line max-line-length
require(_isLockApprovedOrOwner(_msgSender(), tokenId), "ERC5058: lock caller is not owner nor approved");
require(expired > block.number, "ERC5058: expired time must be greater than current block number");
require(!isLocked(tokenId), "ERC5058: token is locked");
_lock(_msgSender(), tokenId, expired);
}
/**
* @dev See {IERC5058-unlock}.
*/
function unlock(uint256 tokenId) public virtual override {
require(lockerOf(tokenId) == _msgSender(), "ERC5058: unlock caller is not lock operator");
address from = ERC721.ownerOf(tokenId);
_beforeTokenLock(_msgSender(), from, tokenId, 0);
delete lockedTokens[tokenId];
emit Unlocked(_msgSender(), from, tokenId);
_afterTokenLock(_msgSender(), from, tokenId, 0);
}
/**
* @dev Locks `tokenId` from `from` until `expired`.
*
* Requirements:
*
* - `tokenId` token must be owned by `from`.
*
* Emits a {Locked} event.
*/
function _lock(
address operator,
uint256 tokenId,
uint256 expired
) internal virtual {
address owner = ERC721.ownerOf(tokenId);
_beforeTokenLock(operator, owner, tokenId, expired);
lockedTokens[tokenId] = expired;
_lockApprovals[tokenId] = operator;
emit Locked(operator, owner, tokenId, expired);
_afterTokenLock(operator, owner, tokenId, expired);
}
/**
* @dev Safely mints `tokenId` and transfers it to `to`, but the `tokenId` is locked and cannot be transferred.
*
* Requirements:
*
* - `tokenId` must not exist.
*
* Emits {Locked} and {Transfer} event.
*/
function _safeLockMint(
address to,
uint256 tokenId,
uint256 expired,
bytes memory _data
) internal virtual {
require(expired > block.number, "ERC5058: lock mint for invalid lock block number");
_safeMint(to, tokenId, _data);
_lock(_msgSender(), tokenId, expired);
}
/**
* @dev See {ERC721-_burn}. This override additionally clears the lock approvals for the token.
*/
function _burn(uint256 tokenId) internal virtual override {
address owner = ERC721.ownerOf(tokenId);
super._burn(tokenId);
_beforeTokenLock(_msgSender(), owner, tokenId, 0);
// clear lock approvals
delete lockedTokens[tokenId];
delete _lockApprovals[tokenId];
_afterTokenLock(_msgSender(), owner, tokenId, 0);
}
/**
* @dev Approve `to` to lock operate on `tokenId`
*
* Emits a {LockApproval} event.
*/
function _lockApprove(
address owner,
address to,
uint256 tokenId
) internal virtual {
_lockApprovals[tokenId] = to;
emit LockApproval(owner, to, tokenId);
}
/**
* @dev Approve `operator` to lock operate on all of `owner` tokens
*
* Emits a {LockApprovalForAll} event.
*/
function _setLockApprovalForAll(
address owner,
address operator,
bool approved
) internal virtual {
require(owner != operator, "ERC5058: lock approve to caller");
_lockOperatorApprovals[owner][operator] = approved;
emit LockApprovalForAll(owner, operator, approved);
}
/**
* @dev Returns whether `spender` is allowed to lock `tokenId`.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function _isLockApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
require(_exists(tokenId), "ERC5058: lock operator query for nonexistent token");
address owner = ERC721.ownerOf(tokenId);
return (spender == owner || isLockApprovedForAll(owner, spender) || getLockApproved(tokenId) == spender);
}
/**
* @dev See {ERC721-_beforeTokenTransfer}.
*
* Requirements:
*
* - the `tokenId` must not be locked.
*/
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override {
super._beforeTokenTransfer(from, to, tokenId);
require(!isLocked(tokenId), "ERC5058: token transfer while locked");
}
/**
* @dev Hook that is called before any token lock/unlock.
*
* Calling conditions:
*
* - `owner` is non-zero.
* - When `expired` is zero, `tokenId` will be unlock for `from`.
* - When `expired` is non-zero, ``from``'s `tokenId` will be locked.
*
*/
function _beforeTokenLock(
address operator,
address owner,
uint256 tokenId,
uint256 expired
) internal virtual {}
/**
* @dev Hook that is called after any lock/unlock of tokens.
*
* Calling conditions:
*
* - `owner` is non-zero.
* - When `expired` is zero, `tokenId` will be unlock for `from`.
* - When `expired` is non-zero, ``from``'s `tokenId` will be locked.
*
*/
function _afterTokenLock(
address operator,
address owner,
uint256 tokenId,
uint256 expired
) internal virtual {}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC721) returns (bool) {
return interfaceId == type(IERC5058).interfaceId || super.supportsInterface(interfaceId);
}
}Security considerations
Adding locking functionality through ERC5058 can be useful for conducting NFT lending and staking safely. Because locking restricts NFT functionality, however, the mechanism requires careful design and operation.
A locked NFT cannot be transferred until its lock expires or is released. Before granting another project locking authority, the owner should check that the project's contract has a reliable unlocking mechanism. Without one, an NFT could remain non-transferable for an extremely long time.
Lock durations should also be set within a reasonable range. ERC5058 allows an expiration block number to be specified when an NFT is locked. Because the transfer restriction expires automatically, the expiration prevents the NFT from remaining unusable indefinitely even if no unlock operation is executed.
A project that locks users' NFTs should meet at least the following two conditions for safe operation.
- The contract includes functionality for unlocking NFTs.
- The mechanism supports expiration block numbers that allow locks to expire automatically.
NFT owners should likewise choose carefully whom they trust with locking authority to protect their assets.
Citation
Tyler (@radiocaca), Alex (@gojazdev), John (@sfumato00), "ERC-5058: Lockable Non-Fungible Tokens [DRAFT]," Ethereum Improvement Proposals, no. 5058, April 2022. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-5058.
Conclusion
This article covered ERC5058, a proposal for adding locking functionality to NFTs. I hope you found it useful.
If you have any questions, feel free to contact me via Twitter DM.
I also publish on other platforms, which you can find below.



