one 参考官网:https://hardhat.org/hardhat-runner/docs/getting-started
build project
环境:
安装:
npm install --save-dev hardhat
初始化项目:
To create the sample project, run npx hardhat
in your project folder:
终端输入:npx hardhat
选择: create a typeScript project
生成文件:
contracts:存放合约文件夹
script:用于部署合约文件夹,存放doploy.ts
test:存放测试文件夹
hardhat.config.js:用于定义合约版本,部署的网络networks,还可以进行代理(上传源码时,若连接超时可以进行代理)
配置文件
hardhat.config.js:
import * as dotenv from "dotenv";
import type { HardhatUserConfig } from "hardhat/config";
require("@nomiclabs/hardhat-waffle");
require("@nomicfoundation/hardhat-verify");
//引用.env文件
dotenv.config();
// 代理
const { ProxyAgent, setGlobalDispatcher } = require("undici")
const proxyAgent = new ProxyAgent("http://127.0.0.1:7890")
setGlobalDispatcher(proxyAgent)
const config: HardhatUserConfig = {
defaultNetwork: "hardhat",
etherscan: {
// Your API key for Etherscan
// Obtain one at [https://etherscan.io/](https://etherscan.io/)
apiKey: process.env.ETHERSCAN_API_KEY,
},
solidity: {
compilers: [{version: "0.8.17", settings: {}}],
},
networks: {
hardhat: {},
sepolia: {
url: process.env.ROPSTEN_URL,
accounts: [process.env.PRIVATE_KEY],
},
},
paths: {
sources: "./contracts",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
},
mocha: {
timeout: 40000
}
};
export default config;
contract:RuinToken.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract RuinToken is ERC20, Ownable {
constructor(uint256 amount) ERC20("RuinToken", "RUIN") Ownable() {
_mint(msg.sender, amount);
}
function mint(address account, uint256 amount) public onlyOwner {
require(account != address(0), "address is zero");
_mint(account, amount);
}
function burn(address account, uint256 amount) public {
require(account == msg.sender, "burn error");
_burn(account, amount);
}
}
创建文件:mkdir .env
.env:
ETHERSCAN_API_KEY=go to https://etherscan.io/ sign
ROPSTEN_URL=https://sepolia.infura.io/v3/api key
PRIVATE_KEY=your account privateKey
ETHERSCAN_API_KEY 如图获取:
package.json:
"scripts": {
//本地启动服务命令
"node": "npx hardhat node",
//部署合约命令
"deploy-sepolia": "yarn hardhat run --network sepolia scripts/deploy.ts",
//编译合约,生成abi等
"compile": "npx hardhat clean & npx hardhat compile --force",
//验证合约上传源码 arg/RuinTokenArguments.ts存放验证参数
"verify-sepolia": "yarn hardhat verify --network sepolia --constructor-args arg/RuinTokenArguments.ts (contractAddress)"
},
如图:
验证参数:
arg/RuinTokenArguments.ts
const {BigNumber} = require("ethers");
module.exports = [
BigNumber.from(1000)
]
deploy.js详解:
import 'hardhat-deploy';
import 'hardhat-deploy-ethers';
import {BigNumber} from "ethers";
const { ethers } = require("hardhat");
async function main() {
//签名人地址
const signers = await ethers.getSigners();
console.log("Deploying contracts with the account:", signers[0].address);
//获取本地合约
const RuinToken = await ethers.getContractFactory("RuinToken")
//进行打包,并传入参数,此处参数和验证时参数必须一致
const ruinToken = await RuinToken.deploy(
BigNumber.from(1000)
);
//打印合约地址
console.log(ruinToken.address);
};
//执行main function
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
运行效果
根据package.json分别进行,编译,打包,验证
1、编译
yarn compile
2、打包
yarn deploy-sepolia
3、验证
yarn verify-sepolia
部署链上效果图:
github参考连接:源码地址