DAPP开发【05】ERC20/ERC721简介
ERC20是以太坊上最受欢迎和广泛使用的代币标准之一。ERC20代币符合一组通用规则,包括代币的转账方法和余额查询方法。这些规则使得 ERC20 代币易于与钱包、交易所和其他合约进行集成和交互。
ERC20标准规定了代币合约必须实现以下6个函数:
-
balanceOf(address _owner)
:获取给定地址的代币余额。 -
transfer(address _to, uint256 _value)
:将代币从一个地址转移到另一个地址。 -
allowance(address _owner, address _spender)
:返回给定地址的可转移余额。 -
approve(address _spender, uint256 _value)
:允许另一个地址花费指定数量的代币。 -
transferFrom(address _from, address _to, uint256 _value)
:从一个地址转移代币到另一个地址,前提是交易需要经过第三方的批准。 -
totalSupply()
:获取代币的总供应量。
ERC20代币合约必须遵守并实现这些函数,以确保它们与其他代币和钱包兼容。此外,ERC20代币合约还必须包含代币的名称、符号和小数位数等元数据。
任何人都可以创建自己的ERC20代币,并将其用于支付、投票或其他用途。同时, ERC20代币也很容易与其他代币和钱包进行交互。
ERC20代币具有以下属性:
-
总供应量(Total Supply): 该代币发行的总数量。
-
名称(Name): 代币的名称,例如“以太币”。
-
符号(Symbol): 代币的简称,例如“ETH”。
-
小数位数(Decimals): 代币可以拆分的最小单位,例如“18”,代表代币可以被拆分为10的18次方的最小单位。
-
余额(Balance): 代币持有人的余额。
-
转移记录(Transfer Record): 代币的转移记录,包括发送、接收方、数量和时间。
-
批准记录(Approval Record): 代币的批准记录,包括批准者、受批准者和数量。
-
总供应限制(Total Supply Limit): 可以控制代币的总供应量,可以选择是否限制代币的总供应量。
以上属性可以在ERC20代币合约中进行定义和设置,以确保标准化和互操作性。
ERC721是一种以太坊上的代币标准,用于创建不可替代的代币。与ERC20代币不同,每个ERC721代币都是独一无二的,因此它们通常用于代表唯一的资产,如游戏中的虚拟物品、数字艺术品、房屋和土地证书等。
ERC721代币具有以下属性:
-
每个ERC721代币都有一个独一无二的标识符(Token ID),通过这个标识符可以唯一地标识每个代币。
-
ERC721代币不可替代,即每个代币都是唯一的,不能被其他代币所替代。
-
每个ERC721代币都可以被拥有者进行转移,就像普通的代币一样。
-
ERC721合约必须实现一组标准接口,包括
balanceOf
、ownerOf
、approve
、transferFrom
和safeTransferFrom
,以便于与其他合约进行交互和转移代币。 -
ERC721代币可以被继承和扩展,以实现更复杂的功能,例如用于游戏的虚拟物品。
下面是一个以太坊智能合约的示例代码,用于实现一个简单的ERC721代币合约:
pragma solidity ^0.8.0;
contract MyNFT {
// ERC721 标准
string public constant name = "My NFT";
string public constant symbol = "MYNFT";
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
mapping(uint256 => address) private _tokenOwner;
mapping(uint256 => address) private _tokenApprovals;
mapping(address => uint256) private _ownedTokensCount;
function balanceOf(address owner) public view returns (uint256) {
require(owner != address(0), "ERC721: balance query for the zero address");
return _ownedTokensCount[owner];
}
function ownerOf(uint256 tokenId) public view returns (address) {
address owner = _tokenOwner[tokenId];
require(owner != address(0), "ERC721: owner query for nonexistent token");
return owner;
}
function approve(address to, uint256 tokenId) public {
address owner = ownerOf(tokenId);
require(to != owner, "ERC721: approval to current owner");
require(msg.sender == owner || isApprovedForAll(owner, msg.sender),
"ERC721: approve caller is not owner nor approved for all"
);
_tokenApprovals[tokenId] = to;
emit Approval(owner, to, tokenId);
}
function getApproved(uint256 tokenId) public view returns (address) {
require(_exists(tokenId), "ERC721: approved query for nonexistent token");
return _tokenApprovals[tokenId];
}
function setApprovalForAll(address operator, bool approved) public {
require(operator != msg.sender, "ERC721: approve to caller");
_operatorApprovals[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}
function isApprovedForAll(address owner, address operator) public view returns (bool) {
return _operatorApprovals[owner][operator];
}
function transferFrom(address from, address to, uint256 tokenId) public {
require(_isApprovedOrOwner(msg.sender, tokenId), "ERC721: transfer caller is not owner nor approved");
_transfer(from, to, tokenId);
}
function safeTransferFrom(address from, address to, uint256 tokenId) public {
safeTransferFrom(from, to, tokenId, "");
}
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public {
require(_isApprovedOrOwner(msg.sender, tokenId), "ERC721: transfer caller is not owner nor approved");
_safeTransfer(from, to, tokenId, _data);
}
// 扩展功能
function mint(address to, uint256 tokenId) public {
require(to != address(0), "ERC721: mint to the zero address");
require(!_exists(tokenId), "ERC721: token already minted");
_tokenOwner[tokenId] = to;
_ownedTokensCount[to] += 1;
emit Transfer(address(0), to, tokenId);
}
function _exists(uint256 tokenId) internal view returns (bool) {
address owner = _tokenOwner[tokenId];
return owner != address(0);
}
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
require(_exists(tokenId), "ERC721: operator query for nonexistent token");
address owner = ownerOf(tokenId);
return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
}
function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal {
_transfer(from, to, tokenId);
require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
}
function _transfer(address from, address to, uint256 tokenId) internal {
require(ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
require(to != address(0), "ERC721: transfer to the zero address");
_tokenApprovals[tokenId] = address(0);
_ownedTokensCount[from] -= 1;
_ownedTokensCount[to] += 1;
_tokenOwner[tokenId] = to;
emit Transfer(from, to, tokenId);
}
function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data) internal returns (bool) {
if (isContract(to)) {
try IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data) returns (bytes4 retval) {
return retval == IERC721Receiver(to).onERC721Received.selector;
} catch (bytes memory reason) {
if (reason.length == 0) {
revert("ERC721: transfer to non ERC721Receiver implementer");
} else {
assembly {
revert(add(32, reason), mload(reason))
}
}
}
} else {
return true;
}
}
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly { size := extcodesize(account) }
return size > 0;
}
}
interface IERC721Receiver {
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
这个示例代码中包含了ERC721代币标准的主要功能,包括标识符、转移、批准、所有者等。此外,它还包括了一些扩展功能,例如 mint
函数用于发行新的代币。请注意,这个示例代码并不完整,你需要根据自己的需求进行修改和扩展。