AppDividend
Latest Code Tutorials

How To Create Your First Smart Contract In Solidity

Smart Contract Using Solidity

1,516

Get real time updates directly on you device, subscribe now.

How To Create Your First Smart Contract In Solidity? In this tutorial, We will learn how to write the Smart Contract in Solidity Programming Langauge and deploy it to the local ethereum blockchain. If you are new to Blockchain world, then please check out my previous tutorial Ethereum Blockchain Tutorial From Scratch

How Blockchain Works?

Blockchain apps work entirely different from web apps. In the web application, data is stored on the central database server. Clients communicate with a web application, and web application writes and reads to the database then responds according to it. In Blockchain technology, all the clients are nodes, and there is no such thing as a central database. Data is completely immutable, and after created, we can not change it back. So Blockchain provides high-end security. So our application is fully decentralized on the Blockchain. Data is ultimately distributed across the multiple nodes.

The blockchain is the network of peer to peer nodes. They communicate with each other. Once you are connected to the Blockchain, now you are part of the blockchain. You are one of the nodes of the Blockchain.

You can sum it as Blockchain is the database and network. Ethereum Blockchain allows us to write the code that we can deploy it to the blockchain and node of the network can execute this code.

Smart Contract

Ethereum Blockchain allows us to write the code that gets executed on Ethereum Virtual Machine with Smart Contracts. That means all of the business logic of our application resides on the smart contract. It will read and write the data, transferring value and execute any business logic that we program on the blockchain.

Solidity

Solidity is the language, in which we can write our smart contracts. These smart contracts then deploy on the blockchain. All the reading and writing data logic is defined in this Solidity language. It is like a microservices that live on the blockchain.

How To Create Your First Smart Contract In Solidity

We will start our project by installing the dependencies to build this small decentralized application.

If you have not installed Node.js on your machine, then you can install it using the following command.

brew install node

It also comes with NPM, which is node package manager.

Next thing, we need to install is Ganache. It quickly fires up a personal Ethereum blockchain which you can use to run tests, execute commands, and inspect state while controlling how the chain operates. Go to this Link. You can install it according to your OS. After installing, you can see something like this.

Ganache Blockchain Tutorial

Install Truffle Framework

Next, install the Truffle FrameworkTruffle is the most popular development framework for Ethereum with a mission to make your life a whole lot easier. Install using the following command.

sudo npm install -g truffle

Truffle Framework Features

  • Built-in smart contract compilation, linking, deployment and binary management.
  • Automated contract testing for rapid development.
  • Scriptable deployment & migrations framework.
  • Network management for deploying to both public & private networks.
  • External script runner that executes scripts within a truffle environment.
  • Access to hundreds of external packages.
  • Built for speed.

Install Metamask Chrome Extension.

You can install it using this link.

Install Solidity Extension on Visual Studio Code.

If you have not installed Visual Studio Code yet, then please install it, it is by far best editor in my opinion. You can check out my article on How To Install Visual Studio Code On Mac.

Solidity Tutorial with Example

Do not turn off the Ganache. Keep is open. It is Local blockchain, and in that, there are  10 Users are already there. You can see their address. 

Now, we need to create the project to write the smart contracts in Solidity language. We have already installed Truffle Framework, so we can use that to generate the boilerplate for our project. So go to your general project directory and type the following command to create the project folder. Go into that folder.

mkdir blocksol
cd blocksol

Okay, now generate the truffle boilerplate using the following code.

truffle unbox pet-shop

Open the project in your editor using the following command. I have VScode so,

code .

You can see that our primary folder structure looks like below.

 

Truffle Framework Tutorial

Create the Smart Contracts.

In the folder structure, you can see that there is one folder called contracts. We will create all our contracts in that folder. Let us create one contract called Football.sol. Here the file extension is .sol because we are using Solidity language to code the contract. Write the following code inside it.

pragma solidity ^0.4.2;

contract Football {

    string public team;

    function Football () public {
        team = "Real Madrid";
    }
}

In the first line, we have defined the version of solidity, which is above 0.4.2. Then we have established the contract Football. Also, I have taken the variable called a team, and it is public. Now, we can use that public variable as a method to return its value. So the variable, which defined as a public in the contract, we can call as a function as well to get the value of that variable.

Create the migration of Football Contract.

Create one file inside migrations folder called 2_football_migration.js file.

// 2_football_migration

var Football = artifacts.require("./Football.sol");

module.exports = function(deployer) {
  deployer.deploy(Football);
};

Okay, now deploy the contract to the blockchain network by using the following command.

truffle network

Do not worry about compilation warnings; it is okay.

Interact With The Blockchain.

You can interact with our application using the following command.

truffle console

Now, type the following in your terminal.

Football.deployed().then(function(instance) {app = instance})

Smart Contracts returns a Promise, which is mainly a JavaScript concept, which is an Asynchronous behavior. Like if we need to wait for some processes to finish it, it can send us the promise, which either resolves or reject. Now, after complete the event, it will give us a result, and that is app. So we can further query like this.

app.address

Now, In return, you can see the address in the string.

We can also get the team name by the following function.

app.team()

It will return a Real Madrid. But wait, we have not defined any function called team still it will return a value.

Because public variables are itself a getter function. So we can call it as a function, and in return, we can get its value.

Smart Contracts With Pragma Solidity Tutorial

So, finally, we have deployed our First Smart Contract In Solidity. 

That is it for the today’s How To Create Your First Smart Contract In Solidity tutorial. We will build fully decentralized Application using Ethereum Blockchain the future.

2 Comments
  1. Vishal says

    Hi Krunal,
    Thank you so much for the blockchain tutorial.
    We are waiting for your next tutorial on decentralized app. Hope you will find time to make it and upload it. Your tutorials are really helpful.
    Thank you very much once again.

    Regards,
    Vishal

    Thank you once again.

  2. ahmer says

    Hi Krunal,

    This is the second tutorial of yours I will be doing.

    I would also love to have a small, hello world type of full-stack dApp tutorial!

    Thanks again for all the hard work you do!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.