How to Implement a Rental NFT Contract
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
2
Creating the contract
2.1
Examining the contract
user
: The address of the user.expires
: A UNIX timestamp indicating when the user's rental period expires.
2.2
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
4
Interacting with the contract
1
, and then mint the NFT.4.1
ownerOf
button to confirm that the address is the same as the minter of the NFT.4.2
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
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
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!