Sending Ether (transfer, send, call) in Blockchain / Solidity - Time & Space Complexity
When sending Ether in blockchain smart contracts, it's important to understand how the time cost grows as the number of transactions or calls increases.
We want to know how the execution time changes when sending Ether using different methods like transfer, send, or call.
Analyze the time complexity of the following Solidity code snippet that sends Ether to multiple addresses.
function sendEther(address[] memory recipients) public payable {
for (uint i = 0; i < recipients.length; i++) {
payable(recipients[i]).transfer(1 ether);
}
}
This code sends 1 Ether to each address in the recipients list using the transfer method inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that sends Ether to each recipient.
- How many times: The loop runs once for every address in the recipients array.
Each additional recipient means one more transfer operation, so the total work grows directly with the number of recipients.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 transfers |
| 100 | 100 transfers |
| 1000 | 1000 transfers |
Pattern observation: The number of operations grows in a straight line as the input size increases.
Time Complexity: O(n)
This means the time to send Ether grows directly in proportion to the number of recipients.
[X] Wrong: "Sending Ether to multiple addresses happens instantly no matter how many recipients there are."
[OK] Correct: Each transfer requires a separate operation, so more recipients mean more work and longer execution time.
Understanding how sending Ether scales helps you explain gas costs and transaction limits clearly, showing you grasp practical blockchain programming challenges.
"What if we replaced transfer with call inside the loop? How would the time complexity change?"
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
