What exactly is solidity-msg.sender?

What exactly is msg. sender?

msg.sender:

  • The originator of the current wallet.

  • msg is global, and msg.sender is a global variable.

  • from is account , the wallet address that initiates the message, always equal to msg.sender

Test code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.4.0;

contract Test {
  address public _owner;  // / 第一次部署合约时,钱包地址存储到_owner
  uint public _number = 10;

  // 构造函数
  function Test() {
    _owner = msg.sender;    
  }

  // get方法
  function msgSenderAddress() constant returns (address) {
    return msg.sender;
  }

  // set方法, 修改number的值
  function setNumberAdd1() {
    _number = _number + 5;
  }
  // set方法, 修改number的值
  function setNumberAdd2() {
    if(_owner == msg.sender){
      _number = _number + 10;
    }
    
  }
}

Deploy the Test contract (address 5b...) to test those properties and function return values, switch to another account (4b...), and test those properties and function return values.
operation result:


After switching account:

Summary:

  • msg.sender is dynamic, which account (wallet address) is selected during "run", and msg.sender is that address.

  • The value of _owner remains unchanged. The msg.sender when the contract is deployed for the first time is always saved in the constructor, and owner is the address when the contract is deployed for the first time.

Guess you like

Origin blog.csdn.net/qq_44909497/article/details/124278999