Truffle Migrations Explained

Migrations, generally speaking, are ways for developers to automate the deployment of data and its supporting structures. They are very useful for managing the deployment of new software versions, and as such aren’t exclusive to blockchain development.

Truffle migrations enable us to “push” the smart contracts to the Ethereum blockchain (either local, tesnet or mainnet) and to set up necessary steps for linking contracts with other contracts as well as populate contracts with initial data.

Where migrations really shine is the management of contract addresses on the blockchain. This usually tedious job gets almost entirely abstracted away with Truffle.

Prerequisites

Make sure that you have installed the Truffle Framework and Ganache CLI.

Getting Started

For starters, choose a project folder and then run truffle init. You should get an output similar to this:

Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!

Commands:

  Compile:        truffle compile
  Migrate:        truffle migrate
  Test contracts: truffle test

This command creates a barebones Truffle project in the directory where you’re positioned. The directory looks like this:

.
├── contracts
│   └── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
├── test
├── truffle-config.js
└── truffle.js

For starters, in the contracts directory, create a new file called Storage.sol, which should look like this:

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 Migrations

As you might have noticed, two files are created when you run truffle init. They are Migrations.sol and 1_initial_migration.js.

The initial migration files rarely need to be changed. What they do is essentially keep track of addresses on the blockchain.

The Migrations.sol file can look any way you want it to, but it must conform to a fixed interface which looks like the interface created by the truffle init command. What you can do in those files is some advanced mangling of migrations, but as I’ve said, it’s rarely needed.

The same goes for the 1_initial_migration.js file. What it does is simply push the Migrations.sol file to the desired blockchain.

Migrations Data

In order to deploy the smart contracts to the Ethereum blockchain, you must first write migrations. In order to get started, in your migrations directory, create a file called 2_deploy_contracts.js. Your project structure should now look like this:

.
├── contracts
│   ├── Migrations.sol
│   └── Storage.sol
├── migrations
│   ├── 1_initial_migration.js
│   └── 2_deploy_contracts.js
├── test
├── truffle-config.js
└── truffle.js

In order to deploy smart contracts with migrations, first we need to access their artifacts. These are files which describe the contract addresses, the networks on which the contracts have been deployed and the functions which contracts have.

So where does all of this data come from?

In your project directory, run truffle compile. If all goes well, you should have an output similar to this:

Compiling ./contracts/Migrations.sol...
Compiling ./contracts/Storage.sol...

Writing artifacts to ./build/contracts

Depending on the compiler version, you might get some warnings, but as long as there are no errors, you’re good to go.

Now check your project directory structure again:

.
├── 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

Notice 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. The description includes:

  • Contract name
  • Contract ABI (Application Binary Interface — a list of all the functions in the smart contracts along with their parameters and return values)
  • Contract bytecode (compiled contract data)
  • Contract deployed bytecode (the latest version of the bytecode which was deployed to the blockchain)
  • The compiler version with which the contract was last compiled
  • A list of networks onto which the contract has been deployed and the address of the contract on each of those networks.

This file enables Truffle to create a JavaScript wrapper for communicating with the smart contract. For example, when you call contract.address in your JavaScript code, the Truffle framework reads the address from the *.json file and enables effortless transitions between contract versions and networks.

The post Truffle Migrations Explained appeared first on SitePoint.


Source: Sitepoint