5

Can somebody explain what is the magic msg.value? I came across this code: https://learnxinyminutes.com/docs/solidity/

/// @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.

Edit:

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

http://solidity.readthedocs.io/en/develop/contracts.html#function-modifiers

7

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
  • Nico, isn't your definition of wei backward? A wei is ether * 1^e-18, right? Reading your response it sounds like a wei is 10^18 larger than an ether, not 10^18 smaller.... – Fred Douglis Mar 20 '18 at 20:58
  • 1 ether is 1 000 000 000 000 000 000 000 wei so to go from ether to wei we need to add 18 zero's To go from wei to ether we need to move the decimal point forward 18 places. I could be wrong in my notation , i'm not a mathematician :) – Nico Mar 20 '18 at 21:08
  • Right. It's the notation I was carping about, because you described 1 wei as being 10**18 ether, not the other wei around. Sorry, couldn't resist. ... I think most people would understand it was backward, but newbies could get confused. – Fred Douglis Mar 20 '18 at 21:10
  • Just change your "*" to a "/" and it'll be fine. – Fred Douglis Mar 20 '18 at 21:11
  • I guess I need to understand the msg(message) object more, when you send transaction, you are executing a transfer or send function of a contract, how does the message object come into the picture? when a contract is defined, I never see how msg object is defined in the contract or initialized or assigned anywhere, just see msg.sender or msg.value show up or used in a contract. any pointer to read more about? Thanks – idoor Mar 20 '18 at 23:09
4

msg.value is automatically set to the amount of ether sent with that payable function.

More info here: http://solidity.readthedocs.io/en/develop/units-and-global-variables.html?highlight=msg.value#block-and-transaction-properties

  • Do you mean it is the gas fee? the link says: msg.value (uint): number of wei sent with the message, that depends on the message refers to which function call, some functions are not payable, so no msg.value associated, right? also how do I set msg.value or how to send it? – idoor Mar 20 '18 at 18:13
  • @idoor msg.value: number of wei sent with the message. Wei is just the smallest uint of ether. It's just referencing how much was actually sent with the message. Not the gas fee. – ReyHaynes Mar 20 '18 at 18:15
  • Also what happens if the contract has no payable function? and no functions which send in msg.value? – idoor Mar 20 '18 at 18:25
  • @idoor Any function called that is not payable but a msg.value is sent, will be reverted and fail. msg.value goes back to the user minus the gas used. – ReyHaynes Mar 20 '18 at 18:27
  • Thanks very much ReyHaynes, what is the msg.value when a contract is just deployed and nobody has never called any functions yet except the owner deploys it? for instance the contract is deployed with a supply of 100000. and also how do you send msg.value? do I need to write a function in the contract to send msg.value? – idoor Mar 20 '18 at 18:36
2

You can define value parameter while seding the transaction .Below example can give better explanation

   ContractInterface.methods
      .contractmethod(parameter)
       .send({
         from: accounts[0],
         value : "amount in ether or wei"
    });

Above value will be available as msg.value

0

It is a parameter users send along when they call a function in smart contract. It is the amount of wei you send to the smart contract. If you use remix you will see there is a input labeled "value" to put this parameter.

If you call the function from javascript you can put this parameter as Arjun recommended above. ContractInterface.methods .contractmethod(parameter) .send({ from: accounts[0], value : "amount in ether or wei" });

You said that you are a newbie, so the article might be quite difficult to understand. I recommend that you should read easier articles than that one. You can start with https://solidity.readthedocs.io/en/v0.4.25/solidity-in-depth.html or other tutorials. enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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