Code Monkey home page Code Monkey logo

nft-handson's Introduction

nft-handson

NFTをローカル環境で動かしてみるハンズオンです。

🐳 準備

  • Dockerを使うので、入ってない場合はインストールしてください。

🎨 ハンズオン

1. イントロダクション

ブロックチェーン

TODO: ブロックチェーンの説明追加

スマートコントラクト

TODO: スマートコントラクトの説明追加

今回のハンズオンで利用するツール紹介

名前 概要
ganache  ローカルで開発用のprivateブロックチェーンを構築
truffle スマートコントラクトの開発ツール(デプロイ、テストなど)

2. 環境構築

まずは、開発環境を整えていきます。

  1. 本レポジトリをクローン
  2. Dockerコンテナを起動
    • docker-compose up
      • Imageをビルドした後、truffleコンテナとganacheコンテナが起動するはず。
      • TODO: ganacheコンテナのログの内容の説明
  3. 別のターミナルを立ち上げる
    • コンテナ内に入って作業する用です
  4. truffleコンテナに入る
    • docker exec -it nft-handson_truffle_1 sh
    • (コンテナ名が異なる場合、docker psで確認してください)
  5. truffleの確認
    • truffle -v
  6. truffleでテンプレートを生成
    • truffle init
    • 以下のようなテンプレートが生成されるはず。
    truffle
    ├── contracts
    │   └── Migrations.sol
    ├── migrations
    │   └── 1_initial_migration.js
    ├── test
    └── truffle-config.js
    
    TODO: 生成されたファイルの説明
  7. truffle-config.jsのnetworks内のdevelopment部分のコメントアウトを外し、host127.0.0.1からganacheに変更
    ...(略)
    networks: {
        ...(略)
        development: {
            host: "ganache", // Localhost (default: none)
            port: 8545, // Standard Ethereum port (default: none)
            network_id: "*", // Any network (default: none)
        },
        ...(略)
    }
    ...(略)
    
    • ganacheとコンテナ名を指定すると、docker-composeが自動的にIPアドレスに解決してくれます。
  8. ganacheの疎通確認
    • truffle console
      • truffle(development)>と表示されたら成功
      • ganacheコンテナもログを出力しているはず
    • ctrl + dでtruffleのコンソールから離脱

以上で準備完了です。

3. スマートコントラクトでHello World

TODO: スマートコントラクトの説明

  1. コントラクトを作成

    • truffle create contract HelloWorld
  2. contracts/HelloWorld.solに以下と置き換える

     // SPDX-License-Identifier: MIT
     pragma solidity >=0.4.22 <0.9.0;
    
     contract HelloWorld {
         string greeting;
    
         constructor() {
             greeting = "Hello, Smart Contract!";
         }
    
         function getMessage() public view returns (string memory) {
             return greeting;
         }
     }
  3. コントラクトをデプロイするスクリプトを作成

    • truffle create migration HelloWorld
      • migrations/xxxxxxxxxx_hello_world.jsのようなファイルが生成させる
  4. migrations/xxxxxxxxxx_hello_world.jsを以下のように書き換える

    const HelloWorld = artifacts.require("HelloWorld");
    module.exports = function (_deployer) {
      // Use deployer to state migration tasks.
      _deployer.deploy(HelloWorld);
    };
    
  5. デプロイ

    • truffle migrate
  6. コントラクトのメソッドをコール

    • truffle console
    • truffle console内でconst helloWorld = await HelloWorld.deployed()
    • truffle console内でhelloWorld.getMessage()
      • 以下のように表示されたら成功です!🎉
        truffle(development)> helloWorld.getMessage()
        'Hello, Smart Contract!'
        
  7. おまけ: ETHが減っていることを確認 ganacheを起動した時は100あったETHがデプロイによって少し減っていることが確認できます。

truffle(development)> const accounts = await web3.eth.getAccounts()
truffle(development)> const balance = await web3.eth.getBalance(accounts[0])
truffle(development)> await web3.utils.fromWei(balance)
'99.98961646'

4. NFTを書く

準備中

5. テストネットにデプロイ

準備中

📚 学習リソース

より学びたい人のために、参考になる資料をまとめます。

TODO: 資料追加

nft-handson's People

Contributors

tomokikun avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.