Poly Smart Chain中文
  • 介紹
    • 信息
    • 共識
    • 網絡
  • 區塊鏈基礎
    • 區塊鏈的類型
    • 共識機制
    • Solidity
    • 交易
    • GAS
    • 賬戶
  • 經濟學
  • Node
    • 節點 RPC
    • 部署
    • 私有鏈
  • 錢包
    • 連接 Metamask
    • 連接 Imtoken
    • 連接 TokenPocket
    • Metamask
    • Portis
    • Torus
  • 跨鏈橋
    • MetaMask:BSC to PSC(跨鏈)
    • MetaMask:Polygon to PSC(Cross-Chain)
    • WDCSpace:WDC to PSC(Cross-Chain)
    • Imtoken:BSC to PSC(Cross-Chain)
    • TokenPocket:BSC to PSC(Cross-Chain)
    • 跨鏈通用問題
    • 跨鏈數量(每日)
  • 開發者
    • Contract
    • PRC20
    • PRC721
    • RemixIDE
    • Truffle
    • Hardhat
    • Web3.js
  • Explorer
  • DApp
    • Swanswap
      • 如何在 Swanswap 購買 PSC 代幣
      • 如何在 Swanswap 中添加 PSC-USDT 流動性
    • NFT Auction
      • 如何Claim PolyJetClub
      • 在MetaMask錢包中查看PolyJetClub
    • 礦池
      • 如何將 LP Token 質押到礦池
    • Social
      • 註冊Social(V0.1)
    • 如何遷移到V2礦池
      • 如何遷移到V2礦池(MetaMask)
      • 如何遷移到V2礦池(TP錢包)
  • 資源
  • 常問問題
Powered by GitBook
On this page
  • 什麼是 Hardhat
  • 設置開發環境
  • 安裝
  • 創建項目
  • 在 PSC 網絡上部署
  1. 開發者

Hardhat

PreviousTruffleNextWeb3.js

Last updated 3 years ago

什麼是 Hardhat

Hardhat 是一個用於編譯、部署、測試和調試智能合約的開發環境。

設置開發環境

在我們開始之前,有一些技術要求。 請安裝以下內容: 要求:

安裝

在我們開始之前,有一些技術要求。 請安裝以下內容: 要求:

  • Windows, Linux or Mac OS X

首先需要創建一個空項目 npm init --yes 一旦你的項目準備好了,你應該運行

npm install --save-dev hardhat

建議安裝一些依賴項。

npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers

要使用本地安裝的 Hardhat,您需要使用 npx 來運行它(即 npx hardhat)。

創建項目

要創建您的 Hardhat 項目,請在項目文件夾中運行 npx hardhat:

mkdir MegaCoin
cd MegaCoin
  • 初始化你的項目:

$ npx hardhat
888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

Welcome to Hardhat v2.0.8

? What do you want to do? …
❯ Create a sample project
  Create an empty hardhat.config.js
  Quit

初始化此項目後,您現在將擁有一個包含以下項目的項目結構:

  • contract/: Solidity 合約目錄

  • scripts/:可編寫腳本的部署文件的目錄

  • test/:用於測試應用程序和合約的測試文件目錄

  • hardhat-config.js:安全帽配置文件

創建合同

您可以編寫自己的智能合約或下載PRC20代幣智能合約模板。

  • 為 PSC 配置安全帽

  • 轉到 hardhat.config.js 使用 Psc-network-credentials 更新配置。

require("@nomiclabs/hardhat-waffle");
require('@nomiclabs/hardhat-ethers');
const { mnemonic } = require('./secrets.json');

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async () => {
  const accounts = await ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  defaultNetwork: "mainnet",
  networks: {
    localhost: {
      url: "http://127.0.0.1:8545"
    },
    hardhat: {
    },
    testnet: {
      url: "https://data-seed-prebsc-1-s1.binance.org:8545",
      chainId: 97,
      gasPrice: 20000000000,
      accounts: {mnemonic: mnemonic}
    },
    mainnet: {
      url: "https://bsc-dataseed.binance.org/",
      chainId: 56,
      gasPrice: 20000000000,
      accounts: {mnemonic: mnemonic}
    }
  },
  solidity: {
  version: "0.5.16",
  settings: {
    optimizer: {
      enabled: true
    }
   }
  },
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  },
  mocha: {
    timeout: 20000
  }
};

筆記

它需要為 Provider 傳入助記詞,這是您要部署的帳戶的助記詞。 在根目錄中創建一個新的 .secret 文件並輸入您的 12 字助記詞助記詞以開始使用。 要從 metamask 錢包中獲取種子詞,您可以轉到 Metamask 設置,然後從菜單中選擇安全和隱私,您將在其中看到一個顯示種子詞的按鈕。

編譯合約

要編譯 Hardhat 項目,請切換到項目所在目錄的根目錄,然後在終端中鍵入以下內容:

npx hardhat compile

在 PSC 網絡上部署

在項目目錄的根目錄中運行此命令:

$  npx hardhat run --network testnet scripts/deploy.js

請記住您提供的地址、transaction_hash 和其他詳細信息會有所不同,以上只是提供一個結構概念。

恭喜! 您已成功部署 PRC20 智能合約。 現在您可以與智能合約進行交互。

Node.js v8.9.4 LTS or later
Git