Yield farming concepts in Blockchain / Solidity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with yield farming smart contracts, it is important to understand how the time to process rewards grows as more users participate.
We want to know how the contract's execution time changes when the number of farmers increases.
Analyze the time complexity of the following code snippet.
function distributeRewards(address[] memory farmers, uint256 totalReward) public {
uint256 rewardPerFarmer = totalReward / farmers.length;
for (uint256 i = 0; i < farmers.length; i++) {
balances[farmers[i]] += rewardPerFarmer;
}
}
This function divides a total reward equally among all farmers by looping through each farmer and updating their balance.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of farmers to update balances.
- How many times: Once for each farmer in the array.
As the number of farmers increases, the number of balance updates grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 balance updates |
| 100 | 100 balance updates |
| 1000 | 1000 balance updates |
Pattern observation: The work grows directly with the number of farmers, so doubling farmers doubles the work.
Time Complexity: O(n)
This means the time to distribute rewards grows linearly with the number of farmers.
[X] Wrong: "The reward distribution happens instantly no matter how many farmers there are."
[OK] Correct: Because the contract must update each farmer's balance one by one, more farmers mean more work and more time.
Understanding how loops affect execution time helps you explain how smart contracts handle many users efficiently and why gas costs increase with more participants.
"What if the contract used a mapping to track rewards and updated balances only when farmers claim them? How would the time complexity change?"
Practice
Solution
Step 1: Understand yield farming basics
Yield farming involves staking crypto assets to earn rewards.Step 2: Compare options to yield farming
Mining, trading, and creating blockchains are different activities.Final Answer:
To earn rewards by staking cryptocurrency in pools -> Option BQuick Check:
Yield farming = earning rewards by staking [OK]
- Confusing yield farming with mining
- Thinking yield farming is trading
- Believing yield farming creates new blockchains
Solution
Step 1: Define a yield farming pool
A pool is where users stake crypto to earn rewards.Step 2: Eliminate unrelated options
Wallets store crypto, blockchains create tokens, and software trades crypto.Final Answer:
A place where users stake crypto to earn rewards -> Option DQuick Check:
Pool = staking place for rewards [OK]
- Mixing pools with wallets
- Thinking pools create tokens
- Confusing pools with trading software
def calculate_rewards(staked_amount, reward_rate):
return staked_amount * reward_rate
print(calculate_rewards(1000, 0.05))
What is the output of this code?Solution
Step 1: Understand the function calculation
The function multiplies staked_amount (1000) by reward_rate (0.05).Step 2: Calculate the multiplication
1000 * 0.05 = 50.0Final Answer:
50.0 -> Option AQuick Check:
1000 * 0.05 = 50 [OK]
- Confusing multiplication with addition
- Using reward rate as output directly
- Mixing up staked amount and reward rate
def calculate_rewards(staked_amount, reward_rate):
rewards = staked_amount + reward_rate
return rewards
print(calculate_rewards(1000, 0.05))Solution
Step 1: Review the reward calculation logic
The code adds staked_amount and reward_rate instead of multiplying.Step 2: Understand correct reward formula
Rewards should be staked_amount * reward_rate, not addition.Final Answer:
Using addition instead of multiplication for rewards -> Option CQuick Check:
Rewards = stake * rate, not stake + rate [OK]
- Adding instead of multiplying rewards
- Forgetting to return value
- Misnaming functions
- Incorrect print usage
pools = {"PoolA": (1000, 0.05), "PoolB": (2000, 0.03), "PoolC": (1500, 0.04)}
total_rewards = sum(stake * rate for stake, rate in pools.values())
print(total_rewards)
What is the output of this code?Solution
Step 1: Calculate rewards for each pool
PoolA: 1000*0.05=50, PoolB: 2000*0.03=60, PoolC: 1500*0.04=60Step 2: Sum all rewards
Total = 50 + 60 + 60 = 170Step 3: Verify code output
The code prints the sum of rewards, which is 170.0 (float)Final Answer:
170.0 -> Option AQuick Check:
Sum of all pool rewards = 170.0 [OK]
- Adding rates instead of multiplying
- Forgetting to sum all pools
- Expecting integer output instead of float
- Misreading dictionary values
