Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Staking mechanisms in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - Staking mechanisms
User Locks Tokens
Tokens are Staked
Network Validates Stake
User Earns Rewards
User Can Unstake Tokens
Tokens Returned After Lock Period
This flow shows how users lock tokens to stake, earn rewards, and then unstake tokens after a lock period.
Execution Sample
Blockchain / Solidity
def stake_tokens(user_balance, stake_amount):
    if stake_amount > user_balance:
        return "Insufficient balance"
    staked = stake_amount
    user_balance -= stake_amount
    rewards = staked * 0.05
    return user_balance, staked, rewards
This code simulates staking tokens, checking balance, deducting stake, and calculating rewards.
Execution Table
Stepuser_balancestake_amountCondition (stake_amount > user_balance)ActionOutput
11000200200 > 1000? FalseStake 200 tokens, reduce balance(800, 200, 10.0)
2100012001200 > 1000? TrueReturn error"Insufficient balance"
💡 Execution stops when stake amount exceeds user balance or after successful staking and reward calculation.
Variable Tracker
VariableStartAfter Step 1After Step 2
user_balance10008001000
stake_amount2002001200
staked02000
rewards010.00
Key Moments - 2 Insights
Why does staking fail when stake_amount is greater than user_balance?
Because the condition 'stake_amount > user_balance' becomes True (see execution_table row 2), the function returns 'Insufficient balance' and does not proceed to stake tokens.
How are rewards calculated after staking?
Rewards are calculated as 5% of the staked tokens (see execution_table row 1 output: rewards = 10.0 for 200 staked tokens).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the user_balance after successfully staking 200 tokens?
A800
B1000
C1200
D200
💡 Hint
Check execution_table row 1 under user_balance after staking.
At which step does the staking fail due to insufficient balance?
AStep 1
BStep 2
CNo failure
DBoth steps
💡 Hint
Look at the condition column in execution_table row 2.
If the reward rate changes to 10%, what would be the rewards for staking 200 tokens?
A5.0
B10.0
C20.0
D200.0
💡 Hint
Calculate 10% of 200 tokens as shown in variable_tracker rewards calculation.
Concept Snapshot
Staking mechanisms:
- User locks tokens to stake
- Stake amount must be <= user balance
- Rewards are earned based on staked tokens
- Tokens are locked during staking period
- User can unstake after lock period ends
Full Transcript
Staking mechanisms involve users locking their tokens to support the network. The code example shows a function where a user stakes tokens if they have enough balance. If the stake amount is greater than the user's balance, staking fails. Otherwise, the stake amount is deducted from the balance, and rewards are calculated as 5% of the staked tokens. The execution table traces these steps, showing variable changes and conditions. Key moments clarify why staking can fail and how rewards are computed. The visual quiz tests understanding of balance changes, failure conditions, and reward calculations. Overall, staking locks tokens, earns rewards, and allows unstaking after a period.

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

  1. Step 1: Understand staking concept

    Staking means locking tokens to support blockchain security.
  2. Step 2: Identify staking benefits

    Users earn rewards for staking, helping network stability.
  3. Final Answer:

    To help secure the network and earn rewards -> Option C
  4. 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

  1. Step 1: Understand staking variables

    Locked tokens represent the amount staked by user.
  2. Step 2: Match correct assignment

    lockedTokens should equal stakeAmount to show tokens locked.
  3. Final Answer:

    lockedTokens = stakeAmount -> Option D
  4. 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:
reward = stakedAmount * rewardRate * stakingDuration
print(reward)
If stakedAmount = 100, rewardRate = 0.05, and stakingDuration = 10, what is the output?
medium
A. 50
B. 5
C. 0.5
D. 500

Solution

  1. Step 1: Substitute values into formula

    reward = 100 * 0.05 * 10
  2. Step 2: Calculate reward

    100 * 0.05 = 5; then 5 * 10 = 50
  3. Final Answer:

    50 -> Option A
  4. Quick 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
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

  1. Step 1: Check variable declarations

    lockedTokens is assigned without declaration, causing error in strict languages.
  2. Step 2: Understand variable scope

    lockedTokens should be declared (e.g., let or var) before use.
  3. Final Answer:

    lockedTokens is not declared before assignment -> Option A
  4. 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

  1. Step 1: Understand reward calculation per user

    Each user's reward depends on their own stake and duration.
  2. Step 2: Sum individual rewards for total

    Calculate each reward separately, then add for total rewards.
  3. Final Answer:

    Loop through each user, calculate reward = stakedAmount * rewardRate * duration, then sum all rewards -> Option B
  4. Quick 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