Solidity Basics II

Predefined global variables

A small number of global objects are accessible to a contract during execution in the EVM. These objects are msg, block, and tx. The values of these objects are read-only and cannot be modified within the smart contract.

The msg object

The msg object contains several fields that provide information about the current transaction call. Here’s a list of the most commonly used fields:

  • msg.sender: The address that initiated the contract call (If the contract is called directly by an EOA transaction, then this is the address of the EOA account, but otherwise, it’s a contract address).

  • msg.value: The value (in Wei) of Ether sent with the transaction.

  • msg.data: The data payload for calls to a CA.

  • msg.sig: It’s the function selector.

The block object

The block object contains several fields that provide information about the current block. Here’s a list of the most commonly used fields:

  • block.number: The number of the current block.

  • block.timestamp: The timestamp of the current block, calculated in seconds since the Unix epoch.

  • block.gaslimit: The maximum amount of gas the current block can contain.

  • block.coinbase: The address of the miner that mined the current block.

The tx object

The tx object contains transaction related information:

  • tx.gasprice: The gas price specified by the sender of the transaction.

  • tx.origin: The original sender of the transaction.

Functions

In Solidity, functions are used to perform specific tasks or operations within a smart contract. Functions can be defined with specific input parameters and may return a value. The syntax to declare a function in Solidity is as follows:

Get hands-on with 1200+ tech skills courses.