脚手架的使用"/>
Web3.0之Hardhat脚手架的使用
##初始化项目
yarn init
##创建hardhat的开发环境
yarn add --dev hardhat
##创建hardhat的工程
npx hardhat
##安装工程依赖
yarn add --dev "hardhat@^2.14.0" "@nomicfoundation/hardhat-toolbox@^3.0.0" "@nomicfoundation/hardhat-network-helpers@^1.0.0" "@nomicfoundation/hardhat-chai-matchers@^2.0.0" "@nomicfoundation/hardhat-ethers@^3.0.0" "@nomicfoundation/hardhat-verify@^1.0.0" "chai@^4.2.0" "ethers@^6.4.0" "hardhat-gas-reporter@^1.0.8" "solidity-coverage@^0.8.0" "@typechain/hardhat@^8.0.0" "typechain@^8.1.0" "@typechain/ethers-v6@^0.4.0" @nomiclabs/hardhat-waffle
##关注deploy目录和test目录和hardhat.config.js
一个是部署项目的脚本目录 一个项目测试的脚本目录
hardhat.config.js是编写的所有脚本的执行入口
##重新编写deploy.js
// import some packages
const{ethers} = require("hardhat")//async main functionasync function main() {const StorageSimpleFactory = await ethers.getContractFactory("StorageSimple");console.log("Deploying contract ..");const StorageSimple = await StorageSimpleFactory.deploy();const contractAddress = await StorageSimple.getAddress();console.log(contractAddress);}//call main functionmain().then(() => {process.exit(0);}).catch((error) => {console.log(error);process.exit(1);});
##编写完成deploy.js执行进行测试,hardhat 有自己内部的网络,自动部署到它的这个网络当中。这个网络环境类似于Ganache
npx hardhat run deploy.js
执行成功后有如下打印信息
$ npx hardhat run ./scripts/deploy.js
Deploying contract ..
0x5FbDB2315678afecb367f032d93F642f64180aa3
##hardhat.config.js 配置默认的网络
module.exports = {defaultNetwork:"hardhat", //默认网络hardhat,如果不配置默认也是它solidity: "0.8.19",
};
测试一下 ,使用全局变量network指定网络
npx hardhat run ./scripts/deploy.js --network hardhat
##配置更多的网络信息
可以参考如下配置,SEPOLIA_RPC_URL 可以去Infure进行注册,它会提供很多区块链网络的节点信息,进行注册绑定即可获取节点的RPC的信息。如下二图所示,注册成功后,会看到一下画面,Ethereum的所示的链接,即是我们SEPOLIA_RPC_URL的链接
const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL
const PRIVATE_KEY = process.env.PRIVATE_KEY;/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {defaultNetwork:"hardhat",solidity: "0.8.19",networks:{sepolia:{url:SEPOLIA_RPC_URL,accounts:[PRIVATE_KEY],chainId:11155111,},}
};
然后进行测试 ,执行一下命令,你会发现部署起来变得慢了。因为合约是在真实的测试环境进行部署了。
npx hardhat run ./scripts/deploy.js --network sepolia
##安装 @nomicfoundation/hardhat-verify
可以使用Etherscan服务器 验证发布的合约
yarn add --dev @nomicfoundation/hardhat-verify
继续完善deploy.js按照以下代码进行verify
async function main() {const StorageSimpleFactory = await ethers.getContractFactory("StorageSimple");console.log("Deploying contract ..");const StorageSimple = await StorageSimpleFactory.deploy();const contractAddress = await StorageSimple.getAddress();console.log(`ContractAddress is ${contractAddress}`);//如果在测试网,再验证合约if(network.config.chainId == 11155111 && process.env.ETHERSCAN_API_KEY ){//等待6个区块确认await StorageSimple.deploymentTransaction().wait(6);//开始验证await verify(contractAddress,[]);}//执行合约函数1const number1 = await StorageSimple.retrieve();console.log(`number1=${number1}`);//执行合约函数2const transactionResp = await StorageSimple.store(7);//等待一个区块确认await transactionResp.wait(1)//执行合约函数1const number2 = await StorageSimple.retrieve();console.log(`number2=${number2}`);}async function verify(contractAddress,argument){try {console.log("Verifying contract...");//执行commandawait run("verify:verify",{address:contractAddress,constractArguments:argument})} catch (error) {console.error(error);}}
##拓展一个hardhat功能
如以下新增一个打印当前网络的最新区块的功能。 新增的功能在hardhat.config.js中import即可。这样使用npx hardhat命令时,可查到block-number的功能。执行npx hardhat block-number即可执行
const { task } = require("hardhat/config")task("block-number","Prints the current block number.").setAction(async(taskArgs,hre)=>{const blockNumber = await hre.ethers.provider.getBlockNumber();console.log(`${hrework.name} : The lastest block number is ${blockNumber}`);
})module.exports={}
##配置本地网络
使用npx hardhat node ,可获取许多本地网络信息。执行成功后,不要关闭此进程,它启动了一个虚拟的区块链服务!!可以切换 terminal窗口,执行其他的commands.
然后将,本地网络添加进hardhat.config.js,如之前sepolia网络的方式配置即可。
localhost:{url: 'http://127.0.0.1:8545/',// accounts is not essential to configure,hardhat has provided lots of accounts in the local.// accounts:[PRIVATE_KEY],chainId:31337,}
验证本地网络是否配置成功,执行
npx hardhat run ./scripts/deploy.js --network localhost
可以在terminal 运行的节点窗口看点如下的配置和交易信息
eth_sendTransactionContract deployment: StorageSimpleContract address: 0x5fbdb2315678afecb367f032d93f642f64180aa3Transaction: 0x13ab85499659bd15afe33e2b60faacf146be9f5971a1032f254fbc522027008bFrom: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266Value: 0 ETHGas used: 125701 of 30000000Block #1: 0xf371fa80b198ed970a8c01a1f91b0518f2aa4d792ef34dc8c504eddd7c57ef94eth_getTransactionByHash
eth_callContract call: StorageSimple#retrieveFrom: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266To: 0x5fbdb2315678afecb367f032d93f642f64180aa3eth_blockNumber
eth_feeHistory
eth_sendTransactionContract call: StorageSimple#storeTransaction: 0xc42d0e7c78eb3affccc475b9f7b44da6b9005eaedc7b7d118a2d7fcacecec784From: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266To: 0x5fbdb2315678afecb367f032d93f642f64180aa3Value: 0 ETHGas used: 43724 of 30000000Block #2: 0xbe8251e5ba574205cbbef7fa8f0c0696a0c1aedaefd96f008857d3b243cd897aeth_getTransactionByHash
eth_getTransactionReceipt
eth_blockNumber
eth_callContract call: StorageSimple#retrieveFrom: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266To: 0x5fbdb2315678afecb367f032d93f642f64180aa3## 使用hardhat提供的console功能,可以将脚本在控制台进行运行。方便测试
npx hardhat console --network localhost
Welcome to Node.js v18.16.0.
Type ".help" for more information.
> const StorageSimpleFactory = await ethers.getContractFactory("StorageSimple");
undefined
> const StorageSimple = await StorageSimpleFactory.deploy();
undefined
> const contractAddress = await StorageSimple.getAddress();
undefined
> console.log(`ContractAddress is ${contractAddress}`);
ContractAddress is 0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0
undefined
> const number1 = await StorageSimple.retrieve();
undefined
> console.log(`number1=${number1}`);
number1=0
undefined
> const transactionResp = await StorageSimple.store(7);
undefined
> console.log(`number2=${number2}`);
Uncaught ReferenceError: number2 is not defined
> const number2 = await StorageSimple.retrieve();
undefined
> console.log(`number2=${number2}`);
number2=7
undefined
更多推荐
Web3.0之Hardhat脚手架的使用
发布评论