msg.value
is a read-only property reflecting value of the incoming transaction. If you want to send an ETH amount, you can do it in one of two ways.
Notes:
- These examples work on Solidity 0.8. Some previous versions also allow the
send()
method that is now deprecated, don't require the payable
type, and have slightly different syntax for the call()
method.
bonus
is amount of wei (1 ETH is 10^18 wei)
The transfer()
method
uint256 bonus = calculateBonus();
payable(msg.sender).transfer(bonus);
The transfer()
method only allows consuming 2300 gas, which is enough for non-contract recipients. If the recipient is a contract requiring more than 2300 gas to receive the ETH (for instance it sets some local variable), the transaction reverts.
Low-level call()
uint256 bonus = calculateBonus();
msg.sender.call{value: bonus}("");
With the low-level call()
method, you can also specify the gas limit, function to call and it's arguments. Use this method only if you know what you're doing.