Ethereum Development Certification (9 Blogs) Become a Certified Professional
AWS Global Infrastructure

Blockchain

Topics Covered
  • Blockchain (23 Blogs)
  • Ethereum Development Certification (9 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Truffle Ethereum Tutorial – Developing Ethereum DApps with Truffle

Last updated on May 22,2019 2.5K Views
Omkar S Hiremath
Tech Enthusiast in Blockchain, Hadoop, Python, Cyber-Security, Ethical Hacking. Interested in anything... Tech Enthusiast in Blockchain, Hadoop, Python, Cyber-Security, Ethical Hacking. Interested in anything and everything about Computers.
5 / 7 Blog from Ethereum

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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:

  1. What is Truffle Suite?
  2. Features of Truffle Ethereum
  3. What is MetaMask?
  4. Installing Truffle and creating a Truffle project on Ubuntu 
  5. Installing MetaMask on Google Chrome
  6. Installing TestRPC on Ubuntu
  7. Demo: Developing a simple DApp with truffle and MetaMask and making a transaction

If you are interested in becoming an Ethereum developer, you might want to look into this Ethereum Developer Live Training.

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 - Edureka

Truffle Suite – Truffle Ethereum Tutorial

Truffle Suite has three components: 

  1. Truffle : It is a Development Environment, Testing Framework and Asset pipeline for Ethereum Blokchains
  2. 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
  3. 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


MetaMask - Truffle Ethereum tutorial - Edureka

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.

Create truffle project - Truffle Ethereum tutorial - Edureka

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.

Here are the steps to install MetaMask browser plugin:

  1. First go to the following link: https://metamask.io/
  2. Click on “GET CHROME EXTENSION” button. This will open a new tabMetaMask Installation - Truffle Ethereum tutorial - Edureka
  3. Click on the “Add to Chrome” button and then “Add Extension“.
  4. Now, on the Top-right corner of your browser, you can see the MetaMask icon. MetaMask Plugin - Truffle Ethereum tutorial - Edureka
  5. 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.

TestRPC - Truffle Ethereum tutorial - Edureka

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:

module.exports = {
      networks: {
            development: {
                  host: 'localhost',
                  port: 8545,
                  network_id: '*' //* will match to any network id
                  }
         }
};

truffle.js - Truffle Ethereum tutorial - Edureka

Save the file and exit.

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. 

truffle compile - Truffle Ethereum tutorial - Edureka

truffle migrate - Truffle Ethereum tutorial - Edureka

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“.

MetaMask import - Truffle Ethereum tutorial - Edureka

MetaMask login - Truffle Ethereum tutorial - Edureka

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.

MetaMask network - Truffle Ethereum tutorial - Edureka

MetaMask account1 - Truffle Ethereum tutorial - Edureka

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.

MetaMask account2 - Truffle Ethereum tutorial - Edureka

MetaMask create account - Truffle Ethereum tutorial - Edureka

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

MetaMask copy - Truffle Ethereum tutorial - Edureka

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“.

MetaMask send - Truffle Ethereum tutorial - Edureka

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

MetaMask transaction - Truffle Ethereum tutorial - Edureka

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

MetaMask account1 balance - Truffle Ethereum tutorial - Edureka

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.

MetaMask account2 balance - Truffle Ethereum tutorial - Edureka

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.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.