Posted on

Developing Etherium Dapp on your Windows PC using Truffle – Step 2

This is a barebones walkthrough for developing a blockchain decentralized app (Dapp) for Etherium (ERC-20) based tokens. Goal is to go through all the steps needed to set up initial environment and build a first contract that compiles, deploys, test, and can interact with via web3.js Javascript in a web browser.

Step 2: Creating a Contract

In this step we will set up the project, then create and compile an empty contract. Be sure to complete Step 1 before attempting this step.

1. Open a Command Prompt terminal. Can search for “cmd” in the start menu.

Open a cmd prompt window

2. Create a folder to house your project. For this example create a projects folder and myfirstdapp subfolder.

c:
cd c:\
mkdir projects
cd projects
mkdir myfirstdapp
cd myfirstdapp
Create folder for project

3. Create the folder structure for the Truffle project with the command:

truffle init
Initialize Truffle project

4. The project files can be created and edited with any text editor. It is helpful to use a tool that helps navigate the project. In this example we are using Atom. If you have Atom installed, you can just type “atom .” at the command prompt. Otherwise open your editor and navigate to the project folder.

atom .
Project folder open in Atom

5. We will be working mostly with the “contracts” and “test” folders for now.

contracts/: Directory for Solidity contracts
migrations/: Directory for scriptable deployment files
test/: Directory for test files for testing your application and contracts
truffle-config.js: Truffle configuration file

6. Navigate to the contracts folder and create a file named “MyContract.sol”

Your first contract

7. The very first line of every contract file will be the pragma indicating what version of solidity the contract was coded for. If you are following this tutorial using a newer version, then may need to update this value. More info on the structure of contracts is available here.

pragma solidity ^0.5.0;
First line is version pragma

8. Next will be the declaration of the contract. Note that the name of the contract and the file should match.

contract MyContract {
}
Contract declaration

9. Next will be the construction declaration for the contract which goes inside the contract declaration. Note that the constructor is declared as public.

   constructor() public{
   }
An empty contract

10. Now save the file (in most editors is shortcut keys “Ctrl + s”). You now have the minimum code for a contract. To compile the contract return to the Command Prompt window and type:

truffle compile

You should get results like the following. If you get any errors be sure the file is located in the right folder, the code matches, and the file has been saved.

Your first compile… of many…

Continue to Step 3 – Variables and Functions