简体   繁体   English

msg.sender 在solidity中有什么用?

[英]What is the use of msg.sender in solidity?

In this codpiece, I am finding it hard to figure out what is msg.sender is and how it works internally.在这个代码片段中,我发现很难弄清楚msg.sender是什么以及它在内部是如何工作的。

What I am understanding is, we have a mapping favoriteNumber, and the key is an address and the value is a uint.我的理解是,我们有一个映射 favoriteNumber,key 是地址,value 是 uint。

What is the meaning of comment - "Update our favoriteNumber mapping to store _myNumber under msg.sender , I am understanding that we are updating favoriteNumber, but what does it mean that under msg.sender. What is the role of this method, how it's working?评论是什么意思-“更新我们的favoriteNumber映射到_myNumber下存储msg.sender ,我理解是我们在更新favoriteNumber,但是在msg.sender下是什么意思。这个方法的作用是什么,它是怎样的在职的?

    mapping (address => uint) favoriteNumber;

function setMyNumber(uint _myNumber) public {
  // Update our `favoriteNumber` mapping to store `_myNumber` under `msg.sender`
  favoriteNumber[msg.sender] = _myNumber;
  // ^ The syntax for storing data in a mapping is just like with arrays
}

function whatIsMyNumber() public view returns (uint) {
  // Retrieve the value stored in the sender's address
  // Will be `0` if the sender hasn't called `setMyNumber` yet
  return favoriteNumber[msg.sender];
}

Every smart contract invocation has a caller address.每个智能合约调用都有一个调用者地址。 Each EVM (Ethereum Virtual Machine that executes the code) knows which account carries out each action.每个 EVM(执行代码的以太坊虚拟机)都知道哪个帐户执行每个操作。 In Solidity, you can access the calling account by referencing msg.sender在 Solidity 中,您可以通过引用msg.sender来访问调用帐户

So when you call a function of solidity contract, your contract already gets the information of your account, so your account is the msg.sender所以当你调用一个solidity合约的function时,你的合约已经获取到你账户的信息,所以你的账户就是msg.sender

favoriteNumber is a mapping. favoriteNumber是一个映射。 think it like a javascript object.把它想象成 javascript object。 It maps the account addresses to their favourite number.它将帐户地址映射到他们最喜欢的号码。

 0x9C6520Dd9F8d0af1DA494C37b64D4Cea9A65243C -> 10 

So when you call setMyNumber(_myNumber) , you are passing your favourite number.因此,当您调用setMyNumber(_myNumber)时,您传递的是您最喜欢的号码。 so this number will be stored in favoriteNumber mapping like this:所以这个数字将像这样存储在favoriteNumber映射中:

 yourAccountAdress -> yourFavouriteNumber

So when you call whatIsMyNumber function, since EVM already gets your account number, checks in the mappings and returns you your favourite number.因此,当您调用whatIsMyNumber function 时,由于 EVM 已经获取了您的帐号,因此请检查映射并返回您喜欢的号码。

In solidity exists 3 types of variables: state, local and global.在 Solidity 中存在 3 种类型的变量:state、局部变量和全局变量。 example of global variables:全局变量示例:

  • msg.sender (sender of the message) msg.sender(消息的发送者)
  • msg.value (number of wei sent with the message) msg.value(随消息发送的wei数)

pseudocode from favoriteNumber[msg.sender] = _myNumber;来自favoriteNumber[msg.sender] = _myNumber;

  • given a favoriteNumber list,给定一个 favoriteNumber 列表,
  • select the address of the account calling this function, select 调用此 function 的账户地址,
  • assign _myNumber to that address将 _myNumber 分配给该地址

note: global variables are available in all contracts by default.注意:默认情况下,全局变量在所有合约中都可用。 see more info here: solidity docs - global variable在此处查看更多信息: solidity docs - 全局变量

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

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