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.

ERC-6982 proposes a gas-efficient approach to lockable ERC-721 tokens.

This article translates, summarizes, and expands on the following proposal.

As of October 19, 2023, ERC-6982 was in the Review stage.

I have also written about other ERC proposals.

Overview

The proposal introduces a lockable ERC-721 token interface and improves gas efficiency by eliminating unnecessary events. The interface provides a foundation for creating and managing lockable ERC-721 tokens. Specifically, the contract emits a DefaultLocked(bool locked) event upon deployment to establish the initial lock status of all tokens. Subsequent changes to individual token states are recorded using the Locked(uint256 indexed tokenId, bool locked) event. The interface also includes a view function, locked(uint256 tokenId), for retrieving a token's current lock status. Another view function, defaultLocked(), returns the default status of newly minted tokens.

This ERC-721 contract provides a gas-efficient way to manage token locking and unlocking. It establishes the default lock status at deployment and uses Locked(uint256 indexed tokenId, bool locked) to record subsequent changes to individual token states. Call locked(uint256 tokenId) to retrieve a token's current status and defaultLocked() to check the default lock status of newly minted tokens. This allows token locks to be managed efficiently.

For more on ERC-721, see the following article.

https://chaldene.net/erc721

Motivation

Earlier proposals for lockable tokens generally emit an event whenever a token is minted. This consumes unnecessary gas, especially when tokens are permanently locked, as with soulbound tokens or non-transferable badges. The proposal offers a more gas-efficient solution by emitting events only at contract deployment and when an individual token's lock status changes.

Conventional lockable token proposals emit an event whenever a token is minted, resulting in unnecessary gas consumption. This proposal instead emits events at deployment and when individual token states change. This reduces unnecessary gas costs, particularly for permanently locked tokens.

Specification

The interface is defined as follows.

javascript
// ERC165 interfaceId 0x6b61a747
interface IERC6982 {
  // This event MUST be emitted upon deployment of the contract, establishing 
  // the default lock status for any tokens that will be minted in the future.
  // If the default lock status changes for any reason, this event 
  // MUST be re-emitted to update the default status for all tokens.
  // Note that emitting a new DefaultLocked event does not affect the lock 
  // status of any tokens for which a Locked event has previously been emitted.
  event DefaultLocked(bool locked);

  // This event MUST be emitted whenever the lock status of a specific token 
  // changes, effectively overriding the default lock status for this token.
  event Locked(uint256 indexed tokenId, bool locked);

  // This function returns the current default lock status for tokens.
  // It reflects the value set by the latest DefaultLocked event.
  function defaultLocked() external view returns (bool);

  // This function returns the lock status of a specific token.
  // If no Locked event has been emitted for a given tokenId, it MUST return 
  // the value that defaultLocked() returns, which represents the default 
  // lock status.
  // This function MUST revert if the token does not exist.
  function locked(uint256 tokenId) external view returns (bool);
}

The interface ID used with ERC-165 is 0x6b61a747.

For more on ERC-165, see the following article.

[ERC-165] Checking Whether a Contract Supports an Interface

Rationale

The standard aims to minimize gas consumption by optimizing how often events are emitted. The DefaultLocked event establishes the default lock status of tokens, eliminating the need for a lock event whenever a new token is minted. Importantly, a DefaultLocked event can be emitted at any time, either before or after a Locked event.

Token behavior may change under particular conditions, such as after a reveal. The contract may emit another DefaultLocked event to reflect the new default status. The main objective is to save gas by avoiding the need to emit an individual Locked event for every token when the default status changes.

The Locked event records changes to the lock status of individual tokens.

The defaultLocked function returns the current default lock status of tokens. This facilitates interaction with other contracts and helps avoid potential conflicts with ERC-5192, which was already Final at the time.

ERC-5192 and ERC-6982 offer different approaches to locking non-fungible tokens (NFTs) on Ethereum. Both provide functionality for controlling token lock status, but their implementations and objectives differ.

ERC5192: Soulbound NFTs

ERC-5192 extends EIP-721 with a minimal interface for making a token soulbound to an account. The proposal uses a function named locked(uint256 tokenId) to query a token's lock status. If a token is locked, all EIP-721 functions that transfer that token must reject the transfer.

ERC-6982: Efficient Default Lockable Tokens

