0
0
Blockchain / Solidityprogramming~20 mins

Sending Ether (transfer, send, call) in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ether Transfer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Solidity code when sending Ether using transfer?

Consider the following Solidity contract snippet. What happens when sendEther is called with 1 ether?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract TransferExample {
    event Sent(address to, uint amount);

    function sendEther(address payable recipient) public payable {
        recipient.transfer(msg.value);
        emit Sent(recipient, msg.value);
    }
}
AThe transaction returns false if the recipient rejects the Ether.
BThe transaction always succeeds regardless of recipient's fallback gas usage.
CThe transaction reverts if the recipient's fallback function uses more than 2300 gas.
DThe transaction sends Ether asynchronously and does not wait for confirmation.
Attempts:
2 left
💡 Hint

Recall that transfer forwards a fixed gas stipend of 2300 gas.

Predict Output
intermediate
2:00remaining
What does the send method return in this Solidity example?

Given this contract snippet, what is the value of success after calling sendEther?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract SendExample {
    function sendEther(address payable recipient) public payable returns (bool) {
        bool success = recipient.send(msg.value);
        return success;
    }
}
ATrue if the Ether was sent successfully, false otherwise.
BThrows an exception on failure instead of returning false.
CAlways true, send never fails.
DReturns the amount of gas used by the send operation.
Attempts:
2 left
💡 Hint

Remember that send returns a boolean indicating success or failure.

Predict Output
advanced
2:00remaining
What is the output of this Solidity code using call to send Ether?

Analyze the following contract. What does the sendViaCall function return when called with 1 ether?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract CallExample {
    event Sent(address to, uint amount, bool success);

    function sendViaCall(address payable recipient) public payable returns (bool) {
        (bool success, ) = recipient.call{value: msg.value}("");
        emit Sent(recipient, msg.value, success);
        return success;
    }
}
AAlways reverts if the recipient rejects the Ether.
BReturns true if the Ether was sent successfully, false otherwise.
CReturns the amount of gas used by the call.
DReturns false if the recipient's fallback function uses more than 2300 gas.
Attempts:
2 left
💡 Hint

Recall that call returns a boolean success and forwards all remaining gas by default.

🧠 Conceptual
advanced
2:00remaining
Which statement correctly describes the difference between transfer, send, and call for sending Ether?

Choose the option that best explains the key difference between transfer, send, and call when sending Ether in Solidity.

A<code>transfer</code> and <code>send</code> forward 2300 gas, <code>call</code> forwards all remaining gas and returns success status.
B<code>transfer</code> forwards all gas, <code>send</code> reverts on failure, <code>call</code> forwards 2300 gas.
C<code>send</code> throws on failure, <code>transfer</code> returns false, <code>call</code> always succeeds.
D<code>call</code> forwards 2300 gas, <code>send</code> forwards all gas, <code>transfer</code> returns success status.
Attempts:
2 left
💡 Hint

Think about gas forwarding and error handling differences.

🔧 Debug
expert
3:00remaining
Why does this call-based Ether transfer fail unexpectedly?

Consider this contract snippet. The sendViaCall function returns false unexpectedly when sending Ether to a contract with a fallback function that uses more than 2300 gas. Why?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Receiver {
    fallback() external payable {
        // Uses more than 2300 gas
        for (uint i = 0; i < 1000; i++) {}
    }
}

contract Sender {
    function sendViaCall(address payable recipient) public payable returns (bool) {
        (bool success, ) = recipient.call{value: msg.value, gas: 2300}("");
        return success;
    }
}
ABecause the call does not send any Ether due to missing value parameter.
BBecause call always forwards all gas, so the fallback function runs out of gas.
CBecause the fallback function is not payable, causing the call to fail.
DBecause the call explicitly limits gas to 2300, which is insufficient for the fallback function.
Attempts:
2 left
💡 Hint

Check the gas parameter in the call and the fallback function's gas usage.