{"id":7336,"date":"2024-03-19T09:34:36","date_gmt":"2024-03-19T09:34:36","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7336"},"modified":"2024-03-19T09:34:40","modified_gmt":"2024-03-19T09:34:40","slug":"modify-a-variable-in-another-contract-in-solidity","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/modify-a-variable-in-another-contract-in-solidity\/","title":{"rendered":"Modify a Variable In Another Contract In Solidity"},"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\/modify-a-variable-in-another-contract-in-solidity\/#Why_the_Block_on_Direct_Modification\" title=\"Why the Block on Direct Modification?\">Why the Block on Direct Modification?<\/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\/modify-a-variable-in-another-contract-in-solidity\/#So_How_Do_Contracts_Interact_and_Share_Data\" title=\"So, How Do Contracts Interact and Share Data?\">So, How Do Contracts Interact and Share Data?<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/modify-a-variable-in-another-contract-in-solidity\/#Example_A_Token_Contract_and_a_Marketplace\" title=\"Example: A Token Contract and a Marketplace\">Example: A Token Contract and a Marketplace<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/modify-a-variable-in-another-contract-in-solidity\/#Inheritance_A_Special_Case\" title=\"Inheritance: A Special Case\">Inheritance: A Special Case<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/modify-a-variable-in-another-contract-in-solidity\/#Conclusion_Modify_a_Variable_In_Another_Contract_In_Solidity\" title=\"Conclusion: Modify a Variable In Another Contract In Solidity\">Conclusion: Modify a Variable In Another Contract In Solidity<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>In the world of Solidity, smart contracts are the building blocks for decentralized applications on the blockchain. These contracts hold data and execute code, but there&#8217;s a crucial rule to remember: <strong>they generally can&#8217;t directly modify a variable in another contract in Solidity.<\/strong><\/p>\n\n\n\n<p>Imagine a neighborhood with several houses (contracts). Each house has its own set of rules (functions) and valuables (variables). While you can visit your neighbor&#8217;s house (call their functions), you wouldn&#8217;t be able to just walk in and rearrange their furniture (change their variables) without their permission. This principle ensures security and prevents chaos in the blockchain ecosystem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-the-block-on-direct-modification\"><span class=\"ez-toc-section\" id=\"Why_the_Block_on_Direct_Modification\"><\/span>Why the Block on Direct Modification?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are a few key reasons why Solidity enforces this restriction:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Security:<\/strong>\u00a0Direct modification could create vulnerabilities. If any contract could change the value of another&#8217;s variable, it could lead to manipulation or unintended consequences. Imagine a contract controlling a valuable asset, and another contract suddenly changing its ownership \u2013 that wouldn&#8217;t be good!<\/li><li><strong>Predictability:<\/strong>\u00a0Solidity prioritizes predictable behavior. When a contract interacts with another, it should expect the other contract&#8217;s variables to remain constant unless explicitly changed through its own functions. This makes reasoning about smart contract interactions more reliable.<\/li><li><strong>Decentralization:<\/strong>\u00a0The beauty of blockchain lies in its distributed nature. Each node on the network needs to agree on the state of all contracts. Direct modification from one contract would create inconsistencies if not properly synchronized, undermining decentralization.<\/li><\/ul>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:\u00a0<a href=\"https:\/\/metaschool.so\/courses\/build-marketplace-erc404-tokens\" target=\"_blank\" rel=\"noreferrer noopener\">Build a Semi-Fungible ERC404 Tokens\u2019 Marketplace<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"so-how-do-contracts-interact-and-share-data\"><span class=\"ez-toc-section\" id=\"So_How_Do_Contracts_Interact_and_Share_Data\"><\/span>So, How Do Contracts Interact and Share Data?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>While direct modification is off-limits, there are ways for contracts to communicate and influence each other&#8217;s behavior:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Public Functions:<\/strong> Each contract can define public functions that allow other contracts to interact with its data. These functions act as the gates through which changes can be made. They often perform specific actions and potentially update the contract&#8217;s internal variables based on the provided input.<\/li><li><strong>Events:<\/strong> Contracts can emit events to notify other contracts about changes to their state. Think of it like a doorbell \u2013 the contract rings the bell (emits the event) to let others know something has happened, but it doesn&#8217;t directly invite them in (change the variable). The listening contracts can then react to the event and potentially take further actions.<\/li><li><strong>Interfaces:<\/strong> Interfaces define the functions a contract exposes without revealing its implementation details. This allows contracts to interact with each other based on a common understanding of functionality, even if they&#8217;re written differently.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-a-token-contract-and-a-marketplace\"><span class=\"ez-toc-section\" id=\"Example_A_Token_Contract_and_a_Marketplace\"><\/span>Example: A Token Contract and a Marketplace<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let&#8217;s illustrate this with an example. Imagine a contract representing a token (like a digital currency) and another contract for a marketplace where users can buy and sell these tokens.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The token contract wouldn&#8217;t allow another contract to directly change its total supply or individual user balances.<\/li><li>Instead, it would have public functions like\u00a0<code>transfer<\/code>\u00a0or\u00a0<code>approve<\/code>\u00a0that users (or other contracts acting on their behalf) can call to move tokens around.<\/li><li>The marketplace contract could call the token contract&#8217;s\u00a0<code>transferFrom<\/code>\u00a0function to facilitate token purchases, but it wouldn&#8217;t be able to tamper with the token contract&#8217;s internal variables directly.<\/li><\/ul>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:\u00a0<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><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"inheritance-a-special-case\"><span class=\"ez-toc-section\" id=\"Inheritance_A_Special_Case\"><\/span><strong>Inheritance: A Special Case<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There&#8217;s one exception to the &#8220;no direct modification&#8221; rule: inheritance. When a contract inherits from another contract (like a child inheriting from a parent), it has access to the parent&#8217;s internal variables (with some restrictions). However, inheritance is generally not recommended for unrelated contracts due to tight coupling and potential security risks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion-modify-a-variable-in-another-contract-in-solidity\"><span class=\"ez-toc-section\" id=\"Conclusion_Modify_a_Variable_In_Another_Contract_In_Solidity\"><\/span><strong>Conclusion: Modify a Variable In Another Contract In Solidity<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Solidity&#8217;s design encourages contracts to collaborate through well-defined channels like functions and events. This fosters a secure and predictable environment for building robust decentralized applications. While you can&#8217;t directly reach into another contract&#8217;s house and rearrange the furniture, you can always knock on the door (call a function) and see if they&#8217;re willing to make some changes themselves!<\/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":16,"featured_media":7343,"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,31,46,29],"class_list":["post-7336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-ethereum-blockchain","tag-solidity","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7336","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\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7336"}],"version-history":[{"count":1,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7336\/revisions"}],"predecessor-version":[{"id":7346,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7336\/revisions\/7346"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7343"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}