Solidity-101 (part 2)

Solidity-101 (part 2)

In this article, we will look how we can also restrict values in int and uint, And why we require that.

Β·

3 min read

You can read about basics of solidity in my last article here πŸ‘‡

Solidity-101 (part1)

Let's Start

πŸ’œ VARIABLES

  • We have different type of values to store in program like it can be an Integer, String, Characters, boolean etc.
  • There are variables for integer or unsigned Integer, we can add power of 2 after variables data type and it will restrict it to certain value.
  • Variables in solidity are :

    Integers

  • Numbers can be negative as well as positive but not decimals
    int -> This is to tell solidity that we will store integer in this variable.
    int8 -> This will store values from -2^8-1 to  2^8-1 i.e. -255 to 255.
    int16 -> This will store values from -2^16-1 to  2^16-1 i.e. -65536 to 65536.
    .
    .
    .
    int256 -> This is the maximum integer value solidity store i.e. -2^256-1 to 2^256-1.
    
    πŸ‘‰ Remember, writing just int or int256 is same. By default int is taken as int256.

Unsigned Integers

  • For Numbers that will be only positive but not negative or decimals
    uint -> This is to tell solidity that we will store unsigned integer in this variable.
    uint8 -> This will store values from 0 to  2^8-1 i.e. 0 to 255.
    uint16 -> This will store values from 0 to  2^16-1 i.e. 0 to 65536.
    .
    .
    .
    uint256 -> This is the maximum integer value solidity store i.e. 0 to 2^256-1.
    
    πŸ‘‰ Remember, writing just uint or uint256 is same. By default int is taken as uint256.

πŸ‘‰ This is only limited to integer and unsigned Integer.

πŸ’œ DEFINING A VARIABLE

  • We define variable in solidity as this pattern.
<DATA_TYPE> <ACCESS IDENTIFIER> <VARIABLE NAME>

int8 private fav_num = -8;
uint16 public fav_num = 8;
bool internal is_fav_num = true;
string external word = "Hello";
address private contractAddress = 0x70997970C.......;

πŸ’œ SPECIAL ACCESS IDENTIFIER FOR FUNCTIONS

  • These access identifier is just to tell EVM how the function will interact with the storage.
PURE -> This means that it will not even access the storage.

VIEW-> This means that it will only access the storage but wont change anything.

πŸ’œ EXAMPLE

address private i_owner;
uint256 public minimumUsd;

function getOwner() public view returns (address) {
        return i_owner;
}

function getAddressToAmountFunded(address funder) public view returns (uint256) {
        return s_addressToAmountFunded[funder];
}

πŸ’œ Summary

  • This is done because we want to eliminate unwanted storage usage.
  • So, if we know that values will be small then we can use uint8, uint16...etc.
  • This is done to keep our gas prices in control.
  • Depending on different task, EVM requires amount of gas to be paid. So to keep it in control we use these.

πŸ‘‰ Remember using PURE or VIEW -> SPECIAL ACCESS IDENTIFIER, its because these denotes that they tells EVM that they have very limited functionality and gas charge will be taken as per.

That's all.

In the next article, we will look into different Storage, Data Structures that we basically use in writing SMART CONTRACT.

Hello, I am Tanisk Annpurna

I post about

πŸš€web3, Blockchain, Ethereum

🐦Smart Contract, Solidity

πŸŽ‰JavaScript, ReactJS, NodeJS

Follow and like for more such posts. !!✌️!!

Did you find this article valuable?

Support Tanisk Annpurna by becoming a sponsor. Any amount is appreciated!

Β