3. Smart Contracts. Creating the Smart Contracts required for the dapp

A Smart Contract is a programming file usually written in the language Solidity where you define all the logic required for communicating with the blockchain.

Smart Contracts store the information that you would have stored on a centralized database.

They also contain the logic for processing that information in the blockchain. It’s like your server side code but simpler.

The first step for creating the Smart Contracts required for decentralizing your web app, is to write the variables used. In the previous section you wrote down the information stored on the database. That’s the information that will be stored in the Smart Contract.

So open remix.ethereum.org and create the variables:

pragma solidity 0.4.21; contract Linked {

// User profile

struct User {

bytes32 name;

bytes32 occupation;

string bio;

}



// The structure of a message

struct Message {

string content;

address writtenBy;

uint256 timestamp;

}



// Each address is linked to a user with name, occupation and bio

mapping(address => User) public userInfo;



// Each address is linked to several follower addresses

mapping(address => address[]) public userFollowers;



// The messages that each address has written

mapping(address => Message[]) public userMessages;



// All the messages ever written

Message[] public messages;

}

A struct is a type of variable that allows you to create a new type of variable with several internal properties. In this case I’ve created the structs User and Message:

Each user will have a name, occupation and bio.

The Message has the content of the message, the address of the user that wrote the message and the timestamp when it was written.

You can learn more about each type of variable in the official docs: http://solidity.readthedocs.io/en/v0.4.21/types.html

After creating your variables, you want to code functions that will use those variables. My web app has the following functions:

Set the user profile by saving the data in a database

2. Write a message that will be stored in the database with the content of the message, who wrote it and the timestamp of the message

3. Follow and unfollow users. Each user is linked with several followers

Let’s create them one-by-one in your Smart Contract using unique functions.

1. Set the user profile by saving the data in a database:

Here’s how I’d do it in my Smart Contract:

// Sets the profile of a user

function setProfile(bytes32 _name, bytes32 _occupation, string _bio) public {

User memory user = User(_name, _occupation, _bio);

userInfo[msg.sender] = user;

}

This function is receiving the name, occupation and bio of the user and it’s updating the mapping of that user. The mapping links the address of the sender with his user information. We are overriding that information here.

In your web app you may would have stored that information by making an API call to a node.js server which would store that information inside a mondodb database. In decentralized apps, all that logic is inside the Smart Contract written in Solidity.

2. Write a message that will be stored in the database with the content of the message, who wrote it and the timestamp of the message

// Adds a new message

function writeMessage(string _content) public {

Message memory message = Message(_content, msg.sender, now);

userMessages[msg.sender].push(message);

messages.push(message);

}

The function receives the content of the message. Then it creates a temporary message struct instance with the content, the address of the sender and the current timestamp. Finally it adds the message to the array of messages of that user and it also adds the message to the public array of all the messages.

The keyword now is the current time in a unix format of 10 numbers. I’m using it for the timestamp of each message.

3. Follow and unfollow users. Each user is linked with several followers

// Follows a new user

function followUser(address _user) public {

userFollowers[msg.sender].push(_user);

}



// Unfollows a user

function unfollowUser(address _user) public {

for(uint i = 0; i < userFollowers[msg.sender].length; i++) {

if(userFollowers[msg.sender][i] == _user) {

delete userFollowers[msg.sender][i];

}

}

}

The follow function adds that follow address to your mapping of follows.

The unfollow user function loops through all your follows and removes that user address for the array. Note that this will only work for a small amount of addresses, up to about 200 loops since the number of loops is limited by the gas sent in the transaction when executing the unfollow function.

You can see that the for loop is very similar to the one of similar languages lika java or c++.

Great, we now have the variables and functions using those variables.

After your Smart Contract code is ready, deploy it on ropsten by going to the Run tab on the right or the Remix code editor:

Make sure you’re using the Injected Web3 from Metamask and that your metamask account is connected to Ropsten like you saw before:

You’ll need test ropsten Ether for deploying and using the contract. You can get test ether by going to https://faucet.metamask.io/ and clicking on request 1 Ether:

Then go back to Remix and deploy the Smart Contract by clicking on Create and confirming the transaction in Metamask:

The last thing is to use those functions on your web app. Learn how in the next section.