Solidity
msg.value 是如何發送的?
我想知道“msg.value”究竟是如何工作的,就像這個例子中取自https://solidity-by-example.org/hacks/self-destruct
當函式沒有參數時,存款函式如何要求 msg.value 等於 1 乙太幣?換句話說,這個合約/函式是如何接收 msg.value 的?msg.value 是在部署時發送的嗎?
uint public targetAmount = 7 ether; address public winner; function deposit() public payable { require(msg.value == 1 ether, "You can only send 1 Ether"); uint balance = address(this).balance; require(balance <= targetAmount, "Game is over"); if (balance == targetAmount) { winner = msg.sender; } } function claimReward() public { require(msg.sender == winner, "Not winner"); (bool sent, ) = msg.sender.call{value: address(this).balance}(""); require(sent, "Failed to send Ether"); } }
msg.value
包含在交易中,您可以閱讀 Solidity 文件的這一部分:https ://docs.soliditylang.org/en/v0.8.14/units-and-global-variables.html?#special-variables-and-函式 因此,沒有理由將它包含在函式參數中。