メインコンテンツまでスキップ

3 ERC721

もっと詳細な解説はこちらを読んでみてください。)

こちらのコントラクトは簡易的な ERC721 コントラクトの内容である。ここでは uri 関連の関数がないが、基本的な関数がすべて備えている立派な NFT である。こちらの関数を remix でデプロイして色々遊んで見てください。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

interface IERC165 {
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}

interface IERC721 is IERC165 {
function balanceOf(address owner) external view returns (uint balance);

function ownerOf(uint tokenId) external view returns (address owner);

function safeTransferFrom(
address from,
address to,
uint tokenId
) external;

function safeTransferFrom(
address from,
address to,
uint tokenId,
bytes calldata data
) external;

function transferFrom(
address from,
address to,
uint tokenId
) external;

function approve(address to, uint tokenId) external;

function getApproved(uint tokenId) external view returns (address operator);

function setApprovalForAll(address operator, bool _approved) external;

function isApprovedForAll(address owner, address operator)
external
view
returns (bool);
}

interface IERC721Receiver {
function onERC721Received(
address operator,
address from,
uint tokenId,
bytes calldata data
) external returns (bytes4);
}

contract ERC721 is IERC721 {
event Transfer(address indexed from, address indexed to, uint indexed id);
event Approval(address indexed owner, address indexed spender, uint indexed id);
event ApprovalForAll(
address indexed owner,
address indexed operator,
bool approved
);

// token ID -> 所有者のaddressのMapping変数
mapping(uint => address) internal _ownerOf;

// 所有者のaddress -> token countのMapping変数
mapping(address => uint) internal _balanceOf;

// token ID -> アプルーブされたaddress
mapping(uint => address) internal _approvals;

// owner -> operatorかどうかを判断するMapping
mapping(address => mapping(address => bool)) public isApprovedForAll;

// interfaceがsupportされるかどうかをチェックするための関数
function supportsInterface(bytes4 interfaceId) external pure returns (bool) {
return
interfaceId == type(IERC721).interfaceId ||
interfaceId == type(IERC165).interfaceId;
}

// tokenのownerを返す関数
function ownerOf(uint id) external view returns (address owner) {
owner = _ownerOf[id];
require(owner != address(0), "token doesn't exist");
}

// とあるアドレスの持っているトークン数を返す
function balanceOf(address owner) external view returns (uint) {
require(owner != address(0), "owner = zero address");
return _balanceOf[owner];
}

// 全部のNFTの管理権限をするアカウントをリボークあるいセットする
function setApprovalForAll(address operator, bool approved) external {
isApprovedForAll[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}

// idのtokenについて権限を持つアカウントをセットする
function approve(address spender, uint id) external {
address owner = _ownerOf[id];
require(
msg.sender == owner || isApprovedForAll[owner][msg.sender],
"not authorized"
);

_approvals[id] = spender;

emit Approval(owner, spender, id);
}

// 管理権限を持つアカウントを返す
function getApproved(uint id) external view returns (address) {
require(_ownerOf[id] != address(0), "token doesn't exist");
return _approvals[id];
}

// 管理権限を持つあるいはオーナーであるかを確認する関数
function _isApprovedOrOwner(
address owner,
address spender,
uint id
) internal view returns (bool) {
return (spender == owner ||
isApprovedForAll[owner][spender] ||
spender == _approvals[id]);
}

// fromからtoへtoken idを送る
function transferFrom(
address from,
address to,
uint id
) public {
require(from == _ownerOf[id], "from != owner");
require(to != address(0), "transfer to zero address");

require(_isApprovedOrOwner(from, msg.sender, id), "not authorized");

_balanceOf[from]--;
_balanceOf[to]++;
_ownerOf[id] = to;

delete _approvals[id];

emit Transfer(from, to, id);
}

// transferするとともに、送り先がERC721を受信できるかをチェックする。そうでない場合は送らないようにした(dataがない)
function safeTransferFrom(
address from,
address to,
uint id
) external {
transferFrom(from, to, id);

require(
to.code.length == 0 ||
IERC721Receiver(to).onERC721Received(msg.sender, from, id, "") ==
IERC721Receiver.onERC721Received.selector,
"unsafe recipient"
);
}

// 上記と同じ(dataがある)
function safeTransferFrom(
address from,
address to,
uint id,
bytes calldata data
) external {
transferFrom(from, to, id);

require(
to.code.length == 0 ||
IERC721Receiver(to).onERC721Received(msg.sender, from, id, data) ==
IERC721Receiver.onERC721Received.selector,
"unsafe recipient"
);
}

// internalなmint関数。toへid tokenをmintする
function _mint(address to, uint id) internal {
require(to != address(0), "mint to zero address");
require(_ownerOf[id] == address(0), "already minted");

_balanceOf[to]++;
_ownerOf[id] = to;

emit Transfer(address(0), to, id);
}

// burn関数。ownerだけがNFTの所有者などを削除する
function _burn(uint id) internal {
address owner = _ownerOf[id];
require(owner != address(0), "not minted");

_balanceOf[owner] -= 1;

delete _ownerOf[id];
delete _approvals[id];

emit Transfer(owner, address(0), id);
}
}

// NFTの本体コントラクト、ERC721を継承する
contract MyNFT is ERC721 {
// mint関数、_mintを呼び出す。特にここでは制限などをしていないが、本当ならホワイトリストなどの制限がかかるだろう
function mint(address to, uint id) external {
_mint(to, id);
}

// burn関数。_burnを呼び出す
function burn(uint id) external {
require(msg.sender == _ownerOf[id], "not owner");
_burn(id);
}
}

Remixで試す