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
的新文件,內容如下:
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; } }
初始遷移
你可能已經註意到,運行 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>
根據編譯器版本,你可能會收到一些警告,但只要沒有錯誤,你就可以繼續了。
現在再次檢查你的項目目錄結構:
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; } }
請注意,現在有一個 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
文件中:
// 从 Storage.json 文件中获取 Storage 合约数据 var Storage = artifacts.require("./Storage.sol"); // JavaScript 导出 module.exports = function(deployer) { // deployer 是 Truffle 用于将合约部署到网络的包装器 // 将合约部署到网络 deployer.deploy(Storage); }
這僅僅意味著你正在將你的合約部署到在 localhost:8545 上運行的網絡。
現在運行 truffle migrate
。你應該得到類似這樣的輸出:(輸出略,與原文相同)
Truffle 將你的合約遷移到網絡並保存了工件。在 build
目錄中,在 Storage.json
文件中,通過檢查 networks
對象來驗證這是正確的。你應該看到類似這樣的內容:(內容略,與原文相同)
...(後續內容與原文相同,包括多個合約,網絡,賬戶,庫的處理,以及最後的總結和FAQ,這裡不再重複。)
以上是松露遷移解釋了的詳細內容。更多資訊請關注PHP中文網其他相關文章!