Loading...
blockhash(uint blockNumber) returns (bytes32)
: hash of the given block - only works for 256 most recent, excluding current, blocksblock.chainid
(uint
): current chain idblock.coinbase
(address payable
): current block miner’s addressblock.difficulty
(uint
): current block difficultyblock.gaslimit
(uint
): current block gaslimitblock.number
(uint
): current block numberblock.timestamp
(uint
): current block timestamp as seconds since unix epochgasleft() returns (uint256)
: remaining gasmsg.data
(bytes calldata
): complete calldatamsg.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 messagetx.gasprice
(uint
): gas price of the transactiontx.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!!!!!