{"id":7311,"date":"2024-03-18T07:47:00","date_gmt":"2024-03-18T07:47:00","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7311"},"modified":"2024-03-19T09:44:15","modified_gmt":"2024-03-19T09:44:15","slug":"solidity-global-variables","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/solidity-global-variables\/","title":{"rendered":"Solidity Global Variables"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#Understanding_Solidity_Global_Variables\" title=\"Understanding Solidity Global Variables\">Understanding Solidity Global Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#Solidity_data_types\" title=\"Solidity data types\">Solidity data types<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#Different_types_of_Solidity_variables\" title=\"Different types of Solidity variables\">Different types of Solidity variables<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#Local_variables_example\" title=\"Local variables example\">Local variables example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#State_variables_example\" title=\"State variables example\">State variables example<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#What_are_global_variables\" title=\"What are global variables?\">What are global variables?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#Types_of_Solidity_global_variables\" title=\"Types of Solidity global variables\">Types of Solidity global variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#Accessing_global_variables\" title=\"Accessing global variables\">Accessing global variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#Use_cases_and_examples\" title=\"Use cases and examples\">Use cases and examples<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#Best_practices_and_considerations\" title=\"Best practices and considerations\">Best practices and considerations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/metaschool.so\/articles\/solidity-global-variables\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2 class=\"wp-block-heading\" id=\"understanding-solidity-global-variables\"><span class=\"ez-toc-section\" id=\"Understanding_Solidity_Global_Variables\"><\/span>Understanding Solidity Global Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In <a href=\"https:\/\/metaschool.so\/articles\/guide-solidity-contracts\/\" target=\"_blank\" rel=\"noreferrer noopener\">Solidity<\/a>, global variables play a crucial role in facilitating smart contract development. They are accessible across different functions within a contract and hold essential information about the contract&#8217;s state, Ethereum network, and transaction details.<\/p>\n\n\n\n<p>Before understanding the global variables, we need to understand what variables are, what data types Solidity uses, and what type of variables Solidity has. Let\u2019s understand these concepts one by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"solidity-data-types\"><span class=\"ez-toc-section\" id=\"Solidity_data_types\"><\/span>Solidity data types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Like all other programming languages, variables are used to store data in Solidity. In Solidity smart contracts, variables are used to track data like arrays, boolean, strings, addresses, and signed\/unsigned integers. Here is the list of data types Solidity offers.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>bool<\/code>: Boolean data type is used to store <code>true<\/code> or <code>false<\/code>.<\/li><li><code>uint<\/code>: <code>unit<\/code> data type is used to store the unsigned integers. These are the few unsigned integers: <code>uint8<\/code>, <code>uint16<\/code>, <code>uint32<\/code>, and <code>uni265<\/code>.<\/li><li><code>int<\/code>: <code>int<\/code> data type is used to store the signed integers, for example, -12, -567, and -10, etc. Just like <code>uint<\/code>, we also have different variations of the <code>int<\/code> variables: <code>int8<\/code>, <code>int16<\/code>, <code>int32<\/code>, and <code>int265<\/code>.<\/li><li><code>string<\/code>: String data type is used to store strings in a variable.<\/li><li><code>address<\/code>: The <code>address<\/code> data type is commonly used to store wallet and smart contract addresses.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"different-types-of-solidity-variables\"><span class=\"ez-toc-section\" id=\"Different_types_of_Solidity_variables\"><\/span>Different types of Solidity variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are three types of variables in Solidity. Let\u2019s learn what they are.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Type of Variables<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>Local variables<\/td><td>Local variables are the variables defined inside of the function. These variables work like temporary variables.<\/td><\/tr><tr><td>State variables<\/td><td>State variables in Solidity can be accessed or modified by internal or external function calls from within the contract.<\/td><\/tr><tr><td>Global variables<\/td><td>Global variables in Solidity can be accessed by any contract on the network. They are declared outside of a contract.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>It is important to understand the difference between all types of variables so that you know how different types of variables work. Before moving forward, let\u2019s look at short coding examples of local and state variables.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>&nbsp;<a href=\"https:\/\/metaschool.so\/courses\/one-piece-personality-dapp-solidity\" target=\"_blank\" rel=\"noreferrer noopener\">Build a One Piece Personality dApp With Solidity<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"local-variables-example\"><span class=\"ez-toc-section\" id=\"Local_variables_example\"><\/span>Local variables example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here\u2019s an example of local variables in Solidity.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract LocalVariablesExample {\n    function exampleFunction() public view returns (uint64) {\n\t\t    \/\/ Following are all local variables\n        uint64 a = 1;\n        uint64 b = 2;\n        uint64 sum = a + b;\n        return sum;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"state-variables-example\"><span class=\"ez-toc-section\" id=\"State_variables_example\"><\/span>State variables example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here\u2019s an example of state variables in Solidity.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract StateVariablesExample {\n\t\t\t\t\/\/ These two are state variables\n\t\t    uint64 public a = 1;\n        uint64 public b = 2;\n\n    function exampleFunction() public view returns (uint64) {\n\t\t    \/\/ Following is the local variable\n        uint64 sum = a + b;\n        return sum;\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-global-variables\"><span class=\"ez-toc-section\" id=\"What_are_global_variables\"><\/span>What are global variables?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Global variables in Solidity are predefined variables that hold specific values or provide information about the current state of the contract or the Ethereum blockchain. Unlike local variables, which are confined to a specific function&#8217;s scope, global variables can be accessed from any function within the contract.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-solidity-global-variables\"><span class=\"ez-toc-section\" id=\"Types_of_Solidity_global_variables\"><\/span>Types of Solidity global variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Solidity offers various global variables, each serving a distinct purpose. Some of the essential global variables include:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Block Variables<\/strong>: These variables provide information about the current block on the Ethereum blockchain. Examples include <code>block.number<\/code>, <code>block.timestamp<\/code>, <code>block.difficulty<\/code>, and <code>block.coinbase<\/code>.<\/li><li><strong>Transaction Variables<\/strong>: They offer insights into the current transaction&#8217;s details, such as <code>msg.sender<\/code>, <code>msg.value<\/code>, and <code>msg.data<\/code>.<\/li><li><strong>Contract Variables<\/strong>: These variables provide information about the current contract itself. Examples include <code>this.balance<\/code>, which indicates the contract&#8217;s Ether balance.<\/li><li><strong>Gas Variables<\/strong>: Solidity also includes global variables related to gas, such as <code>gasleft()<\/code>, which returns the amount of gas remaining in the current transaction.<\/li><\/ol>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>&nbsp;<a href=\"https:\/\/metaschool.so\/courses\/build-marketplace-erc404-tokens\" target=\"_blank\" rel=\"noreferrer noopener\">Build a Semi-Fungible ERC404 Tokens&#8217; Marketplace<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"accessing-global-variables\"><span class=\"ez-toc-section\" id=\"Accessing_global_variables\"><\/span>Accessing global variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Accessing global variables is straightforward in Solidity. You can directly reference them within any function in your contract. For example, to retrieve the sender&#8217;s address, you would use <code>msg.sender<\/code>. Similarly, to obtain the block number, you would use <code>block.number<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract GlobalVariablesExample {\n    function getSender() public view returns (address) {\n        return msg.sender;\n    }\n\n    function getBlockNumber() public view returns (uint) {\n        return block.number;\n    }\n}<\/code><\/pre>\n\n\n\n<p>In this example, the <code>getSender<\/code> function returns the sender\u2019s address, while the <code>getBlockNumber<\/code> function returns the current block number.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use-cases-and-examples\"><span class=\"ez-toc-section\" id=\"Use_cases_and_examples\"><\/span>Use cases and examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Global variables find applications in various scenarios within smart contracts. Let&#8217;s explore some common use cases and examples:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Access Control<\/strong>: Global variables like <code>msg.sender<\/code> are often used for access control mechanisms. By verifying the sender&#8217;s address, contracts can restrict certain functions to specific users or roles.<\/li><li><strong>Timestamp Manipulation<\/strong>: <code>block.timestamp<\/code> enables contracts to incorporate time-dependent logic, such as scheduling future events or implementing time-based restrictions.<\/li><li><strong>Gas Optimization<\/strong>: Understanding gas-related global variables like <code>gasleft()<\/code> allows developers to optimize contract execution and minimize transaction costs.<\/li><li><strong>Contract Interaction<\/strong>: Global variables like <code>this.balance<\/code> are useful for managing Ether within a contract, enabling functionalities like sending funds to other contracts or addresses.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"best-practices-and-considerations\"><span class=\"ez-toc-section\" id=\"Best_practices_and_considerations\"><\/span>Best practices and considerations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>While leveraging global variables in Solidity, it&#8217;s essential to adhere to best practices and consider potential pitfalls:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Considerations<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td>Security<\/td><td>Avoid using global variables for access control as they can be manipulated by attackers. Use role-based access control (RBAC) to improve contract security.<\/td><\/tr><tr><td>Gas Limitations<\/td><td>Avoid using too many as-related global variables to prevent running out of gas during contract execution. Complex operations or loops can increase transaction costs by consuming significant gas.<\/td><\/tr><tr><td>Immutable State<\/td><td>Avoid using mutable states derived from immutable global variables like <code>block.number<\/code> for critical contract logic.<\/td><\/tr><tr><td>Version Compatibility<\/td><td>Check the compatibility of global variables&#8217; Solidity version to prevent compilation issues due to updates or deprecation.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>&nbsp;<a href=\"https:\/\/metaschool.so\/courses\/build-hogwarts-sorting-cap-dapp-on-polygon-mumbai\" target=\"_blank\" rel=\"noreferrer noopener\">Build Hogwarts Sorting Hat dApp on Polygon<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In conclusion, global variables in Solidity are indispensable tools for smart contract development, offering insights into the blockchain&#8217;s state and transaction details.<\/p>\n\n\n\n<p>Developers can build robust and efficient smart contracts tailored to diverse use cases by understanding and utilizing these variables effectively.<\/p>\n\n\n\n<p>However, it&#8217;s crucial to exercise caution, adhere to best practices, and stay updated with Solidity&#8217;s evolving ecosystem to harness the full potential of global variables in smart contract development.<\/p>\n\n\n\n<p><strong>Try it out, ask us questions, and tell us how it went by tagging Metaschool on Social Media.<\/strong><\/p>\n\n\n\n<p><strong>Follow us on<\/strong>&nbsp;\u2013<\/p>\n\n\n\n<p>\ud83d\udd2eTwitter \u2013&nbsp;<a href=\"https:\/\/twitter.com\/0xmetaschool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/twitter.com\/0xmetaschool<\/a><\/p>\n\n\n\n<p>\ud83d\udd17LinkedIn \u2013&nbsp;<a href=\"https:\/\/www.linkedin.com\/company\/0xmetaschool\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.linkedin.com\/company\/0xmetaschool\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":15,"featured_media":7312,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[51,46,54,29],"class_list":["post-7311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-solidity","tag-solidity-programming-language","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7311"}],"version-history":[{"count":3,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7311\/revisions"}],"predecessor-version":[{"id":7349,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7311\/revisions\/7349"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7312"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}