Decentralized Programmer: Part 2 Truffle
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
Leave Decentralized Programmer: Part 2 Truffle to:
Read more #ethereum posts
Best Posts From cryptoizotx
We have not curated any of cryptoizotx's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From cryptoizotx
- Keyframe animations with Swift
- One algorithm a day: Keeping track of the largest element in a stack
- One algorithm a day. Write a function that returns best profit you could made from trading.
- One algorithm a day: floor of the square root of the input
- iOS Interview: Delegation Pattern in Swift
- Delegation Pattern in Objective-C
- Swift Interview Questions Recursion: Print numbers from 0 to n without using for loop.
- Bitwise Operators in Swift
- S O L I D Principle Rules in iOS
- Decentralized Programmer: Solidity 101
- Decentralized Programmer: My first Erc20 token on Ethereum blockchain!
- How to decentralize your resume
- Decentralized Programmer: Simple KuCoin Trading Bot
- Decentralized Programmer Part 6 My very first Dapp!
- Decentralized Programmer Part 5 Deployment of your contract with Remix IDE!
- Decentralized Programmer Part 4 web3 and specifying value of the transaction
- Decentralized Programmer: Part 3 Remix IDE + Ganache-CLI
- Decentralized Programmer: Part 2 Truffle
- Decentralized Programmer: Part 1 Environment Metamask and Ganache
- Crypto Market Sentiment Update, December 21, 2017