简体   繁体   English

下面代码中的 msg.sender 和 address(this) 有什么区别?

[英]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我是初学者,最近我开始学习 Solidity 请帮助我理解下面的代码 下面代码中 msg.sender 和 address(this ) 之间的区别是什么

**pragma solidity ^0.8.0; **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. msg.sender是合约调用者的地址。

address(this) is the address of the smart contract itself. address(this)是智能合约本身的地址。

They are both addresses in Solidity, but there is a big difference between msg.sender and address(this) .它们都是msg.sender address(this) ,但是msg.senderaddress(this)之间有很大的区别。

Allow me to use a simplified Smart Contract below to highlight the difference.请允许我使用下面的简化智能合约来强调差异。 All screenshots are from the Remix-Ethereum IDE (click here ).所有屏幕截图均来自 Remix-Ethereum IDE(单击此处)。

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 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:例如,假设在 Remix Ethereum (IDE) 中, Escrow智能合约是从 ACCOUNT 地址部署的:

0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4

在此处输入图片说明

In that case, the State Variable owner will have the same address mentioned above.在这种情况下,状态变量owner将拥有与上述相同的地址。 This is because the constructor function was called from that address.这是因为constructor函数是从该地址调用的。

在此处输入图片说明

Now, suppose we change the ACCOUNT address to:现在,假设我们将 ACCOUNT 地址更改为:

0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2

在此处输入图片说明

We then call the function depositNothing from the Smart Contract which was deployed earlier.然后我们从之前部署的智能合约中调用功能depositNothing You will get the following error, however:但是,您将收到以下错误:

在此处输入图片说明

This is because the msg.sender in the depositNothing function equates to the second ACCOUNT address.这是因为msg.senderdepositNothing功能等同于第二个帐户地址。 This obviously does not equate to the first ACCOUNT Address - owner .这显然不等同于第一个 ACCOUNT Address- owner The second argument in the require function was therefore returned along with the error.因此, require函数中的第二个参数与错误一起返回。

address(this)地址(这个)

This is not the same as the ACCOUNT Address discussed earlier.这与前面讨论的 ACCOUNT 地址不同。 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 0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8

暂无
暂无

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

相关问题 Solidity 基础知识:“msg.sender”代表什么 - Solidity basics: what "msg.sender" stands for msg.sender 在solidity中有什么用? - What is the use of msg.sender in solidity? 为什么 msg.sender 有部署者的地址(地址不是调用者) - Why msg.sender has the address of the deployer (address isnt the caller) 在传递给另一个合约作为参数时,msg.sender地址是否已更改? - Is the msg.sender address changed while passing to another contract as a parameter? 如何在不使用设置 msg.value 的情况下将资金从 msg.sender(amount) 转移到收件人地址 - how to transfer fund from msg.sender(amount) to recipient address without using setting msg.value 为什么msg.sender具有已部署合同的帐户的地址(而不是与之交互的帐户的地址)? - Why msg.sender has the address of the account which has deployed the contract (and not the one's which interacts with it)? 如果我将地址作为参数传递,为什么我需要在 function 中使用`msg.sender`? - Why do I need to use `msg.sender` in my function if I pass an address as an argument? 检查 msg.sender 是否是特定类型的合约 - Check if msg.sender is a specific type of contract ParserError:应为“;” 但得到了“事件”——solidity 0.8 address payable(msg.sender) - ParserError: Expected ';' but got 'event' -- solidity 0.8 address payable(msg.sender) function 调用中的参数类型无效。 从地址到请求的应付地址的隐式转换无效。对于 msg.sender 和地址 0 - Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested.for msg.sender and adress 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM