30+ Solidity and Smart Contract concepts for Beginners

1. Describe the role of a Validator

Validators are clients which make use of consensus algorithms to ensure that valid blocks are added to the chain. This overall ensures the security and integrity of the blockchain. They do so in the following steps:

  • They verify and authenticate transactions by making sure that these transactions comply with the network's rules and if the sender has sufficient funds to complete the transaction.
  • They vote on the validity of new blocks by using their copy of the blockchain's history to assess whether the new blocks are valid.
  • In ethereum 2.0 validators in the pos mechanism are required to stake a portion of their resources as a guarantee of their honesty and commitment to the network. They can earn transaction fees and block rewards from this. So, in general they ensure that only valid blocks are added to the blockchain and they actively participate in the the consensus process to reach agreement.

2. What are Execution Clients

These are software programs that allow users to interact with the ethereum virtual machine and execute smart contracts on the ethereum network. Examples include GETH written in go, Parity written in Rust, Nethermind written in C# etc

3. What are the steps involved in the transaction lifecycle.

  • transactions are first initialized and broadcasted to the network.
  • validators and miners validate or verify the transaction. Each validator is expected to verify the transaction validation rules set by the creators as well as its own local state.
  • Valid transactions are hashed and stored into a block which should contain a collection of transactions to be added to the existing blockchain.
  • This new block can only be added when consensus is reached by majority of the participating nodes.
  • Finally, the transaction is added to the blockchain. It becomes immutable. This record is stored on the distributed ledger which is maintained by the connected network nodes.

4. What is the difference between compiled and interpreted languages.

The major difference lies in the execution of the source codes.

The source code for interpreted languages is executed without compilation. And it is usually executed line by line.

The source code for compiled languages are translated directly in to machine code by built in compilers. The compiler analyses the entire source code and generates and executable binary that can be directly executed by the computer's processor.

Compilation adds an extra step before execution, which can result in longer development cycles as the code needs to be compiled each time changes are made.

Examples of compiled languages are c, c++, go Examples of interpreted languages are Javascript, Python, Ruby, PHP

5. Is solidity a compiled or interpreted language?

Solidity is a high-level compiled language.

Solidity code is written by developers and then compiled using the Solidity compiler into bytecode that can be executed by the Ethereum network. This bytecode is what gets deployed as a smart contract on the blockchain.

Therefore, Solidity can be considered a compiled language in the sense that the code is not executed directly but is translated into a format that can be executed on the Ethereum blockchain.

6. Explain the relationships between the eth units

  1. Wei: This is the smallest and indivisible unit of ether.
  2. Kwei (Babbage) 1 kwei = 10^3 wei.
  3. Mwei (Shannon) 1 mwei = 10^6 wei
  4. Gwei (Szabo) 1Gwei = 10^9 wei
  5. Microether (Finney) = 1 microether = 10^12 wei
  6. Milliether (Ether) = 1 milliether = 10^15 wei
  7. Ether 1 ether = 10^18 wei.
  8. Kether (Grand) 1 kether = 10^21 wei.
  9. Mether 1 mether = 10^24 wei.
  10. Gether 1 gether = 10^27 wei.

7. What does it mean to invoke a contract?

This process involves calling and executing a function within a deployed smart contract. The following processes are involved in contract invocation:

  1. Definition of the function
  2. Creation of a transaction. This usually involves information of the contracts address, function to the invoked and any input data or arguments required for the function.
  3. Execution of the function after the transaction has been included in a block by validators.
  4. change of state.
  5. Return values or data.

8. When was base fee, max fee and max priority fee introduced?

These concepts were introduced in EIP 1559 which aims to improve the predictability and efficiency of transaction fees on the ethereum network.

