文章目录
- 1、非同质化货币(NFT)- 维基百科
- 2、IERC165
- 3、IERC721
- 4、IERC721Receiver
- 5、IERC721Metadata
- 6、ERC721
- 7、ERC721 NFT 的实现
- 8、编译部署
1、非同质化货币(NFT)- 维基百科
非同质化代币(英语:Non-Fungible Token,简称:NFT),是一种众筹扶持项目方的方式,也是区块链(数位账本)上的一种数据单位,每个代币可以代表一个独特的数字资料,作为虚拟商品所有权的电子认证或证书。由于其不能互换的特性,非同质化代币可以代表数字资产,如画作、艺术品、声音、影片、游戏中的项目或其他形式的创意作品。虽然作品本身是可以无限复制的,但这些代表它们的代币在其底层区块链上能被完整追踪,故能为买家提供所有权证明。诸如以太币、比特币等加密货币都有自己的代币标准以定义对NFT的使用。
2、IERC165
ERC165 是一个非常简单的以太坊标准,主要用于检测该合约是否支持查询的接口。调用者只需要传入想要检测的接口的ID(如ERC-165的ID是0x01ffc9a7),该函数以布尔值的方式(true支持,false不支持)告诉调用者该合约是否实现了这个接口。
IERC165 是 ERC165 标准的接口合约,规定了ERC165要实现的基本函数 supportsInterface
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;interface IERC165 {// ERC165 代币标准就是检查一个智能合约是不是支持了例如 ERC721 ,ERC1155 的接口。 // 如果支持,返回truefunction supportsInterface(bytes4 interfaceId) external view returns (bool);
}
3、IERC721
IERC721 是 ERC721 标准的接口规范,它定义和规范了一个标准 ERC721 代币合约应该实现的功能。这里让 ERC721 合约直接继承自 IERC721() 接口。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;import "./IERC165.sol";// ERC721标准接口.
interface IERC721 is IERC165 {// 转账时触发event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);// 授权时触发 event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);// 在批量授权时触发 event ApprovalForAll(address indexed owner, address indexed operator, bool approved);// 返回某地址的NFT持有量balancefunction balanceOf(address owner) external view returns (uint256 balance);// 返回某 tokenId 的主人 ownerfunction ownerOf(uint256 tokenId) external view returns (address owner);// 实现了 ERC721Receiver 接口的安全转账和重载函数 function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;function safeTransferFrom(address from,address to,uint256 tokenId) external;// 普通转账,参数为转出地址from,接收地址to和tokenId。function transferFrom(address from,address to,uint256 tokenId) external;// 授权NFT给另外一个地址 tofunction approve(address to, uint256 tokenId) external;// 持有的该系列NFT批量授权给某个地址operatorfunction setApprovalForAll(address operator, bool _approved) external;// 查询tokenId被批准给了哪个地址。function getApproved(uint256 tokenId) external view returns (address operator);// 查询某地址的NFT是否批量授权给了另一个operator地址function isApprovedForAll(address owner, address operator) external view returns (bool);
}
4、IERC721Receiver
为了防止 NFT 转入黑洞合约,ERC721 实现了 safeTransferFrom() 安全转账函数,目标合约必须实现了 IERC721Receiver 接口才能接收ERC721代币,不然会 revert 。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;interface IERC721Receiver {function onERC721Received(address operator,address from,uint tokenId,bytes calldata data) external returns (bytes4);
}
5、IERC721Metadata
IERC721Metadata 是 ERC721 的拓展接口,它定义了合约的元数据信息,包括合约名字、标志以及每个代币的 tokenURI。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;interface IERC721Metadata {// 代币名称function name() external view returns (string memory);// 代币符号 function symbol() external view returns (string memory);// 通过 tokenId 查询 metadata 的链接urlfunction tokenURI(uint256 tokenId) external view returns (string memory);
}
6、ERC721
ERC-721 - 非同质化代币标准,主要用于发行独一无二的代币化资产如加密收藏品、游戏装备等。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;import "./IERC165.sol";
import "./IERC721.sol";
import "./IERC721Receiver.sol";
import "./IERC721Metadata.sol";
import "./Address.sol";
import "./Strings.sol";contract ERC721 is IERC721, IERC721Metadata {using Address for address; // 使用Address库,用isContract来判断地址是否为合约using Strings for uint256; // 使用String库,// Token名称 string public override name;// Token代号 string public override symbol;// tokenId 到 owner address 的持有人映射mapping(uint => address) private _owners;// address 到持仓数量的持仓量映射mapping(address => uint) private _balances;// tokenID 到授权地址的授权映射mapping(uint => address) private _tokenApprovals;// owner地址。到operator地址的批量授权映射mapping(address => mapping(address => bool)) private _operatorApprovals;// 构造函数,初始化`name` 和`symbol` .constructor(string memory name_, string memory symbol_) {name = name_;symbol = symbol_;}// 实现IERC165接口 supportsInterfacefunction supportsInterface(bytes4 interfaceId) external pure override returns (bool) {return interfaceId == type(IERC721).interfaceId ||interfaceId == type(IERC165).interfaceId ||interfaceId == type(IERC721Metadata).interfaceId;}// 实现IERC721的balanceOf,利用_balances变量查询owner地址的balance。function balanceOf(address owner) external view override returns (uint) {require(owner != address(0), "owner = zero address");return _balances[owner];}// 实现IERC721的ownerOf,利用_owners变量查询tokenId的owner。function ownerOf(uint tokenId) public view override returns (address owner) {owner = _owners[tokenId];require(owner != address(0), "token doesn't exist");} // 实现IERC721的isApprovedForAll,利用_operatorApprovals变量查询owner地址是否将所持NFT批量授权给了operator地址。function isApprovedForAll(address owner, address operator) external view override returns (bool) {return _operatorApprovals[owner][operator];}// 实现IERC721的setApprovalForAll,将持有代币全部授权给operator地址。function setApprovalForAll(address operator, bool approved) external override {_operatorApprovals[msg.sender][operator] = approved;emit ApprovalForAll(msg.sender, operator, approved);}// 实现IERC721的getApproved,利用_tokenApprovals变量查询tokenId的授权地址。function getApproved(uint tokenId) external view override returns (address) {require(_owners[tokenId] != address(0), "token doesn't exist");return _tokenApprovals[tokenId];}// 授权函数。通过调整_tokenApprovals来,授权 to 地址操作 tokenId,同时释放Approval事件。function _approve(address owner, address to, uint tokenId) private {_tokenApprovals[tokenId] = to;emit Approval(owner, to, tokenId);}// 实现IERC721的approve,将tokenId授权给 to 地址。function approve(address to, uint tokenId) external override {address owner = _owners[tokenId];require(msg.sender == owner || _operatorApprovals[owner][msg.sender],"not owner nor approved for all");_approve(owner, to, tokenId);}// 查询 spender地址是否可以使用tokenId(他是owner或被授权地址)。function _isApprovedOrOwner(address owner, address spender, uint tokenId) private view returns (bool) {return (spender == owner ||_tokenApprovals[tokenId] == spender ||_operatorApprovals[owner][spender]);}// 转账函数function _transfer( address owner, address from, address to, uint tokenId) private {require(from == owner, "not owner");require(to != address(0), "transfer to the zero address");_approve(owner, address(0), tokenId); // 清空token的授权 _balances[from] -= 1;_balances[to] += 1;_owners[tokenId] = to;emit Transfer(from, to, tokenId);}// 实现IERC721的transferFrom,非安全转账,不建议使用。调用_transfer函数function transferFrom(address from, address to, uint tokenId) external override {address owner = ownerOf(tokenId);require(_isApprovedOrOwner(owner, msg.sender, tokenId),"not owner nor approved");_transfer(owner, from, to, tokenId);}// 安全转账,安全地将 tokenId 代币从 from 转移到 tofunction _safeTransfer( address owner, address from, address to, uint tokenId, bytes memory _data ) private {_transfer(owner, from, to, tokenId);require(_checkOnERC721Received(from, to, tokenId, _data), "not ERC721Receiver");}// 实现IERC721的safeTransferFrom,安全转账,调用了_safeTransfer函数。function safeTransferFrom(address from, address to, uint tokenId, bytes memory _data) public override {address owner = ownerOf(tokenId);require(_isApprovedOrOwner(owner, msg.sender, tokenId), "not owner nor approved");_safeTransfer(owner, from, to, tokenId, _data);}// safeTransferFrom重载函数function safeTransferFrom( address from, address to, uint tokenId) external override {safeTransferFrom(from, to, tokenId, "");}// 铸造函数。通过调整_balances和_owners变量来铸造tokenId并转账给 to,同时释放Transfer事件。。function _mint(address to, uint tokenId) internal virtual {require(to != address(0), "mint to zero address");require(_owners[tokenId] == address(0), "token already minted");_balances[to] += 1;_owners[tokenId] = to;emit Transfer(address(0), to, tokenId);}// 销毁函数,通过调整_balances和_owners变量来销毁tokenId,同时释放Transfer事件。function _burn(uint tokenId) internal virtual {address owner = ownerOf(tokenId);require(msg.sender == owner, "not owner of token");_approve(owner, address(0), tokenId);_balances[owner] -= 1;delete _owners[tokenId];emit Transfer(owner, address(0), tokenId);}// 调用IERC721Receiver-onERC721Received, 以防 tokenId 被不小心转入黑洞。function _checkOnERC721Received(address from, address to, uint tokenId, bytes memory _data) private returns (bool) {if (to.isContract()) {// 是否实现了 IERC721Receiver接口return IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data) == IERC721Receiver.onERC721Received.selector;} else {return true;}}// 实现IERC721Metadata的tokenURI函数,查询metadata。function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {require(_owners[tokenId] != address(0), "Token Not Exist");string memory baseURI = _baseURI();return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";}// 计算{tokenURI}的BaseURI,tokenURI就是把baseURI和tokenId拼接在一起,需要开发重写。function _baseURI() internal view virtual returns (string memory) {return "";}
}
7、ERC721 NFT 的实现
再写一个合约继承 ERC721 , 并写好 MAX_APES 状态变量、构造函数、baseURI 函数以及锻造 mint 函数。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;import "./ERC721.sol";contract NFTSang is ERC721 {uint public MAX_APES = 10000; // 总量// 构造函数constructor(string memory name_, string memory symbol_) ERC721(name_, symbol_){}// ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariAR3jxcaWtq/function _baseURI() internal pure override returns (string memory) {return "ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariAR3jxcaWtq/";}// 铸造函数function mint(address to, uint tokenId) external {require(tokenId >= 0 && tokenId < MAX_APES, "tokenId out of range");_mint(to, tokenId);}
}
8、编译部署
redmix 可以直接编译运行。
免费锻造代币,正式使用需要开发者继续完善,比如规定哪些人有锻造此代币的权限。