ERC-6982 introduces a lockable interface for EIP-721 tokens and reduces gas usage by eliminating unnecessary events. The interface establishes the initial default lock status using DefaultLocked(bool locked) and records changes to individual token lock states through Locked(uint256 indexed tokenId, bool locked) events. It also provides the view function defaultLocked() to query the default status of newly minted tokens.

Potential conflicts

The defaultLocked() function in ERC-6982 is intended to facilitate interactions with other contracts and avoid potential conflicts with ERC-5192. It helps other contracts interact with the interface and distinguishes it from proposals such as ERC-5192, which control token lock status differently.

For more on ERC-5192, see the following article.

[ERC-5192] Understanding Non-Transferable Soulbound Tokens

The locked function returns the current lock status of a particular token and helps other contracts interact with it. If no override has been made for a particular token ID, the function should return the value provided by defaultLocked.

Designating a token as locked does not necessarily mean it is completely non-transferable. A locked token may still be transferred under certain conditions. The lock status primarily concerns whether the token can be transferred through marketplaces or external exchanges.

Consider the Cruna protocol as an example. In this system, an NFT owner can activate what is called an initiator. An initiator is a sub-wallet authorized to initiate key transactions. Setting an initiator locks the token. However, the token can still be transferred if the designated initiator initiates the transfer.

Backward Compatibility

The standard is fully backward compatible with existing ERC-721 contracts. It provides a way to extend existing contracts with new functionality. In other words, the standard can be introduced without modifying existing tokens or contracts. (The Japanese source refers to ERC-7507 here.)

The Japanese source also refers to ERC-7507 in the claim that the standard may add functionality and improve gas efficiency for existing tokens and applications. The standard allows new requirements to be addressed while maintaining compatibility with existing tokens. It can be integrated without disrupting existing tokens or contracts.

Reference Implementation

The following GitHub repository contains an implementation addressing a particular use case.

https://github.com/ethereum/EIPs/tree/master/assets/eip-6982

The main purpose of the implementation is to allow token owners to stake assets in a pool without losing ownership under normal circumstances. Ordinarily, staking a token transfers ownership, potentially making it difficult for the owner to recover it. In this implementation, however, the pool can lock an asset while allowing the token owner to retain ownership.

The README explains the procedures and how to compile and test the contracts. The implementation manages interactions between the owner and the pool and helps maintain token liquidity and ownership in specific use cases.

javascript
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

// Authors: Francesco Sullo <francesco@sullo.co>

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./IERC721Lockable.sol";
import "./IERC6982.sol";

// This is an example of lockable ERC721 using IERC6982 as basic interface