EIP-1559 was introduced in Ethereum's London Hard Fork, which went live on August 5, 2021.

  1. Base fee is the part of the transaction fee that is always burned upon inclusion in a block. It is determined by the algorithm based on the congestion of the network.
  2. Max fee is the maximum amount of that a user is willing to pay for a transaction. It is the total of base fess and priority fee. If the max fee is set higher than the actual fee required for inclusion in a block, the difference is refunded to the user.
  3. Max priority fee is the incentive to miners to include the transaction in the next block quickly.

9. What is the difference between transaction cost and execution cost in ethereum?

Transaction encompasses all the expenses related to the transaction, including both the execution cost and the network fee while execution cost refers specifically to the amount of gas consumed by the computation and processing of a smart contract's code or an ethereum transaction.

10. What is a state variable?

In ethereum smart contracts, state variables are variables that store data on the blockchains state. They hold information that can be updated and modified by the execution of functions within the contract. They are available to all functions and other contracts depending on the visibility status.

11. What are free functions??

These are function that are not bound to a contract. They are defined outside of the contract and can be called independently without deploying or interacting with a contract. They are not stored on the blockchain.

12. What is the difference between parameters and arguments?

Parameters are placeholders or variables declared in the function definition while arguments are the actual values that are passed to a function when it is called.

for example:

function add(a, b) {
    return a + b;
}

In the above function, a and b are the parameters. They define the input that the add function expects to receive when it's called.

let result = add(5, 3);

In this example, 5 and 3 are the arguments passed to the addNumbers function, and they correspond to the a and b parameters defined in the function declaration.

13. What is the difference between msg.value and msg.data?

msg.value is a special variable that represents the amount of Ether (in wei) that was sent with a transaction.

msg.data is a special variable that contains the complete data payload of the transaction or message being processed. When a contract receives a message (transaction), the first four bytes of the msg.data represent the function signature, which is a keccak256 hash of the function name and its parameter types.

14. Outline the properties of msg in ethereum smart contracts

Sure! Here's the provided information in the form of an Markdown table:

DataData TypeDescription
msg.senderaddressRepresents the address of the sender of the message or transaction.
msg.valueuint256Represents the amount of Ether (in wei) sent with the transaction.
msg.databytesRepresents the complete data payload of the transaction. Contains function signature and more.
msg.gasuint256Represents the amount of gas remaining for the current transaction.
msg.sigbytes4Represents the first four bytes of msg.data. Contains function signature of the called function.
msg.affiliatesaddress payableIntroduced in Solidity 0.8.8. Represents the address that introduced the transaction.
msg.originaddressRepresents the original sender of the transaction, not the intermediate contracts.
msg.gaspriceuint256Represents the gas price provided by the sender for the transaction.
msg.data.lengthuint256Represents the length of the data payload in bytes. Used to check the length of encoded arguments.

15. What does coinbase mean?

In the Ethereum context, the "coinbase" refers to the address of the miner who successfully mined the block. It is also sometimes referred to as the "beneficiary" address.

16. What does uncle block mean?

The uncle block in Ethereum is also called Ommer block and it is used to refer to blocks that were almost included in the blockchain but did not make it into the main chain.

17. what differentiates verified contract from unverified contracts?

They simply refer to whether the source codes of a smart contract on the Ethereum blockchain has been independently reviewed and confirmed by third-party auditing services.

The goal of this verification process is to ensure that the contract's code is free from vulnerabilities, bugs, and security issues.

Once the code is verified, the contract's source code and the audit report are often published on platforms like Etherscan, making it transparent and accessible to the community.

18. what is the concept of canonical chain?

This refers to the chain of blocks within a blockchain network that has been agreed upon by majority of the participating nodes.

19. differentiate between implicit and explicit conversions in solidity

Implicit conversion occurs automatically and it is done by the programming language without the programmers intervention.

For example, in Solidity, an implicit conversion might occur when assigning an integer value to a larger-sized integer type. Solidity would automatically perform the conversion because there's no loss of data.

Explicit conversion is also known as type casting and it requires the programmer to specify the required conversion between data-types.

20. what is function signature?

