首先安装nodejs
可以看上一篇文章

mkdir a1
cd a1
npm init -y

npm install hardhat

npx hardhat init

contract Add {
function add(uint a, uint b) public pure returns(uint) {
return a+b;
}
}

删除自带的

npx hardhat compile

npm install --save-dev @nomicfoundation/hardhat-toolbox
npm install --save-dev @nomicfoundation/hardhat-toolbox 是一个命令,用于使用 npm 安装 @nomicfoundation/hardhat-toolbox 包,并将其作为项目的开发依赖保存。
这个命令执行后,npm 将会连接到 npm 仓库,下载 @nomicfoundation/hardhat-toolbox 包及其相关依赖,并将其保存在项目的 package.json 文件中的 devDependencies 部分。
请确保你已经在命令行工具中切换到了你的项目目录下,然后执行该命令以安装并保存 @nomicfoundation/hardhat-toolbox 作为开发依赖。
npx hardhat compile


const { expect } = require(“chai”);
describe(“Add”, function () {
it(“should return the sum of two numbers”, async function () {
const Add = await ethers.getContractFactory(“Add”);
const add = await Add.deploy();
const result = await add.add(2, 3);
expect(result).to.equal(5);
});
});
npx hardhat test



部署hardhat默认网络下面这个命令也行,也就是部署到hardhat内部的vm


账户里填的是私钥,直接方比较危险
可以放在文件里,通过读取环境变量的方式使用
例如 process.env.Private key
infura申请
打印某些信息
import “hardhat/console.sol”;

查看hardhat的自有区块链的信息
npx hardhat node
npx hardhat node | more
看全部信息
本文介绍了如何使用Node.js和Hardhat进行Ethereum智能合约的开发,包括安装必要的依赖、创建合同、部署、测试以及使用Infura和私钥安全操作。


被折叠的 条评论
为什么被折叠?



