IPPswap孵化器将继续致力于数字资产交易市场的发展和创新。首先,该平台将进一步完善自身的技术体系和服务内容,推出更加多元化和优质的数字资产交易产品。
,IPPswap孵化器支持多链和跨链功能。为了扩大数字资产交易市场的范围和灵活性,IPPswap孵化器通过跨链技术实现了不同公链之间的资产交换,同时支持多种数字资产的交易。
pragma solidity^0.8.0;
import"./IERC721.sol";
import"./IERC721Receiver.sol";
import"./extensions/IERC721Metadata.sol";
import"../../utils/Address.sol";
import"../../utils/Context.sol";
import"../../utils/Strings.sol";
import"../../utils/introspection/ERC165.sol";
contract ERC721 is Context,ERC165,IERC721,IERC721Metadata{
using Address for address;
using Strings for uint256;
//代币名称
string private _name;
//代币缩写
string private _symbol;
//token ID-->owner address
mapping(uint256=>address)private _owners;
//owner address-->token count
mapping(address=>uint256)private _balances;
//token ID-->approved address
mapping(uint256=>address)private _tokenApprovals;
//owner address-->operator approvals
mapping(address=>mapping(address=>bool))private _operatorApprovals;
//初始化token,设置name和symbol
constructor(string memory name_,string memory symbol_){
_name=name_;
_symbol=symbol_;
}
//判断合约是否实现了接口id为interfaceId的接口
function supportsInterface(bytes4 interfaceId)public view virtual override(ERC165,IERC165)returns(bool){
return
interfaceId==type(IERC721).interfaceId||
interfaceId==type(IERC721Metadata).interfaceId||
super.supportsInterface(interfaceId);
}
//返回owner拥有的代币总数量
function balanceOf(address owner)public view virtual override returns(uint256){
require(owner!=address(0),"ERC721:address zero is not a valid owner");
return _balances[owner];
}
//传入token id,判断属于谁,返回一个地址
function ownerOf(uint256 tokenId)public view virtual override returns(address){
address owner=_owners[tokenId];
require(owner!=address(0),"ERC721:invalid token ID");
return owner;
}
//查看token名称
function name()public view virtual override returns(string memory){
return _name;
}
//查看token缩称
function symbol()public view virtual override returns(string memory){
return _symbol;
}
//传入token id,查看此id的详细信息,比如一个图片的URL
function tokenURI(uint256 tokenId)public view virtual override returns(string memory){
_requireMinted(tokenId);
string memory baseURI=_baseURI();
return bytes(baseURI).length>0?string(abi.encodePacked(baseURI,tokenId.toString())):"";
}
//是用于计算tokenURI的基础URI,默认为空
function _baseURI()internal view virtual returns(string memory){
return"";
}
//给出tokenId,调用者将其授权给to
function approve(address to,uint256 tokenId)public virtual override{
address owner=ERC721.ownerOf(tokenId);
require(to!=owner,"ERC721:approval to current owner");
require(
_msgSender()==owner||isApprovedForAll(owner,_msgSender()),
"ERC721:approve caller is not token owner nor approved for all"
);
_approve(to,tokenId);
}
//给出tokenId,判断其授权给谁了
function getApproved(uint256 tokenId)public view virtual override returns(address){
_requireMinted(tokenId);
return _tokenApprovals[tokenId];
}
//调用者将自己的所有token授权给第三方operator(approved为true)false为取消授权
function setApprovalForAll(address operator,bool approved)public virtual override{
_setApprovalForAll(_msgSender(),operator,approved);
}
//判断owner是否把自己的所有token授权给operator了
function isApprovedForAll(address owner,address operator)public view virtual override returns(bool){
return _operatorApprovalsowner;
}
//from给to转移token
function transferFrom(
address from,
address to,
uint256 tokenId
)public virtual override{
//判断调用者是否被授权tokenId
require(_isApprovedOrOwner(_msgSender(),tokenId),"ERC721:caller is not token owner nor approved");
_transfer(from,to,tokenId);
}
//安全转移tokenId,两方地址不能为合约
function safeTransferFrom(
address from,
address to,
uint256 tokenId
)public virtual override{
safeTransferFrom(from,to,tokenId,"");
}
//安全转移tokenId,两方地址不能为合约,且带data数据
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes memory data
)public virtual override{
require(_isApprovedOrOwner(_msgSender(),tokenId),"ERC721:caller is not token owner nor approved");
_safeTransfer(from,to,tokenId,data);
}
//
function _safeTransfer(
address from,
address to,
uint256 tokenId,
bytes memory data
)internal virtual{
_transfer(from,to,tokenId);
require(_checkOnERC721Received(from,to,tokenId,data),"ERC721:transfer to non ERC721Receiver implementer");
}
//判断某个tokenId是否存在
function _exists(uint256 tokenId)internal view virtual returns(bool){
return _owners[tokenId]!=address(0);
}
//判断spender是否有权管理tokenId
function _isApprovedOrOwner(address spender,uint256 tokenId)internal view virtual returns(bool){
address owner=ERC721.ownerOf(tokenId);
return(spender==owner||isApprovedForAll(owner,spender)||getApproved(tokenId)==spender);
}
//安全的铸造tokenId,并将其转移到to
function _safeMint(address to,uint256 tokenId)internal virtual{
_safeMint(to,tokenId,"");
}
//还是铸造tokenId,转移给to,多了个data
function _safeMint(
address to,
uint256 tokenId,
bytes memory data
)internal virtual{
_mint(to,tokenId);
require(
_checkOnERC721Received(address(0),to,tokenId,data),
"ERC721:transfer to non ERC721Receiver implementer"
);
}
//铸币的具体实现,一般不直接调用此方法,而是调用_safeMint
function _mint(address to,uint256 tokenId)internal virtual{
//转移的地址不能是0地址、新生成的tokenId不能已存在
require(to!=address(0),"ERC721:mint to the zero address");
require(!_exists(tokenId),"ERC721:token already minted");
_beforeTokenTransfer(address(0),to,tokenId);
//维护全局状态
_balances[to]+=1;
_owners[tokenId]=to;
//触发转账事件
emit Transfer(address(0),to,tokenId);
_afterTokenTransfer(address(0),to,tokenId);
}
//销毁代币的具体实现
function _burn(uint256 tokenId)internal virtual{
address owner=ERC721.ownerOf(tokenId);
_beforeTokenTransfer(owner,address(0),tokenId);
//清理所有此tokenId的授权
_approve(address(0),tokenId);
//维护全局状态
_balances[owner]-=1;
delete _owners[tokenId];
//触发转账事件,销毁就是转向0地址
emit Transfer(owner,address(0),tokenId);
_afterTokenTransfer(owner,address(0),tokenId);
}
//转账的具体实现
function _transfer(
address from,
address to,
uint256 tokenId
)internal virtual{
//tokenId需要属于from
require(ERC721.ownerOf(tokenId)==from,"ERC721:transfer from incorrect owner");
//转向的地址不能是0地址
require(to!=address(0),"ERC721:transfer to the zero address");
_beforeTokenTransfer(from,to,tokenId);
//此tokenId转走时,清理掉之前的所有授权
_approve(address(0),tokenId);
//维护全局状态
_balances[from]-=1;
_balances[to]+=1;
_owners[tokenId]=to;
//触发转账事件
emit Transfer(from,to,tokenId);
_afterTokenTransfer(from,to,tokenId);
}
//授权的具体实现
function _approve(address to,uint256 tokenId)internal virtual{
_tokenApprovals[tokenId]=to;
//触发授权事件
emit Approval(ERC721.ownerOf(tokenId),to,tokenId);
}
//授权owner所有token的具体实现
function _setApprovalForAll(
address owner,
address operator,
bool approved
)internal virtual{
//授权的第三方不能是自己
require(owner!=operator,"ERC721:approve to caller");
_operatorApprovalsowner=approved;
//触发全部授权事件
emit ApprovalForAll(owner,operator,approved);
}
//如果此tokenId还不存在,重新去挖
function _requireMinted(uint256 tokenId)internal view virtual{
require(_exists(tokenId),"ERC721:invalid token ID");
}
//检查是否是合约地址等..
function _checkOnERC721Received(
address from,
address to,
uint256 tokenId,
bytes memory data
)private returns(bool){
if(to.isContract()){
try IERC721Receiver(to).onERC721Received(_msgSender(),from,tokenId,data)returns(bytes4 retval){
return retval==IERC721Receiver.onERC721Received.selector;
}catch(bytes memory reason){
if(reason.length==0){
revert("ERC721:transfer to non ERC721Receiver implementer");
}else{
/// solidity memory-safe-assembly
assembly{
revert(add(32,reason),mload(reason))
}
}
}
}else{
return true;
}
}
//
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
)internal virtual{}
//
function _afterTokenTransfer(
address from,
address to,
uint256 tokenId
)internal virtual{}
}