简体   繁体   English

如果我将地址作为参数传递,为什么我需要在 function 中使用`msg.sender`?

[英]Why do I need to use `msg.sender` in my function if I pass an address as an argument?

Learning Solidity.学习坚固性。 Love it so far but would like to understand why the following block doesnt work.到目前为止喜欢它,但想了解为什么以下块不起作用。

contract Whatever {   
    mapping (address => uint) public ledger;    
    function withdrawAllMoney(address payable _to) public {
        require(ledger[_to] > 0, "You do not have permission to withdrawl funds.  Deposit first");
        uint sent = ledger[_to];   // <----- error here - telling me to use msg.sender
        ledger[_to] = 0;
        _to.transfer(address(this).balance);
    }
}

I'm curious as to why I cant index into my mapping at the addres I'm passing into the function: ledger[_to] as the require block seems to work.我很好奇为什么我不能在我传递给 function: ledger[_to]的地址处索引到我的映射,因为 require 块似乎有效。 Is there a security reason behind not being able to transfer without msg.sender ?在没有msg.sender的情况下无法传输是否存在安全原因?

I think that because you want to withdraw all the balance我认为那是因为您想提取所有余额

  address(this).balance

Technically only the owner of the contract should be able to call this.从技术上讲,只有合约的所有者才能调用它。 Based on your code anyone can withdraw all the balance.根据您的代码,任何人都可以提取所有余额。

暂无
暂无

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

相关问题 为什么 msg.sender 有部署者的地址(地址不是调用者) - Why msg.sender has the address of the deployer (address isnt the caller) 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 Msg.sender在“视图”功能中不起作用,为什么? 有解决方法吗? - Msg.sender does not work inside a “view” function, why? Is there a workaround? msg.sender 在solidity中有什么用? - What is the use of msg.sender in solidity? 为什么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)? 在传递给另一个合约作为参数时,msg.sender地址是否已更改? - Is the msg.sender address changed while passing to another contract as a parameter? 下面代码中的 msg.sender 和 address(this) 有什么区别? - what is the differnce between msg.sender and address(this) in below code? 如何在不使用设置 msg.value 的情况下将资金从 msg.sender(amount) 转移到收件人地址 - how to transfer fund from msg.sender(amount) to recipient address without using setting msg.value ParserError:应为“;” 但得到了“事件”——solidity 0.8 address payable(msg.sender) - ParserError: Expected ';' but got 'event' -- solidity 0.8 address payable(msg.sender) 检查 msg.sender 是否是特定类型的合约 - Check if msg.sender is a specific type of contract
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM