Truffle migration: a powerful tool for automated deployment of smart contracts
Migrations is a way for developers to automate the deployment of data and its support structure. They are very useful in managing the deployment of new software versions and are not limited to blockchain development.
Truffle migration allows us to "push" smart contracts to the Ethereum blockchain (local, testnet or mainnet) and set the necessary steps to connect the contract and populate the initial data of the contract.
The real advantage of Truffle migration is to manage contract addresses on the blockchain. This usually tedious work is almost completely abstracted through Truffle.
Key Points
truffle compile
to compile contracts to generate artifacts that facilitate the interaction between the contract and the blockchain before running the migration. Precautions
Make sure the Truffle framework and Ganache CLI are installed.
Beginner
First, select a project folder and run truffle init
. You should get an output like this:
<code>Downloading... Unpacking... Setting up... Unbox successful. Sweet! Commands: Compile: truffle compile Migrate: truffle migrate Test contracts: truffle test</code>
This command creates a basic Truffle project in your directory. The directory structure is as follows:
<code>. ├── contracts │ └── Migrations.sol ├── migrations │ └── 1_initial_migration.js ├── test ├── truffle-config.js └── truffle.js</code>
First, in the contracts
directory, create a new file named Storage.sol
with the following content:
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; } }
Initial migration
You may have noticed that two files are created when running truffle init
: Migrations.sol
and 1_initial_migration.js
.
Initial migration files rarely require changes. Their role is essentially tracking addresses on the blockchain.
TheMigrations.sol
file can be written the way you want, but it must comply with the fixed interface created by the truffle init
command. You can do some advanced migrations in these files, but as I said, this is rarely needed.
1_initial_migration.js
Files. Its purpose is simply to push the Migrations.sol
file to the target blockchain.
Migrate data
In order to deploy smart contracts to the Ethereum blockchain, you must first write the migration file. First, in your migrations
directory, create a file named 2_deploy_contracts.js
. Your project structure should now look like this:
<code>Downloading... Unpacking... Setting up... Unbox successful. Sweet! Commands: Compile: truffle compile Migrate: truffle migrate Test contracts: truffle test</code>
In order to deploy smart contracts using migration, we first need to access their artifacts. These files describe the contract address, the network where the contract has been deployed, and the functions that the contract has.
So where does all this data come from?
In your project directory, run truffle compile
. If everything goes well, you should get an output like this:
<code>. ├── contracts │ └── Migrations.sol ├── migrations │ └── 1_initial_migration.js ├── test ├── truffle-config.js └── truffle.js</code>
Depending on the compiler version, you may receive some warnings, but as long as there are no errors, you can continue.
Now check your project directory structure again:
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; } }
Note that there is now a build
folder containing two files - Migrations.json
and Storage.json
- which match the smart contract files in the contracts
directory.
These *.json files contain descriptions of their respective smart contracts. Descriptions include:
This file enables Truffle to create JavaScript wrappers for communicating with smart contracts. For example, when you call contract.address
in JavaScript code, the Truffle framework reads the address from the *.json file and implements easy conversion between the contract version and the network.
Writing migration files
With this knowledge, let's write the first migration file. In the 2_deploy_contracts.js
file, write the following:
<code>. ├── contracts │ ├── Migrations.sol │ └── Storage.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_deploy_contracts.js ├── test ├── truffle-config.js └── truffle.js</code>
Writing a migration file is that simple. To run the migration script, run the following command in the terminal:
<code>Compiling ./contracts/Migrations.sol... Compiling ./contracts/Storage.sol... Writing artifacts to ./build/contracts</code>
You should receive an error message:
<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>
This means that Truffle cannot find the network you want to deploy to.
To use the simulated Ethereum blockchain, run ganache-cli
in the new terminal tab. You should get an output like this: (The output is omitted, the same as the original text)
This means you have started a private blockchain that is running on localhost:8545. Now let's set up Truffle to deploy to the network.
Put the following into the truffle.js
file:
// 从 Storage.json 文件中获取 Storage 合约数据 var Storage = artifacts.require("./Storage.sol"); // JavaScript 导出 module.exports = function(deployer) { // deployer 是 Truffle 用于将合约部署到网络的包装器 // 将合约部署到网络 deployer.deploy(Storage); }
This simply means that you are deploying your contract to a network running on localhost:8545.
Run now truffle migrate
. You should get an output like this: (The output is omitted, the same as the original text)
Truffle Migrates your contract to the network and saves artifacts. In the build
directory, in the Storage.json
file, verify that this is correct by checking the networks
object. You should see something like this: (The content is omitted, the same as the original text)
...(The subsequent content is the same as the original text, including the processing of multiple contracts, networks, accounts, libraries, as well as the final summary and FAQ, which will not be repeated here.)
The above is the detailed content of Truffle Migrations Explained. For more information, please follow other related articles on the PHP Chinese website!