Get Started With Building Ethereum dApps and Smart Contracts

You may have started messing around with Solidity or another language made for the Ethereum Virtual Machine. Maybe you even deployed a contract or two and you’re ready get started creating full on dApps (decentralized applications) on the Ethereum platform. This tutorial will introduce you to some common tools, and show you how to generate a project with your own token that you can use as a jumping off point to build the next big dApp.

For our dApp we’re going to be using three basic tools: Truffle, MetaMask, and TestRPC. Truffle is a development environment, testing framework, and asset pipeline for Ethereum. Basically, it helps you deploy your contracts to the blockchain, swap old contracts for new ones (called a migration), and hook your front-end up to your deployed contracts. MetaMask is an Ethereum light client and Chrome extension that lets users interact with the blockchain without downloading the whole blockchain. In other words, it allows you to interact with dApps in an ordinary Chrome browser. Lastly, TestRPC is a Node.js based Ethereum client for testing and development. It runs 100% locally and simulates a full Ethereum client but is much much faster making it ideal for testing and development.

Getting Started

First, we need to install our tools. We can install Truffle and TestRPC using npm (Node Package Manager). If you do not have npm installed you can find instructions on how to install it here. Once npm is installed you can install Truffle and TestRPC with the following commands.

$ npm install -g truffle
$ npm install -g ethereumjs-testrpc

Also, make sure you add the MetaMask Chrome plugin which you can find here.

Run TestRPC

Now that we have our tools installed, we can get started on our dApp. Our first step will be to run TestRPC in the background. This will be the Ethereum client that Truffle and MetaMask interact with. It can be swapped out with a full Ethereum node if you want to work with the main Ethereum network or one of the test networks. In order to run TestRPC simply run this command:

$ testrpc

This will start up the client and generate 10 accounts for you to use, each with 100 ETH. You should see something like this:

$ testrpc
EthereumJS TestRPC v3.0.5
Available Accounts
==================
(0) 0x2d63053485f9478ba94104c7e2a4de9b18daf6ae
(1) 0x2b0f0e5d13dfffdad785fa17bd6fc8854c7b0131
(2) 0x9ebc0420840989343ae69bfec4a6315c7d99cde7
(3) 0x64f4d2eedd7a77ef97e4415fb936434833898943
(4) 0x5f2d104f40ac2718a50bc2cb37cbb0eacebac10d
(5) 0x9139fdaca5cefa2b330c485c4c94207de8469871
(6) 0xbe96d7335914dce66f490ebfec88ff4107e3449c
(7) 0x66d2986a3bcafd4cf02f211904cc20f99c38d766
(8) 0x867f7461ccc0788b2ea58caed53d83f1ef32fc1a
(9) 0x8641854e6c588290d09f4d7ab122f71f49b90494
Private Keys
==================
(0) 18f6b11deec6f06d34adcb502ab448c5bcdd6f91adbacc78559ec40e23ade3a2
(1) 650281add7a157f2675719758be71b7a14314e300a029e92e917b24e673c2bd5
(2) 2615fb68b0760bccc17b027296c1989757b29dd81f07e012764b75150ebe57ec
(3) f422eff1b25c4e6e22f9d9ff4e534d5209d411c8287672d4e63676f27d39274a
(4) b9894880d7edd835e5d782f3a85bf27ca54f6e8ff50e059bdd1ad6001c6a39c5
(5) 56eca9f0ac19f7cbc0521f8ccf2ddf3bfbf45d7e4e79b29cd5c517ed11ca18cc
(6) 80c1d90be78a4df0aac0cf8bd91aaa182f9243806783f7bb5a88407255cf085b
(7) 573838c100badd7852e358b0350d3e02dc8b13a57d3f2e83470b13a6b588da8c
(8) 4425d489490f6e4b82572767af7459803a025d0638c1c4548c2247f8953cefa2
(9) 5f6d8c4157de90889339ffd2fba7e32980a53b1ce7bc007ef4b245599d3a1fe4
HD Wallet
==================
Mnemonic:      hedgehog piano receive route diagram detect category stand spike original evil tape
Base HD Path:  m/44'/60'/0'/0/{account_index}
Listening on localhost:8545

The first list is the public address of each of the 10 accounts and the second list is the private key associated with each account.

Setup MetaMask

We now want to set up MetaMask to use our TestRPC as it’s network. Open MetaMask and follow the initial setup steps.

Once MetaMask is set up, select the network menu in the upper-left. Then select “Localhost 8585” as your network. Next, add a couple accounts to MetaMask by going to ☰ -> Import Account, and then copying in a private key from the list printed out in your terminal when you started TestRPC. Make sure you add the first account because that account will have all of the tokens you create when your dApp is deployed. Any account will do for the second one.

Our environment is now set up, and we are ready to create and deploy our dApp. Let’s start by creating a new project directory and initializing the default Truffle dApp with Webpack integrated. I’m going to call my project MetaCoin, but you can call yours whatever you’d like.

$ mkdir MetaCoin && cd MetaCoin
$ truffle init webpack

Take a second to explore the project directory. Truffle has created a basic token for us called “MetaCoin” complete with contracts, a front-end in the app folder, and migrations. Migrations will help us deploy our contracts to our Ethereum client, and replace those contracts when changes have been made. You can read more about truffle migrations here.

Deploying our Contracts and Running our dApp

To deploy your contracts to TestRPC run the following command:

$ truffle migrate

Once the migration is complete we can run our dApp with:

$ npm run dev

Now go to localhost:8080. You should see your dApp running and ready to use! If you switch the current MetaMask account to the first account generated by your TestRPC and refresh the page you should see your balance is 10,000 META. You can send META to your other accounts using the dApp interface.

You have now created and deployed your first dApp. From here you can create more powerful dApps by adding functionality to your smart contracts. Your tokens can be used for things like paying out the network, voting/governance, and rewarding those who put work towards making your network function while penalizing those who act maliciously.

This is the first of many posts on Ethereum dApps and smart contract development from BlockOne, so stay tuned! Also, please leave a comment below if you have any questions or feedback.