Skip to content

deep0072/BLockchain-tuts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BLockchain-tuts

solidity style Guide

  • error code
  • library
  • interfaces
  • contract
  • then type Declarations
  • state variable decelration
  • event
  • modifier
  • constructor
  • recieve fallback fucntion
  • external function
  • public function
  • internal function
  • view pure function
  • before dive into blockchain check this one of the best explaination of Consesus

    image

    in solidity data types are of 2 types

    values , reference

    values ==> these store data values. e.g. int store number

    refrence ==> these store the refrence to the exact data. e.g. array that refrence to exact element of array.

    Solidity functions

    two type of function

    1. create transaction:==> means write data on blokchain

    2. reads and retrun value of variable.

    Pure function

    a function that do not modify the state of variable in existing code

    function that do not read the existing variable in code

    view function

    a function that do not modify the current the state of variable.AND READ DATA FROM BLOCKCHAIN

    also read the existing variable in code

    example of invalid pure function

    image

    example of invalid view function

    image

    view and pure function only cost gas when they are called by function which cost gas

    modifier function :

    these function are used to remove the redundancy in solidity.

    understand with example

    image

    here in this snap shot we can use the require multiple time in these function but that would be redundant in nature


    Contructor

    these are the function that call once when we deploy the smartcontract. mostly used to intialise the variables

    image

    Variables

    type of variable

    1. local 2. global 3. state

    1. state variable ==> which store data on blockchain and decalred outside the function. lets take an example of it. also store permanently on blockchain

    image

    storage keyword ==> if want to change statevariable infos in function then i need to used storage keyword. just to tell the solidity data will be changed in permanent storage.cant be used in parameter though

    image



    memory keyword ==> if you use memory then statevariable will not be updated. as you can see the company name in given below snap shots. btw in latest version of solidity.all variable intialised inside the function set to memory localtion by default.

    image

    2. Local variables ==> these var that declared inside the function. these store data in memory which is temporary in nature

    image

    3. Global variable ==> these var that store information such blockchain transactions. block number, block timestamp.account address (msg.sender) that call the function.

    image

    Constants

    Constant ==> some time there is a state variable that do not change. so we need to declate regular state variable as constant. constant variable always use less gas fee.


    immutable variable

    Immutbale ==> some time there is a state variable that do not change like constant variable . so we need to declate regular state variable as immutable. these can bde declared one time in the "constructor".

    lets understand it with example

    image

    Default values in solidity

    bool public b; // false

    int256 public i; // 0

    uint256 public u; // 0

    address public addr; // 0X00000000000000000000000000000000

    bytes32 public b32; // 0x000000000000000000000000000000000000

    Errors

    error handling are moslty done to prevent the uneceesary use of gas. means refund of gas, any variable that is updated will be reverted

    error handling done using require, assert, revert

    require ==> it is mostly used to validate the user inputs and for access control who gets to call the function

    image

    revert ==> it is also does same thing as require but it is mostly used to prefer in nested condtions.

    image

    assert ==> it will be always used to check for conditions that should always be true

    image

    Array

    Dynamic array ==> not fixed size of array

    syntax = uint [] public arr

    fixed array => fixed size of array

    uint [4] public fixedArr; // can store upto 4 elements

    intialise the array

    uint [] public num = [45,67,23,6,45];

    // intialise the array with fixed size

    uint [5] public num = [45,67,23,6,45]

    Array operation

    adding element in num array

    nums = [45,67,23,6,45]

    nums.push(50) ==> nums = [45,67,23,6,45,50]

    delete element from array

    delete num [2]; // delete element at index 2 but it will set 0 element at 2nd postion

    nums = [45,67,0,6,45,50]

    pop == this method will remove element from last index of an array

    num.pop(); // removed last element nums = [45,67,23,6,45] that is 50

    get size of array

    nums.length

    create an array in memory

    uint[] memeory nums = new uint; array in memory with fixed size of 5

    in this pop and push will not work

    returning array in function

    image

    MAPPING

    mapping is like dictionary in python and object in js. store key value pair. {"0x5B38Da6a701c568545dCfcB03FcB875f56beddC4": 565666}

    syntax = mapping(type of key==> type of value) public balances

    to add key and update key in mapping

    image

    to delete thevalue from mapping. it will leave the value of key default value that is declared varibale

    image

    STRUCTS

    structs are used to create own types of variable

    store multiple types of data

    syntax : struct {type1 var1, type2 var2}

    initialization of struct

    image

    with the help of dot notation we can also put data into struct

    ![image](https://user-images.githubusercontent.com/46425800/170838054-9460f6a7-75ab-449a-bad4-b40829ec6742.png

    Call Data

    this is type of memory location where variable is declared and used in inputs. so whenever it is used by another function then that data do not copied in memory. it directly used. so it save gas. """once we declare the variable it can't be modified"""

    string can be stored in calldata

    Memory

    this is type of memory location where variable is declared and used in inputs. used for temporary value also can be modified

    data like structs,mapping,array, string can be stored in memory

    image

    to import specific contract from file use

    import {specificContract} from "file .sol"


    # 3 ways to send ether

    transfer ==> ### use 2308 gas, if trasfer fail for some reason then whole function fails

    send ==> ### use 2300 gas, returns bool in case of successful trasaction or not

    call - ### all gas, returns bool and data

    falback function

    these are executed when functions are not existed sends directly ether if fallback function is payable

    so if someone call function foo inside this contract but it is not available then fallback function will call by default

    if msg.data is not empty then fallback function are called

    if msg.data is empty then receive function are called

    image

    InterFace

    these are act as abstraction. these found on top of contract. theae are used to import other contract code. that helps use to avoid the duplication.

    lets understand with it example. here in this snapshot there is Counter smart contract. we want to use its Inc() function in other external contract.

    image

    this is another contract in which we import above given smartcontract by using interface keyword.

    image

    if we want to modifie the parent function. then we need to use virtual keyword in parent function and use that same function in child and use override keyword in that function. also visibility of parent and child function should not be differd. check the snap shot of it.

    image





    Library in solidity

    similar to contracts that contain reusable codes. A library has functions that can be called by other contracts.

    Deploying a common code by creating a library reduces the gas cost.

    but you can't declare any state variable and you can't send ether.

    in this snap shot library is declared outside the function. then in contract library function are accesed using dot notations

    image image

    selfDestruct inBUilt function

    used to delete the contract and send its all eth to mentioned address in built function it self

    when we try to call the function from deleted contract. they will not work

    check snap shot

    image

    Erc20 standard with function

    It effectively allows smart contracts to act very similarly to a conventional cryptocurrency like Bitcoin, or Ethereum itself. In saying this, a token hosted on the Ethereum blockchain can be sent, received, checked of its total supply, and checked for the amount that is available on an individual address

    ERC20 defines the functions balanceOf , totalSupply , transfer , transferFrom , approve , and allowance

    balanceOf() ==> it allaows smartcontract to store and return the balance of the provided address.

    transfer() ==> This function lets the owner of the contract send a given amount of the token to another address just like a conventional cryptocurrency transaction

    approve function ==> This function lets the owner of the contract approve a given amount of tokens to be spent on behalf of another address.means lets say i am the owner of wallet and i decide whoever want to spend my tokne. i will allow them to spend my token. by giving approval. i will set the limit that the other wallet spend my token.

    image










    allowance function ==> This function lets the owner of the contract check the amount of tokens that have been approved for spending on behalf of another address.menas if i am the owner of my wallet and my friend has spend my token on behalf of my wallet. so this function to check how much allowance is remaining for my friend.

    !image









    transferFrom function ==> This function lets the owner of the contract transfer a given amount of tokens from one address to another address.one address corresponds to the who actual have the token and other address is the one who want to spend the token. transferFrom function is used to spend the token on behalf of the owner of the contract.

    image

    PRIVATE key, Public Key, address

    public key = it is the function of private key and Elliptic Curve Digital Signature Algorithm

    ECDSA to the private key, we get a 64-byte integer, which is two 32-byte integers that represent X and Y of the point on the elliptic curve

    Address of Account = To make an address from the public key, all we need to do is to apply Keccak-256 to the key and then take the last 20 bytes of the result

    image

    contract address = The contract address is usually given when a contract is deployed to the Ethereum Blockchain. The address comes from the creator's address and the number of transactions sent from that address (the “nonce”)

    Compilation and deploying using JS

    solc js is used to compile and deploy the solidity code.


    to compile with the help of "solcjs" use the following command

    yarn solcjs --bin --abi --include-path node_modules/ --base-path . -0 . simpleStorage.sol

    --base path ==> it is used to specify the path where the solidity code is present.

    -o with dot ==> it is used to specify the path where the output file is to be stored.

    function signature

    This is a string that defines the function name and its parameter

    function selector

    its the first four bytes of a function signature. which gives the idea about which function is called.

    image

    callData

    call data are function parameter passed to function

    abi.encodeWithSignature(functionSignature(), args)

    it takes two params first is function signature( function name with its params in string) and second is the function parameter. used to call function directly by just mentioning its name in string and passing arguments.

    abi.encodeWithSignature("transfer(address,uint256)", someAddress, amount)

    abi.encodeWithSelector(dunctionSelectior(), args)

    it takes two params first is the function signature( function name with its params in the string) and the second is the function parameter. used to call function directly by just mentioning function selector(bytes format of function name) and passing arguments.

    abi.encodeWithSelector(getFunctionSelectorOne(), someAddress, amount) but in this we first need to get the functionSelector

    deelegatecall()

    image

    About

    No description, website, or topics provided.

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published