当前位置: 首页 > news >正文

hardhat 本地连接matemask钱包

Hardhat 安装

https://hardhat.org/hardhat-runner/docs/getting-started#quick-start

Running a Local Hardhat Network

Hardhat greatly simplifies the process of setting up a local network by having an in-built local blockchain which can be easily run through a single line of code:

npx hardhat node

Running the above command, you will get the RPC endpoint as well as a list of locally generated accounts. We will need the endpoint as well as the private keys for configuring Metamask.

Do take note that this local blockchain only stores the interactions until the console is closed hence the state is not preserved between runs. Additionally, take the time to read through the details of the accounts as it is important that you do not use these sample accounts for sending any real money.

Add Local Network to Metamask

With our local Hardhat network running, we can then configure our Metamask to connect to it. In a browser with Metamask installed, select the network dropdown (this will likely be the dropdown with “Ethereum Mainnet” listed). Do note that you will need to have enabled “Show test networks” in order to view the full list as per the below screenshot.

The quickest way to get connected is by selecting “Localhost 8545” which connects to the default RPC endpoint that many developer tools implement. In the interest of completeness, this guide will cover how to manually setup the network with customised parameters.

Select the “Add Network” button and you will be greeted with a form requesting for the relevant network details. In order to connect to our local network, we will be using the following:

  • Network Name: Hardhat— This is up to you and defines how the network will show up in your network dropdown.
  • New RPC URL: http://127.0.0.1:8545/ — The endpoint returned from running npx hardhat node earlier.
  • Chain ID: 31337 — This is the default chain identifier that is implemented by Hardhat. You can refer to their documentation here.
  • Currency Symbol: HardhatETH — This is up to you and defines the symbol for the local network currency (ie. ETH).

After saving the above, you will be able to see that a new network named “Hardhat” was created with a native currency symbol of HARDHATETH. Although the network is now connected, we are unable to see the accounts which have been credited with our test HARDHATETH as these accounts have yet to be added to Metamask.

Importing Test Accounts

Upon starting up the local network, Hardhat has also funded a list of accounts with the native test ETH (renamed HARDHATETH in our Metamask). In order to add these accounts to Metamask, we will need to add the private keys which were returned when we ran the npx hardhat node command.

With the private keys in hand, we can then select the accounts tab on Metamask (this is the colourful circle on top right) which will display various options for connecting an account.

Select the “Import Account” option and Metamask will prompt you for the private key string. Paste the private keys from earlier, it should look something like this:

 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

Once imported, an account with the corresponding address will have been added to Metamask. You should also be able to see a 10000 HARDHATETH in the imported account.

For easier reference, you can also change the name of the account by navigating to the “Account details” section in the settings.

We will also continue to add the second test account to Metamask using the respective private key.

Testing the Network

To ensure that Metamask is working with our network, we can utilise its existing send function to transfer HARDHATETH between our development accounts. Notice that we do not have to write any code for this as Metamask already implements the standard functionality.

Of note, Metamask also allows us to easily transfer between our connected accounts by selecting the “Transfer between my accounts” option on the send to address page.

We will be transferring 32 HARDHATETH from our Hardhat1 to Hardhat2 account.

Upon confirming the send, you should be able to see 32 HARDHATETH being deducted from Hardhat1 and credited into Hardhat2.

In your Hardhat console, also notice that the transaction has been logged:

Congrats, your local Hardhat node is now connected to Metamask!

Do note that Metamask stores a nonce connected to the imported accounts hence if the local network is constantly restarted, you will need to enable the “Customize transaction nonce” option.

http://www.lryc.cn/news/61607.html

相关文章:

  • 【华为OD机试真题】1001 - 在字符串中找出连续最长的数字串含-号(Java C++ Python JS)| 机试题+算法思路+考点+代码解析
  • CrackMapExec 域渗透工具使用
  • Modbus协议学习
  • camunda如何处理流程待办任务
  • git部分文件不想提交解决方案
  • 2023年全国最新道路运输从业人员精选真题及答案58
  • Zimbra 远程代码执行漏洞(CVE-2019-9670)漏洞分析
  • 【数据结构初阶】第七节.树和二叉树的性质
  • 车载软件架构——闲聊几句AUTOSAR BSW(一)
  • 我国元宇宙行业分析:政策、技术、资金助推行业探索多元化应用场景
  • 都已经那么卷了,用户还需要开源的 API 管理工具么
  • 工信部教育与考试中心-软件测试工程师考试题A卷-答
  • 【设计模式】模板方法模式--让你的代码更具灵活性与可扩展性
  • 搞明白Redis持久化机制
  • C# 中的正则表达式,如何使用正则表达式进行字符串匹配和替换?
  • 7年时间,从功能测试到测试开发月薪30K,有志者事竟成
  • ES6 块级作用域
  • ShardingSphere-JDBC垂直分片
  • Node 04-http模块
  • 记录项目过程中的编译错误及解决方法(持续更新中)
  • Android Hilt依赖注入框架
  • LeetCode:59. 螺旋矩阵 II
  • 信息安全复习六:公开密钥密码学
  • YOLOv8 更换主干网络之 ShuffleNetv2
  • async/await最详细的讲解
  • 学习数据结构第6天(栈的基本概念)
  • 自动化添加时间戳版本号
  • 【C语言】指针进阶[上] (字符、数组指针、指针数组、数组传参和指针传参)
  • 软件测试外包干了4年,感觉废了..
  • ai改写句子软件-ai改写