From the previous Ethereum blog, we have learnt that Smart-contract contains a set of rules that govern the Blockchain. And to make working with Ethereum smart-contracts easy, a development environment called Truffle Suite was built. In this truffle ethereum tutorial, we will look at the following topics:

What Is Truffle Suite?

Truffle Suite is a development environment based on Ethereum Blockchain, used to develop DApps (Distributed Applications). Truffle is a one-stop solution for building DApps: Compiling Contracts, Deploying Contracts, Injecting it into a web app, Creating front-end for DApps and Testing.

Truffle Suite – Truffle Ethereum Tutorial

Truffle Suite has three components:

Truffle: It is a Development Environment, Testing Framework and Asset pipeline for Ethereum Blokchains

Ganache: Ganache is a personal Ethereum Blockchain used to test smart contracts where you can deploy contracts, develop applications, run tests and perform other tasks without any cost

Drizzle: Drizzle is a collection of libraries used to create easy and better front-end for Ethereum DApps

Features Of Truffle Ethereum

Here’s a list of features that makes Truffle a powerful tool to build Ethereum based DApps:

Built-in support to Compile, Deploy and Link smart contracts

Automated Contract testing

Supports Console apps as well as Web apps

Network Management and Package Management

Truffle console to directly communicate with smart contracts

Supports tight integration

What Is MetaMask?

MetaMask is an easy-to-use browser plugin (for Google-Chrome, Firefox and Brave browser), that provides a graphical user interface to make Ethereum transactions. It allows you to run Ethereum DApps on your browser without running a full Ethereum node on your system. Basically, MetaMask acts as a bridge between Ethereum Blockchain and the browser. MetaMask is open-source and provides the following exciting features:

You can change the code of MetaMask to make it what you want it to be

Provides built-in coin purchasing

Local-key Storage

Truffle MetaMask – Truffle Ethereum Tutorial

Now, that we know about Truffle and MetaMask, let’s get to the hands-on part of how to use these for DApps.

Installing Truffle and Creating a Truffle Project on Ubuntu

In this section of Truffle Ethereum tutorial, we will see how to install Truffle and how to create a Truffle project.

To install Truffle, you will have to run a simple command as below:

$ npm install -g truffle

Now, let’s get to creating a project in Truffle. First, let us create a new directory and get into that directory using the following command:

$ mkdir truffle-pro
$ cd truffle-pro

To create a project, execute the following command:

$ truffle unbox metacoin

When this command is successfully executed, you will see a project structure present in that directory with minimal files necessary for a project.

That’s it! You have created a simple Truffle Ethereum project.

Installing MetaMask On Google Chrome

In this section of Truffle Ethereum tutorial, we will look at how to install MetaMask plugin for Google-Chrome browser.

Now, on the Top-right corner of your browser, you can see the MetaMask icon.

Accept the Terms and Conditions.

And Bam! MetaMask is installed.

Now that we have Truffle Ethereum and MetaMask installed in the system, let’s see how we can develop a DApp using Truffle Ethereum and make transactions using MetaMask.

Installing TestRPC On Ubuntu

For this Truffle Ethereum tutorial, we will use “TestRPC”, which is a Blockchain emulator, to develop our DApp. TestRPC allows you to run a network for testing. It allows you to make calls to the Blockchain without running an actual Ethereum node.

To install TestRPC, run the following command:

$ npm install -g ethereumjs-testrpc

Demo: Developing A Simple DApp With Truffle And MetaMask And Making A Transaction

Open a new terminal and run TestRPC with the following command. This will start a test network on your system.

$ testrpc

You will see a list of available accounts, private keys for these accounts, a mnemonic phrase, and the port on which TestRPC is listening.

Note: Do not use the mnemonic phrase on the main Ethereum Network. Use it only on a private network.

Now, let’s setup truffle.

Open a new terminal and go into the directory where the project was created.

To run truffle on our network, we need to edit the “truffle.js” file. Open this file and make the following entries:

Now, we will have to compile the contract and migrate it to the network. The commands to do this is as follows:

$ truffle compile
$ truffle migrate

You can see that the code was successfully migrated and deployed on the network.

Now, open Chrome browser and click on the MetaMask icon. Click on “Import Existing DEN“. Enter the mnemonic phrase displayed when you executed the “testrpc” command, enter the password and click “Ok“.

By default, MetaMask runs on the main network. We don’t want to spend money just for a demo, right? For that reason, we have to change network to a private network. In our case, this network is Localhost 8545.

We can now see an account with 99+ ethers in it. “WOW! Free ethers!” Well, to disappoint you, these are not real ethers. These are test ethers provided only for testing purpose and has got no real-world value.

We need two accounts to make a transaction: a sender and a receiver. So, let’s create a new account. To do this, in the MetaMask plugin, click on “Switch accounts” and then click “Create Account“. Your new account is created.

Now, to send ethers to this account, we need to copy the address of this account.

For this Truffle Ethereum tutorial, we will send ethers from Account 1 to Account 2. So, let us switch the account back to Account 1. Here, click on “SEND“, enter the address to which you want to send the account (the address of Account 2 that I copied) and the number of ethers to be sent and click “NEXT“.

It will show you a summary of the transaction and ask for confirmation. Click “SUBMIT” and the transaction is done.

We can see now that there are 50 ethers less in Account 1.

To verify the transaction, switch to Account 2. Here, there are 50 ethers more. This shows that 50 ethers were transferred from Account 1 to Account 2.

Congratulations! You have created your first Truffle Ethereum DApp and made a transaction. I hope this truffle ethereum tutorial blog was informative and helped you understanding about Truffle. Now, go ahead and try building new DApps.

Got a question for us? Please post it on Edureka Community and we will get back to you.

If you wish to learn Blockchain and build a career in Blockchain Technologies, then check out our Blockchain Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand what is Blockchain in an exhaustive manner and help you achieve mastery over the subject.