Neon EVM:如何使用 Hardhat 部署 Neon dApps (Neon EVM:How to Deploy Neon dApps Using Hardhat)

The third and final article in our Deploying Neon dApps series explores how to deploy Solidity dApps using Hardhat. (We previously discussed how to deploy Neon with Remix and Truffle.

我们部署 Neon dApps 系列的第三篇也是最后一篇文章探讨了如何使用 Hardhat 部署 Solidity dApp。 (我们之前讨论过如何使用 RemixTruffle。)

Hardhat is a development and deployment tool for EVM contracts and applications. Of the three deployment tools explored in this series, Hardhat is the most versatile, allowing for plenty of customizable settings and secure deployment to Neon’s mainnet, devnet, and testnet. However, unlike Remix, hHardhat can only be used via a command-line interface, and is not an IDE.

Hardhat 是用于 EVM 合约和应用的开发和部署工具。在本系列探讨的三种部署工具中,Hardhat 是最通用的,它允许大量可自定义的设置并安全地部署到 Neon 的主网、开发网和测试网。但是,与 Remix 不同的是,Hardhat 只能通过命令行界面使用,而不是 IDE。

准备工作(Before You Begin)

Before you start the tutorial below, make sure that the following is true:

在开始下面的教程之前,请确保满足以下条件:

  • MetaMask is installed on your device.
    MetaMask 已安装在您的设备上。

  • MetaMask is configured for the Neon EVM.
    MetaMask已为Neon EVM 配置过。

  • NodeJS v8.9.4 or later is installed on your device.
    NodeJS v8.9.4 或更高版本已安装在您的设备上。

  • Git is installed on your device. On Windows, use Git Bash.
    Git 已安装在您的设备上。在 Windows 上,使用 Git Bash。

Follow this guide to install and configure MetaMask.

按照本指南安装和配置 MetaMask

ERC-20 教程 (ERC-20 Tutorial)

The following tutorial will explain how to deploy a simple ERC-20 token contract to the Neon Devnet using Hardhat.

以下教程将解释如何使用 Hardhat 将简单的 ERC-20 代币合约部署到 Neon Devnet。

第1步:安装Hardhat (Step 1: Install Hardhat)

Using Git, clone the ERC-20 Hardhat project from Neon’s remote repository and navigate to it with the following commands:

使用 Git,从 Neon 的远程存储库中克隆 ERC-20 Hardhat 项目,并使用以下命令导航到该项目:

git clone https://github.com/neonlabsorg/examples.git

cd examples/simple-erc20-hardhat

Then, run the following command to install Hardhat and the dependencies necessary to run the example project:

然后,运行以下命令来安装 Hardhat 和运行示例项目所需的依赖项:

npm install

If the above command results in an error, run the following command:

如果上述命令导致错误,请运行以下命令:

npm cache clear --force

npm install

第2步:设置 MetaMask 帐户 (Step 2: Set Up MetaMask Accounts)

To interact with the soon-to-be-deployed contracts, you’ll need to create two new accounts in MetaMask. Before you begin this step, make sure that MetaMask is connected to the Neon Devnet.

要与即将部署的合约交互,您需要在 MetaMask 中创建两个新账户。在开始此步骤之前,请确保 MetaMask 已连接到 Neon Devnet。

In MetaMask, create two new accounts. To create a new account in MetaMask, click on your current account’s icon in the top right of the MetaMask extension pop-up. A drop-down menu will appear. From the drop-down menu, select“Create an Account”.

在 MetaMask 中,创建两个新帐户。要在 MetaMask 中创建新帐户,请单击 MetaMask 扩展弹出窗口右上角的当前帐户图标。将出现一个下拉菜单。从下拉菜单中,选择“创建帐户”。

Then, obtain some Devnet NEON tokens for these accounts using the NeonFaucet.

然后,使用 NeonFaucet 为这些帐户获取一些 Devnet NEON代币。

Next, click on the three vertical dots to the right of your currently displayed account name and wallet address. In this drop-down menu that opens, select “Account Details” then “Export Private Key”. Enter your password and select “Confirm” to get access to the private key for that account.

接下来,单击您当前显示的帐户名称和钱包地址右侧的三个垂直点。在打开的下拉菜单中,选择“帐户详细信息”,然后选择“导出私钥”。输入您的密码并选择“确认”,访问该帐户的私钥。

Copy both accounts’ private keys and paste them into the hardhat.config.js file in the project folder, replacing the placeholder text in lines 11 and 12 of that file. Make sure to prefix these keys with “0x” in the configuration file.

复制两个帐户的私钥并将它们粘贴到项目文件夹中的 hardhat.config.js 文件中,替换该文件第 11 行和第 12 行中的占位符文本。确保在配置文件中为这些键添加前缀“0x”。

第3步:编译合约 (Step 3: Compile Contracts)

To compile the project’s contracts (located in the “contracts/” folder), run the following command:

要编译项目的合约(位于“contracts/”文件夹中),请运行以下命令:

./node_modules/.bin/hardhat compile

The output from this command should be similar to the following:

此命令的输出应类似于以下内容:

第4步:运行测试 (Step 4: Run Tests)

To run the project tests before deploying, run the following command. It will compile the contracts, deploy them to the Neon Devnet, and run all the tests in the “tests/” folder to make sure the contracts are working as expected.

要在部署之前运行项目测试,请运行以下命令。它将编译合约,将它们部署到 Neon Devnet,并运行“tests/”文件夹中的所有测试,确保合约按预期工作。

./node_modules/.bin/hardhat test

If the tests all pass, the output of this command should look something like this:

如果测试全部通过,则此命令的输出应如下所示:

第5步:部署合约 (Step 5: Deploy Contracts)

To deploy the project’s contracts, simply run the command below to run the deployment script in the “scripts/” directory:

要部署项目的合约,只需运行以下命令以运行“scripts/”目录中的部署脚本:

./node_modules/.bin/hardhat run ./scripts/deploy.js

You should see output in your terminal similar to the following:

您应该会在终端中看到类似于以下内容的输出:

第6步:将项目连接到 (MetaMaskStep 6: Connect Project to MetaMask)

To use this newly deployed ERC-20-style Neon Devnet token with ease, follow these instructions to import the token as an asset in MetaMask. Use the “contract address” from the output in Step 5 for the “Token Contract Address” in the instructions.

要轻松使用这个新部署的 ERC-20 式 Neon Devnet 代币,请按照 这些说明将代币作为资产导入 MetaMask。将步骤 5 输出中的“合约地址”用于说明中的“代币合约地址”。

Once you complete this final step, you will be able to see your new ERC-20 assets in the MetaMask profiles of the new test accounts.

完成最后一步后,您将能够在新测试帐户的 MetaMask 配置文件中看到您的新 ERC-20 资产。

结论 (Conclusion)

Of the three tools for Neon deployment explored over the course of this article series, Hardhat is the most useful and versatile. With a variety of customization options available, plus the ability to deploy to Mainnet due to no security vulnerabilities like with Truffle, Hardhat is the go-to tool for developers looking to launch their project into the “real world” using Neon.

在本系列文章中探讨的用于 Neon 部署的三种工具中,Hardhat 是最有用和最通用的。凭借各种可用的自定义选项,以及没有Truffle类似的安全漏洞,而有能力可以部署到主网,Hardhat 是希望使用 Neon 将项目启动到“现实世界”的开发人员的首选工具。

Visit the Neon docs for a complete tutorial on using Hardhat to deploy your dApps.

访问 Neon 文档,获取有关使用 Hardhat 部署 dApp 的完整教程。

赞赏