简体   繁体   中英

what is the differnce between msg.sender and address(this) in below code?

I am begginer and recntily i start learning solidity please help me to understand the below code What is the differnce between msg.sender and address(this ) in the below code

**pragma solidity ^0.8.0;

contract Escrow{
  address public payer;
  address payable public payee;
  address public lawyer;
  uint public amount;
  
  constructor(
    address _payer, 
    address payable _payee, 
    uint _amount) {
    payer = _payer;
    payee = _payee;
    lawyer = msg.sender; 
    amount = _amount;
  }

  function deposit() payable public {
    require(msg.sender == payer, 'Sender must be the payer');
    require(address(this).balance <= amount, 'Cant send more than escrow amount');
  }

  function release() public {
    require(address(this).balance == amount, 'cannot release funds before full amount is sent');
    require(msg.sender == lawyer, 'only lawyer can release funds');
    payee.transfer(amount);
  }
  
  function balanceOf() view public returns(uint) {
    return address(this).balance;
  }
}**

msg.sender is the address of the contract caller.

address(this) is the address of the smart contract itself.

They are both addresses in Solidity, but there is a big difference between msg.sender and address(this) .

Allow me to use a simplified Smart Contract below to highlight the difference. All screenshots are from the Remix-Ethereum IDE (click here ).

pragma solidity ^0.8.0;

contract Escrow {
    
    address public owner;
    
    constructor() {
        owner = msg.sender;
    }
    
    function depositNothing() public view {
        require(msg.sender == owner, 'You are not the owner!');
    }
    
    function balanceOf() view public returns(uint) {
        return address(this).balance;
    }
}

msg.sender

We are talking about the ACCOUNT address from which the function in the Smart Contract was called. For example, suppose in the Remix Ethereum (IDE), the Escrow Smart Contract was deployed from the ACCOUNT address:

0x5B38Da6a701c568545dCfcB03FcB875f56beddC4

在此处输入图片说明

In that case, the State Variable owner will have the same address mentioned above. This is because the constructor function was called from that address.

在此处输入图片说明

Now, suppose we change the ACCOUNT address to:

0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2

在此处输入图片说明

We then call the function depositNothing from the Smart Contract which was deployed earlier. You will get the following error, however:

在此处输入图片说明

This is because the msg.sender in the depositNothing function equates to the second ACCOUNT address. This obviously does not equate to the first ACCOUNT Address - owner . The second argument in the require function was therefore returned along with the error.

address(this)

This is not the same as the ACCOUNT Address discussed earlier. This strictly refers to the address given to the Smart Contract when it is deployed to the Ethereum blockchain.

This can be found here:

在此处输入图片说明

0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8

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