truffle部署智能合约

tech2022-09-03  179

truffle部署智能合约

In the early days of smart contract development (circa 2016) the way to go was to write smart contracts in your favorite text editor and deploy them by directly calling geth and solc.

在智能合约开发的早期(大约在2016年),方法是在您喜欢的文本编辑器中编写智能合约,然后直接调用geth和solc 。

The way to make this process a little bit more user friendly was to make bash scripts which could first compile and then deploy the contract … which was better, but still pretty rudimentary — the problem with scripting, of course, being the lack of standardization and the suboptimal experience of bash scripting.

使该过程更加用户友好的方法是制作bash脚本,该脚本可以先编译然后部署合同……虽然更好,但仍然非常基本-脚本问题当然是缺乏标准化和兼容性。 bash脚本的次优体验。

The answer came in two distinct flavors — Truffle and Embark — with Truffle being the more popular of the two (and the one we’ll be discussing in this article).

答案来自两种不同的口味- 松露和Embark-松露在两种口味中更受欢迎(这也是我们将在本文中讨论的一种)。

To understand the reasoning behind Truffle, we must understand the problems it’s trying to solve, which are detailed below.

要了解Truffle背后的原因,我们必须了解其试图解决的问题,下面将对其进行详细介绍。

Compilation Multiple versions of the solc compiler should be supported at the same time, with a clear indication which one is used.

在汇编多个版本solc编译器应该在同一时间得到支持,与一个使用明确的指示。

Environments Contracts need to have development, integration and production environments, each with their own Ethereum node address, accounts, etc.

环境合同需要具有开发,集成和生产环境,每个环境都具有自己的以太坊节点地址,账户等。

Testing The contracts must be testable. The importance of testing software can’t be overstated. For smart contracts, the importance is infinitely more important. So. Test. Your. Contracts!

测试合同必须是可测试的。 测试软件的重要性不可高估。 对于智能合约,重要性无限重要。 所以。 测试。 你的。 合同!

Configuration Your development, integration and production environments should be encapsulated within a config file so they can be committed to git and reused by teammates.

配置您的开发,集成和生产环境应封装在配置文件中,以便可以将它们提交给git并由队友重用。

Web3js Integration Web3.js is a JavaScript framework for enabling easier communication with smart contracts from web apps. Truffle takes this a step further and enables the Web3.js interface from within the Truffle console, so you can call web functions while still in development mode, outside the browser.

Web3js集成 Web3.js是一个JavaScript框架,用于实现与来自Web应用程序的智能合约更轻松的通信。 Truffle进一步迈出了一步,并在Truffle控制台中启用了Web3.js界面,因此您可以在仍处于开发模式下在浏览器外部调用Web函数。

安装松露 (Installing Truffle)

The best way to install Truffle is by using the Node Package Manager (npm). After setting up NPM on your computer, install Truffle by opening the terminal and typing this:

安装Truffle的最佳方法是使用Node Package Manager(npm)。 在计算机上设置NPM后,通过打开终端并键入以下命令来安装Truffle:

npm install -g truffle

Note: the sudo prefix may be required on Linux machines.

注意:Linux机器上可能需要sudo前缀。

入门 (Getting Started)

Once Truffle is installed, the best way to get a feel for how it works is to set up the Truffle demo project called “MetaCoin”.

一旦安装了Truffle,了解其工作原理的最佳方法就是设置名为“ MetaCoin”的Truffle演示项目。

Open the terminal app (literally Terminal on Linux and macOS, or Git Bash, Powershell, Cygwin or similar on Windows) and position yourself in the folder where you wish to initialize the project.

打开终端应用程序(在Linux和macOS上为终端机,在Windows上为Git Bash,Powershell,Cygwin或类似产品),然后将自己置于要初始化项目的文件夹中。

Then run the following:

然后运行以下命令:

mkdir MetaCoin cd MetaCoin truffle unbox metacoin

You should see output like this:

您应该看到如下输出:

Downloading... Unpacking... Setting up... Unbox successful. Sweet! Commands: Compile contracts: truffle compile Migrate contracts: truffle migrate Test contracts: truffle test

If you get some errors, it could be that you’re using a different version of Truffle. The version this tutorial is written for is Truffle v4.1.5, but the instructions should stay relevant for at least a couple of versions.

