PaymentReceiverreceive() function to accept plain Ether transfersfallback() function to accept Ether with unknown dataPaymentReceiverreceive() function to accept plain Ether transfersfallback() function to accept Ether with unknown dataPaymentReceiver and inside it, declare an event named ReceivedEther that logs an address sender and a uint amount.Use the event keyword to declare an event inside the contract.
PaymentReceiver contract, add a receive() external payable function that emits the ReceivedEther event with msg.sender and msg.value.The receive() function has no name and no arguments. It must be marked external payable.
PaymentReceiver contract, add a fallback() external payable function that emits an event named FallbackCalled with msg.sender and msg.value. Declare the FallbackCalled event above.The fallback() function is called when no other function matches. It must be external payable to accept Ether.
getBalance() that returns the contract's Ether balance as uint. Then add a print statement to display the balance after deployment and after sending Ether (simulate this in your test environment).Use address(this).balance to get the contract's Ether balance.
What is the main purpose of the receive() function in a Solidity contract?
receive()receive() function is designed to accept Ether sent to the contract without any accompanying data.receive() is only for plain Ether transfers.Which of the following is the correct syntax for a receive() function in Solidity?
?
receive() function must be declared without the function keyword, must be external and payable.receive() external payable {} matches the correct syntax. Options using the function keyword are invalid, as are those lacking payable or using fallback instead of receive.Consider this Solidity contract snippet:
contract Test {
event Log(string func);
receive() external payable {
emit Log("receive");
}
fallback() external payable {
emit Log("fallback");
}
}What event is emitted when someone sends 1 Ether with empty data?
receive() function if it exists.receive() function emits the event with "receive" string.Given this contract snippet, what is the error?
contract Example {
fallback() external {
// code
}
receive() external {
// code
}
}receive() and fallback() must be marked payable.payable, so contract cannot receive Ether via these functions.You want your contract to accept Ether sent with or without data, and log which function was called. Which is the best way to implement this?
?
receive() and fallback() as external payable lets the contract accept Ether in both cases and emit distinct events.Pass the test and this pattern moves unknown