0
0
Blockchain / Solidityprogramming~5 mins

Staking mechanisms in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

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.

When you want to earn rewards by holding and locking your cryptocurrency.
When you want to help validate transactions and secure a blockchain network.
When you want to participate in governance decisions by staking tokens.
When you want to reduce the chance of attacks by increasing the cost to cheat.
When you want to support a blockchain project and earn passive income.
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
User stakes 100 tokens to participate in network security.
Blockchain / Solidity
stakeTokens(100);
Function to remove tokens from staking and return them to user balance.
Blockchain / Solidity
function unstakeTokens(amount) {
    if (amount > stakedBalance) {
        throw new Error('Not enough staked tokens');
    }
    stakedBalance -= amount;
    userBalance += amount;
    updateRewards();
}
Simple reward calculation based on amount staked and time.
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()}`);
OutputSuccess
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.