Jua*_*ola 3 rsk ethers.js hardhat
我正在 Hardhat 中开发智能合约并在RSK Testnet上进行测试。\n为了创建签名者帐户,我使用助记符种子短语\n和以下 Hardhat 配置:\n\xe2\x80\x8b
\nrequire(\'@nomicfoundation/hardhat-toolbox\');\nconst { mnemonic } = require(\'./.secret.json\');\n\xe2\x80\x8b\n/** @type import(\'hardhat/config\').HardhatUserConfig */\nmodule.exports = {\n solidity: \'0.8.16\',\n networks: {\n hardhat: {},\n rsktestnet: {\n chainId: 31,\n url: \'https://public-node.testnet.rsk.co/\',\n accounts: { // <-- here\n mnemonic,\n path: "m/44\'/60\'/0\'/0",\n },\n },\n },\n // ...\n};\n
Run Code Online (Sandbox Code Playgroud)\n\xe2\x80\x8b\n在我的测试中,我通常使用ethers.js
辅助函数\n在我的测试中获取签名者数组:\n\xe2\x80\x8b
const signers = await hre.ethers.getSigners();\n
Run Code Online (Sandbox Code Playgroud)\n\xe2\x80\x8b\n但是这个函数返回 20 个签名者钱包。\n如果我不想要这个默认数量,有没有办法获得特定数量的钱包?
\n您可以使用Wallet.fromMnemonic
ethers.js 中的函数来执行此操作。
在 内hardhat.config.js
或导入hre
变量的任何地方,您可以通过像这样调用它来实现:
hre.ethers.Wallet.fromMnemonic(mnemonic, derivationPath?, wordList?)
该函数用于生成单个钱包。该函数的第二个参数是 BIP-32派生路径。由于您在配置中使用的是m/44'/60'/0'/0
,那么默认情况下该函数将附加/0
,从而m/44'/60'/0'/0/0
生成第一个钱包。
如果您想生成另一个,您可以指定m/44'/60'/0'/0/1
(/1
附加)。
要生成特定数量的钱包,只需循环执行上述操作,并在每次迭代中更改派生路径中的最终索引。
例如,以下函数将获得 10:
function getSigners(amount = 40) {
// getting seed phrase and derivation path from the hardhat config
const { mnemonic, path } = hre.network.config.accounts
return [...Array(amount).keys()].map((i) =>
hre.ethers.Wallet.fromMnemonic(mnemonic, `${path}/${i}`)
.connect(hre.ethers.provider),
)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
530 次 |
最近记录: |