What to Know about Solidity Built-in-Variables.

Solidity has built-in Variables, that show specific details about your contract. With the built-in variables, you can call know information about your contracts, such as the Etherum address that calls out your smart contract or how much of Ether can be transferred to your smart contract.

In solidity, we can write these built-in-variables in three categories:

  • The First category is transaction

  • The Second Category is msg

  • The last category is block

  • Transaction: In transaction, the most useful information is the tx.origin: This represents the Etherum Address that sends the transaction. Next we have
  • msg: That stands for messages. This will provide information about the message that is being transferred. for example msg.sender will provide the Etherum address of the sender of the message while msg.value will provide the value of the Ether that is being sent.

I know you might be asking the difference between txn.origin, and msg.sender. Let's say for example Bob calls smart contract X: In this case, Bob address = txn.orign, and then the smart contract X is = msg.sender. When smart contract X calls another smart contract, let us call it smart contract Y, then it means that Bob address is still = txn.origin, while the msg.sender is the smart contract X. you get the logic now?.

buit-in-variable.PNG

So in essence there is only one transaction origin, however, the messages are inside the smart contracts.

  • block built-in-variables: The most useful information here is the block.timestamp, this will give you the timestamp for when the block is mined. Data on the blockchain is always stored as a block, to get the timestamp for each of the blocks mined on the blockchain, you use the block.timestamp variable to get that information. The timestamp is measured in seconds.

These built-in-variables are not the only ones in solidity, but the most common ones used when writing solidity, if you want to check out more information about other built-in-variable, you can check the link built-in-variables: Solidity docs