🐡

OpenZeppelinのERC721メモ

2022/04/18に公開約4,000字

とりあえず

公式ドキュメント

https://docs.openzeppelin.com/contracts/4.x/erc721

ソースコード(github)

https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC721

クラス変数

クラス変数
    // Token name トークンの名前
    string private _name;

    // Token symbol トークンのシンボル
    string private _symbol;

    // Mapping from token ID to owner address トークンIDに対するアドレス
    mapping(uint256 => address) private _owners;

    // Mapping owner address to token count アドレスのトークン数
    mapping(address => uint256) private _balances;

    // Mapping from token ID to approved address 
    mapping(uint256 => address) private _tokenApprovals;

    // Mapping from owner to operator approvals
    mapping(address => mapping(address => bool)) private _operatorApprovals;

メソッド

supportsInterface

ERC721のInterfaceを実装しているか

/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
return
    interfaceId == type(IERC721).interfaceId ||
    interfaceId == type(IERC721Metadata).interfaceId ||
    super.supportsInterface(interfaceId);
}

balanceOf

NFTの所有数を返す

/**
* @dev See {IERC721-balanceOf}.
*/
function balanceOf(address owner) public view virtual override returns (uint256) {
	require(owner != address(0), "ERC721: balance query for the zero address");
	return _balances[owner];
}

ownerOf

NFTの所有者のアドレスを返す

/**
* @dev See {IERC721-ownerOf}.
*/
function ownerOf(uint256 tokenId) public view virtual override returns (address) {
	address owner = _owners[tokenId];
	require(owner != address(0), "ERC721: owner query for nonexistent token");
	return owner;
}

name

トークンの名前を返す

/**
* @dev See {IERC721Metadata-name}.
*/
function name() public view virtual override returns (string memory) {
	return _name;
}

symbol

トークンのsymbolを返す

/**
* @dev See {IERC721Metadata-symbol}.
*/
function symbol() public view virtual override returns (string memory) {
	return _symbol;
}

tokenURI

NFTのURIを返す

/**
* @dev See {IERC721Metadata-tokenURI}.
*/
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
	require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");

	string memory baseURI = _baseURI();
	return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";
}

baseURI

baseのURI、コントラクト作る際に書いておく

/**
* @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
* token will be the concatenation of the `baseURI` and the `tokenId`. Empty
* by default, can be overridden in child contracts.
*/
function _baseURI() internal view virtual returns (string memory) {
	return "";
}

approve

指定したアドレスに対して転送の許可を与える

/**
* @dev See {IERC721-approve}.
*/
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 owner nor approved for all"
	);

	_approve(to, tokenId);
}

getApproved

トークンIDに対しどのアドレスがapproveされているか

/**
* @dev See {IERC721-getApproved}.
*/
function getApproved(uint256 tokenId) public view virtual override returns (address) {
	require(_exists(tokenId), "ERC721: approved query for nonexistent token");

	return _tokenApprovals[tokenId];
}

setApprovalForAll

指定したアドレスへ転送の権限をすべて与える

https://qiita.com/rmanzoku/items/a90f265ae499dc21b9cb#setapprovalforall
/**
* @dev See {IERC721-setApprovalForAll}.
*/
function setApprovalForAll(address operator, bool approved) public virtual override {
	_setApprovalForAll(_msgSender(), operator, approved);
}

isApprovedForAll

ApprovedForAllの権限が与えられているかチェック

/**
* @dev See {IERC721-isApprovedForAll}.
*/
function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
	return _operatorApprovals[owner][operator];
}

Discussion

ログインするとコメントできます