Block and Transaction details in Solidity

Block and Transaction details in Solidity

Get the inside scoop on the Ethereum blockchain with Solidity's Block and Transaction detail functions.

The following code implements all major solidity global variables along with block and transaction details

Block and Transaction Variables:

Solidity provides access to a few global variables that are not declared within contracts but are accessible from code within contracts. Contracts cannot access the ledger directly. A ledger is maintained by miners only; however Solidity provides some information about the current transaction and block to contracts so that they can utilize them. Solidity provides both block-as well as transaction-related variables.

  • blockhash(uint blockNumber) returns (bytes32): hash of the given block when blocknumber is one of the 256 most recent blocks; otherwise returns zero

  • block.basefee (uint): current block’s base fee (EIP-3198 and EIP-1559)

  • block.chainid (uint): current chain id

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

  • block.difficulty (uint): current block difficulty

  • block.gaslimit (uint): current block gaslimit

  • block.number (uint): current block number

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

  • gasleft() returns (uint256): remaining gas

  • msg.data (bytes calldata): complete calldata

  • msg.sender (address): sender of the message (current call)

  • msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)

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

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

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

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

contract TransactionVariables{

    event loguint(uint);
    event logbytes(bytes);
    event logaddress(address);
    event logbyte4(bytes4);
    event logblock(bytes32);

    function showBlockVariables() public payable{
        emit logaddress(block.coinbase);
        emit loguint(block.difficulty);
        emit loguint(block.gaslimit);

Note:

  • The values of all members of msg, including msg.sender and msg.value can change for every external function call. This includes calls to library functions.

  • When contracts are evaluated off-chain rather than in context of a transaction included in a block, you should not assume that block.* and tx.* refer to values from any specific block or transaction. These values are provided by the EVM implementation that executes the contract and can be arbitrary.

  • Do not rely on block.timestamp or blockhash as a source of randomness, unless you know what you are doing.

  • Both the timestamp and the block hash can be influenced by miners to some degree. Bad actors in the mining community can for example run a casino payout function on a chosen hash and just retry a different hash if they did not receive any money.

  • The current block timestamp must be strictly larger than the timestamp of the last block, but the only guarantee is that it will be somewhere between the timestamps of two consecutive blocks in the canonical chain.

  • The block hashes are not available for all blocks for scalability reasons. You can only access the hashes of the most recent 256 blocks, all other values will be zero.

  • The function blockhash was previously known as block.blockhash, which was deprecated in version 0.4.22 and removed in version 0.5.0.

  • The function gasleft was previously known as msg.gas, which was deprecated in version 0.4.21 and removed in version 0.5.0.

  • In version 0.7.0, the alias now (for block. Timestamp) was removed.

For more content, follow me on - https://linktr.ee/shlokkumar2303

Did you find this article valuable?

Support Shlok Kumar by becoming a sponsor. Any amount is appreciated!