Sending Ether means moving money from one account to another on the blockchain. It is important to know how to do this safely and correctly.
Sending Ether (transfer, send, call) in Blockchain / Solidity
address payable recipient = payable(address); // Using transfer recipient.transfer(amount); // Using send bool sent = recipient.send(amount); require(sent, "Send failed"); // Using call (bool success, ) = recipient.call{value: amount}(""); require(success, "Call failed");
transfer sends Ether and reverts on failure automatically.
send sends Ether but returns false on failure, so you must check it.
call is the recommended way now; it returns success and allows sending gas.
transfer. It will stop if it fails.address payable recipient = payable(0x123...); recipient.transfer(1 ether);
send. You must check if it worked.address payable recipient = payable(0x123...); bool sent = recipient.send(1 ether); require(sent, "Send failed");
call. It is the safest and most flexible way.address payable recipient = payable(0x123...); (bool success, ) = recipient.call{value: 1 ether}(""); require(success, "Call failed");
This contract shows three ways to send Ether to a recipient address. You can call each function with some Ether to send it.
pragma solidity ^0.8.20; contract SendEtherExample { address payable public recipient; constructor(address payable _recipient) { recipient = _recipient; } // Function to receive Ether receive() external payable {} // Send Ether using transfer function sendUsingTransfer() public payable { require(msg.value > 0, "Send some Ether"); recipient.transfer(msg.value); } // Send Ether using send function sendUsingSend() public payable { require(msg.value > 0, "Send some Ether"); bool sent = recipient.send(msg.value); require(sent, "Send failed"); } // Send Ether using call function sendUsingCall() public payable { require(msg.value > 0, "Send some Ether"); (bool success, ) = recipient.call{value: msg.value}(""); require(success, "Call failed"); } }
transfer forwards 2300 gas and reverts on failure.
send forwards 2300 gas and returns false on failure; you must handle it.
call forwards all gas by default and returns success status; it is now recommended.
Use transfer for simple sending with automatic revert on failure.
Use send if you want to handle failure manually.
Use call for the most control and safety in sending Ether.
Practice
Which Solidity function automatically reverts the transaction if sending Ether fails?
Solution
Step 1: Understand
transferbehaviortransfersends Ether and automatically reverts if the transfer fails.Step 2: Compare with other methods
sendreturns false on failure without reverting;callrequires manual check;approveis unrelated to sending Ether.Final Answer:
transfer -> Option DQuick Check:
Automatic revert on failure = transfer [OK]
- Confusing send with transfer behavior
- Thinking call auto-reverts on failure
- Mixing approve with Ether sending
Which of the following is the correct syntax to send 1 Ether using send in Solidity?
recipient.?{value: 1 ether}();Solution
Step 1: Recall
sendsyntaxsendis called asrecipient.send{value: amount}()in Solidity 0.6+.Step 2: Match syntax with options
Only send matchessendmethod usage for sending Ether.Final Answer:
send -> Option AQuick Check:
send syntax = recipient.send{value: 1 ether}() [OK]
- Using transfer syntax with send
- Confusing call syntax with send
- Using approve which is unrelated
What will be the output of the following Solidity code snippet?
bool success = recipient.send{value: 1 ether}();
if (!success) {
revert("Send failed");
}Solution
Step 1: Analyze send return value
sendreturns false if sending Ether fails, but does not revert automatically.Step 2: Check revert condition
The code checks!successand callsrevertmanually, so transaction reverts on failure.Final Answer:
Transaction reverts if send fails -> Option AQuick Check:
send returns false + manual revert = revert on failure [OK]
- Assuming send auto-reverts
- Ignoring manual revert check
- Thinking code always succeeds
Identify the error in this Solidity code snippet that uses call to send Ether:
(bool sent, ) = recipient.call{value: 1 ether}();
require(sent, "Failed to send Ether");Solution
Step 1: Review call syntax
The syntaxrecipient.call{value: 1 ether}()is correct for sending Ether with call.Step 2: Check error handling
The code checkssentboolean and reverts if false, which is proper error handling.Final Answer:
No error; code is correct -> Option CQuick Check:
call with value + require check = correct usage [OK]
- Thinking gas parameter is mandatory
- Confusing call syntax with send or transfer
- Ignoring manual success check
You want to send Ether to a contract that might consume more than 2300 gas on fallback. Which method should you use to ensure the transfer succeeds?
Solution
Step 1: Understand gas limits of transfer and send
transferandsendforward only 2300 gas, which may be insufficient for complex fallback functions.Step 2: Use call for flexible gas forwarding
callallows forwarding all available gas, ensuring the receiving contract can execute complex logic.Final Answer:
call -> Option BQuick Check:
call forwards all gas, suitable for complex fallback [OK]
- Using transfer or send which limit gas to 2300
- Confusing approve with sending Ether
- Ignoring fallback gas requirements
Ready to prove it?
Pass the test and this pattern moves unknown
