如何用hardhat部署fuji测试网
1 2 3
| 我们将经常使用名为 Hardhat 的工具。这将使我们能够轻松启动本地以太坊网络,并为我们提供假测试 ETH 和假测试账户。请记住,它就像一个本地服务器,除了“服务器”是区块链。
快速编译智能合约并在我们本地的区块链上进行测试。
|
Hardhat 是一个编译、部署、测试和调试以太坊应用的开发环境。它可以帮助开发人员管理和自动化构建智能合约和 DApps 过程中固有的重复性任务,并围绕这一工作流程轻松引入更多功能。这意味着 hardhat 最核心的地方是编译、运行和测试智能合约。
# 安装 nvm
英文版
中文版
# hardhat 方面
# 1. 创建 npm 项目
1 2
| mkdir hardhat_fuji_demo npm init -y
|
# 2.npm 安装 hardhat
npm install --save-dev hardhat
# 3. 执行 npx hardhat
选择:
Create an advanced sample project that uses TypeScript
=> Create a TypeScript project(12.29)
其他选择默认
效果如下:
# 3. 启动本地测试节点
1 2 3
| npx hardhat node
会生成20个供测试的account和private key
|
# 4. 测试
1
| npm install @openzeppelin/contracts
|
1. 在 contracts 目录下创建 superToken 文件
touch contracts/SuperToken.sol
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| // SPDX-License-Identifier: MIT pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; import "@openzeppelin/contracts/utils/Counters.sol";
contract SuperToken is ERC721URIStorage { using Counters for Counters.Counter; Counters.Counter private _tokenIds;
constructor() ERC721("SuperToken", "SUP") {}
function mint(address to, string memory tokenURI) public returns (uint256) { uint256 newItemId = _tokenIds.current(); _mint(to, newItemId); _setTokenURI(newItemId, tokenURI);
_tokenIds.increment(); return newItemId; } }
|
2.touch test/supertoken.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import { expect } from "chai"; import { ethers } from "hardhat"; import { Signer, BigNumber } from "ethers";
describe("SuperToken", function () { it("Should return the new greeting once it's changed", async function () { const SuperToken = await ethers.getContractFactory("SuperToken"); const sup = await SuperToken.deploy(); await sup.deployed();
let one: Signer, two: Signer, three: Signer, four: Signer, five: Signer; let oneAddr: string, twoAddr: string, threeAddr: string, fourAddr: string, fiveAddr: string; [one, two, three] = await ethers.getSigners(); [oneAddr, twoAddr, threeAddr] = await Promise.all([one, two, three].map((s) => s.getAddress()));
console.log(await sup.mint(oneAddr, "one")); console.log(await sup.mint(twoAddr, "two")); console.log(await sup.ownerOf(0)); console.log(await sup.ownerOf(1)); console.log(oneAddr);
}); });
|
npx hardhat test --network localhost test/index.ts
npx hardhat test --network localhost scripts/deploy.ts
npx hardhat test --network localhost test/supertoken.ts
# 部署
npx hardhat run scripts/deploy.ts --network fuji
1 2 3
| Lock with 1 ETH and unlock timestamp 1703820977 deployed to 0xd2E73998A99BB3bf32823Eff6968a1732E5e97c5
Lock with 1 ETH and unlock timestamp 1703821199 deployed to 0x1D9b332ADC215Dd6373257eab92D3B00e1066043
|