Friday, 8 June, 2018 UTC


Summary

In the early days of smart contract development (circa 2016) the way to go was to write smart contracts in your favorite text editor and deploy them by directly calling geth and solc.
The way to make this process a little bit more user friendly was to make bash scripts which could first compile and then deploy the contract … which was better, but still pretty rudimentary — the problem with scripting, of course, being the lack of standardization and the suboptimal experience of bash scripting.
The answer came in two distinct flavors — Truffle and Embark — with Truffle being the more popular of the two (and the one we’ll be discussing in this article).
To understand the reasoning behind Truffle, we must understand the problems it’s trying to solve, which are detailed below.
Compilation
Multiple versions of the solc compiler should be supported at the same time, with a clear indication which one is used.
Environments
Contracts need to have development, integration and production environments, each with their own Ethereum node address, accounts, etc.
Testing
The contracts must be testable. The importance of testing software can’t be overstated. For smart contracts, the importance is infinitely more important. So. Test. Your. Contracts!
Configuration
Your development, integration and production environments should be encapsulated within a config file so they can be committed to git and reused by teammates.
Web3js Integration
Web3.js is a JavaScript framework for enabling easier communication with smart contracts from web apps. Truffle takes this a step further and enables the Web3.js interface from within the Truffle console, so you can call web functions while still in development mode, outside the browser.
Installing Truffle
The best way to install Truffle is by using the Node Package Manager (npm). After setting up NPM on your computer, install Truffle by opening the terminal and typing this:
npm install -g truffle
Note: the sudo prefix may be required on Linux machines.
Getting Started
Once Truffle is installed, the best way to get a feel for how it works is to set up the Truffle demo project called “MetaCoin”.
Open the terminal app (literally Terminal on Linux and macOS, or Git Bash, Powershell, Cygwin or similar on Windows) and position yourself in the folder where you wish to initialize the project.
Then run the following:
mkdir MetaCoin
cd MetaCoin
truffle unbox metacoin
You should see output like this:
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!

Commands:

  Compile contracts: truffle compile
  Migrate contracts: truffle migrate
  Test contracts:    truffle test
If you get some errors, it could be that you’re using a different version of Truffle. The version this tutorial is written for is Truffle v4.1.5, but the instructions should stay relevant for at least a couple of versions.
The Truffle Project Structure
Your Truffle folder should look a little bit like this:
.
├── contracts
│   ├── ConvertLib.sol
│   ├── MetaCoin.sol
│   └── Migrations.sol
├── migrations
│   ├── 1_initial_migration.js
│   └── 2_deploy_contracts.js
├── test
│   ├── TestMetacoin.sol
│   └── metacoin.js
├── truffle-config.js
└── truffle.js
The post Introducing Truffle, a Blockchain Smart Contract Suite appeared first on SitePoint.