Created
February 20, 2023 23:49
-
-
Save katorie/6e233c7d1543b390acf7962488f48f07 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity >=0.8.0 <0.9.0; | |
import "@openzeppelin/contracts/token/ERC721/extension/ERC721URIStorage.sol"; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
contract SampleERC721 is ERC721URIStorage, Ownable { | |
// ERC721URIStorage, Ownable というinterfaceを継承している(多重継承? | |
// ERC721URIStorage -> tokenURI を備えたNFTの基本機能を提供してくれるコントラクト | |
// Ownable -> 実行したユーザーがコントラクトのオーナーかどうかを判定する機能を提供してくれるコントラクト。作者だけが利用できる機能を作ることができる | |
using Strings for unit256; | |
using Counters for Counters.Counter; | |
Counters.Counter private _current_token_id; | |
string base_uri = ""; | |
constructor () ERC721 ("SampleERC721", "SMP721") { | |
} | |
// コントラクトのコンストラクタを定義しているんだけど、ERC721のコンストラクタを呼び出している | |
// 第1引数はNFTの名前 | |
// 第2引数はNFTのSymbol(識別子) | |
function mint() public returns (unit256) { | |
unit256 _token_id = _current_token_id.current(); | |
_mint(msg.sender, _token_id); // 親クラスのmintメソッドを呼び出している | |
_setTokenURI(_token_id, string(abi.encodePacked(_token_id.toString(), ".json"))); | |
// ERC721Metadataの規格で定義されているtokenURIメソッドでトークンのメタデータが取得できるようになる | |
_current_token_id.increment(); // OpenZeppelinで提供されているCounters.solというライブラリで実現 | |
return _token_id; | |
} | |
function baseURI() internal view virtual override returns (string memory) { | |
return base_uri; | |
} | |
function setBaseURI(string calldata _base_uri) public { | |
require(msg.sender == owner()); | |
// require関数 -> 条件に一致しない場合にコントラクトの実行を中断する | |
base_uri = _base_uri; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment