The Wayback Machine - https://web.archive.org/web/20240708020734/https://www.geeksforgeeks.org/solidity-global-variables/
Open In App

Solidity Global Variables

Last Updated : 08 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Global variables in Solidity are predefined variables available in any function or contract. These variables reveal blockchain, contract, and transaction data. Some common global variables in Solidity:

Variable Type Description
msg.sender address The address of the account that sent the current transaction.
msg.value  uint The amount of Ether sent with the current transaction.
block.coinbase  address The address of the miner who mined the current block.
block.difficulty  uint The difficulty of the current block.
block.gaslimit  uint The maximum amount of gas that can be used in the current block.
block.number  uint The number of the current block.
block.timestamp  uint  The timestamp of the current block.
now uint An alias for block.timestamp.
tx.origin  address  The address of the account that originally created the transaction (i.e., the sender of the first transaction in the call chain).
tx.gasprice  uint The gas price (in Wei) of the current transaction.

These variables are handy for getting blockchain and transaction information but use them carefully. Authentication and authorization should not utilize tx.origin since attackers may simply fake it. Use msg.sender instead.

  • The contract cannot directly modify read-only global variables.
  • They can make contract choices and execute logic. msg.sender can restrict contract functions to the owner.
  • Global variables’ values vary based on context. In a transaction, msg.sender refers to the sender’s address, but outside of a transaction, it refers to the contract’s address (such as in a constructor).

Below is the Solidity program to implement global variables:

Solidity




// Solidity program to implement 
// global variables
pragma solidity ^0.8.0;
  
contract GlobalVariablesExample {
    
  address public owner;
  constructor() 
  {
    // set the contract owner to the address 
    // that deployed the contract
    owner = msg.sender; 
  }
    
  function getOwner() public view returns (address) 
  {
    // return the contract owner address
    return owner; 
  }
    
  function isOwner(address _address) public view returns (bool) 
  {
    // check if the provided address matches 
    // the contract owner
    return _address == owner; 
  }
    
  function sendEther(address payable _recipient) public payable 
  {
    // send ether to the specified recipient
    require(msg.sender == owner, 
            "Only the contract owner can send ether.");
    _recipient.transfer(msg.value); 
  }
    
  function getCurrentBlock() public view returns (uint, 
                                                  uint, address) 
  {
    // return the current block number, timestamp, 
    // and coinbase address
    return (block.number, block.timestamp, block.coinbase); 
  }
}


Explanation: This example defines the GlobalVariablesExample contract. The function Object() { [native code] } initializes the owner state variable to the contract’s deployment address using the msg.sender global variable.

The contract specifies global variable functions:

  • GetOwner returns owner.
  • IsOwner returns a boolean value if an address matches the owner.
  • If msg.sender matches owner, the contract owner may transmit ether to a receiver using the sendEther method.
  • The getCurrentBlock method uses global variables block.number, block.timestamp, and block.coinbase to retrieve block information.
  • The sendEther method requires the contract owner to send ether. If the need statement is false, the transaction will be reversed with the error notice.

Global variables may be used in Solidity contracts to make choices and execute logic.

Global Variables

 



Similar Reads

Solidity - Variables
A Variable is basically a placeholder for the data which can be manipulated at runtime. Variables allow users to retrieve and change the stored information. Rules For Naming Variables 1. A variable name should not match with reserved keywords. 2. Variable names must start with a letter or an underscore (_), and may contain letters from "a to z" or
4 min read
Solidity - Special Variables
There exist special variables and functions in solidity which exist in the global namespace and are mainly used to provide information about the blockchain or utility functions. They are of two types: 1) Block and Transaction Properties: Block Transaction Properties block.coinbase (address payable)Current block miner's addressblock.difficulty (uint
3 min read
Solidity Local Variables
Local variables in Solidity are defined within functions or code blocks. They are only available inside that function or block and deleted when that scope ends. They hold temporary data that is only relevant inside a function or block of code. They may store intermediate computations, data, or function return values.Solidity local variables need da
2 min read
Solidity - State Variables
Any variable created at the contract level is called the state variable (variables that aren't inside a function) and it is stored permanently on the blockchain i.e., they are persistent and cannot be deleted. So, if you were to create a state variable in your contract and assign it some value, and came back after some time then its value would sti
10 min read
Solidity - Types
Solidity is a statically typed language, which implies that the type of each of the variables should be specified. Data types allow the compiler to check the correct usage of the variables. The declared types have some default values called Zero-State, for example for bool the default value is False. Likewise other statically typed languages Solidi
4 min read
How to use GANACHE Truffle Suite to Deploy a Smart Contract in Solidity (Blockchain)?
There are various processes involved in deploying a smart contract using Ganache and Truffle Suite: 1. Install Ganache first. Ganache is a personal blockchain for Ethereum development. You must first download and install it. It is available for download from https://www.trufflesuite.com/ganache, the official website. 2. Install Truffle Suite: The n
4 min read
Solidity - Functions
A function is basically a group of code that can be reused anywhere in the program, which generally saves the excessive use of memory and decreases the runtime of the program. Creating a function reduces the need of writing the same code over and over again. With the help of functions, a program can be divided into many small pieces of code for bet
4 min read
What are Events in Solidity?
Solidity Events are the same as events in any other programming language. An event is an inheritable member of the contract, which stores the arguments passed in the transaction logs when emitted. Generally, events are used to inform the calling application about the current state of the contract, with the help of the logging facility of EVM. Event
2 min read
Solidity - Inheritance
Inheritance is one of the most important features of the object-oriented programming language. It is a way of extending the functionality of a program, used to separate the code, reduces the dependency, and increases the re-usability of the existing code. Solidity supports inheritance between smart contracts, where multiple contracts can be inherit
6 min read
Solidity - Polymorphism
Polymorphism is the ability to process data in more than one form. Like any other programming language, Solidity also supports polymorphism. Solidity supports two types of polymorphism, Function Polymorphism, and Contract Polymorphism. Function Polymorphism Function Polymorphism is also known as method overloading. In function polymorphism, multipl
3 min read
Article Tags :
three90RightbarBannerImg