How To Create Smart Contract With Solidity?

By Sumedha Bose

Solidity is a well known programming language, knowledge of which has now become an integral part of becoming a blockchain developer. It is an advanced programming language that has been used in the process of developing several blockchains.

What is Solidity?

For those not familiar with Solidity, you can read our beginner’s guide to Solidity for a brief overview. In short, it is an advanced programming language that is meant for writing smart contracts in blockchain and also for implementing them within the blockchain networks or platforms. It is one of the main languages used in Ethereum and is specifically used for writing smart contracts.

What is a Smart Contract?

A smart contract is a stand-alone script which is usually written with the help of Solidity and compiled into binary or JSON and deployed to a specific address on the blockchain. It is basically a collecton of codes stored in the blockchain network that consolidates all the conditions to which all parties using the contract have agreed upon.

In this article, we’ll take you through the process of creating a smart contract with solidity.

How to create a Smart Contract with Solidity?

To begin with, you have to set the source code version in the first line itself so that the contract doesn’t behave differently with a new compiler version.

1
pragma solidity ^0.4.0;

Let’s assume the name of the contract is Hiatus. Then the code would look something like this:

contract Hiatus {
    address creator;
    string message;
    // functions that interact with state variables
}

In Soliditty, the addresses of accounts is stored in the data type named address. In the above example, there are two states variables: creator and message. Next, we need to initialize both variables in the constructor.

The constructor is a special function that can be used only once when a contract is first deployed to the Ethereum blockchain. It can be declared by using the function keyword followed by the name of the contract (just like in Java).

function Haitus(string _message) {
    message = _message;
    creator = msg.sender;
}

Finally, we implement the setter and getter methods for the message:

function greet() constant returns (string) {
    return message;
}
function setHiatus(string _message) {
    message = _message;
}

Invoking the function greet will allow the for the return of the currently saved message. The constant keyword signifies that this function will not modify the contract state and will not trigger any writes to the blockchain.

The value of the state can now be changed by calling the function setHiatus. This value can be altered by anyone just by calling this function.

Well that is basically the gist of how to create a Smart Contract using Solidity. Once the contract is ready, it can be deployed into the Ethereum blockchain for everybody to use it. If you want an idea on how big the Ethereum blockchain is right now, take a look at our article on it.

Sumedha Bose

Sumedha uses words as her crutch to get by in life. She takes a keen interest in debating, dancing and destroying the patriarchy in her free time. She can be contacted at [email protected].

Related Posts