Staking helps secure a blockchain by letting users lock up their coins to support network operations. It rewards them for helping keep the system safe and running smoothly.
Staking mechanisms in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Blockchain / Solidity
function stakeTokens(amount) {
if (amount <= 0) {
throw new Error('Amount must be positive');
}
userBalance -= amount;
stakedBalance += amount;
updateRewards();
}This example shows a simple function to stake tokens by moving them from user balance to staked balance.
Real staking mechanisms often include locking periods, reward calculations, and unstaking rules.
Examples
Blockchain / Solidity
stakeTokens(100);Blockchain / Solidity
function unstakeTokens(amount) {
if (amount > stakedBalance) {
throw new Error('Not enough staked tokens');
}
stakedBalance -= amount;
userBalance += amount;
updateRewards();
}Blockchain / Solidity
function calculateRewards() {
return stakedBalance * rewardRate * timeStaked;
}Sample Program
This program shows a user staking 200 tokens from their balance. It then calculates rewards based on the staked amount, a fixed reward rate, and time staked.
Blockchain / Solidity
let userBalance = 1000; let stakedBalance = 0; const rewardRate = 0.05; // 5% per time unit let timeStaked = 10; // example time units function stakeTokens(amount) { if (amount <= 0) { throw new Error('Amount must be positive'); } if (amount > userBalance) { throw new Error('Not enough balance'); } userBalance -= amount; stakedBalance += amount; console.log(`Staked ${amount} tokens.`); } function calculateRewards() { return stakedBalance * rewardRate * timeStaked; } stakeTokens(200); console.log(`User balance: ${userBalance}`); console.log(`Staked balance: ${stakedBalance}`); console.log(`Rewards earned: ${calculateRewards()}`);
Important Notes
Staking usually requires locking tokens for a set time before you can withdraw.
Rewards depend on how much you stake and how long you keep it staked.
Always check the rules of the blockchain you are staking on, as they can vary.
Summary
Staking means locking tokens to help secure a blockchain and earn rewards.
You stake by moving tokens from your balance to a locked state.
Rewards grow based on staked amount and time locked.
Practice
1. What is the main purpose of staking tokens in a blockchain network?
easy
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 CQuick 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
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 DQuick 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:
reward = stakedAmount * rewardRate * stakingDuration print(reward)If stakedAmount = 100, rewardRate = 0.05, and stakingDuration = 10, what is the output?
medium
Solution
Step 1: Substitute values into formula
reward = 100 * 0.05 * 10Step 2: Calculate reward
100 * 0.05 = 5; then 5 * 10 = 50Final Answer:
50 -> Option AQuick Check:
100 * 0.05 * 10 = 50 [OK]
Hint: Multiply all values stepwise: amount * rate * duration [OK]
Common Mistakes:
- Multiplying only two values
- Confusing rewardRate as 5 instead of 0.05
- Adding values instead of multiplying
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
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 AQuick 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
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 BQuick Check:
Calculate per user, then sum [OK]
Hint: Calculate rewards individually, then add for total [OK]
Common Mistakes:
- Summing stakes before multiplying
- Ignoring individual durations
- Calculating reward for only one user
