Peera.

帖子

分享您的知识。

专家问答

How to fund a account with ETH on a local fork or local node in hardhat?

Let's say I have an account in hardhat

[owner, user, user2] = await ethers.getSigners();
userAddress = user.address;

How would I fund userAddress with funds on my local fork?

  • blockchain
  • solidity
1
3
分享
评论
.

答案

3
0x8744...6532.
Oct 12 2022, 20:50

You can use setBalance from the hardhat docs

    await network.provider.send("hardhat_setBalance", [
      userAddress,
      "0x1000000000000000000000000", // we are giving ourselves a LOT eth
    ]);
    // then we can print out how much we have in hex
    console.log(
      await network.provider.send("eth_getBalance", [
        impersonatedSigner.address,
      ])
    );
1
最佳答案
评论
.
Sergey Ilin.
Oct 12 2022, 21:02

Whenever Hardhat spins up a local instance, it creates pre-funded accounts. You can access those accounts if import ethers from hardhat.

const { ethers } = require("hardhat");

async function main() {
  const accounts = await ethers.getSigners();
  const provider = ethers.provider;

  for (const account of accounts) {
      console.log("%s (%i ETH)", account.address, ethers.utils.formatEther(
              await provider.getBalance(account.address)
          )
      );
  }
}


main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });
0
评论
.
Marcador.
Mar 15 2024, 07:17

在模仿以太坊主网的条件下测试智能合约的关键步骤,是在Hardhat的本地分叉或本地节点上使用ETH为账户注资的关键步骤. 本教程利用基本关键字以结构化和优化的方式指导您完成整个过程:

  1. mkdir mainnet-fork-tutorial创建 JavaScript 项目:打开终端并输入为教程创建目录的命令来初始化项目,例如. 导航到您的项目目录并设置一个新的 JavaScript 项目.

  2. 安装 Hardhat:在你的项目目录中,使用 npm 安装 Hardhat,这是本教程的核心工具. 运行npm install --save-dev hardhat将其添加到项目的开发依赖项中.

3.安装 Dotenv:为了安全地管理您的 API 密钥和私钥,您需要安装 dotenv 软件包. npm i dotenv在您的终端中运行. 此步骤确保您可以将环境变量从.env文件加载到项目中,从而确保私钥和 API 密钥等敏感信息的安全.

  1. npx hardhat设置您的 Hardhat 项目:通过在终端中运行来初始化您的 Hardhat 项目. 按照提示创建示例项目,当系统询问时,您可以选择创建 JavaScript 项目并使用 requirenomicfoundation/hardhat-toolbox,这是一组对开发有用的工具和插件.

  2. 为主网分叉配置硬帽:在您的 Hardhat 配置文件 (hardhat.config.js) 中,为 Hardhat 添加指定主网分叉的网络配置. 这涉及使用该networks属性来定义一个名为的新网络hardhat,其中包含该forking选项. 要获得诸如 Alchemy 或 Infura 之类的完整存档节点服务,你需要一个 API 密钥. 配置可能如下所示:

async function main() {
  const [deployer] = await ethers.getSigners();
  const receiver = "0xYourAccountAddress";
  const amount = ethers.utils.parseEther("1.0");

  const tx = await deployer.sendTransaction({
    to: receiver,
    value: amount,
  });

  console.log(`Funded ${receiver} with 1 ETH.`);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

  1. npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/yourAlchemyApiKey --fork-block-number 12345678分叉主网:将 Hardhat 配置设置为主网分叉后,启动一个通过运行来分叉以太坊主网的本地 Hardhat 节点. 此命令用于npx hardhat node分叉主网,允许您与以太坊网络的先前存在的状态和合约进行交互. yourAlchemyApiKey替换为您的实际API密钥,并可以选择指定要从中分叉的区块号.

  2. 向您的账户注资:要在这种本地环境中使用以太坊向账户注资,您可以使用Hardhat的控制台或脚本. 首先,确保你有一个可以访问其私钥的账户. 您可以使用 Hardhat 生成新账户或使用现有的以太坊账户. 在你的.env文件中,像这样存储私钥:PRIVATE_KEY=yourPrivateKey.

  3. 执行资金脚本:在您的项目中创建一个脚本,从 Hardhat 节点中预先注资的其中一个账户向您的账户发送 ETH. 以下是一个示例脚本fundAccount.js,你可以用它来运行npx hardhat run scripts/fundAccount.js --network localhost

async function main() {
  const [deployer] = await ethers.getSigners();
  const receiver = "0xYourAccountAddress";
  const amount = ethers.utils.parseEther("1.0");

  const tx = await deployer.sendTransaction({
    to: receiver,
    value: amount,
  });

  console.log(`Funded ${receiver} with 1 ETH.`);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

  1. 验证余额:通过运行简单的 Hardhat 任务或脚本来检查并打印账户余额,验证账户的 ETH 余额.
0
评论
.

你知道答案吗?

请登录并分享。

Web3 (also known as Web 3.0) is an idea for a new iteration of the World Wide Web which incorporates concepts such as decentralization, blockchain technologies, and token-based economics.

142帖子198答案
我们使用 cookie 确保您在我们的网站上获得最佳体验。
更多信息