I'm wondering how exactly "msg.value" works, like here in this example taken from https://solidity-by-example.org/hacks/self-destruct
How can the deposit function require msg.value to eqaul 1 ether when there are no arguments for the function? In other words, how does this contract/function take in msg.value? Is msg.value sent when it is deployed?
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");
}
}
Best Answer
msg.value
is included along with the transaction, you can read this part of the Solidity documentation: https://docs.soliditylang.org/en/v0.8.14/units-and-global-variables.html?#special-variables-and-functions Thus, there is no reason to include it in the function parameters.