contract ERC721Lockable is IERC6982, IERC721Lockable, Ownable, ERC721, ERC721Enumerable {
  using Address for address;

  mapping(address => bool) private _locker;
  mapping(uint256 => address) private _lockedBy;

  bool internal _defaultLocked;

  modifier onlyLocker() {
    require(_locker[_msgSender()], "Not a locker");
    _;
  }

  constructor(
    string memory name,
    string memory symbol,
    bool defaultLocked_
  ) ERC721(name, symbol) {
    updateDefaultLocked(defaultLocked_);
  }

  function defaultLocked() external view override returns (bool) {
    return _defaultLocked;
  }

  function updateDefaultLocked(bool defaultLocked_) public onlyOwner {
    _defaultLocked = defaultLocked_;
    emit DefaultLocked(defaultLocked_);
  }

  function _beforeTokenTransfer(
    address from,
    address to,
    uint256 tokenId,
    uint256 batchSize
  ) internal override(ERC721, ERC721Enumerable) {
    require(
      // during minting
      from == address(0) ||
        // later
        !locked(tokenId),
      "Token is locked"
    );
    super._beforeTokenTransfer(from, to, tokenId, batchSize);
  }

  function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721Enumerable) returns (bool) {
    return
      interfaceId == type(IERC6982).interfaceId ||
      interfaceId == type(IERC721Lockable).interfaceId ||
      super.supportsInterface(interfaceId);
  }

  function locked(uint256 tokenId) public view virtual override returns (bool) {
    require(_exists(tokenId), "Token does not exist");
    return _lockedBy[tokenId] != address(0);
  }

  function lockerOf(uint256 tokenId) public view virtual override returns (address) {
    return _lockedBy[tokenId];
  }

  function isLocker(address locker) public view virtual override returns (bool) {
    return _locker[locker];
  }

  function setLocker(address locker) external virtual override onlyOwner {
    require(locker.isContract(), "Locker not a contract");
    _locker[locker] = true;
    emit LockerSet(locker);
  }

  function removeLocker(address locker) external virtual override onlyOwner {
    require(_locker[locker], "Not an active locker");
    delete _locker[locker];
    emit LockerRemoved(locker);
  }

  function hasLocks(address owner) public view virtual override returns (bool) {
    uint256 balance = balanceOf(owner);
    for (uint256 i = 0; i < balance; i++) {
      uint256 id = tokenOfOwnerByIndex(owner, i);
      if (locked(id)) {
        return true;
      }
    }
    return false;
  }

  function lock(uint256 tokenId) external virtual override onlyLocker {
    // locker must be approved to mark the token as locked
    require(isLocker(_msgSender()), "Not an authorized locker");
    require(getApproved(tokenId) == _msgSender() || isApprovedForAll(ownerOf(tokenId), _msgSender()), "Locker not approved");
    _lockedBy[tokenId] = _msgSender();
    emit Locked(tokenId, true);
  }

  function unlock(uint256 tokenId) external virtual override onlyLocker {
    // will revert if token does not exist
    require(_lockedBy[tokenId] == _msgSender(), "Wrong locker");
    delete _lockedBy[tokenId];
    emit Locked(tokenId, false);
  }

  // emergency function in case a compromised locker is removed
  function unlockIfRemovedLocker(uint256 tokenId) external virtual override {
    require(locked(tokenId), "Not a locked tokenId");
    require(!_locker[_lockedBy[tokenId]], "Locker is still active");
    require(ownerOf(tokenId) == _msgSender(), "Not the asset owner");
    delete _lockedBy[tokenId];
    emit ForcefullyUnlocked(tokenId);
  }

  // manage approval

  function approve(address to, uint256 tokenId) public virtual override(IERC721, ERC721) {
    require(!locked(tokenId), "Locked asset");
    super.approve(to, tokenId);
  }

  function getApproved(uint256 tokenId) public view virtual override(IERC721, ERC721) returns (address) {
    if (locked(tokenId) && lockerOf(tokenId) != _msgSender()) {
      return address(0);
    }
    return super.getApproved(tokenId);
  }

  function setApprovalForAll(address operator, bool approved) public virtual override(IERC721, ERC721) {
    require(!approved || !hasLocks(_msgSender()), "At least one asset is locked");
    super.setApprovalForAll(operator, approved);
  }

  function isApprovedForAll(address owner, address operator) public view virtual override(IERC721, ERC721) returns (bool) {
    if (hasLocks(owner)) {
      return false;
    }
    return super.isApprovedForAll(owner, operator);
  }
}

_locker

javascript
mapping(address => bool) private _locker;

Overview A mapping associating an address with a Boolean value.

Details

  • The _locker mapping associates each address with a Boolean indicating whether it acts as a locker.
  • _locker[_msgSender()] returns the Boolean corresponding to the current caller's address returned by _msgSender().
  • The _locker mapping is private, so it cannot be accessed directly from outside the contract.

Parameters

  • address
    • Key-value pairs associating addresses with their locker status (a Boolean).

_lockedBy

javascript
mapping(uint256 => address) private _lockedBy;

Overview A mapping associating each token ID with a locker address. It tracks which address locked a particular token.

Details

  • The _lockedBy mapping associates each token ID with the address that locked it.
  • _lockedBy[tokenId] returns the locker address for the specified token ID.
  • The _lockedBy mapping is also private, so it cannot be accessed directly from outside the contract.

Parameters

  • uint256
    • A token ID is associated with the address that locked it.

_defaultLocked

javascript
bool internal _defaultLocked;

Overview A Boolean indicating the default lock status. It specifies whether tokens are locked by default.

Details

  • The _defaultLocked Boolean defines the default lock status in the contract.
  • It represents the initial status at deployment.
  • If _defaultLocked is true, tokens are locked by default; if it is false, they are unlocked by default.
  • Because the variable is internal, derived contracts can access it.

Parameters

  • bool
    • A Boolean representing the default lock status.
    • true means locked, and false means unlocked.

onlyLocker

javascript
modifier onlyLocker() {
    require(_locker[_msgSender()], "Not a locker");
    _;
}

Overview A modifier that restricts access to callers satisfying a particular condition. Only callers designated as lockers can access the modified function or code block.

Details

  • The onlyLocker modifier checks a condition before the function executes.
  • It checks whether _locker[_msgSender()] is true, meaning that the caller is a locker.
  • If the condition is not satisfied, the require statement reverts execution with the error message Not a locker.
  • If the condition is satisfied, the modified function or code block executes.

