Skip to main content

はじめに

この記事では、QuaiネットワークのOrchard Testnetで完全に機能するNFT dAppを構築してデプロイする方法を説明します。

前提条件

コントラクトをデプロイしてWebアプリフロントエンドを構築するには、いくつかのツールキットと依存関係が必要です。使用する主な依存関係の概要は以下の通りです。

Walkthrough

Today we’re going to be building an NFT dApp on QUAI complete with a web interface for interacting with it and managing some of the properties like mint price and total supply. To have the best experience when following this guide we recommend you use the Chrome browser on either MacOS or Linux. Additionally, download and install VSCode if you don’t already have it as we’ll be working both in the terminal and editing code directly in order to build this app. Lastly, we’ll be building our webapp using Node.js so it will be important to have that installed and setup ahead of time.

Get a Quai Wallet with Funds

In order to build this app you will need to have a wallet with some funds in it. You can get the Pelagus wallet at pelaguswallet.io and acquire some testnet Quai from the faucet.

Setup The Development Environment

Let’s make a directory for our development work.
Now let’s initialize our new Next.js app. Run the following command which asks you a few questions. You can use the values below.
npxcreate

Building and Deploying the NFT Smart Contract

Once the Next.js app is created we can open the quai-nft-dapp folder in VSCode and run a new terminal. For building and deploying the NFT smart contract on Quai, we’ll use the official hardhat-example repo as a reference. The reference repo on github has solidity examples for deployment. You can follow along by referencing the Solidity folder in the hardhat-example repo.

Install Dependencies

In order to build and deploy our smart contract we’ll need a few more dependencies. First we’ll install @openzeppelin/contracts which provides helper functions for building standard ERC721 (NFT) smart contracts.
Next we’ll install dotenv which will allow us to configure our app via environment variables.
Additionally, we’ll leverage hardhat for compiling and deploying our smart contracts.
Lastly we’ll install quais.js which is the ethers-like SDK for building on Quai Network.

Initialize & Setup Hardhat

To initialize our new hardhat project we will run the following, selecting defaults when asked.
npxhardhat This command creates a basic folder structure for hardhat and a few examples for us. We will need to configure hardhat to deploy our smart contract using .env.local environment variables.
Be sure to save your changes along the way throughout this walkthrough.
In the root folder find hardhat.config.js and replace it with the following:
hardhat.config.js
これにより、後でコントラクトをデプロイするためにQuaiネットワークについて知る必要があることをhardhatに通知します。次に設定する環境変数に依存します。 VSCodeのフォルダ構造のルートに.env.localファイルを作成し、正しい情報を入力します。
.env.local
CYPRUS1_PKINITIAL_OWNERの値を、それぞれQuaiウォレットの秘密鍵と公開鍵に置き換えます。Pelagusを使用している場合は、公開鍵をコピーしてINITIAL_OWNERを置き換えることができます。秘密鍵をエクスポートして、それでCYPRUS1_PKを置き換える必要があります。 pelaguspriv 次に、コントラクトをデプロイするためのスクリプトを作成する必要があります。
これは、後でスマートコントラクトをデプロイするために呼び出すスクリプトです。
ルートディレクトリにscriptsというフォルダを作成し、その中にdeployERC721.jsというファイルを作成します。deployERC721.jsに以下を入力します。
scripts/deployERC721.js
ここで、コンパイル時にhardhatが取り込まないように、contracts/フォルダ下のLock.solファイルを削除しましょう。

スマートコントラクトの作成

contractsフォルダにERC721.solファイルを作成し、以下を入力します:
contracts/ERC721.sol
このコントラクトには3つのパブリック変数があります:tokenIds、mintPrice、supply。これらはコントラクトがデプロイされた後、コントラクト所有者またはINITIAL_OWNERによって変更可能です。初期tokenIdは0、mintPriceは5 Quai、総供給量は10,000です。 NFTコントラクトの名前とシンボルは、SolidityコードにTestERC721TNFTとしてハードコードされています。 コントラクト所有者のみが利用できる関数があり、mintPriceとsupplyを更新できます。さらに、NFTがミントされるにつれて、コントラクトの残高が増加します。所有者がコントラクトからこれらの資金を引き出すためのwithdraw関数も提供しています。