This is a unique identifier that represents a function based on:

  1. The name of the function.
  2. Concatenation of the parameter types.
  3. The keccak256 hash of the concatenated string.

For example, consider the following Solidity function:

function transfer(address to, uint256 amount) external returns (bool);

The function signature would be calculated as follows:

Function name: "transfer" Parameter types: "address" and "uint256" Concatenated string: "transfer(address,uint256)" Calculate hash: Keccak-256("transfer(address,uint256)") => 0xa9059cbb

21. Differentiate between function selector and function signature.

Function Selector: The function selector is the first four bytes of the Keccak-256 (SHA-3) hash of the function signature. It's a unique identifier that represents a function based on its name and parameter types. The function selector is used when encoding function calls in transaction data or when interacting with contracts at a low level.

Function Signature: This is a unique identifier that represents a function based on:

  1. The name of the function.
  2. Concatenation of the parameter types.
  3. The keccak256 hash of the concatenated string. It is used to calculate the function signature.

22. How is call used in solidity?

The call function allows your contract to interact with other contracts on the ethereum blockchain. It returns a boolean value indicating whether the external call was successful or not.

This is how to use the call function in solidity:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MainContract {
    address public anotherContract;

    constructor(address _anotherContract) {
        anotherContract = _anotherContract;
    }

    function callOtherContractFunction(uint256 _value) public returns (bool) {
        // Encode the function signature and parameters
        bytes memory payload = abi.encodeWithSignature("yourFunction(uint256)", _value);

        // Use the call function to invoke the other contract's function
        (bool success, bytes memory returnData) = anotherContract.call(payload);
        
        // Check if the call was successful
        if (success) {
            // Process the return data from the other contract if needed
            // ...
        }

        return success;
    }
}

23. Explain the types of call.

In Solidity, there are three main types of function calls you can use to interact with other contracts: call, delegatecall, and staticcall:

  1. call: The call function is used to invoke external contract functions and send Ether. When using call, the calling contract's storage and state are not affected by the external contract's function execution. However, note that the gas stipend for call is limited, and if the external contract uses more gas than provided, the call will revert.

  2. delegatecall: The delegatecall function is used to execute code from another contract while maintaining the context of the calling contract. This means that the code is executed using the calling contract's storage and state. delegatecall does not transfer Ether and only modifies the state of the calling contract, not the target contract.

  3. staticcall: The staticcall function is used to read data from external contracts without modifying the state of the calling contract. It's useful when you want to retrieve information from another contract without the risk of accidentally modifying your own contract's state. staticcall does not consume any gas stipend, but it also cannot send Ether or modify storage.

Here's a simple example demonstrating the use of these three types of calls:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MainContract {
    address public targetContract;

    constructor(address _targetContract) {
        targetContract = _targetContract;
    }

    function callTarget() public returns (bool) {
        (bool callResult, ) = targetContract.call(abi.encodeWithSignature("externalFunction()"));
        return callResult;
    }

    function delegateCallTarget() public returns (bool) {
        (bool callResult, ) = targetContract.delegatecall(abi.encodeWithSignature("externalFunction()"));
        return callResult;
    }

    function staticCallTarget() public view returns (uint256) {
        (bool callResult, bytes memory returnData) = targetContract.staticcall(abi.encodeWithSignature("getValue()"));
        require(callResult, "Static call failed");
        return abi.decode(returnData, (uint256));
    }
}

contract TargetContract {
    uint256 public value;

    function externalFunction() external {
        value = 42;
    }

    function getValue() external view returns (uint256) {
        return value;
    }
}

In this example, the MainContract interacts with the TargetContract using call, delegatecall, and staticcall. The externalFunction in the TargetContract sets the value to 42, and the getValue function returns the stored value.

24. what led to the ethereum having a metering mechanism?

Because the Ethereum smart contract platform is turing complete, it can run any program that is computationally possible. This also implies that it is vulnerable to the halting issue. If a contract contains an infinite loop, it would impede the execution of other contracts, thereby disrupting the entire network.

