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


As a developer, you know the importance of understanding how to deploy the smart contract code you’ve written onto the blockchain. Our Deploying Neon dApps article series provides this crucial information in a simple and straightforward way. This article, the second in the series, will show you how to deploy Solidity dApps using Truffle. You can also check out our articles on deploying Neon with Remix and Hardhat.

作为开发人员,您知道这件事的重要性:了解如何将您编写的智能合约代码部署到区块链上。我们的 Deploying Neon dApps(部署Neon dApps) 系列文章以简单直接的方式提供了这些重要信息。本文是该系列的第二篇,将向您展示如何使用 Truffle 部署 Solidity dApp。您还可以查看我们关于使用 Remix 和 Hardhat 部署 Neon 的文章。

Truffle is a deployment tool, development environment, and asset pipeline that makes it easy for developers to test and debug their code on-chain. Unlike Remix, which only supports a few functions and can’t run automated tests, Truffle is an advanced and flexible tool that can support much more complex code. However, Truffle can only be used via a command-line interface and is not an IDE.

Truffle 是一个部署工具、开发环境和资产管道,可让开发人员轻松测试和调试其链上代码。与仅支持少数功能且无法运行自动化测试的 Remix 不同,Truffle 是一种先进且灵活的工具,可以支持更复杂的代码。但是,Truffle 只能通过命令行界面使用,而不是 IDE。

To deploy your contracts successfully, you will need to modify the publicly available Truffle configuration file, which contains the private keys for the wallets the contract interacts with. For this reason, you should only use Truffle for debugging or testing purposes and not for mainnet deployment.

要成功部署您的合约,您需要修改公开可用的 Truffle 配置文件,其中包含与合约交互的钱包的私钥。出于这个原因,你应该只将 Truffle 用于调试或测试目的,而不是用于主网部署。

Before You Begin
准备工作

Before you start the tutorial below, make that the following is true:
在开始下面的教程之前,请确保以下内容为真:

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

  • MetaMask is configured for 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)

To install and configure MetaMask, follow this guide.
要安装和配置 MetaMask,请按照 本指南

ERC-20 Tutorial
ERC-20 教程

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

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

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

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

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

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

cd examples/simple-erc20-truffle

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

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

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 the MetaMask Accounts and Configuration File)

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

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

In MetaMask, create two new accounts by clicking on your current account’s icon in the top right of the MetaMask extension pop-up, and then when the drop-down menu appears, click on “Create an Account”. Then, use the NeonFaucet to obtain some Devnet NEON tokens for these accounts.

在 MetaMask 中,通过单击 MetaMask 扩展弹出窗口右上角的当前帐户图标创建两个新帐户,然后在出现下拉菜单时单击“创建帐户”。然后,使用 NeonFaucet 为这些帐户获取一些 Devnet NEON代币。

Next, click on the three vertical dots to the right of your currently displayed account name and wallet address to open a drop-down menu. 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 truffle-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.

接下来,单击您当前显示的帐户名称和钱包地址右侧的三个垂直点,打开下拉菜单。选择“帐户详细信息”,然后选择“导出私钥”。输入您的密码并选择“确认”,访问该帐户的私钥。复制两个帐户的私钥并将它们粘贴到项目文件夹中的 truffle-config.js 文件中,替换该文件第 11 行和第 12 行中的占位符文本。确保在配置文件中为这些键添加前缀“0x”。

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

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

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

./node_modules/.bin/truffle 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/truffle test --network neonlabs

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

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

第5步:运行迁移 (Step 5: Run Migrations)

Deploying the contracts to Neon requires running some migration scripts, which are located in the “migrations/” folder. To do this, run the following command:

将合约部署到 Neon 需要运行一些迁移脚本,这些脚本位于“migrations/”文件夹中。为此,请运行以下命令:

./node_modules/.bin/truffle migrate --network neonlabs

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

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

第6步:将项目连接到 MetaMask (Step 6: Connect the 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)

Truffle is a versatile, flexible solution for deploying dApps on Neon, with a lot of room for customization and playing with advanced features. However, because it has some security vulnerabilities when deploying to Neon, it remains usable only for testing and development. Therefore, while Truffle is great for Neon developers during the development process itself, the final deployment must be done with a different tool.

Truffle 是用于在 Neon 上部署 dApp 的多功能、灵活的解决方案,具有很大的定制和使用高级功能的空间。但是,由于它在部署到 Neon 时存在一些安全漏洞,因此只能用于测试和开发。因此,虽然 Truffle 在开发过程中非常适合 Neon 开发人员,但最终部署必须使用不同的工具来完成。

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

访问 Neon docs,获取有关使用 Truffle 部署 dApp 的完整教程。

赞赏