PtahDao/ProTradex/Meta2032智能合約系統(tǒng)開發(fā)詳情介紹及源碼案例
區(qū)塊鏈是分布式數(shù)據(jù)存儲、點對點傳輸、共識機制、加密算法等計算機技術的新型應用模式。
狹義來講,區(qū)塊鏈是一種按照時間順序將數(shù)據(jù)區(qū)塊以順序相連的方式組合成的一種鏈式數(shù)據(jù)結構,并以密碼學方式保證的不可篡改和不可偽造的分布式賬本。
廣義來講,區(qū)塊鏈技術是利用塊鏈式數(shù)據(jù)結構來驗證與存儲數(shù)據(jù)、利用分布式節(jié)點共識算法來生成和更新數(shù)據(jù)、利用密碼學的方式保證數(shù)據(jù)傳輸和訪問的安全、利用由自動化腳本代碼組成的智能合約來編程和操作數(shù)據(jù)的一種全新的分布式基礎架構與計算方式。
在區(qū)塊鏈技術中的數(shù)據(jù)有一定的順序性,搭建詳細:I35模式7O98開發(fā)O7I8 每個數(shù)據(jù)區(qū)塊都有一個“哈希值”代碼,在鏈狀數(shù)據(jù)結構中,任意區(qū)塊中的數(shù)據(jù)改變都會影響后續(xù)與之相關所有區(qū)塊的信息變化。這一技術確保區(qū)塊鏈上的每個區(qū)塊數(shù)據(jù)都不能隨意被篡改、刪除或破壞。因此,區(qū)塊鏈技術在保證電子檔案完整、真實的基礎上還具有較強的追溯性
//SPDX-License-Identifier:MIT
pragma solidity^0.8.0;
import"openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import"./IERC4907.sol";
contract ERC4907 is ERC721URIStorage,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 override{
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
override
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
override
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);
}
}
}