My Image
CoursesQuizzesProblemsContestsSmartBooks
Contest!

No results found

LOGINREGISTER
My ProgressCoursesQuizzesProblemsContestsSmartbooks
Published on 6 Jul 2021
Functions to get Block and Transaction Properties
Block and Transaction Properties in Solidity
img
Rajendra Bisoi
0
Like
546

Block and Transaction Properties

  • blockhash(uint blockNumber) returns (bytes32): hash of the given block - only works for 256 most recent, excluding current, blocks
  • 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 payable): 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 payable): sender of the transaction (full call chain)

Now let's learn how to use them

See the data types thet have been written inside the brackets near the pre-defined functions are the datatypes that is returned when called; not to confused with Arguments

Here are some examples how to use them

pragma solidity ^0.8.3;
contract Task2{
    

 // create a functions to return following things (chainid, blockhash, difficulty, gas limit, block number)
    
   // block.number(uint);
   function getBlockNumber()public view returns (uint){
          return block.number;
    }
    
    
   // blockhash(uint blockNumber) returns (bytes32);
   function getBlockHash() public view  returns (bytes32){
          return blockhash(getBlockNumber()); //Since here getBlockNumber() is defined before, so we used it
         //or simply write  return blockhash(block.number); 
    }
    
    
    //Block Hash of previous block
     function getPreviousBlockHash() public view  returns (bytes32){
          return blockhash(block.number-1);
    }
    
    
    // block.chainid (uint);
    function getChainId()public view returns (uint){
          return block.chainid;
    }
    
  
    // block.gaslimit (uint);
    function getGasLimit()public view returns (uint){
          return block.gaslimit;
    }
    
    // block.difficulty (uint);
    function getBlockDifficulty()public view returns (uint){
          return block.difficulty;
    }
   
  
}

For many more exciting tutorials keep following my username @BlockTalks_Raj

!!!!!HAPPY LEARNING!!!!!

Enjoyed the SmartBook?
Like
logo
contact@dapp-world.com
Katraj, Pune, Maharashtra, India - 411048

Follow Us

linkedintwitteryoutubediscordinstagram

Products

  • SmartBooks
  • Courses
  • Quizzes
  • Assessments

Support

  • Contact Us
  • FAQ
  • Privacy Policy
  • T&C

Backed By

ah! ventures

Copyright 2023 - All Rights Reserved.

Recommended from DAppWorld
img
1 May 2021
How to connect Ganache with Metamask and deploy Smart contracts on remix without
Set up your development environment with (Metamask + Ganache + Remix) and skip truffle :)
3 min read
11441
5
img
8 Jul 2021
How to interact with smart contarct from backend node js
call and send functions from backend server side using nodejs
3 min read
7959
2
img
18 Aug 2021
Send transaction with web3 using python
Introduction to web3.py and sending transaction on testnet
3 min read
6184
5
img
5 Aug 2021
Deploy Smart Contract on Polygon POS using Hardhat
how to deploy smart contracts on polygon pos chain using hardhat both mainnet and testnet ?
3 min read
5512
3