{"id":7082,"date":"2024-01-31T07:40:45","date_gmt":"2024-01-31T07:40:45","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7082"},"modified":"2025-01-16T08:23:11","modified_gmt":"2025-01-16T08:23:11","slug":"solidity-basics-msg-sender","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/solidity-basics-msg-sender\/","title":{"rendered":"Understanding msg.sender in Solidity: Address and Smart Contract Basics"},"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-basics-msg-sender\/#Introduction_to_msgsender\" title=\"Introduction to&nbsp;msg.sender\">Introduction to&nbsp;msg.sender<\/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-basics-msg-sender\/#Solidity_msgsender_example\" title=\"Solidity&nbsp;msg.sender&nbsp;example\">Solidity&nbsp;msg.sender&nbsp;example<\/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-basics-msg-sender\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>In Solidity,&nbsp;&#8220;msg.sender&#8221;&nbsp;is something that is used frequently and it is important to get familiar with it, know what exactly it does, and what is it used for.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introduction-to-msg-sender\"><span class=\"ez-toc-section\" id=\"Introduction_to_msgsender\"><\/span>Introduction to&nbsp;msg.sender<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So,&nbsp;&#8220;msg.sender&#8221;&nbsp;is a dynamic global variable in <a href=\"https:\/\/metaschool.so\/articles\/guide-solidity-contracts\/\" target=\"_blank\" rel=\"noreferrer noopener\">Solidity<\/a> that you can access anywhere in the code without importing anything. This variable is filled automatically when any transaction happens. It is called a dynamic variable because it can change throughout the transaction process.<\/p>\n\n\n\n<p>&#8220;msg.sender&#8221;&nbsp;represents the address of the account that called the function present within the <a href=\"https:\/\/metaschool.so\/articles\/what-is-a-smart-contract\/\" target=\"_blank\" rel=\"noreferrer noopener\">smart contract<\/a>. What if one smart contract calls another smart contract? In this case, the&nbsp;&#8220;msg.sender&#8221;&nbsp;will have the address of the calling smart contract instead of an account address.<\/p>\n\n\n\n<p>For example, if contract A calls contract B, then <code>msg.sender<\/code> inside of contract B will have the address of contract A. And if a certain wallet calls the contract then the <code>msg.sender<\/code> will have the <a href=\"https:\/\/metaschool.so\/articles\/wallet-address-blockchain\/\" target=\"_blank\" rel=\"noreferrer noopener\">wallet address<\/a>.<\/p>\n\n\n\n<p><code>msg.sender<\/code>&nbsp;is used wherever we need to track the source of the transaction within the smart contract. So,&nbsp;&#8220;msg.sender&#8221;&nbsp;can be the address of a smart contract or the address of the account.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this out:<\/strong>&nbsp;<a href=\"https:\/\/metaschool.so\/build-in-public\">B<\/a><a href=\"https:\/\/metaschool.so\/build-in-public\" target=\"_blank\" rel=\"noreferrer noopener\">uild in Public<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"solidity-msg-sender-example\"><span class=\"ez-toc-section\" id=\"Solidity_msgsender_example\"><\/span>Solidity&nbsp;msg.sender&nbsp;example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here is how you can use&nbsp;<code>msg.sender<\/code>&nbsp;in your Solidity code.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:16px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.20;\n\ncontract ExampleContract {\n    address private _owner;\n    constructor() public {\n        _owner = msg.sender;\n    }\n    function getOwner() public view returns (address) {\n        return _owner;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"explanation\">Explanation<\/h3>\n\n\n\n<p>Let\u2019s look at what we did here.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>First of all, we defined the version for our Solidity compiler.<\/li><li>Then, we defined the contract named&nbsp;<code>ExampleContract<\/code>.<\/li><li>Inside of the contract, we defined a private variable&nbsp;<code>_owner<\/code>&nbsp;of the&nbsp;<code>address<\/code>&nbsp;type.<\/li><li>Inside the <code>constructor<\/code> function, we initialized the&nbsp;<code>_owner<\/code>&nbsp;variable with the&nbsp;<code>msg.sender<\/code>&nbsp;address. Now, whoever will call the contract or define the contract will be its owner.<\/li><li>To get the&nbsp;<code>_owner<\/code>&nbsp;address, we define the&nbsp;<code>getOwner()<\/code>&nbsp;function inside of the&nbsp;<code>ExampleContract<\/code>&nbsp;that returns the value of&nbsp;<code>_owner<\/code>&nbsp;variable.<\/li><\/ol>\n\n\n\n<p>\ud83d\udd25<strong>&nbsp;Check this course out:<\/strong>&nbsp;<a href=\"https:\/\/metaschool.so\/courses\/how-to-write-a-smart-contract-and-mint-elon-musk-nft-on-opensea\">Write an <\/a><a href=\"https:\/\/metaschool.so\/courses\/how-to-write-a-smart-contract-and-mint-elon-musk-nft-on-opensea\" target=\"_blank\" rel=\"noreferrer noopener\">Elon Musk NFT Smart Contract on OpenSea<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output<\/h3>\n\n\n\n<p>After deploying the contract on <a href=\"https:\/\/metaschool.so\/articles\/remix-ide-ethereum\/\" target=\"_blank\" rel=\"noreferrer noopener\">Remix IDE<\/a>, you\u2019ll see a button&nbsp;<code>getOwner<\/code>&nbsp;that will allow you to view the&nbsp;<code>msg.sender<\/code>&nbsp;value. Let\u2019s look at where you can find the&nbsp;<code>getOwner<\/code>&nbsp;button.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1712\" style=\"aspect-ratio: 3024 \/ 1712;\" width=\"3024\" controls src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/01\/Screen-Recording-2024-01-31-at-11.05.27-AM.mov\"><\/video><\/figure>\n\n\n\n<p>Here\u2019s what the output looks like in my case:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/01\/msg.sender-output-1024x576.png\" alt=\"ExampleContract Output\" class=\"wp-image-7103\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/01\/msg.sender-output-1024x576.png 1024w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/01\/msg.sender-output-300x169.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/01\/msg.sender-output-150x84.png 150w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/01\/msg.sender-output-768x432.png 768w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/01\/msg.sender-output-1536x864.png 1536w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/01\/msg.sender-output-1320x743.png 1320w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/01\/msg.sender-output.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\ud83d\udd25&nbsp;<strong>Check this course out:<\/strong>&nbsp;<a href=\"https:\/\/metaschool.so\/courses\/create-your-own-ethereum-token-in-just-30-mins\">Create Your Own Ethereum Token in Just 30 Mins<\/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>Note that,&nbsp;&#8220;msg.sender&#8221;&nbsp;can save either the contract or the wallet address. This means that if one contract calls for another contract, the 2nd contract will receive the 1st contract address as&nbsp;<code>msg.sender<\/code>. This helps track the source of the transaction within the smart contract.<\/p>\n\n\n\n<p><strong>Tried this out? Let us know how it went by tagging Metaschool on your 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":11071,"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,69,46,54,29],"class_list":["post-7082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-learn-web3","tag-solidity","tag-solidity-programming-language","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7082","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=7082"}],"version-history":[{"count":10,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7082\/revisions"}],"predecessor-version":[{"id":11890,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7082\/revisions\/11890"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/11071"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}