Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
If you are interested in becoming an Ethereum developer, you might want to look into this Blockchain course.
Find out our Blockchain Training in Top Cities/Countries
India | Other Cities/Countries |
Bangalore | New York |
Hyderabad | UK |
Kerala | USA |
Chennai | Canada |
Mumbai | Australia |
Pune | Singapore |
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:
Here’s a list of features that makes Truffle a powerful tool to build Ethereum based DApps:
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:
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.
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.
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:
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.
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
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.
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 Training in Bangalore 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.