DEV Community

Cover image for Guide for Smart contracts in Solidity
Gourav Singh Rawat
Gourav Singh Rawat

Posted on

3 2

Guide for Smart contracts in Solidity

You might heard people in web3 and blockchain talking about smart contracts and solidity.

What even is a smart contract

In Solidity, smart contract can be considered as collection of constructor, functions and state variables, or to be more beginner friendly we can take contracts as a class in c++.
In a smart contract, we can implement any algorithm or whatever you want to be done once your contract is deployed or compiled.

here is a quick tutorial of the same:

1. Constructor

Sometimes we want some variables to be declared as soon as our contract is deployed or compiled. So to achieve this we have constructors. These are initialised with a constructor keyword.
constructor() {}
Since constructor is just a function that is run on contract creation, you can define or write code for initialising the contract.

2. State variables

General variables where we store the state about our contract.

3. Functions

You must be knowing what functions are if you're familiar with any other programming language. If there is something or some block of code that is to be run different times on many places, you can define those code lines in a functions and call that function to run them.
Same applies in Solidity.

contract my_contract{
   uint num_1 = 10;
   uint num_2 = 20;

   function add(uint a, uint b) public pure returns(uint){
      return a + b;
   }
}
Enter fullscreen mode Exit fullscreen mode

But here's a thing to know in solidity is that if in a contract you declare a function and doesn't put any implementation inside it, our parent contract will become an abstract contract. So our contract will look like this.

abstract contract my_contract{
   function add(uint a, uint b) public virtual returns(uint);
}
Enter fullscreen mode Exit fullscreen mode

As you can see above, our contract my_contract has a function add that is not yet implemented, so we have marked our contract as abstract contract.

I hope this topic was clear, thanks for reading!
Do checkout the video in the blog that describes the same

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay