Token Reward Calculation:Staking Smart contract with Solidity.

0xandyeth
3 min readJun 1, 2021

--

Introduction

A staking pool allows multiple stakeholders (or bagholders) to combine their computational resources as a way to increase their chances of being rewarded. In other words, they unite their staking power in the process of verifying and validating new blocks, so they have a higher probability of earning the block rewards. I will not explain the conception of staking deeply in here. Before a few days , my friend asked how to build staking platform by using solidity programming language which can build the smart contract .So I am going to focus the build of staking with smart-contract which was known well.

Environment of Developing

  • Solidity programming language
  • Openzeppelin-Solidity

Design of Project

As we can see in the picture, staking pool box has too many crypto assets and outputs the reward assets now as if the water comes out. But I only am going to deal one token.So future staking platform will offer token holders the ability to earn a passive income in exchange for staking their tokens and helping support the network’s infrastructure.

1.Token Contract building

Token name: HappyMoney

Symbol : HANEY

Total Supply: 100,000,000

Decimal: 18

Our Haney token will be released based on ERC20 standard token and will be exchanged by Uniswap exchange platform later.

So I am going to describe the token contract code simply in here.

pragma solidity ^0.8.0;

import “@openzeppelin/contracts/token/ERC20/ERC20.sol”;

import “@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol”;

contract HaneyToken is ERC20, ERC20Detailed {

constructor(uint256 initialSupply) ERC20Detailed(“HappyMoney”, “HANEY”, 18) public {

_mint(msg.sender, initialSupply);

} }

When we create the token contract , we can give 100 M to initial supply and finally 100 M HANEY tokens will mint and they will be sent to a creator’s wallet. We assume we already have designed the Tokenomics to grow our token price value and to incentive the marketing . So let’s say we are going to allocate 10 M tokens for staking reward to stakeholders who give the influence to our network.

2.Staking Contract building

All stakeholders will participate to stake their token and they will get the some rewards to our token during some period .

They have to buy our tokens to stake through exchange sites. We will assume our Haney token is listing on the popular exchange sites after finish the presale round already.

  • APY (Annual percentage yield):40%
  • Stake fee : 1.5%
  • Unstake fee :0.5%

The detailed values will be used to calculate the staking reward amount as much as stake holders do stake. The stake holders can stake or unstake their tokens anytime.

At first time ,we are going to build the staking reward calculation like following.

Reward amount = Staked Amount * Reward Rate * TimeDiff / RewardInterval

  • Current staked Amount : staked amount *stake fee — unstaked amount *unstake fee
  • RewardRate : APY %
  • TimeDiff : current timestamp — last timestamp
  • RewardInterval: 365 days

So the stake holders will get reward amount by above calculation.

Conclusion

I didn’t describe the code in detail because I only wanted to let know how to implement staking reward calculation algorithm. Hopefully this article will help for smart contract beginners who are starting to build the staking contract. Thanks for reading my article. If you guys want to get full codebase. Please contact me here . Mail : jjg02016891@gmail.com

--

--

0xandyeth
0xandyeth

Written by 0xandyeth

😀 Blockchain Specialist | Smart contract Developer

No responses yet