简体   繁体   English

Solidity 基础知识:“msg.sender”代表什么

[英]Solidity basics: what "msg.sender" stands for

I just started to learn Solidity as a personal challenge.我刚开始学习 Solidity 作为个人挑战。 I'm not a developer so I've a loooooong way to go.我不是开发人员,所以我对 go 有很长的路要走。

I'm following the Ethereum.org tutorial, here is what I've got doubt about: What does [msg.sender] stand for?我正在关注 Ethereum.org 教程,这是我有疑问的: [msg.sender]代表什么? 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 ( address ): 消息的发送者(当前通话)

msg.sender will be the person who's currently connecting with the contract. msg.sender将是当前与合约连接的人。

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 .在这种情况下,创建调用的合约将是msg.sender

Check out the documentation here: https://docs.soliditylang.org/en/develop/units-and-global-variables.html#block-and-transaction-properties在此处查看文档: 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 . personA (EOA)的钱包地址是0x0cE446987406E92DF41614C46F1d6df9Cc925847

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

Contract Address of Addition.sol is 0x0cE446987406E92DF41614C46F1d6df9Cc357241 and Addition.sol contains ContractB Addition.sol的合约地址是0x0cE446987406E92DF41614C46F1d6df9Cc357241并且Addition.sol包含ContractB

Here, one of the functions of ContractB is called from ContractA .在这里,从ContractB调用ContractA的功能之一。

So, whenever personA calls any functions of ContractA .因此,每当personA调用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) msg.sender (address) : 表示消息的发送者(当前通话)

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. msg.sender是调用或发起 function 或创建事务的地址。 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()这就是为什么你可以使用msg.sender而不是msg.sender()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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