DEV Community

Cover image for Create Your First Smart Contract
Chinmay Mhatre
Chinmay Mhatre

Posted on

Create Your First Smart Contract

I wrote my first smart contract in Solidity. Here is how you can do it too!

What are Smart Contract?

Smart contract are function and data that are deployed at a particular address on the Ethereum blockchain. They are immutable and cannot be changed once deployed. They are written in a solidity.

As an example, in a backend developed using Nodejs or Python, we use functions to do specific activities and store data in the database. In the case of smart contracts, these functionalities and databases are stored on the decentralized blockchain rather than a centralized server.

Your First Smart Contract

We use the Remix IDE to compile and deploy the smart contract without any local setup.

remix ide

We create a new file with the .sol extension.

Solidity is an object-oriented, statically typed language used to write smart contracts.

Code explanation

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract MyContract {
    uint public count = 0;

    function incrementCount() public {
        count++;
    }
}
Enter fullscreen mode Exit fullscreen mode

The Remix ide requires a license identifier to be present in the contract. We use the MIT license identifier.

We use the pragma keyword to specify the version of solidity we are using. This is done to avoid breaking changes in the future versions.

We use the contract keyword to create a new contract. We can think of this as a class in object oriented programming.

We create a variable count of type uint . uint is an unsigned integer. We initialize it to 0.

When we use the public keyword, it creates a getter function for the variable. This means that we can access the value of the variable from outside the contract, using count().

The incrementCount function increments the value of the count variable by 1. Deploying the contract and using the increment function costs gas.

Gas can be thought of like the toll you pay for using a bridge or a road. Since, computing the contracts requires resources, each transaction requires a certain amount of gas to be paid. The gas is paid in ether which is the currency of the Ethereum blockchain.

Executing the contract

navigate to compile

We compile our contract by navigating to the compile tab and clicking on the compile button.

compile tab

We then navigate to the deploy tab we can use the Remix VM as the test environment.

Deploy tab

We get access to a list of accounts that we can use to test our contract.

accounts

Once we deploy our contract we can see that an amount of gas has been used.

gas

We can also see the address of the contract.

address

We can now access our two functions, count and incrementCount.

functions

We run the count function to see the value of the count variable. We can see that the count is 0.

count 0

Now we run the incrementCount function to increment the count by 1.

increment

Now, the value of the function becomes 1.
count 1


There you have it. You have written your very first smart contract.

In the next lesson, We'll learn how to add rules to the contract so that only the user who created the contract can perform certain action. Since, in this contract, anyone can access and update the count variable.

Top comments (1)

Collapse
 
ashwith25 profile image
Ashwith Poojary

Everything is well explained 👏