掌握了 Hardhat 开发框架的基本用法;创建了一个web3项目;编写修改了智能合约;执行了单元测试;启动运行了本地区块链节点;将合约文件成功进行编译和部署;
还安装了 MetaMask 钱包,并连接了本地网络和测试账户进行测试。
https://nodejs.org/en/download/
https://baijiahao.baidu.com/s?id=1727989741134662012&wfr=spider&for=pc
https://hardhat.org/
contract :声明一个合约;
memory:和 storage 关键字相反,代表了变量只会临时放在内存中,不会存储在合约的状态中;
view:声明该方法为只读方法,不会改变合约本身的状态。
在本地网络上部署合约
npx hardhat node 命令,启动区块链网络一个本地节点
这个命令的意思是,按照 hardhat.config.js 中声明的配置,启动一个本地区块链网络的节点。在每次启动时,默认会提供 20 个钱包账户和私钥,每个钱包提供 10000 个 ETH 做测试。
把合约部署到了本地网络上
npx hardhat run ./scripts/deploy.js --network localhost
Downloading compiler 0.8.19
Compiled 1 Solidity file successfully
Lock with 0.001ETH and unlock timestamp 1694768447 deployed to 0x5FbDB2315678afecb367f032d93F642f64180aa3
eth_chainId (8)
eth_blockNumber
eth_getBlockByNumber
net_version
eth_getBalance
net_version
eth_blockNumber
eth_getBalance (2)
eth_blockNumber
eth_getBalance
hardhat_addCompilationResult
eth_accounts
hardhat_metadata (20)
eth_blockNumber
eth_getBlockByNumber
eth_feeHistory
eth_sendTransaction
Contract deployment: Lock
Contract address: 0x5fbdb2315678afecb367f032d93f642f64180aa3
Transaction: 0xeeb241920a3ac1cf2f11fdc6d813c0fe974691fb7ca5c9137a9b69a5864c1531
From: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
Value: 0.001 ETH
Gas used: 326112 of 30000000
Block #1: 0xca6f8c148a15a93d25f03342eaf46e1e233d68f8baafb39dc64ff3d5d5ca7b83
eth_getTransactionByHash
eth_getTransactionReceipt
eth_blockNumber (2)
eth_getBlockByNumber
eth_getBalance (3)
eth_blockNumber (3)
到这步为止,我们的 Greeter 合约就成功在本地网络上部署好了,也成功使用 MetaMask 钱包,进行了本地网络连接、测试账户导入。
这个命令用于执行单元测试
npx hardhat test
Lock
Deployment
✔ Should set the right unlockTime (896ms)
✔ Should set the right owner
✔ Should receive and store the funds to lock
✔ Should fail if the unlockTime is not in the future
Withdrawals
Validations
✔ Should revert with the right error if called too soon
✔ Should revert with the right error if called from another account
✔ Shouldn't fail if the unlockTime has arrived and the owner calls it
Events
✔ Should emit an event on withdrawals
Transfers
✔ Should transfer the funds to the owner
9 passing (962ms)
启动一个控制台程序,方便交互式输入输出
npx hardhat console
这个命令的作用就是编译
npx hardhat compile
把编译出来的文件清理一下。
npx hardhat clean
npx hardhat help
https://github.com/Fankouzu/smart-contract/tree/master/Solidity%20Lesson%2002
https://web3.tryblockchain.org/Web3.js-api-refrence.html#toc_1