Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Staking Mechanism Simulation
📖 Scenario: You are building a simple simulation of a staking mechanism for a blockchain project. Users can stake tokens, and the system will calculate rewards based on the amount staked and a reward rate.
🎯 Goal: Create a program that stores user stakes, sets a reward rate, calculates rewards for each user, and then displays the rewards.
📋 What You'll Learn
Create a dictionary to hold user stakes with exact user names and stake amounts
Create a variable for the reward rate as a decimal
Use a dictionary comprehension to calculate rewards for each user based on their stake and the reward rate
Print the rewards dictionary showing each user and their reward
💡 Why This Matters
🌍 Real World
Staking is a common way in blockchain projects to encourage users to hold tokens and secure the network while earning rewards.
💼 Career
Understanding staking mechanisms is important for blockchain developers and engineers working on decentralized finance (DeFi) platforms.
Progress0 / 4 steps
1
DATA SETUP: Create user stakes dictionary
Create a dictionary called user_stakes with these exact entries: 'Alice': 1000, 'Bob': 1500, 'Charlie': 500
Blockchain / Solidity
Hint
Use curly braces to create a dictionary with keys as user names and values as their stake amounts.
2
CONFIGURATION: Set the reward rate
Create a variable called reward_rate and set it to 0.05 to represent a 5% reward rate
Blockchain / Solidity
Hint
Use a simple assignment to create the reward_rate variable with the value 0.05.
3
CORE LOGIC: Calculate rewards using dictionary comprehension
Create a dictionary called rewards using dictionary comprehension that calculates each user's reward by multiplying their stake from user_stakes by reward_rate
Blockchain / Solidity
Hint
Use {user: stake * reward_rate for user, stake in user_stakes.items()} to create the rewards dictionary.
4
OUTPUT: Display the rewards dictionary
Write a print statement to display the rewards dictionary
Blockchain / Solidity
Hint
Use print(rewards) to show the rewards dictionary on the screen.
Practice
(1/5)
1. What is the main purpose of staking tokens in a blockchain network?
easy
A. To create new tokens instantly
B. To transfer tokens to another user
C. To help secure the network and earn rewards
D. To delete tokens from the blockchain
Solution
Step 1: Understand staking concept
Staking means locking tokens to support blockchain security.
Step 2: Identify staking benefits
Users earn rewards for staking, helping network stability.
Final Answer:
To help secure the network and earn rewards -> Option C
Quick Check:
Staking = Security + Rewards [OK]
Hint: Staking locks tokens to secure network and gain rewards [OK]
Common Mistakes:
Confusing staking with token transfer
Thinking staking creates new tokens
Believing staking deletes tokens
2. Which of the following is the correct way to represent staking tokens in a smart contract pseudocode?
easy
A. stakeAmount = userBalance - lockedTokens
B. lockedTokens = stakeAmount + userBalance
C. userBalance = stakeAmount + lockedTokens
D. lockedTokens = stakeAmount
Solution
Step 1: Understand staking variables
Locked tokens represent the amount staked by user.
Step 2: Match correct assignment
lockedTokens should equal stakeAmount to show tokens locked.
Final Answer:
lockedTokens = stakeAmount -> Option D
Quick Check:
Locked tokens = stake amount [OK]
Hint: Locked tokens equal the stake amount directly [OK]
Common Mistakes:
Adding stakeAmount to userBalance incorrectly
Subtracting lockedTokens from userBalance wrongly
Mixing variable roles in assignment
3. Consider this pseudocode for calculating staking rewards:
4. The following pseudocode has an error. What is the problem?
function stakeTokens(userBalance, stakeAmount) {
if (stakeAmount > userBalance) {
return "Error: Not enough balance";
}
lockedTokens = stakeAmount;
userBalance = userBalance - stakeAmount;
return lockedTokens;
}
medium
A. lockedTokens is not declared before assignment
B. The function does not return userBalance
C. The if condition should be stakeAmount < userBalance
D. The subtraction should add stakeAmount instead
Solution
Step 1: Check variable declarations
lockedTokens is assigned without declaration, causing error in strict languages.
Step 2: Understand variable scope
lockedTokens should be declared (e.g., let or var) before use.
Final Answer:
lockedTokens is not declared before assignment -> Option A
Quick Check:
Undeclared variable causes error [OK]
Hint: Always declare variables before assigning [OK]
Common Mistakes:
Ignoring variable declaration errors
Misreading the if condition logic
Thinking subtraction should be addition
5. You want to write a function that calculates total rewards for multiple users staking different amounts for different durations. Which approach correctly applies staking mechanisms?
hard
A. Add all staked amounts first, then multiply by rewardRate and total duration
B. Loop through each user, calculate reward = stakedAmount * rewardRate * duration, then sum all rewards
C. Calculate reward only for the user with the highest stake
D. Multiply rewardRate by duration only once, ignoring staked amounts
Solution
Step 1: Understand reward calculation per user
Each user's reward depends on their own stake and duration.
Step 2: Sum individual rewards for total
Calculate each reward separately, then add for total rewards.
Final Answer:
Loop through each user, calculate reward = stakedAmount * rewardRate * duration, then sum all rewards -> Option B
Quick Check:
Calculate per user, then sum [OK]
Hint: Calculate rewards individually, then add for total [OK]