What is the difference between msg.sender and address(this)?
Asked Answered
B

2

5

What is the difference 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;
  }
}**
Burnedout answered 15/11, 2021 at 8:13 Comment(1)
This could help regarding address(this) ethereum.stackexchange.com/questions/40018/…Cusk
R
9

msg.sender is the address of the contract caller.

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

Radcliffe answered 15/11, 2021 at 9:59 Comment(0)
C
7

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

enter image description here

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.

enter image description here

Now, suppose we change the ACCOUNT address to:

0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2

enter image description here

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

enter image description here

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:

enter image description here

0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8

Chinua answered 16/11, 2021 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.