[英]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.sender
和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 ).所有屏幕截图均来自 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.sender
在depositNothing
功能等同于第二个帐户地址。 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.