Ethereum solidity tutorial – Build a step-by-step Blockchain Dapp

ethereum solidity tutorial :This article details a complete example of smart contract solidity creation for ethereum. This article is the continuation of the first 2 which also details the creation of the contract.

The first 2 articles provide a better understanding of the concept of blockchain and decentralized application.

Creating the project

A directory is created for the voting application project on songs.

mkdir vote-song-dapp

To accelerate development we will use a « truffle box »:

It’s sort of a template, an application canvas that allows you to focus on the Dapp by having a structure already created.

I will base my explanation on the pet-shop box available here: This is one of the first truffle tutorials to create a Dapp.

This truffle box includes the basic structure of the project as well as the user interface code.

Use the truffle unbox command:

truffle unbox pet-shop

As a reminder the installation of truffle is possible via the order:

npm install -g truffle

If you open the vote-chason-dapp folder with vscode then you get the following tree:

ethereum solidity tutorial
Dapp app project tree example (based on truffle pet-shop)
  • contract: storage of the application’s smart contract
  • migration: Migrations allow smart contracts to be transferred to the Ethereum blockchain (local test or mainnet). Migrations also allow you to link smart contracts with other smart contracts and start them.
  • node_modules: The node_modules folder contains libraries downloaded from npm.
  • src: Front-end application directory (customer)
  • test: Storage of tests for the application
  • truffle-config.js: Javascript file that can execute any code needed to create your configuration.

Creating the smart contract

As a reminder we are developing an application that allows to elect the favorite song of voters.

We will first create the part that allows to create a vote for the best song based on 3 eligible songs.

The contract written in solidity is as follows:

TopChanson contract
        struct Song
        uint ID;
        title string;
        uint counter;
    uint public counterDeChansons;
    mapping (uint - Song) public songs;

    function addChansonElligible (string memory nomChanson) private
        songs -[compteurDeChansons] Song (CounterDeChansons, NameChanson, 0);

    TopChansons(public) function
        addChansonElligible ("Moonlight");
        addChansonElligible ("Mom the little boats");
        addChansonElligible ("Ah! Crocodiles");


Note the use of « mapping (uint – Song) public songs; »

Related: Website

This data structure will allow us to store the titles of eligible songs like a hash table. That is, a table that takes as an access key in our case a uint that is the song ID and allows to recover the value that is a structured song type.

The Song type is structured, see solidity documentation:

Here there is a special case on the feature addChansonElligible, it takes into argument the name of the song which is a string character chain. If you don’t add the keyword « memory » you get the following error:

TypeError: Data location must be « storage » or « memory » for parameter in function, but none was given.

For function settings and return variables, the location of the data must be explained for all type variables (struct, mapping, thong).

The contract is migrated via the order:

truffle migrate --reset

We then get:

Compiling your contracts...
Compiling ./contracts/Migrations.sol
Compiling ./contracts/TopChanson.sol

To follow …

Ethereum solidity tutorial – Liens internes :

Retour en haut