Decentralized Programmer: Part 2 Truffle

in #ethereum7 years ago

internet.png

In this tutorial you will learn about Truffle. Truffle is must-have tool for an Ethereum programmer. It serves several purposes:

  • development environment
  • testing framework
  • deployment pipeline

Installation

$ npm install -g truffle

Usage:

In terminal open a folder that you want to use for working on your solidity project and type:

truffle init

Here is what you will see:

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

Commands:

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

If you check the contents of the folder you will see the following new items there:

  • contracts/: Directory for Solidity contracts
  • migrations/: Directory for scriptable deployment files
  • test/: Directory for test files for testing your application and contracts
  • truffle.js: Truffle configuration file
  • truffle-config.js: Truffle configuration file

You can use following commands with Truffle.

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

Type the truffle compile in the terminal:

truffle compile
Compiling ./contracts/Migrations.sol...

Compilation warnings encountered:

/Users/jc_admin/Documents/GitHub/smart-contracts/TruffleFun/contracts/Migrations.sol:11:3: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
  function Migrations() public {
  ^ (Relevant source part starts here and spans across multiple lines).

Writing artifacts to ./build/contracts

What happened here? Truffle tried to compile your code, checking it for correct syntax during the process. You received a following warning:

Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }"

Open a contracts/migration.sol in any text editor. You should be able to see following:

pragma solidity ^0.4.17;

contract Migrations {
  address public owner;
  uint public last_completed_migration;

  modifier restricted() {
    if (msg.sender == owner) _;
  }

  function Migrations() public {
    owner = msg.sender;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }

  function upgrade(address new_address) public restricted {
    Migrations upgraded = Migrations(new_address);
    upgraded.setCompleted(last_completed_migration);
  }
}

Our suspect is a Migrations function, in the newest version of Solidity we should use constructor(). Go ahead and make a change and replace Migrations() with constructor():

pragma solidity ^0.4.17;

contract Migrations {
  //code removed for clarity

  function constructor() {
    owner = msg.sender;
  }

  //code removed for clarity

Run the truffle compile again.

 truffle compile
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts

This time your code executed without any warning!

Good job! You learn basics about Truffle, very powerful framework for working with Ethereum. You also found and correct a warning, that's a very important step in debugging your software.

If you want to learn more about Truffle visit the following page: http://truffleframework.com/docs/

Resources

https://github.com/trufflesuite/truffle