# hardhat使用 **Published by:** [plsdm7](https://paragraph.com/@dogsuisui/) **Published on:** 2022-11-12 **URL:** https://paragraph.com/@dogsuisui/hardhat ## Content hardhat是一个基于nodejs的solidity开发环境。可以很方便的编译部署以及测试合约。当然直接使用hardhat编写合约不是很方便,例如没有高亮和code complete,官方推荐了vs code插件版本,这个我还待尝试。 官方教程: https://hardhat.org/tutorial 大概来说跟着教程来就可以了 1、安装nodejs 这个没什么好说了,网上下载一个就可以 2、创建hardhat工程 这部分也按官方文档 hardhat是以task的方式来执行,使用npx运行task。 例如:npx hardhat compile \ npx hardhat run filename.js 3、编写合约 编写完成后,调用npx hardhat compile进行编译 4、部署合约 合约部署需要在js文件中执行// scripts/deploy.js const hre = require("hardhat"); async function main() { // We get the contract to deploy. const BuyMeACoffee = await hre.ethers.getContractFactory("MerkleProof"); const buyMeACoffee = await BuyMeACoffee.deploy(); await buyMeACoffee.deployed(); console.log("BuyMeACoffee deployed to:", buyMeACoffee.address); } // We recommend this pattern to be able to use async/await everywhere // and properly handle errors. main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); }); 可以看到hardhat通过插件的形式,集成了ether.js,因此可以直接获取到合约对象并且部署。 编写完成后,调用npx hardhat run filepath 执行js文件,filepath是js文件地址。 合约的测试同样通过js实现,因为集成了ether.js,所以js与合约交互方式和ether.js一致,使用起来比较方便。 总结 感觉hardhat是remix的简化版,暂时没看出他的优势,可以直接跳过他使用remix。当然本地随便写个demo也可以考虑使用。另外hardhat提供了自己的测试网络,可以直接在本地部署运行。 ## Publication Information - [plsdm7](https://paragraph.com/@dogsuisui/): Publication homepage - [All Posts](https://paragraph.com/@dogsuisui/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@dogsuisui): Subscribe to updates - [Twitter](https://twitter.com/plsdm7): Follow on Twitter