前言
入門的話可以通過Remix開發(fā)工具完成solidity項(xiàng)目的編寫、編譯、部署等操作。專業(yè)點(diǎn)的開發(fā)工具有 Truffle 和 Hardhat,先看一看hardhat的簡介:
Hardhat is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.
開發(fā)工具的話,常用的還有一個(gè)本地模擬節(jié)點(diǎn)的Ganache(Ganache屬于truffle),因?yàn)殒溕习ㄖ骶W(wǎng)和測試網(wǎng)調(diào)試比較慢,可以先在本地測試。
常用開發(fā)模式:
- 初級:Metamask + Remix + Ganache
- 進(jìn)階:Truffle / Hardhat
Hardhat基本操作
npm (npx) version 8.5.5
node version 16.13.1
新手第一次如下:
- 創(chuàng)建新文件夾,初始化一個(gè)npm項(xiàng)目:
npm init -y
,此時(shí)目錄中會生成package.json
文件。 - 安裝hardhat :
npm install --save-dev hardhat
。 - 初始化hardhat:
npx hardhat
,會出現(xiàn)終端選項(xiàng)自行選擇。
如果安裝過hardhat,以上不用執(zhí)行,直接運(yùn)行 npx hardhat init
- 以下是常用的包:
npm install --save-dev hardhat@^2.9.3 @nomiclabs/hardhat-waffle@^2.0.0 ethereum-waffle@^3.0.0 chai@^4.2.0 @nomiclabs/hardhat-ethers@^2.0.0 ethers@^5.0.0
;npm install --save dotenv@^16.0.0
npm install --save @openzeppelin/contracts
安裝以上包的package.json
文件如下:
{
"name": "yourdappname",
"version": "version",
"description": "",
"main": "yourmain.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@nomiclabs/hardhat-waffle": "^2.0.0",
"chai": "^4.2.0",
"ethereum-waffle": "^3.0.0",
"ethers": "^5.0.0",
"hardhat": "^2.9.3"
},
"dependencies": {
"dotenv": "^16.0.0",
"@openzeppelin/contracts": "^4.7.3"
}
}
生成目錄結(jié)構(gòu)如下:
.
├── README.md
├── contracts
├── hardhat.config.js
├── node_modules
├── .env 手動創(chuàng)建,用于放置privateKey,需安裝dotenv包
├── package-lock.json
├── package.json
├── scripts
└── test
- contracts - 編寫合約
- scripts - 編寫js代碼
- hardhat.config.js - 配置solidity版本等信息
通過js代碼連接以太坊有兩個(gè)框架 Web3js和Ethers.js,一般要通過http或者wss代理去連接,比如通過alchemy或者infura等節(jié)點(diǎn)供應(yīng)商提供的api去連接。文章來源:http://www.zghlxwxcb.cn/news/detail-406317.html
-
npx hardhat compile
編譯solidity -
npx hardhat test
測試tests目錄下js文件 -
npx hardhat run
或者npx hardhat run scripts/deploy.ts
運(yùn)行js ,默認(rèn)在本地網(wǎng)絡(luò)運(yùn)行。注意指定網(wǎng)絡(luò):npx hardhat run scripts/deploy.js --network mumbai
參考:Road to WEB3 - buymeacoffee文章來源地址http://www.zghlxwxcb.cn/news/detail-406317.html
到了這里,關(guān)于hardhat開發(fā)dapp初始化操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!