Ethereum created the idea of gas and the gas limit to address this problem. A specific amount of gas is used for each action in a smart contract. The amount of gas used to carry out the contract matches the amount of computation. Users use Ether to pay for gas, while miners are compensated for their labor with gas fees.

Gas is introduced for a number of reasons:

  1. Avoiding Endless Loops: Contracts are allocated a set amount of computing work they may complete via the gas method. When a contract uses up all the gas allotted to it, it stops running. This avoids endless loops and guarantees that contracts eventually come to an end.
  2. Resource Administration: The gas mechanism makes sure contracts only employ a suitable amount of processing power. In doing so, the network's resources are not monopolized by any one contract.
  3. Fairness: Users are charged for the computing power that their contracts use. By ensuring that contracts are carried out in a manner consistent with the fees paid by users, a fair and motivating environment is created.
  4. Security of the Network: By ensuring that transactions are carried out effectively and minimizing resource exhaustion, the gas mechanism helps to maintain the stability and predictability of the network.

25. if you have int 8 in solidity, what range of values does it cover?

In Solidity, the int8 type represents a signed 8-bit integer or 256 bytes i.e. It covers a range of values from -128 to 127 (in bytes), inclusive. This range is a result of using 8 bits to represent the integer, with one bit used for the sign and the remaining 7 bits for the magnitude of the value.

26. what is the difference between uint and int in terms of the range of values they cover

In Solidity, both uint and int are used to represent integers, but they have different ranges and interpretations of those ranges:

  1. uint: This represents an unsigned integer, meaning it can only hold non-negative values. It covers a range of values from 0 to 2^256 - 1.

  2. int: This represents a signed integer, meaning it can hold both positive and negative values. An int can cover a range from -2^255 to 2^255 - 1.

27. Outline the parts of a solidity function

A Solidity function consists of several parts that define its behavior, inputs, outputs, and access control. Here are the main parts of a Solidity function:

  1. Function Declaration: This is the first line of the function and defines its name, parameters, and visibility/access control. It has the following format:

    function functionName(parameterTypes) visibility returns (returnTypes) {
        // function body
    }
    
  2. Function Modifiers: These are additional conditions or behaviors that can be applied to a function. They are typically used to enforce access control or modify the behavior of the function. Modifiers are defined elsewhere and are specified using the modifier keyword. They are applied using the modifierName syntax:

    modifier onlyOwner() {
        require(msg.sender == owner, "Only owner can call this function");
        _;
    }
    
    function changeOwner(address newOwner) public onlyOwner {
        owner = newOwner;
    }
    
  3. Function Parameters: These are the values that you pass to the function when you call it. Parameters are defined within parentheses after the function name. They specify the types and names of the values that the function expects to receive:

    function transfer(address to, uint amount) public {
        // function body
    }
    
  4. Visibility Keywords: Solidity functions can have different visibility levels that determine who can call the function. The visibility keywords are public, internal, external, and private:

    • public: The function is accessible from within and outside the contract.
    • internal: The function is only accessible from within the current contract and derived contracts.
    • external: The function is only callable from outside the contract and cannot be accessed internally.
    • private: The function is only accessible from within the current contract and not by derived contracts.
  5. Return Types: If a function returns a value, you need to specify its return type using the returns keyword:

    function getBalance(address account) public view returns (uint) {
        // function body
    }
    
  6. Function Body: This is the block of code enclosed by curly braces {} that defines what the function does. It contains the logic and operations that the function performs:

    function add(uint a, uint b) public pure returns (uint) {
        uint result = a + b;
        return result;
    }
    
  7. State Mutability: Functions can have different state mutability, indicated by keywords like pure, view, payable, and no keyword.

    • pure: The function does not read or modify contract state and does not use msg.sender. It's used for computational purposes only.
    • view: The function does not modify contract state but can read from it. It's used for reading data.
    • payable: The function can receive Ether as part of the transaction.
    • No keyword: The function can read and modify contract state, but it cannot receive Ether.

