Develop Ethereum DApps with Truffle

Aryya Paul
Edureka
Published in
7 min readJan 8, 2019
Truffle Ethereum Tutorial — Edureka

From the previous article on Ethereum, we have learned 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

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

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.

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 tab

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.

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.

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
}
}
};

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.

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.

With this, we come to an end to this article on Developing Ethereum DApps with Truffle. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Blockchain.

1. Blockchain Tutorial

2. Ethereum Tutorial

3. Ethereum Private Networks

4. What are Smart Contracts?

5. Solidity Tutorial

6. Best Ethereum Development Tools

7. Hyperledger Fabric

8. Hyperledge vs Ethereum

Originally published at www.edureka.co on January 8, 2019.

--

--