Writing a DApp — Part ☝️
Recently I wrote my first DApp, or decentralised application in case you haven’t heard of it. And in this tutorial, I am planning to create a guide with the steps you need to do the same as I did, maybe a bit more simple since this is just to give you a pice of the cake.
In case you want to go more straightforward, here you have the link to the app I did so you can check it to help you in yours.
Installation 🔧
Here comes the boring part… But it will be easy, I promise!
I assume you have npm already because it’s a typical requirement nowadays that everything runs under node and javascript. The next thing you need to do is to install Truffle. This is a framework that will help us a lot creating our ethereum solidity application, testing it and deploying it to the blockchain specified. To install it, just:
$ npm install -g truffleI recommend you to check out their Github and the documentation, very helpful with a lot of advices and good code.
The second and last requisite we have to start is testrpc. To install it, we’ll use npm again:
$ npm install -g ethereumjs-testrpcThis tool will create a local ethereum blockchain for us, very useful.
You can also use Metamask chrome extension to check the blockchain we will create with testrpc just installed.
Let’s get the party started !🎉
Go to the terminal and create a directory wherever you want, you can call it NotesDApp since that is what we are going to program. Go inside of it and write:
$ truffle unbox pet-shopThis is just one way to start a project with truffle. It comes with a few useful stuff that we will discuss later. It also comes with a lot of other things that we won’t use, but it’s easier to clean them than creating them and since they are not part of the real dapp it does not matter. You may have seen that it is a pet shop project, the reason it’s because it’s the starting tutorial of truffle. You can find that tutorial here, and even when I am trying to go a bit beyond, a really recommend you to take a look at that tutorial as well.
In case you are brave enough and you want start from zero, truffle also offers us a way to create a bare metal project:
$ truffle init bareProject structure 📑
Once we created the project with truffle, it will have created the project structure for us. I am going to explain it so you can fin all you need and distinguish what you don’t.
/contractsThe contract directory is where we will write our Solidity contracts. There will be also another file called Migration.sol don’t worry about it yet.
/migrationsHere is where the magic will appear. Truffle uses the migrations as the way to deploy the smart contracts. Also, the migration will keep track of the changes we make in our creations. We will learn how to use them in another episode.
/testNo need to explain this, you have to write tests. We have two options to create tests in truffle, you can choose between Javascript and Solidity. In these tutorials we will use Solidity.
/srcThis one is not always needed, but is where you will write the frontend for the application. In case you don’t want to have a frontend, you don’t need this folder. In this example it already has all the files and the server we need to develop our web. It also contains files that we won’t need, but we will erase them later.
truffle.jsThis is basically the truffle’s configuration file. You probably won’t have to touch it unless you want to deploy it to the real ethereum blockchain.
