Solidity basics: what "msg.sender" stands for
Asked Answered
F

3

29

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.

Fredi answered 1/2, 2018 at 12:11 Comment(1)
RTFM "msg.sender (address): sender of the message (current call)"Fredi
A
42

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

Apocarpous answered 1/2, 2018 at 21:38 Comment(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?Thievery
L
4

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
Leek answered 25/5, 2022 at 13:45 Comment(2)
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.Hypothesize
The answer is edited.Leek
P
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()

Picker answered 14/9, 2022 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.