Consider the following Solidity contract snippet. A user calls sendEther with 1 ether. What will be the values emitted in the event?
pragma solidity ^0.8.0;
contract Test {
event Log(address sender, uint amount);
function sendEther() public payable {
emit Log(msg.sender, msg.value);
}
}Remember, msg.sender is the caller's address and msg.value is the amount of ether sent with the call.
When a function is called with ether, msg.sender is the caller's address and msg.value is the amount of ether sent. The event logs these values.
msg.value represent in Solidity?Choose the best description of msg.value in a Solidity function call.
Think about what value is transferred along with a payable function call.
msg.value holds the amount of ether (in wei) sent with the current call or transaction.
msg.value with ether sent?Given this contract, what will happen if nonPayableFunction is called with 1 ether?
pragma solidity ^0.8.0;
contract Test {
function nonPayableFunction() public {
uint amount = msg.value;
}
}Consider the payable modifier and what happens when ether is sent to a non-payable function.
Sending ether to a non-payable function causes the transaction to revert. Accessing msg.value alone does not cause error, but the call with ether to a non-payable function is rejected.
msg.sender is true?Choose the correct statement about msg.sender in Solidity.
Think about calls between contracts and external accounts.
msg.sender is the immediate caller of the function. It can be an external account or another contract.
Examine the contract below. Why does sending ether to receiveEther fail?
pragma solidity ^0.8.0;
contract Test {
function receiveEther() public {
uint received = msg.value;
}
}Check the function modifiers required to accept ether.
In Solidity, functions must be marked payable to receive ether. Without it, sending ether causes the transaction to revert.