发布时间: 2024-03-28 22:53:00
refer to:
https://hardhat.org/hardhat-runner/docs/advanced/scripts
创建任意文件, yourscript.js
内容如下:
const hre = require("hardhat") async function main() { let accounts = await hre.ethers.getSigners() console.info("== accounts: ", accounts) for(let account of accounts) { console.info(account.address, await hre.ethers.provider.getBalance(account.address)) } } main()
注意:想要让操作结果持久的话。
1. 必须指定 --network ,例如 npx hardhat run your.script --network localhost
2. 必须用 npx hardhat run 来运行,而不是node your.script
使用正确的手法运行:
使用错误的(没有参数的,默认的)手法运行:
进行转账:
const hre = require("hardhat") async function main() { let to_address = '0x959922be3caee4b8cd9a407cc3ac1c251c2007b1' // 下面2行,这里必须这样分开些 let signers = await hre.ethers.getSigners() let signer = signers[0] console.info("== before send ETH, balance is: ", await hre.ethers.provider.getBalance(to_address)) let options = { to: to_address, value: "3000000000000000000", } let tx = await signer.sendTransaction(options) let result = tx.wait() console.info("== result: ", result) console.info("== after, balance: ", await hre.ethers.provider.getBalance(to_address)) console.info("== after, balancesender: ", await hre.ethers.provider.getBalance(signer.address)) } main()Back