0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Dapps開発でコントラクトをMumbaiにデプロイするまでの流れのカンペ(仮)

Last updated at Posted at 2023-06-15

Solidityで以下の要件を満たすコントラクトを記述し、mumbaiにデプロイするまでの流れ。

要件:
・コントラクト名「SuperCoin」
・シンボル「SPC」
・SPCをアドレスからアドレスへ移転できる
・当然、自分の保持するSPCしか移転権限を持たない
・アドレスが保持するSPCを確認できる、確認は誰でも可能
・移転がイベントとして記録される
・SPC発行上限は10000
・初めはデプロイヤーが10000SPC保持

流れ:

  1. Hardhat初期設定
  2. コントラクト定義
  3. コンパイル
  4. デプロイスクリプト作成
  5. ローカルチェーンへデプロイ
  6. mumbaiへデプロイ
  7. Verify

1. Hardhat初期設定

ディレクトリを作成し、そのディレクトリでHardhatを初期設定。

mkdir SuperCoin
cd SuperCoin
npx hardhat

2. コントラクト定義

contractsディレクトリにSuperCoin.solという名前でコントラクトを作成。

contracts/SuperCoin.sol
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SuperCoin is ERC20 {
    constructor() ERC20("SuperCoin", "SPC") {
        _mint(msg.sender, 10000 * 10 ** decimals());
    }
}

※ERC20の継承で以下の機能が実装される

  • transfer: トークンをアドレスからアドレスへ転送する関数
  • balanceOf: 特定のアドレスが保有するトークン数を返す関数、誰でも任意のアドレスのトークン数を参照可
  • Transferイベント: transfer実行でトリガーされるイベント

3. コンパイル

コントラクトをコンパイル。

npx hardhat compile

4. デプロイスクリプト作成

scriptsディレクトリを作成し、その中にdeploy.jsを作成。

scripts/deploy.js
async function main() {
    const [deployer] = await ethers.getSigners();
    const Token = await ethers.getContractFactory("SuperCoin");
    const token = await Token.deploy();
    console.log("Token address:", token.address);
}

main()
    .catch(error => {
        console.error(error);
        process.exit(1);
    });

5. ローカルチェーンへデプロイ

デプロイ実行。

npx hardhat run scripts/deploy.js

6. Mumbaiへデプロイ

.envに、MumbaiネットワークAPIキーと、PolygonSCANのAPIキー、Metamaskのプライベートキーを設定。
MumbaiネットワークAPIキーは、AlchemyかInfuraで取得。
POLYGONSCAN_API_KEYはVerify用。

.env
MUMBAI_URL="https://polygon-mumbai.g.alchemy.com/v2/xxxxxxxxxxxxxxxxxx"
PRIVATE_KEY="xxxxxxxxxxxxxxxxxx"
POLYGONSCAN_API_KEY="xxxxxxxxxxxxxxxxxx"

hardhat.config.js を編集。

hardhat.config.js
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-etherscan");
require('dotenv').config();

module.exports = {
  solidity: "0.8.9",
  networks: {
    mumbai: {
      url: process.env.MUMBAI_URL,
      accounts: [process.env.PRIVATE_KEY]
    }
  },
  // Verify用
  etherscan: {
    apiKey: process.env.POLYGONSCAN_API_KEY
  }
};

デプロイ実行。

npx hardhat run scripts/deploy.js --network mumbai

7. Verify

Verify実行。

npx hardhat verify --network mumbai <Token address>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?