defaultLocked

javascript
function defaultLocked() external view override returns (bool) {
    return _defaultLocked;
}

Overview A function that retrieves the default lock status.

Details

  • External callers use it to determine whether tokens are locked by default.
  • The function is declared external view override, providing read-only external access.
  • It returns the value of _defaultLocked.

Return value

  • bool
    • The default lock status.
    • true means locked, and false means unlocked.

updateDefaultLocked

javascript
function updateDefaultLocked(bool defaultLocked_) public onlyOwner {
    _defaultLocked = defaultLocked_;
    emit DefaultLocked(defaultLocked_);
}

Overview A function that updates the default lock status. It is called by the contract owner.

Details

  • The updateDefaultLocked function sets the default lock status to the supplied value and emits a DefaultLocked event.
  • It updates the value of _defaultLocked.
  • Only the contract owner can call this function.

Parameters

  • defaultLocked_
    • A Boolean specifying the default lock status.
    • true means locked, and false means unlocked.

Return value

  • None.

_beforeTokenTransfer

javascript
function _beforeTokenTransfer(
    address from,
    address to,
    uint256 tokenId,
    uint256 batchSize
) internal override(ERC721, ERC721Enumerable) {
    require(
        // during minting
        from == address(0) ||
        // later
        !locked(tokenId),
        "Token is locked"
    );
    super._beforeTokenTransfer(from, to, tokenId, batchSize);
}

Overview A hook that runs before a token transfer. It rejects transfers of locked tokens.

Details

  • It is called before a token transfer and checks the transfer conditions.
  • It permits transfers during minting, when from is the zero address, or when the token is not locked.
  • Otherwise, execution reverts with the error message Token is locked.
  • This function overrides the corresponding functions in ERC-721 and ERC721Enumerable.
  • Finally, it calls the parent implementation of _beforeTokenTransfer.

Parameters

  • from
    • The address sending the token.
  • to
    • The address receiving the token.
  • tokenId
    • The token ID.
  • batchSize
    • The batch size for batch operations.

supportsInterface

javascript
function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721Enumerable) returns (bool) {
    return
        interfaceId == type(IERC6982).interfaceId ||
        interfaceId == type(IERC721Lockable).interfaceId ||
        super.supportsInterface(interfaceId);
}

Overview A function that checks whether a specified interface is supported. It supports multiple interfaces.

Details

  • It checks whether the supplied interface ID is supported.
  • Supported interfaces include IERC6982 and IERC721Lockable.
  • Interfaces supported by the parent classes ERC-721 and ERC721Enumerable are also supported.
  • The function takes interfaceId and returns true if the corresponding interface is supported.

Parameters

  • interfaceId
    • A byte sequence representing the interface ID.

Return value

  • bool
    • true if the interface is supported; otherwise false.

locked

javascript
function locked(uint256 tokenId) public view virtual override returns (bool) {
    require(_exists(tokenId), "Token does not exist");
    return _lockedBy[tokenId] != address(0);
}

Overview A function that checks whether a specified token is locked.

Details

  • The locked function checks whether the token with the supplied ID is locked.
  • If the token does not exist, execution reverts with Token does not exist.
  • It checks whether the associated locker address is nonzero.
  • A nonzero address indicates that the token is locked.

Parameters

  • tokenId
    • The ID of the token to check.

Return value

  • bool
    • true if the specified token is locked; otherwise false.

lockerOf

javascript
function lockerOf(uint256 tokenId) public view virtual override returns (address) {
    return _lockedBy[tokenId];
}

Overview A function that retrieves the locker (the address that locked a token) of a specified token.

Details

  • It retrieves the locker address associated with the supplied token ID.
  • It returns the mapped address even if the token does not exist. For an unlocked token, this is normally the zero address.

Parameters

  • tokenId
    • The ID of the token whose locker is being queried.

Return value

  • address
    • The locker address of the specified token.

isLocker

javascript
function isLocker(address locker) public view virtual override returns (bool) {
    return _locker[locker];
}

Overview A function that checks whether a specified address is a locker.

Details

  • It checks whether the given address is designated as a locker.
  • It returns true if the address is a locker, and false otherwise.

Parameters

  • locker
    • The address to check.

Return value

  • bool
    • true if the specified address is a locker; otherwise false.

setLocker

