0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 modes available
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.