Msg.value
1 min read
Pronunciation
[mess-ayj val-yoo]
Analogy
`msg.value` is like the cash envelope amount attached to a letter—indicating how much money was sent.
Definition
Key Points Intro
`msg.value` conveys payment info via:
Key Points
Wei unit: Denominated in Wei (1 ETH = 10¹⁸ Wei).
Used in payable: Only nonzero in `payable` functions.
Immutable per call: Cannot be altered by contract logic.
Access to funds: Enables conditional payment checks.
Example
```
function deposit() external payable {
balances[msg.sender] += msg.value;
}
``` credits the sender with the Ether amount sent.
Technical Deep Dive
When a transaction or call includes value, the EVM deducts it from caller’s balance and credits callee before execution. Solidity makes this available as `msg.value`. Contracts can then forward or record the value.
Security Warning
Always check `msg.value` in payable functions to prevent unintended free calls.
Caveat
Zero `msg.value` calls still trigger fallback/receive if payable.
Msg.value - Related Articles
No related articles for this term.