帖子
分享您的知识。
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
答案
3You 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,
])
);
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);
});
在模仿以太坊主网的条件下测试智能合约的关键步骤,是在Hardhat的本地分叉或本地节点上使用ETH为账户注资的关键步骤. 本教程利用基本关键字以结构化和优化的方式指导您完成整个过程:
-
mkdir mainnet-fork-tutorial
创建 JavaScript 项目:打开终端并输入为教程创建目录的命令来初始化项目,例如. 导航到您的项目目录并设置一个新的 JavaScript 项目. -
安装 Hardhat:在你的项目目录中,使用 npm 安装 Hardhat,这是本教程的核心工具. 运行
npm install --save-dev hardhat
将其添加到项目的开发依赖项中.
3.安装 Dotenv:为了安全地管理您的 API 密钥和私钥,您需要安装 dotenv 软件包. npm i dotenv
在您的终端中运行. 此步骤确保您可以将环境变量从.env
文件加载到项目中,从而确保私钥和 API 密钥等敏感信息的安全.
-
npx hardhat
设置您的 Hardhat 项目:通过在终端中运行来初始化您的 Hardhat 项目. 按照提示创建示例项目,当系统询问时,您可以选择创建 JavaScript 项目并使用 requirenomicfoundation/hardhat-toolbox
,这是一组对开发有用的工具和插件. -
为主网分叉配置硬帽:在您的 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;
});
-
npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/yourAlchemyApiKey --fork-block-number 12345678
分叉主网:将 Hardhat 配置设置为主网分叉后,启动一个通过运行来分叉以太坊主网的本地 Hardhat 节点. 此命令用于npx hardhat node
分叉主网,允许您与以太坊网络的先前存在的状态和合约进行交互.yourAlchemyApiKey
替换为您的实际API密钥,并可以选择指定要从中分叉的区块号. -
向您的账户注资:要在这种本地环境中使用以太坊向账户注资,您可以使用Hardhat的控制台或脚本. 首先,确保你有一个可以访问其私钥的账户. 您可以使用 Hardhat 生成新账户或使用现有的以太坊账户. 在你的
.env
文件中,像这样存储私钥:PRIVATE_KEY=yourPrivateKey
. -
执行资金脚本:在您的项目中创建一个脚本,从 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;
});
- 验证余额:通过运行简单的 Hardhat 任务或脚本来检查并打印账户余额,验证账户的 ETH 余额.
你知道答案吗?
请登录并分享。
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.