javascript
function setLocker(address locker) external virtual override onlyOwner {
    require(locker.isContract(), "Locker not a contract");
    _locker[locker] = true;
    emit LockerSet(locker);
}

Overview A function that registers a locker contract.

Details

  • It designates the supplied address as a locker.
  • It checks that the address being registered belongs to a contract.
  • Upon successful registration, the function updates the _locker mapping and emits a LockerSet event.
  • Only the contract owner can execute it.

Parameters

  • locker
    • The address of the locker contract to register.

removeLocker

javascript
function removeLocker(address locker) external virtual override onlyOwner {
    require(_locker[locker], "Not an active locker");
    delete _locker[locker];
    emit LockerRemoved(locker);
}

Overview A function that removes an active locker.

Details

  • It checks that the specified address is an active locker and removes it.
  • The _locker mapping is used to determine whether the locker is active.
  • Upon successful removal, the function deletes the locker from _locker and emits a LockerRemoved event.
  • Only the contract owner can execute it.

Parameters

  • locker
    • The address of the active locker to remove.

hasLocks

javascript
function hasLocks(address owner) public view virtual override returns (bool) {
    uint256 balance = balanceOf(owner);
    for (uint256 i = 0; i < balance; i++) {
        uint256 id = tokenOfOwnerByIndex(owner, i);
        if (locked(id)) {
            return true;
        }
    }
    return false;
}

Overview A function that checks whether an owner holds any locked tokens.

Details

  • It checks whether the specified owner holds locked tokens.
  • It retrieves the owner's token balance and checks the lock status of each token.
  • It returns true if any token is locked and false if all tokens are unlocked.

Parameters

  • owner
    • The address of the owner whose tokens are being checked.

Return value

  • bool
    • true if the owner holds a locked token; otherwise false.

lock

javascript
function lock(uint256 tokenId) external virtual override onlyLocker {
    require(isLocker(_msgSender()), "Not an authorized locker");
    require(getApproved(tokenId) == _msgSender() || isApprovedForAll(ownerOf(tokenId), _msgSender()), "Locker not approved");
    _lockedBy[tokenId] = _msgSender();
    emit Locked(tokenId, true);
}

Overview A function that locks a token.

Details

  • It locks the specified token, subject to authorization from the token owner.
  • It checks whether the caller is an authorized locker and whether the token owner has approved that locker.
  • Upon successful locking, the function updates _lockedBy and emits a Locked event.

Parameters

  • tokenId
    • The ID of the token to lock.

unlock

javascript
function unlock(uint256 tokenId) external virtual override onlyLocker {
    require(_lockedBy[tokenId] == _msgSender(), "Wrong locker");
    delete _lockedBy[tokenId];
    emit Locked(tokenId, false);
}

Overview A function that unlocks a token.

Details

  • It unlocks the specified token.
  • It verifies that the caller is the authorized locker that locked the token.
  • Upon successful unlocking, the function deletes the lock information from _lockedBy and emits a Locked event.

Parameters

  • tokenId
    • The ID of the token to unlock.

unlockIfRemovedLocker

javascript
function unlockIfRemovedLocker(uint256 tokenId) external virtual override {
    require(locked(tokenId), "Not a locked tokenId");
    require(!_locker[_lockedBy[tokenId]], "Locker is still active");
    require(ownerOf(tokenId) == _msgSender(), "Not the asset owner");
    delete _lockedBy[tokenId];
    emit ForcefullyUnlocked(tokenId);
}

Overview A function that forcibly unlocks a token if its locker has been removed.

Details

  • It forcibly unlocks the specified token.
  • It checks that the token is locked, that its locker is no longer active, and that the caller is the asset owner.
  • Upon successful forced unlocking, the function deletes the lock information from _lockedBy and emits a ForcefullyUnlocked event.

Parameters

  • tokenId
    • The ID of the token to forcibly unlock.

Security Considerations

This EIP introduces no known security considerations. As with other smart contract standards, however, it is important to implement appropriate security measures when using this interface.

Citation

Francesco Sullo (@sullof), Alexe Spataru (@urataps), "ERC-6982: Efficient Default Lockable Tokens [DRAFT]," Ethereum Improvement Proposals, no. 6982, May 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-6982.

Conclusion

This article covered ERC-6982, a standard proposing a gas-efficient approach to lockable ERC-721 tokens. I hope this explanation was helpful.

If you have any questions, feel free to contact me via Twitter DM.

Twitter @cardene777

I also publish articles on the following platforms.

https://chaldene.net/