EIPs Explained

ERC-4907 · Final · ERC · 2022-03-11

ERC-4907: Rental NFT, an Extension of EIP-721

Adds a time-limited `user` role to ERC-721 NFTs, so an owner can rent out an NFT's utility without giving away ownership, and access ends automatically at the expiry time.

What problem does it solve?

Many NFTs have utility: game items you can equip, virtual land you can build on, memberships that unlock access. Owners often want to lend that utility out, but ERC-721 only knows one role, the owner. Handing over the NFT itself means trusting the borrower to give it back.

Projects invented their own "operator" or "controller" roles, each with different names and rules, so rental platforms could not support them generically. And ending a rental needed a second transaction from the owner, costing gas and time.

ERC-4907 standardises a separate user role with a built-in expiry.

How it works

Each token has, alongside its owner, a user address and an expires UNIX timestamp:

  • setUser(tokenId, user, expires) assigns the role. Only the owner or an approved address may call it.
  • userOf(tokenId) returns the current user, or the zero address once expires has passed. No transaction is needed to end the rental.
  • userExpires(tokenId) returns the stored expiry.
  • UpdateUser is emitted whenever the user or expiry changes.

The user can use the NFT, but cannot transfer it or set another user. Applications (games, metaverses, gated sites) simply check userOf instead of, or in addition to, ownerOf.

The reference implementation also clears the user when the NFT is transferred to a new owner. Contracts signal support via ERC-165 with interface ID 0xad092b5c.

Sample code

Minimal implementation (based on the reference, adapted to OpenZeppelin v5):

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

interface IERC4907 {
    event UpdateUser(uint256 indexed tokenId, address indexed user, uint64 expires);
    function setUser(uint256 tokenId, address user, uint64 expires) external;
    function userOf(uint256 tokenId) external view returns (address);
    function userExpires(uint256 tokenId) external view returns (uint256);
}

contract RentableNFT is ERC721, IERC4907 {
    struct UserInfo { address user; uint64 expires; }
    mapping(uint256 => UserInfo) internal _users;

    constructor() ERC721("Rentable", "RENT") {}

    function setUser(uint256 tokenId, address user, uint64 expires) public virtual {
        address owner = ownerOf(tokenId);
        require(_isAuthorized(owner, msg.sender, tokenId), "not owner nor approved");
        _users[tokenId] = UserInfo(user, expires);
        emit UpdateUser(tokenId, user, expires);
    }

    function userOf(uint256 tokenId) public view virtual returns (address) {
        UserInfo memory info = _users[tokenId];
        return uint256(info.expires) >= block.timestamp ? info.user : address(0);
    }

    function userExpires(uint256 tokenId) public view virtual returns (uint256) {
        return _users[tokenId].expires;
    }

    function _update(address to, uint256 tokenId, address auth) internal override returns (address from) {
        from = super._update(to, tokenId, auth);
        if (from != to && _users[tokenId].user != address(0)) {
            delete _users[tokenId];
            emit UpdateUser(tokenId, address(0), 0);
        }
    }

    function supportsInterface(bytes4 id) public view override returns (bool) {
        return id == type(IERC4907).interfaceId || super.supportsInterface(id);
    }
}

Things to know

  • Status: Final.
  • The owner can change or clear the user at any time. A rental marketplace that wants to guarantee the renter's period usually holds the NFT in escrow itself, then calls setUser.
  • Expiry is based on block.timestamp, so it is accurate to the block, not to the second.
  • Only one user at a time is supported. For multiple simultaneous users (mainly ERC-1155), see ERC-5006. For physical devices with authenticated users, see ERC-4519.

#nft #rental #erc721 #gaming

Details

Authors
Anders (@0xanders), Lance (@LanceSnow), Shrug <[email protected]>
Status
Final
Type
Standards Track / ERC
Created
2022-03-11
Requires
Required by

Machine-readable: Markdown · JSON