如何使用 ethers.js 在安全帽测试中生成以 eth 为种子的任意钱包?

ful*_*ted 4 ethereum solidity ethers.js hardhat

我目前正在尝试在安全帽/华夫饼中运行测试,需要数百个独特的钱包来调用合约,使用新的 ethers.Wallet.createRandom()。然而,这些钱包都没有提供以太坊,所以我无法用它们调用/发送交易。

向任意数量的随机生成的钱包提供 eth 的最简单/最有效的方法是什么?谢谢!

Jay*_*mon 7

我想您可能想要使用安全帽网络方法Hardhat_setBalance,文档使用这样的示例:

await network.provider.send("hardhat_setBalance", [
  "<ACCOUNT ADDRESS>",
  "0x1000", # 4096 wei
]);
Run Code Online (Sandbox Code Playgroud)

虽然我不是用 javascript 进行开发,但我一直在使用web3.pyeth-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)


Pet*_*jda 5

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)通信。