Create Your First Smart Contract
Tags: web3, beginners, solidity, blockchain
Published: 2022-09-19 07:48:07


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](https://remix.ethereum.org/) IDE to compile and deploy the smart contract without any local setup. 

![remix ide](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6f93h7c644orfbbs8o5.png)

We create a new file with the .sol extension.

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

### Code explanation

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

contract MyContract {
    uint public count = 0;

    function incrementCount() public {
        count++;
    }
}
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkxagvgayazazdb6i7a0.png)


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

![compile tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azm8gz6de8wq2kkp5l3k.png)

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

![Deploy tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gx40vi0dgo9dm7959ak.png)

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


![accounts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlle8ol2jamb5quzcgjm.png)

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


![gas](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hywkp0myvvs0gbe6u4a7.png)

 We can also see the address of the contract.

![address](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8mpu4z7ubb69n5lx3a1.png)

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

![functions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnw9f4b2b3ye7seasztt.png)

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

![count 0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dm1dad4nvjlgrr7v4qhn.png)


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

![increment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kubek36xqpa0t6s5tlo2.png)

Now, the value of the function becomes 1. 
![count 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xkpfgjijw4htq3g7dk6j.png)

---

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.