ユーザ用ツール

サイト用ツール


blockchain:openzeppelin_で_erc721_nft_トークンの作成

文書の過去の版を表示しています。


OpenZeppelin で ERC721(NFT) トークンの作成

ほぼ OpenZeppelin オフィシャルのドキュメント通りに NFT スマートコントラクトを実装します。

前提

Truffle と npm をインストールしておいてください。

(参考) Truffleを使ったスマートコントラクト開発

プロジェクト作成

> mkdir nft_project
> cd nft_project
nft_project> truffle init
nft_project> npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
 
See `npm help init` for definitive documentation on these fields
and exactly what they do.
 
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
 
Press ^C at any time to quit.
package name: (nft_project)
version: (1.0.0)
description:
entry point: (truffle-config.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to C:\Users\miyazato\work\ethereum\nft_project\package.json:
 
{
  "name": "nft_project",
  "version": "1.0.0",
  "description": "",
  "main": "truffle-config.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
 
 
Is this OK? (yes)
 
nft_project> npm install --save-dev @openzeppelin/contracts

NFTの実装

nft_project> truffle create contract GameItem
nft_project> code contracts/GameItem.sol
// contracts/GameItem.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
import "../node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";
 
contract GameItem is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
 
    constructor() ERC722("GameItem", "ITM") {}
 
    function awardItem(address player, string memory tokenURI)
        public
        returns (uint256)
    {
        uint256 newItemId = _tokenIds.current();
        _mint(player, newItemId);
        _setTokenURI(newItemId, tokenURI);
 
        _tokenIds.increment();
        return newItemId;
    }
}
blockchain/openzeppelin_で_erc721_nft_トークンの作成.1651958988.txt.gz · 最終更新: 2022/05/07 21:29 by dot