如果您遇到一些错误,则可能是您正在使用其他版本的Truffle。 本教程编写的版本是Truffle v4.1.5 ,但说明至少应与几个版本相关。

松露项目结构 (The Truffle Project Structure)

Your Truffle folder should look a little bit like this:

您的Truffle文件夹应如下所示:

. ├── contracts │ ├── ConvertLib.sol │ ├── MetaCoin.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_deploy_contracts.js ├── test │ ├── TestMetacoin.sol │ └── metacoin.js ├── truffle-config.js └── truffle.js

合同文件夹 (Contracts folder)

This is the folder where you will put all of your smart contracts.

这是您将所有智能合约放入的文件夹。

In your contracts folder, there’s also a Migrations.sol file, which is a special file — but more about that in the following section.

在您的Contracts文件夹中,还有一个Migrations.sol文件,这是一个特殊文件,但在下一节中将对此进行更多介绍。

When Truffle compiles the project, it will go through the contracts folder and compile all the compatible files. For now, the most used files are Solidity files with the .sol extension.

当Truffle编译项目时,它将通过contracts文件夹并编译所有兼容文件。 目前,最常用的文件是扩展名为.sol文件。

In the future, this might transition to Vyper or SolidityX (both better for smart contract development, but less used for now).

将来,这可能会过渡到Vyper或SolidityX(这两种方法对于智能合约开发都比较好,但现在很少使用)。

迁移文件夹 (Migrations Folder)

What is a truffle migration? In essence it’s a script which defines how the contracts will be deployed to the blockchain.

什么是松露迁移? 本质上,它是一个脚本,用于定义如何将合同部署到区块链。

为什么我们需要迁移? (Why do we need migrations?)

As your project becomes more and more complex, the complexity of your deployments becomes more and more complex accordingly.

随着项目变得越来越复杂,部署的复杂性也随之变得越来越复杂。

Let’s take an example:

让我们举个例子:

You have smart contracts One, Two and Three

您有智能合约One , Two和Three

The smart contract Three contains a reference to the smart contract One and requires the address of contract Two in its constructor.

智能合约Three包含对智能合约One的引用, 并需要其构造函数中合约Two的地址。

This example requires that contracts not only to be deployed sequentially, but also that they cross reference each other. Migrations, in a nutshell, enable us to automate this process.

此示例要求合同不仅要按顺序部署,而且还需要相互参照。 简而言之,迁移使我们能够自动执行此过程。

A rough overview of how you would do this would be as follows:

有关如何执行此操作的概述如下:

var One = artifacts.require("One"); var Two = artifacts.require("Two"); var Three = artifacts.require("Three"); module.exports = function(deployer) { deployer.deploy(One).then(function() { deployer.deploy(Two).then(function() { deployer.deploy(Three, One.address); }) }); };

Beyond that, migrations allow you to do a lot of other cool things like:

除此之外,迁移还可以让您执行许多其他很酷的事情,例如:

set max gas for deployments

设置部署的最大天然气

change the from address of deployments

改变from部署的地址

deploy libraries

部署库 call arbitrary contract functions

调用任意合约功能

初始迁移 (Initial migration)

As you’ve noticed in your MetaCoin project, you have a file called 1_initial_migration.js. What this file does is deploy the Migrations.sol contract to the blockchain.

正如您在MetaCoin项目中注意到的MetaCoin ,您有一个名为1_initial_migration.js的文件。 该文件的作用是将Migrations.sol合同部署到区块链。

Usually you don’t have to do anything to this file once it’s initialized, so we won’t focus too much on this.

通常,在初始化该文件后,您无需对其执行任何操作,因此我们不会对此进行过多关注。

测试文件夹 (Test Folder)

As I’ve said: YOU! MUST! TEST! SMART! CONTRACTS! No buts, no ifs, no maybes: you MUST do it.

就像我说的:你! 必须! 测试! 聪明! 合同! 不,但是,如果,就不是:您必须这样做。

But if you’re going to do it, it would be cool to have an automatic tool to enable you to do it seamlessly.

但是,如果您要执行此操作,那么拥有一个自动工具来无缝执行该操作将很酷。

Truffle enables this by having a built-in testing framework. It enables you to write tests either in Solidity or JavaScript.

