我想您可能想要使用安全帽网络方法Hardhat_setBalance,文档使用这样的示例:
await network.provider.send("hardhat_setBalance", [
"<ACCOUNT ADDRESS>",
"0x1000", # 4096 wei
]);
Run Code Online (Sandbox Code Playgroud)
虽然我不是用 javascript 进行开发,但我一直在使用web3.py和eth-account在 python 中做类似的事情,代码如下:
from web3 import Web3
from eth_account import Account
chain = Web3(HTTPProvider("http://127.0.0.1:8545"))
acct = Account.create('<RANDOM VALUE>')
address = Web3.toChecksumAddress(acct.address)
print(chain.eth.get_balance(address)) # 0 wei
# add a balance of eth tokens to the address
chain.provider.make_request("hardhat_setBalance", [address, "0x1000"])
print(chain.eth.get_balance(address)) # 4096 wei
Run Code Online (Sandbox Code Playgroud)
Hardhat 允许使用accounts. count内置hardhat网络的配置选项。
例子:
module.exports = {
networks: {
hardhat: {
accounts: {
count: 1000
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
文档: https: //hardhat.org/hardhat-network/reference/#config
该new ethers.Wallet.createRandom()函数仅在 JS 应用程序上下文中从随机私钥创建一个帐户,但它不与提供者(在您的例子中为 Hardhat)通信。