Welcome to the second part of the ERC-4907 tutorial! In this practical guide, you will discover how to implement a rentable NFT. If you haven't had a chance to explore the previous article, I recommend giving it a read to ensure you're up to date with what we're doing in this second part.

ERC-4907 Code Walkthrough

Estimated time to follow along: 15 to 20 minutes

In this guide, We’ll implement a Rentable NFT. Be sure you’re up to date with what ERC-4907 is all about.  

By building an ERC-4907, you’ll learn:

  • How ERC-4907 works under the hood.

  • How to implement ERC-4907.

Let's get started!

1

Scaffolding the project

Go to the Remix IDE here. Next, on the remix interface in the left top corner, click on the file and create a new file. Here, we’ll create two files, RentableNFT.sol, IERC4907.sol, which are the underlying interface file, and the ERC-4907 contract.

2

Creating the contract

To implement ERC-4907, we’ll need the interface set by the standards, which can be gotten from here. After we’ve got it, we can then proceed to populate it in our IERC4907 file as shown.

The interface defines the ERC-4907 standard for NFTs, including events and functions for setting and retrieving temporary user information and expiration timestamps.

2.1

Examining the contract

RentableNft Contract

This contract is responsible for creating and setting up the renting NFT functionalities.

Import Statements

These lines import the ERC-721 standard implementation from OpenZeppelin's library and the custom interface IERC4907. 

UserInfo Struct

This struct is used to store information about the user of a particular NFT. It includes:

  • user: The address of the user.

  • expires: A UNIX timestamp indicating when the user's rental period expires.

Mapping for UserInfo

This mapping associates each NFT (tokenId) with its corresponding UserInfo struct. This allows the contract to keep track of who is currently using each NFT and when their usage period expires.

Constructor

The constructor is a special function that is executed only once when the contract is deployed. It initializes the RentableNFT contract as an ERC-721 token with the name "CovNFT" and the symbol "CVT.". The ERC721 constructor is called with these parameters.

2.2

Functions

  • setUser: Sets the user and expiration date for a specific NFT, ensuring the caller is authorized.

  • userOf: Returns the current user of an NFT if the rental is valid; otherwise, returns the owner.

2.3
  • userExpires: Retrieves the expiration timestamp for the current user of a specific NFT.

  • supportsInterface: Confirms if the contract supports a specific interface, including IERC4907.

  • _isApprovedOrOwner: Checks if an address is the owner or an approved operator of a specific NFT.

  • checkTime: Returns the current block timestamp.

  • mint: mints a new NFT with a specified token ID to the caller's address.

3

Compiling and Deploying the smart contract

To compile our contract, navigate to the Compiler tab on the left and click on Compile. Once you've successfully compiled the contract, proceed to the Deploy tab to initiate the deployment. Select Injected Provider-Metamask from the available environments.

4

Interacting with the contract

If you’ve reached this section thus far, kudos to you! Let’s now mint an NFT. To achieve this, click on the mint button, enter a token ID of  1, and then mint the NFT.

4.1

Click on the ownerOf button to confirm that the address is the same as the minter of the NFT.

4.2

Let’s now set the user that we want to rent our minted NFT to. For us to achieve this, click on the setUser which takes in the token ID of the NFT, the address of the user we want to rent the NFT to which I used an address from the address provided in the Remix IDE, and the expiration time which should be a time in the future from the current block time.

4.3

After setting the user for token ID 1, if you check now, you'll notice that the user address has been updated to the newly assigned address, while the NFT's owner, which is the minter address, remains unchanged.

4.4

After the rental period for the NFT expires, the userOf function returns to the owner's address.

Conclusion

Congratulations on making it this far! In this guide, we've covered the ERC-4907 Rental NFT Standard, and we looked into the details of renting out non-fungible tokens. We've also explored how to set up and manage rental periods for NFTs, empowering you to create your rental marketplace. Happy coding!