28. Explain the parts of a solidity variable.

A variable in Solidity is a named storage location that holds a value. It can represent data of various types, such as integers, addresses, booleans, strings, arrays, and more. Here are the main parts of a variable in Solidity:

  1. Data Type: The data type of the variable determines the kind of data it can store. Common data types include uint (unsigned integer), int (signed integer), address (Ethereum address), bool (boolean), and more complex types like arrays and structs.

  2. Variable Name: The variable name is a user-defined identifier that you use to refer to the variable. Variable names should follow Solidity's naming conventions, which typically use camelCase (starting with a lowercase letter).

  3. Initial Value: This is the value assigned to the variable when it is declared. It's optional, and if not provided, the variable will be initialized with a default value (usually zero for numeric types and an empty value for other types).

  4. Scope: The scope of a variable determines where it can be accessed and used. Solidity supports various levels of scope, including function scope, contract scope, and block scope.

  5. Visibility Modifiers: In Solidity, you can specify the visibility of a contract-level variable using access modifiers such as public, internal, external, and private. These modifiers control where the variable can be accessed from.

    • public: The variable is readable from outside the contract and has an automatically generated getter function.
    • internal: The variable is readable within the current contract and derived contracts.
    • external: The variable is not accessible internally and can only be read using a getter function.
    • private: The variable is only accessible within the current contract.
  6. State Variables: A state variable is a variable that is permanently stored on the Ethereum blockchain as part of the contract's storage. These variables have a persistent value between function calls and transactions.

  7. Local Variables: Local variables are declared within a function's scope and are used to store temporary data that is relevant only within that function. They do not persist between different function calls.

  8. Constant and Immutable Variables: Solidity supports the concepts of constant and immutable variables. constant variables are evaluated at compile-time and cannot change after deployment. immutable variables are similar but can be assigned values based on other immutable variables.

  9. Modifiers: Modifiers are used to apply additional conditions or behaviors to variable access or modification. They are defined using the modifier keyword and can be used to enforce access control or validation logic.

  10. Visibility and Access: The visibility and access of a variable determine where it can be accessed from and how it can be read or modified. Variables can be read or modified directly if they are public, or they can have getter functions for access.

Variables play a crucial role in storing and managing data within smart contracts. They enable contracts to interact with and manipulate data, making them an essential part of Solidity programming.

29. explain the types of variables with examples and where they are stored

  1. State Variables:

    • Stored in the contract's storage.

    • Persist between function calls and transactions.

    • Can be modified using setter functions.

    • Example:

      contract ExampleContract {
          uint256 public stateVar; // State variable
      }
      
  2. Local Variables:

    • Declared within a function's scope.

    • Temporary and only accessible within the function.

    • Do not persist between different function calls.

    • Example:

      contract ExampleContract {
          function localVarExample(uint256 value) public pure returns (uint256) {
              uint256 localVar = value; // Local variable
              return localVar * 2;
          }
      }
      
  3. Function Parameters:

    • Variables passed to a function when it is called.

    • Can be used within the function's scope.

    • Do not persist outside the function's execution.

    • Example:

      contract ExampleContract {
          function paramExample(uint256 paramValue) public pure returns (uint256) {
              return paramValue * 3;
          }
      }
      
  4. Function Return Values:

    • Values returned by a function after its execution.

    • Used to provide output from a function to its caller.

    • Do not persist outside the function's execution.

    • Example:

      contract ExampleContract {
          function returnExample(uint256 inputValue) public pure returns (uint256) {
              return inputValue * 4;
          }
      }
      
  5. Constant Variables:

    • Evaluated at compile-time and assigned a value.

    • Stored in the contract's bytecode and can be accessed without gas cost.

    • Cannot be modified after deployment.

    • Example:

      contract ExampleContract {
          uint256 constant constantVar = 42; // Constant variable
      }
      
  6. Immutable Variables:

    • Evaluated at deployment-time and assigned a value.

    • Stored in the contract's storage but can be accessed with low gas cost.

    • Can depend on other immutable variables.

    • Cannot be modified after deployment.

    • Example:

      contract ExampleContract {
          uint256 immutable immutableVar = 10; // Immutable variable
      }
      
  7. Function Return Variables (Storage and Memory):

    • Used to store the return value of a function.

    • Stored either in the contract's storage (persistent) or memory (temporary).

    • storage variables persist between transactions, while memory variables do not.

    • Example:

      contract ExampleContract {
          uint256 storageRetVal; // Storage variable
          
          function storeReturn(uint256 value) public {
              storageRetVal = value; // Storage variable assignment
          }
          
          function memoryReturn(uint256 value) public pure returns (uint256) {
              uint256 memoryRetVal = value * 5; // Memory variable
              return memoryRetVal;
          }
      }
      

