26

I just started to learn Solidity as a personal challenge. I'm not a developer so I've a loooooong way to go.

I'm following the Ethereum.org tutorial, here is what I've got doubt about: What does [msg.sender] stand for? I guess it is the wallet address of who triggered the contract, but I'm not sure.

1
  • RTFM "msg.sender (address): sender of the message (current call)"
    – Sumak
    Feb 1, 2018 at 13:49

3 Answers 3

41

msg.sender (address): sender of the message (current call)

msg.sender will be the person who's currently connecting with the contract.

Later on, you'll probably deal with contracts connecting with contracts. In that case, the contract that is creating the call would be the msg.sender.

Check out the documentation here: https://docs.soliditylang.org/en/develop/units-and-global-variables.html#block-and-transaction-properties

1
  • I am a beginner myself, so just to be clear: Can msg.sender in a contract be generally described as the address from the "entity" that currently interacts with this contract?
    – c0mr4t
    May 6, 2022 at 19:58
2

Let's make it very simple.

There are two types of accounts in the Ethereum Chain
1). Externally Owned Accounts(EOA) [Person]
2). Contracts Accounts [Contracts on Chain]

Both accounts have their addresses.

  • Now, look at an example.

Wallet address of personA (EOA) is 0x0cE446987406E92DF41614C46F1d6df9Cc925847.

Contract Address of Math.sol is 0x0cE446987406E92DF41614C46F1d6df9Cc753869 and Math.sol contains ContractA

Contract Address of Addition.sol is 0x0cE446987406E92DF41614C46F1d6df9Cc357241 and Addition.sol contains ContractB

Here, one of the functions of ContractB is called from ContractA.

So, whenever personA calls any functions of ContractA.

In this scenario, if you print or get `msg.sender` and `tx.origin` inside function of `ContractB` then the result would be like this

msg.sender = `0x0cE446987406E92DF41614C46F1d6df9Cc753869` // You will get address of `ContractA`
tx.origin  = `0x0cE446987406E92DF41614C46F1d6df9Cc925847` // personA's (EOA) Wallet Address
2
  • 1
    Where is functionC()? Where is Math.sol? Please give more background to this solution. This may clarify msg.sender and txt.origin, which is an important concept for beginners.
    – Sam Tseng
    Jun 8, 2022 at 4:39
  • The answer is edited. Jul 22, 2022 at 19:06
0

Here,

msg.sender (address): means sender of the message (current call)

on the other hand

address owner = msg.sender;

The msg.sender is the address that has called or initiated a function or created a transaction. Now, this address could be of a contract or even a person like you and me.

That's why you may use msg.sender instead of msg.sender()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.