Truffle 迁移:自动化部署智能合约的利器
迁移(Migrations)是开发者自动化部署数据及其支持结构的一种方法。它们在管理新软件版本的部署方面非常有用,并不局限于区块链开发。
Truffle 迁移使我们能够将智能合约“推送”到以太坊区块链(本地、测试网或主网),并设置必要的步骤来连接合约以及填充合约的初始数据。
Truffle 迁移的真正优势在于管理区块链上的合约地址。这个通常很繁琐的工作通过 Truffle 几乎完全被抽象掉了。
关键要点
truffle compile
编译合约以生成有助于合约与区块链之间交互的工件至关重要。前提条件
确保已安装 Truffle 框架和 Ganache CLI。
入门
首先,选择一个项目文件夹,然后运行 truffle init
。你应该得到类似这样的输出:
<code>Downloading... Unpacking... Setting up... Unbox successful. Sweet! Commands: Compile: truffle compile Migrate: truffle migrate Test contracts: truffle test</code>
此命令在您所在目录中创建一个基本的 Truffle 项目。目录结构如下所示:
<code>. ├── contracts │ └── Migrations.sol ├── migrations │ └── 1_initial_migration.js ├── test ├── truffle-config.js └── truffle.js</code>
首先,在 contracts
目录中,创建一个名为 Storage.sol
的新文件,内容如下:
<code class="language-solidity">pragma solidity ^0.4.21; contract Storage { mapping (string => string) private _store; function addData(string key, string value) public { require(bytes(_store[key]).length == 0); _store[key] = value; } function removeData(string key) public returns (string) { require(bytes(_store[key]).length != 0); string prev = _store[key]; delete _store[key]; return prev; } function changeData(string key, string newValue) public { require(bytes(_store[key]).length != 0); _store[key] = newValue; } }</code>
初始迁移
你可能已经注意到,运行 truffle init
时会创建两个文件:Migrations.sol
和 1_initial_migration.js
。
初始迁移文件很少需要更改。它们的作用本质上是跟踪区块链上的地址。
Migrations.sol
文件可以按照你想要的方式编写,但它必须符合 truffle init
命令创建的固定接口。你可以在这些文件中进行一些高级的迁移操作,但正如我所说,这很少需要。
1_initial_migration.js
文件也是如此。它的作用只是将 Migrations.sol
文件推送到目标区块链。
迁移数据
为了将智能合约部署到以太坊区块链,你必须首先编写迁移文件。首先,在你的 migrations
目录中,创建一个名为 2_deploy_contracts.js
的文件。你的项目结构现在应该如下所示:
<code>Downloading... Unpacking... Setting up... Unbox successful. Sweet! Commands: Compile: truffle compile Migrate: truffle migrate Test contracts: truffle test</code>
为了使用迁移部署智能合约,我们首先需要访问它们的 工件。这些文件描述了合约地址、已部署合约的网络以及合约具有的函数。
那么所有这些数据从哪里来呢?
在你的项目目录中,运行 truffle compile
。如果一切顺利,你应该得到类似这样的输出:
<code>. ├── contracts │ └── Migrations.sol ├── migrations │ └── 1_initial_migration.js ├── test ├── truffle-config.js └── truffle.js</code>
根据编译器版本,你可能会收到一些警告,但只要没有错误,你就可以继续了。
现在再次检查你的项目目录结构:
<code class="language-solidity">pragma solidity ^0.4.21; contract Storage { mapping (string => string) private _store; function addData(string key, string value) public { require(bytes(_store[key]).length == 0); _store[key] = value; } function removeData(string key) public returns (string) { require(bytes(_store[key]).length != 0); string prev = _store[key]; delete _store[key]; return prev; } function changeData(string key, string newValue) public { require(bytes(_store[key]).length != 0); _store[key] = newValue; } }</code>
请注意,现在有一个 build
文件夹,其中包含两个文件——Migrations.json
和 Storage.json
——它们与 contracts
目录中的智能合约文件相匹配。
这些 *.json 文件包含它们各自智能合约的描述。描述包括:
此文件使 Truffle 能够创建用于与智能合约通信的 JavaScript 包装器。例如,当你在 JavaScript 代码中调用 contract.address
时,Truffle 框架会从 *.json 文件中读取地址,并实现合约版本和网络之间的轻松转换。
编写迁移文件
有了这些知识,让我们编写第一个迁移文件。在 2_deploy_contracts.js
文件中,写入以下内容:
<code>. ├── contracts │ ├── Migrations.sol │ └── Storage.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_deploy_contracts.js ├── test ├── truffle-config.js └── truffle.js</code>
编写迁移文件就这么简单。为了运行迁移脚本,在终端中运行以下命令:
<code>Compiling ./contracts/Migrations.sol... Compiling ./contracts/Storage.sol... Writing artifacts to ./build/contracts</code>
你应该会收到一条错误消息:
<code>. ├── build │ └── contracts │ ├── Migrations.json │ └── Storage.json ├── contracts │ ├── Migrations.sol │ └── Storage.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_deploy_contracts.js ├── test ├── truffle-config.js └── truffle.js</code>
这意味着 Truffle 找不到你想要部署到的网络。
为了使用模拟的以太坊区块链,在新终端标签页中运行 ganache-cli
。你应该得到类似这样的输出:(输出略,与原文相同)
这意味着你已经启动了一个私有区块链,它正在 localhost:8545 上运行。现在让我们设置 Truffle 以部署到该网络。
将以下内容放入 truffle.js
文件中:
<code class="language-javascript">// 从 Storage.json 文件中获取 Storage 合约数据 var Storage = artifacts.require("./Storage.sol"); // JavaScript 导出 module.exports = function(deployer) { // deployer 是 Truffle 用于将合约部署到网络的包装器 // 将合约部署到网络 deployer.deploy(Storage); }</code>
这仅仅意味着你正在将你的合约部署到在 localhost:8545 上运行的网络。
现在运行 truffle migrate
。你应该得到类似这样的输出:(输出略,与原文相同)
Truffle 将你的合约迁移到网络并保存了工件。在 build
目录中,在 Storage.json
文件中,通过检查 networks
对象来验证这是正确的。你应该看到类似这样的内容:(内容略,与原文相同)
...(后续内容与原文相同,包括多个合约,网络,账户,库的处理,以及最后的总结和FAQ,这里不再重复。)
以上是松露迁移解释了的详细内容。更多信息请关注PHP中文网其他相关文章!