スマートコントラクトのコンパイルとデプロイ

これがすべて完了したら、スマートコントラクトをコンパイルできます。
hardhatcompile すべてが正しく設定されていれば、deployERC721.jsスクリプトを実行してQuaiにスマートコントラクトをデプロイできます:
hardhatdeploy このスクリプトが最初に行うことは、Quaiネットワーク上でトランザクションをブロードキャストすることです。結果を受け取るにはトランザクションがマイニングされるまで待つ必要がありますが、orchard.quaiscan.ioでトランザクションハッシュを監視できます。 Quaiscanで表示されたハッシュを検索してください。成功すると、スクリプトはコントラクトアドレスを返します。 deployfinished このコントラクトアドレスをメモしてください。後でアプリを構築して対話するために必要になります。.env.localNEXT_PUBLIC_DEPLOYED_CONTRACTとして保存してください。 contractenv orchard.quaiscan.ioでアドレスを検索することで、このコントラクトの詳細を確認することもできます。

dAppの構築

QuaiブロックチェーンにNFTコントラクトができたので、それと対話できるWebインターフェースを構築します。 まず、ユーザーインターフェースを見栄え良くし、ユーザーがスマートコントラクトと対話する際に素敵なトースト通知を表示できるようにする追加の依存関係をインストールしましょう。2番目のコマンドでは、chakra cliをインストールするかどうか尋ねられます。Enterキーを押して続行してください。
このアプリはプロジェクトのsrcフォルダに構築します。始めるために、以下のコードで以下のファイルを作成してください。 src/app/providers.tsxファイルとstore.tsxファイルが必要です。これらを使用してアプリ全体でウォレットの状態を管理します。
src/app/store.tsx
src/app/providers.tsx
TypeScriptを使用するアプリがより詳細を理解できるように、src/app/additional.d.tsに追加の型を追加します。
src/app/additional.d.ts
アプリがユーザーのウォレットと対話するためのユーティリティを以下のファイルで定義します。途中でいくつかのフォルダを作成する必要があるかもしれません。
src/components/utils.ts
src/components/wallet/requestAccounts.ts
src/components/wallet/useGetAccounts.ts
src/components/wallet/index.ts
最後に、アプリのメインページの構築を開始できます。
src/app/layout.tsx
src/app/connectButton.tsx
src/app/page.tsx

アプリの実行

すべての変更を保存したら、ターミナルでnpm run devを実行し、ブラウザでhttps://localhost:3000にアクセスしてアプリを表示できます! runningapp アプリをデプロイしたウォレットで接続すると、資金の引き出し、供給量またはミント価格の更新オプションが表示されます。他のすべてのウォレットはNFTのミントのみ許可されます。アプリは他のウォレットにはこの追加機能を表示せず、スマートコントラクトは所有者のみがこれらの機能を実行できるように記述されています。

まとめ

おめでとうございます!Quaiで最初のNFTアプリを構築しました! このプロジェクトの完全なリポジトリを表示したい場合は、チュートリアルのリポジトリを参照してください。

次のステップ

さらに構築を続けて機能を追加したい場合、このプロジェクトをさらに発展させるためのアイデアをいくつか紹介します。
  • NFTのアートを作成する
    • メタデータが設定されたら、NFTマーケットプレイスやQuaiscanで表示できるNFTのアートを作成して公開できます
  • NFTのメタデータを生成する
    • デフォルトでは、アプリはすべてのミントされたNFTのtokenuriとしてhttps://example.comを使用します。これはpage.tsxの29行目で設定されています。この動作を変更して、自分のサイトまたはIPFSでホストしているJSONファイルを指すようにできます。
  • QuaiMarkでNFTをローンチする!
    • QuaiMarkはQuai Network上のNFTマーケットプレイスで、人々が購入、販売、取引可能なNFTを見に来る場所です。プロジェクトの準備ができたと思ったら、Quaimark.comにアクセスして確認してください!