Different types of variables serve various purposes within Solidity smart contracts, and understanding their characteristics and storage locations is essential for effective contract development.

30. how is an implicit getter function generated in solidity?

In Solidity, public state variables immediately provide an implicit getter function. You can obtain the value of the state variable using this getter function without explicitly defining a special function to do so.

31. what are the phases of creation of smart contract?

  1. Compile Time: The contract's source code is compiled into bytecode that is executable by the Ethereum Virtual Machine (EVM). The bytecode represents the instructions and logic of the smart contract.

  2. Construction Time: The constructor function is executed during contract creation. This function initializes the contract's state variables and performs any necessary setup. It can take constructor arguments that define the initial state of the contract.

  3. Deployment: The contract's bytecode is deployed to the Ethereum network by sending a deployment transaction. This transaction includes the compiled bytecode and any constructor arguments. The contract's bytecode and ABI are stored on-chain.

  4. Runtime: Once deployed, the contract's bytecode is executed by the EVM when transactions interact with the contract. This includes calling functions, modifying state variables, and emitting events. The contract's state can change over time as a result of these interactions.

32. Is contract creation the same as contract initialization?

When a contract is created, its constructor arguments and bytecode are deployed to the blockchain, which results in the establishment of a new contract address. Contrarily, contract initialization refers to the execution of the constructor function during the contract creation process to establish the contract's initial state.

33. Differentiate between EOAs and Contract Accounts

EOAs:

  • EOAs are user accounts controlled by private keys.
  • They are not associated with any code or smart contract.
  • They are used to send transactions, interact with smart contracts, and hold Ether and tokens.
  • EOAs have a balance of Ether and can sign transactions using their private keys.

Contract Accounts:

  • Contract Accounts are created when a smart contract is deployed to the Ethereum network.
  • Each Contract Account is associated with the bytecode and storage of the deployed smart contract.
  • Contract Accounts can receive Ether, execute the code of the associated smart contract, and store data in their storage.
  • They can be created by EOAs and interacted with through transactions.
  • Transactions to Contract Accounts contain data specifying which function to execute and with what arguments.

Related Posts

30+ Blockchain concepts for Beginners

## 1. Explain the EVMThe EVM (Ethereum Virtual Machine) is a decentralized stateful machine and this means that it is not controlled or owned by a single entity and anyone or everyone has equal rig

read more

Functions Storage in Solidity.

- There are three distinct areas for storing and managing data within a smart contract1. Calldata: This is a special, read-only, non-modifiable area where function arguments and external calldata is

read more

StarkNet Messaging Contract

This contract showcases key StarkNet development concepts, including storage, events, and functions. It allows users to send messages to each other and retrieve them.## PrerequisitesBefore we div

read more

Backend Cheat Sheet

# 1. Data Structures and Algorithms:### Linked List Implementation in Python: ```python class Node: def __init__(self, data): self.data = data self.next = Noneclass LinkedList

read more