Posted By : Rishab
NFT renting/leasing is the process in which the owner of an NFT can lend their digital assets or NFTs to someone who wishes to borrow or rent them for some time. The process involves the use of smart contracts that manage the terms and conditions of the rental agreement. At the end of the rental period, the smart contract returns the NFT to its original owner.
In most cases, it all starts with the NFT rental market. A few platforms that let users rent NFTs are Trava NFT, UnitBox DAO, Vera, N3RP, reNFT, Double Protocol, and IQ Protocol.
But, here we focus on creating a smart contract that facilitates the process of renting your NFT:
Using a blockchain platform, you can create a unique token that represents ownership of the asset.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/[email protected]/token/ERC721/ERC721.sol";
interface IERC4907 {
// Logged when the user of a token assigns a new user or updates expires
/// @notice Emitted when the `user` of an NFT or the `expires` of the `user` is changed
/// The zero address for user indicates that there is no user address
event UpdateUser(uint256 indexed tokenId, address indexed user, uint64 expires);
/// @notice set the user and expires of a NFT
/// @dev The zero address indicates there is no user
/// Throws if `tokenId` is not valid NFT
/// @param user The new user of the NFT
/// @param expires UNIX timestamp, The new user could use the NFT before expires
function setUser(uint256 tokenId, address user, uint64 expires) external ;
/// @notice Get the user address of an NFT
/// @dev The zero address indicates that there is no user or the user is expired
/// @param tokenId The NFT to get the user address for
/// @return The user address for this NFT
function userOf(uint256 tokenId) external view returns(address);
/// @notice Get the user expires of an NFT
/// @dev The zero value indicates that there is no user
/// @param tokenId The NFT to get the user expires for
/// @return The user expires for this NFT
function userExpires(uint256 tokenId) external view returns(uint256);
}
contract ERC4907 is ERC721, IERC4907 {
struct UserInfo
{
address user; // address of user role
uint64 expires; // unix timestamp, user expires
}
mapping (uint256 => UserInfo) internal _users;
constructor(string memory name_, string memory symbol_)
ERC721(name_,symbol_)
{
}
/// @notice set the user and expires of a NFT
/// @dev The zero address indicates there is no user
/// Throws if `tokenId` is not valid NFT
/// @param user The new user of the NFT
/// @param expires UNIX timestamp, The new user could use the NFT before expires
function setUser(uint256 tokenId, address user, uint64 expires) public virtual{
require(_isApprovedOrOwner(msg.sender, tokenId),"ERC721: transfer caller is not owner nor approved");
UserInfo storage info = _users[tokenId];
info.user = user;
info.expires = expires;
emit UpdateUser(tokenId,user,expires);
}
/// @notice Get the user address of an NFT
/// @dev The zero address indicates that there is no user or the user is expired
/// @param tokenId The NFT to get the user address for
/// @return The user address for this NFT
function userOf(uint256 tokenId)public view virtual returns(address){
if( uint256(_users[tokenId].expires) >= block.timestamp){
return _users[tokenId].user;
}
else{
return address(0);
}
}
/// @notice Get the user expires of an NFT
/// @dev The zero value indicates that there is no user
/// @param tokenId The NFT to get the user expires for
/// @return The user expires for this NFT
function userExpires(uint256 tokenId) public view virtual returns(uint256){
return _users[tokenId].expires;
}
/// @dev See {IERC165-supportsInterface}.
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IERC4907).interfaceId || super.supportsInterface(interfaceId);
}
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override{
super._beforeTokenTransfer(from, to, tokenId);
if (from != to && _users[tokenId].user != address(0)) {
delete _users[tokenId];
emit UpdateUser(tokenId, address(0), 0);
}
}
}
contract RentableNft is ERC4907 {
constructor(string memory name_, string memory symbol_)
ERC4907(name_,symbol_)
{
}
function mint(uint256 tokenId, address to) public {
_mint(to, tokenId);
}
}
Determine the rental period, rental fee, and any conditions or restrictions on the NFT.
Once the rental conditions are finalized, list your NFT on rental platforms for interested people to browse, once the rental conditions are finalized by the owner.
When someone rents their NFT, they transfer their ownership for the duration of the rental period. This can be done with the help of smart contracts that we created above. At the end of the rental period, ownership will revert to the actual owner.
If an agreement is reached, the price, rental period, and other relevant factors will be executed by the smart contract. The lessee sends the desired amount of cryptocurrency to the smart contract, and once the lease period is over, the smart contract returns the NFT to its rightful owner.
Sometimes smart contracts require tenants to provide some security. If the lessee does not return the NFT within the specified time, the security will be sent to the owner of the NFT. Some platforms require no collateral, but instead of offering real NFTs, smart contracts create and issue wrapped NFTs to tenants.
If you have a project in mind related to related to NFTs, feel free to connect with our skilled NFT and blockchain developers.
September 8, 2024 at 02:23 am
Your comment is awaiting moderation.