使用 Hardhat 部署合约#
本教程将逐步介绍如何使用 Hardhat(智能合约开发环境)在 X Layer 测试网上创建、编译并部署一个简单的智能合约。
什么是 Hardhat#
Hardhat 是一个用于编译、部署、测试和调试智能合约的开发环境。
配置开发环境#
前置条件:
- Node.js v18+ LTS 和 npm(随 Node 一同安装)
- Git
要安装 Hardhat,你需要进入一个空目录,运行 npm init 并按照提示创建一个 npm 项目。你也可以使用 yarn 等其他包管理器,但我们推荐使用 npm 7 或更高版本,因为它能简化 Hardhat 插件的安装。
项目创建好后,运行 npm install --save-dev hardhat 安装 Hardhat,再运行 npm install @nomicfoundation/hardhat-toolbox 安装 Hardhat 工具箱。要使用本地安装的 Hardhat,需要通过 npx 来运行(即 npx hardhat)。
创建合约#
在项目目录中运行 npx hardhat 以创建示例项目:
shell
$ 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.9.9 👷
? What do you want to do? …
❯ Create a JavaScript project
Create a TypeScript project
Create an empty hardhat.config.js
Quit
编译合约#
查看 contracts/ 目录,可以找到 Lock.sol 文件:
shell
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
// Uncomment this line to use console.log
// import "hardhat/console.sol";
contract Lock {
uint public unlockTime;
address payable public owner;
event Withdrawal(uint amount, uint when);
constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);
unlockTime = _unlockTime;
owner = payable(msg.sender);
}
function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");
emit Withdrawal(address(this).balance, block.timestamp);
owner.transfer(address(this).balance);
}
}
运行 npx hardhat compile 即可编译。
设置配置文件#
为了连接到 X Layer 网络,需要配置对应的网络。
在 hardhat.config.js 中导出一个对象来完成配置:
javascript
module.exports = {
defaultNetwork: "hardhat",
networks: {
hardhat: {
},
xlayer: {
url: "https://testrpc.xlayer.tech/terigon",
accounts: [privateKey1, privateKey2, ...]
}
}
}
部署合约#
接下来,我们使用 Hardhat 脚本来部署合约。在 scripts/ 目录中可以找到包含以下代码的文件:
javascript
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");
async function main() {
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
const unlockTime = currentTimestampInSeconds + 60;
const lockedAmount = hre.ethers.utils.parseEther("0.001");
const Lock = await hre.ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
await lock.deployed();
console.log(
`Lock with ${ethers.utils.formatEther(
lockedAmount
)}ETH and unlock timestamp ${unlockTime} deployed to ${lock.address}`
);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
现在,可以使用 npx hardhat run 运行该脚本:
shell
$ npx hardhat run scripts/deploy.js --network XLayer
Lock with 1 ETH deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
