智能合约简单说,它是一种用计算机语言取代法律语言去记录条款的合约,可以由一个计算系统自动执行。
更简单地说,智能合约就是传统合约的数字化版本。
如果说区块链是一个数据库,智能合约就是能够使区块链技术应用到现实当中的应用层。
智能合约是在区块链数据库上运行的计算机程序,可以在满足其源代码中写入的条件时自行执行。
function _transfer(
address from,
address to,
uint256 tokenId
)internal virtual{
//前置检查:被转移的代币属于from用户
require(ERC721.ownerOf(tokenId)==from,"ERC721:transfer from incorrect owner");
//前置检查:接收代币的用户to不能为零地址
require(to!=address(0),"ERC721:transfer to the zero address");
//钩子函数:代币转移前触发,官方实现无做实际操作,如果业务有特殊的逻辑,我理解可重写_beforeTokenTransfer函数
_beforeTokenTransfer(from,to,tokenId);
//清除之前的授权—————指向零地址
_approve(address(0),tokenId);
//from账号代币数-1
_balances[from]-=1;
//to账号代币数+1
_balances[to]+=1;
//执行代币的转移,即更换owner地址
_owners[tokenId]=to;
//触发转移事件
emit Transfer(from,to,tokenId);
//钩子函数:代币转移后触发,官方实现无做实际操作
_afterTokenTransfer(from,to,tokenId);
}
/**
*dev See{IERC721-safeTransferFrom}.
*安全代币转移,真实中使用该函数比较合适。
*/
function safeTransferFrom(
address from,
address to,
uint256 tokenId
)public virtual override{【更全面的开发源码搭建可看我昵称】
safeTransferFrom(from,to,tokenId,"");
}
/**
*dev See{IERC721-safeTransferFrom}.
*安全转移函数,携带回调数据_data
*/
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes memory _data
)public virtual override{
//前置检查:函数调用者是tokenID的owner或者拥有操作权限
require(_isApprovedOrOwner(_msgSender(),tokenId),"ERC721:transfer caller is not owner nor approved");
//内部函数,执行真正的安全转移
_safeTransfer(from,to,tokenId,_data);
}
/**
*dev Safely transferstokenId
token fromfrom
toto
,checking first that contract recipients
*are aware of the ERC721 protocol to prevent tokens from being forever locked.
*_data
is additional data,it has no specified format and it is sent in call toto
.
*This internal function is equivalent to{safeTransferFrom},and can be used to e.g.
*implement alternative mechanisms to perform token transfer,such as signature-based.
*
*Requirements:
*-from
cannot be the zero address.
*-to
cannot be the zero address.
*-tokenId
token must exist and be owned byfrom
.