简体   繁体   中英

Solidity basics: what "msg.sender" stands for

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.

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

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM