1

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");
    }
} 

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.