Skip to content

Msg.Sender Object

One very important object is the globally available msg-object.

View the Full Course Now

The msg-object is a special variables which always exist in the global namespace and is used to provide information about the blockchain or the transaction.

And one of the most important properties of the msg-object is msg.sender. The other property is msg.value, which we will discuss in the next section. msg.sender contains the address of the whoever initiated the current contract call. It sounds complicated, but let's run an example

Solidity Sample Code Msg-Sender

Add the following contract:

//SPDX-License-Identifier: MIT

pragma solidity 0.8.15;

contract ExampleMsgSender {

    address public someAddress;

    function updateSomeAddress() public {
        someAddress = msg.sender;
    }

}

When you run "updateSomeAddress" - without any parameter - the address variable "someAddress" will magically have your address. This is, because in msg.sender the address is stored who initiated the contract call.

You will later see that this is one of the most important concepts in Solidity and that's the reason why ERC20 contracts can exist.