Solidity Global Variables

msg · this · block · tx

Ever wondered how to add blockchain or transaction information to your Smart Contract❓

Use Global Variables 🌐

image.png

In Solidity, there are certain global variables that are used mainly to provide information about the blockchain or for general-purpose utility functions.

Let's review some of them👇

⚡️'msg' Global Variable:

-msg.data (bytes calldata): complete calldata

-msg.sender (address): sender of the message

-msg.sig (bytes4): first four bytes of the calldata

-msg.value (uint): number of wei sent with the message

Example:

image.png

⚡️'this' Global Variable:

Refers to the current smart contract and the returned value can be typed to an address.

image.png

⚡️'block' Global Variable:

-block.coinbase (address payable): current block miner’s address

-block.difficulty (uint): current block difficulty

-block.number (uint): current block number

-block.timestamp (uint): current block timestamp as seconds since unix epoch

⚡️'tx' Global Variable:

-tx.gasprice (uint): gas price of the transaction

-tx.origin (address): sender of the transaction (full call chain)