松露通过内置的测试框架来实现这一目标。 它使您可以使用Solidity或JavaScript编写测试。

The examples in the MetaCoin project speak for themselves basically, so we won’t get too much into this.

MetaCoin项目中的示例基本上可以说明一切,因此我们不会对此进行过多介绍。

The key is, if you’re writing Solidity tests, you import your contracts into the tests with the Solidity import directive:

关键是,如果要编写Solidity测试,则可以使用Solidity import指令将合同导入到测试中:

import "../contracts/MetaCoin.sol";

And if you’re writing them in JavaScript, you import them with the artifacts.require() helper function:

如果您使用JavaScript编写它们,则可以使用artifacts.require()帮助函数导入它们:

var MetaCoin = artifacts.require("./MetaCoin.sol");

配置文件 (Configuration File)

The configuration file is called either truffle.js or truffle-config.js. In most cases it’ll be called truffle.js, but the fallback is there because of weird command precedence rules on Windows machines.

配置文件称为truffle.js或truffle.js truffle-config.js 。 在大多数情况下,其名称为truffle.js ,但由于Windows计算机上的命令优先级规则怪异,因此存在后备之处。

Just know that, when you see truffle.js or truffle-config.js, they’re the same thing, basically. (Also, don’t use CMD on windows; PowerShell is significantly better.)

只是知道,当您看到truffle.js或truffle.js truffle-config.js ,它们基本上是同一回事。 (此外,不要在Windows上使用CMD; PowerShell更好。)

The config file defines a couple of things, detailed below.

配置文件定义了几件事,下面将详细介绍。

Environments Develop, TestNet, Live (Production). You can define the address of the Geth note, the network_id, max gas for deployment, the gas price you’re willing to pay.

环境开发,TestNet,实时(生产)。 您可以定义Geth注释的地址, network_id ,可部署的最大天然气,您愿意支付的天然气价格。

Project structure You can change where the files are built and located, but it isn’t necessary or even recommended.

项目结构您可以更改文件的构建和位置,但这不是必需的,甚至也不建议这样做。

Compiler version and settings Fix the solc version and set the -O (optimization) parameters.

编译器版本和设置修复solc版本并设置-O (优化)参数。

Package management

包装管理

Truffle can work with EthPM (the Ethereum Package Manager), but it’s still very iffy.

Truffle可以与EthPM(以太坊软件包管理器)一起使用,但是仍然很困难。 You can set up dependencies for EthPM to use in your Truffle project.

您可以为EthPM设置依赖项以在Truffle项目中使用。

Project description Who made the project, what is the project name, contact addresses etc.

项目说明谁做项目,项目名称是什么,联系地址等。

License Use GPLv3.

许可证使用GPLv3 。

运行代码 (Running the Code)

In order to compile your contracts, run this:

为了编译您的合同,请运行此:

truffle compile

In order to run migrations, you can just use this:

为了运行迁移,您可以使用以下命令:

truffle migrate

Or you can do it by specifying an environment:

或者,您可以通过指定环境来做到这一点:

truffle migrate --network live

In order to test your contracts run this:

为了测试您的合同,请运行以下命令:

truffle test

Or you can run a specific test by running this:

或者,您可以通过运行以下命令来运行特定的测试:

truffle test ./path/to/FileTest.sol

结论 (Conclusion)

Truffle is a very handy tool that makes development in this brand new ecosystem a little easier. It aims to bring standards and common practices from the rest of the development world into a little corner of blockchain experimentation.

松露是一种非常方便的工具,它使在这个全新的生态系统中的开发更加容易。 其目的是将来自开发领域其他部分的标准和通用实践带入区块链实验的一个小角落。

This quick tutorial has demonstrated and explained the basics, but to truly understand Truffle, you’ll need to dive in deeper and experiment on actual projects. That’s what we’ll explore throughout SitePoint’s blockchain hub. We next take a look in a bit more detail at testing smart contracts and Truffle migrations.

该快速教程已经演示并解释了基础知识,但是要真正理解Truffle,您需要更深入地研究实际项目。 这就是我们将在SitePoint的整个区块链中心中探索的内容。 接下来,我们将更详细地介绍测试智能合约和Truffle迁移 。

翻译自: https://www.sitepoint.com/truffle-introduction/

truffle部署智能合约

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)