DEV Community

Cover image for Smart contracts for dummies
Ibrahim Pima
Ibrahim Pima

Posted on

Smart contracts for dummies

Hi, i know you have a tough time trying to learn concepts on the blockchain faster and today i bring a whole new simple approach to understanding smart contracts

after this concept you can grasp a lot about the blockchain space.

were are going to create a bank vault contract and here are sum keywords you should learn of

blockchain is just a state machine just like redux , context api that keeps track of state. But online the web2 in this whole new world you have to understand certain concepts about state , reading from state, writing from state.

view: to see the state on blockchain would be that you want to see the balance available on a contract.

state: this is what is stored on the blockchain, could be money or any data that can be stored onchain.

contract: a contract is just in short a governor that has all your rights and knows what it should do and not to. Just like the government that has rules the humans must follow, same approach here .

public: in short means that it can be assesible by anyone outside and offers the rights to users to take action.but without public user wont have anything to do with the blockchain.

Remix: Remix is just and ide that allows you to build smart contracts

returns: this keyword specifies that a function is a read only function such as checking state info.

enough of the drama and lets get to the whole thing

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

contract Vault{
    uint initialBalance;
    uint balance;

constructor(uint _balance) {
  initialBalance =_balance;
   balance = _balance;
}

function checkBalance() public view returns (uint){
return balance;
}

function deposit(uint amount) public {
balance+=amount;
}

function withdraw(uint amount ) public {
require(balance>= amount,  "not enough balance");
balance-=amount;

}

}`
Enter fullscreen mode Exit fullscreen mode

this is a smart contract that acts like a bank vault that users can store money on and contains 3 main fuctions such as checkBalance, deposit, withdraw.

the contract is initiated with the contract keyword and then curly braces open close followed by the constructor being declared with stores the state and initializes it on the blockchain.

as you can see the parameters of the function is named with _paramsname because the naming can be same if we omit it and can cause problems in the code.

the first thing was to declare the state variable which was the initial balance followed by the balance itself.

the checkbalance function has a return type and therefore takes from the returns in the function so we add it to the function of checkBalance

the deposit function accepts an amount to be added as state and then adds it to the initial balance changing the behavior of the state and is recognized by the balance+=amount. Which mean s were adding the amount to the balance we first had the other way round applies for the withdraw with goes by -=

the require keyword feels like and if else statement that gives a condition so if the users amount is greater than the balance they should be able to see and insufficient funds message.and the action will not proceed again until the requirement is met.

thats all for this one and i hope to hear from you Lad

Top comments (2)

Collapse
 
umang_suthar_9bad6f345a8a profile image
Umang Suthar

This is one of the simplest smart contract explanations I’ve seen in a while. The vault example makes everything click instantly. Appreciate this!

Collapse
 
ibrahimpima profile image
Ibrahim Pima

thanks a lot bro i am really happy to have helped you grasp the concept of it