当前位置:网站首页>NFT: how to improve rentable NFT (erc-4907)
NFT: how to improve rentable NFT (erc-4907)
2022-07-25 05:57:00 【chinadefi】
NFT: How to improve the leasable NFT (ERC-4907)


NFT It is not only the encryption guarantee of image ownership . With the same technical support , Things will depend on how we work 、 Change through interaction .
What is said here is practical NFT, They give the owner some privileges 、 Rights or rewards .
for example , In the field of games , practical NFT Provides a new way to manage in-game asset ownership , Or in social space , They help ensure that users can safely enter their own communities .
Another use case might be bound to “ Rentable smart physical assets ”( For example, lockers 、 Vacation home or car ) The associated NFT, Proving that he is NFT After the current owner of , Users can send commands to it ( for example “ To lock the door ”、“ Unlock the door ”).
Speaking of practicality NFT, In some cases , Owners and users are not always the same .NFT The owner of can rent it to users for a period of time . in the meantime , The user has temporarily obtained NFT Privileges granted , But its ownership cannot be transferred .
The most advanced technology
Now there is one for rentable NFT Of EIP(EIP- 4907), It points out that :
“ The standard is EIP-721 An extension of . It proposes an additional role that can grant addresses ( user ), And the role is automatically revoked ( expire ) Time for . User role representatives “ Use ”NFT Authority , But it doesn't mean transferring or setting the user's ability .
This standard is intended to be compatible with ERC-721 Fully compatible with , And basically, an interface is introduced to realize (IERC4907.sol), The method is as follows :
function setUser(uint256 tokenId, address user, uint64 expires)
If the caller is NFT The owner of or has been granted an address , Then this method can set new users and NFT The expiration date of .
function userOf(uint256 tokenId) external view returns(address);
This returns NFT The address of the current user , Zero address means there are no users or the lease term has expired .
function userExpires(uint256 tokenId) external view returns(uint256);
The last method returns NFT Expiration time of the user , among 0 Indicates that there are no users .
in the meantime , We will notice how the contract is extended ERC721 The standard , And will also notice the use NFT Mapping with its end users and expiration time .
contract ERC4907 is ERC721, IERC4907 {
struct UserInfo
{
address user; // address of user role
uint64 expires; // unix timestamp, user expires
}
mapping (uint256 => UserInfo) internal _users;
…
Possible improvements
“ If EIP The title is Rental NFT, What then? IERC4907 Or the reference implementation lacks one “ lease ” Methods? ”?
There is one setUser Method , But when using it , Only NFT Owner ( Or an approved address ) You can set the user . In this case , This process sounds more like a loan process , Owners will work with utilities NFT The bound privileges are lent to the user , Besides , And pay for it gas cost .
My idea is similar to :

ad locum , The owner can choose :
- Put him NFT Set to leasable .
- Set to rent him for a period of time NFT Number of ethers required .
If NFT Listed as leasable , Users can rent it , And pay the owner for this .
Want to rent NFT The user of must send a transaction to the contract , Specify what he wants to rent NFT And the rental time . Besides , The transaction must have the correct amount of ether within the selected time interval .
Let's see solidity Relevant parts of the code :
contract RentableNFT is ERC4907, Ownable
Of course ,RentableNFT The contract extends the reference implementation ERC4907 and OpenZeppelin Ownable contract (Ownable Extension is not absolutely necessary , But in this contract , I hope only the owner of the contract is allowed to coin ).
uint256 public baseAmount = 1000000000000000; //0.001 ethers
struct RentableItem {
bool rentable;
uint256 amountPerMinute;
}
mapping(uint256 => RentableItem) public rentables;
- baseAmount Is the default payment amount for one minute of lease ,
- RentableItem It's a structure , Used to store information about a NFT Rentability and cost information ,
- rentables yes NFT and RentableItem Mapping between structures .
function mint() public onlyOwner {
currentTokenId.increment();
uint256 newItemId = currentTokenId.current();
_safeMint(owner(), newItemId);
rentables[newItemId] = RentableItem({
rentable: false,
amountPerMinute: baseAmount
});
}
By default , stay mint Function , The newly created NFT:
- It's for the owner of the contract ;
- Is set as non rentable ;
- Its cost is set to baseAmount.
function setRentFee(uint256 _tokenId, uint256 _amountPerMinute) public {
require(_isApprovedOrOwner(_msgSender(), _tokenId), "Caller is not token owner nor approved");
rentables[_tokenId].amountPerMinute = _amountPerMinute;
}
This is a NFT Of amountPerMinute Value setting method . This method can only be used by NFT Owner ( Or an approved address ) call .
function setRentable(uint256 _tokenId, bool _rentable) public {
require(_isApprovedOrOwner(_msgSender(), _tokenId), "Caller is not token owner nor approved");
rentables[_tokenId].rentable = _rentable;
}
This method will set a NFT Leasable Boolean value of . This method can only be used by NFT Owner ( Or an approved address ) call .
function rent(uint256 _tokenId, uint64 _expires) public payable virtual {
uint256 dueAmount = rentables[_tokenId].amountPerMinute * _expires;
require(msg.value == dueAmount, "Uncorrect amount");
require(userOf(_tokenId) == address(0), "Already rented");
require(rentables[_tokenId].rentable, "Renting disabled for the NFT");
payable(ownerOf(_tokenId)).transfer(dueAmount);
UserInfo storage info = _users[_tokenId];
info.user = msg.sender;
info.expires = block.timestamp + (_expires * 60);
emit UpdateUser(_tokenId, msg.sender, _expires);
}
The core rental method here :
- Calculate lease NFT The amount due
- Verify that the amount sent is correct
- verification NFT Whether it has been rented by others
- verify NFT Whether it is changed to leasable by its owner
- Transfer the amount payable to NFT owner
- Update user information and expiration time
Source:https://medium.com/coinmonks/rentable-nft-eip-4907-how-can-be-improved-b20a1b5a27bf
About
ChinaDeFi - ChinaDeFi.com It's a research driven DeFi Innovation organizations , We are also a blockchain development team . From all over the world every day 500 Close to a good source of information 900 In the content , Looking for deeper thinking 、 Sort out more systematic content , Provide decision-making assistant materials to the Chinese market at the fastest speed .
Layer 2 friends sharing same hobby - Welcome to Layer 2 Interested blockchain technology enthusiasts 、 Study and analyze people and Gavin( WeChat : chinadefi) contact , Discuss together Layer 2 Landing opportunities . Please pay attention to our official account of WeChat “ Decentralized financial community ”.

边栏推荐
- 暑期总结2
- Baidu, Alibaba, Tencent, who fell first?
- Amazoncaptcha 95%成功率绕过亚马逊IP验证码
- Microservices and related component concepts
- Unity model simplification / consolidation one click plug-in
- 对于von Mises distribution(冯·米塞斯分布)的一点心得
- R language uses rowmedians function to calculate the row data median value of all data rows in dataframe
- VO, dto, do, Po distinction and use
- Amazoncaptcha bypasses Amazon IP verification code with 95% success rate
- Microservice - hystrix fuse
猜你喜欢

HTB-Devel

Programming hodgepodge (II)

50 places are limited to open | with the news of oceanbase's annual press conference coming!
![[daily practice] day (14)](/img/83/d924fa0fc5ae01d151a0880da62f7e.png)
[daily practice] day (14)
![(16)[系统调用]追踪系统调用(3环)](/img/b0/011351361135fd9f8e2d0d31749f73.png)
(16)[系统调用]追踪系统调用(3环)

Leetcode 237. delete nodes in the linked list

Amazoncaptcha bypasses Amazon IP verification code with 95% success rate

sqlilabs less-29

Microservice configuration center Nacos

(Niuke multi School II) G-LINK with monotonic subsequence (construction question)
随机推荐
HTB-Optimum
(2022 Niuke multi school) D-Link with game glitch (SPFA)
Leetcode/ binary addition
msys2常用配置
(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
R language uses data.table function to create data.table data (use: operator to create continuous numeric vector)
Singing "Seven Mile fragrance" askew -- pay tribute to Jay
R language uses rowmedians function to calculate the row data median value of all data rows in dataframe
Programming hodgepodge (I)
50: Chapter 5: develop admin management service: 3: develop [query whether the admin user name already exists, interface]; (this interface can only be called when logging in; so we have written an int
Concepts of phase velocity and phase in transmission line theory
Please stop using system The currenttimemillis() statistical code is time-consuming, which is really too low!
Difference between NPX and NPM
The computer accesses the Internet normally with the same network cable, and the mobile phone connects to WiFi successfully, but it cannot access the Internet
【Node】服务端口被占用Error: listen EADDRINUSE: address already in use :::9000-如何关闭node启动的端口
QT qtextedit setting qscrollbar style sheet does not take effect solution
Programming hodgepodge (II)
leetcode/ 前 n 个数字二进制中 1 的个数
Leetcode 202. happy number (not happy at all)
Idea commonly used 10 shortcut keys