[Ethereum] msg.value


Can somebody explain what is the magic msg.value? I came across this code:

/// @return The balance of the user after the deposit is made
function deposit() public payable returns (uint) {
    // Use 'require' to test user inputs, 'assert' for internal invariants
    // Here we are making sure that there isn't an overflow issue
    require((balances[msg.sender] + msg.value) >= balances[msg.sender]);

    balances[msg.sender] += msg.value;
    // no "this." or "self." required with state variable
    // all values set to data type's initial value by default

    LogDepositMade(msg.sender, msg.value); // fire event

    return balances[msg.sender];

I do not see where the msg.value coming from, when a user deposit eth, does he or she needs to pass in the amount from the deposit function?
Thanks and sorry for newbie question.


From the following contract, I do not see anywhere passing in msg.value, so how this msg.value has been assigned? by the way you link of cryptozombie is very interesting, thanks.

pragma solidity ^0.4.0;

contract ClientReceipt {

    event Deposit(
        address indexed _from,
        bytes32 indexed _id,
        uint _value

    function deposit(bytes32 _id) public payable {
        // Events are emitted using `emit`, followed by
        // the name of the event and the arguments
        // (if any) in parentheses. Any such invocation
        // (even deeply nested) can be detected from
        // the JavaScript API by filtering for `Deposit`.
        emit Deposit(msg.sender, _id, msg.value);


Best Answer

msg.value is a member of the msg (message) object when sending (state transitioning) transactions on the Ethereum network.

msg.value contains the amount of wei (ether / 1e18) sent in the transaction.

msg.data (bytes): complete calldata
msg.gas (uint): remaining gas - deprecated in version 0.4.21 and to be replaced by gasleft()
msg.sender (address): sender of the message (current call)
msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)
msg.value (uint): number of wei sent with the message
Related Topic