Request form
Cookbook may introduce a token in the future. Share and contribute to be eligible for future airdrops.
close icon
relevant tag icon
Discrete Staking Rewards Example
copy icon
solidity-by-example
• version 1.0.0
ERC20
Staking
Rewards
Deployable

Discrete Staking Rewards Example

Similar to the staking rewards contract. Difference is that reward amount may vary at each second.

*Visit desktop site to download or deploy

Version

1.0.0

Recent Use

🍞 0x1D34 saved
🥐 0x7015 deployed
🥖 0xbb00 deployed
🥨 0x5F68 deployed
🥯 0x5F68 deployed

Last Publish

12/12/2022
Any contract you deploy is yours.
Fully owned and controlled by your wallet.
Documentation
Source Code
DiscreteStakingRewards.sol
// SPDX-License-Identifier: MIT pragma solidity ^0.8.13; contract DiscreteStakingRewards { IERC20 public immutable stakingToken; IERC20 public immutable rewardToken; mapping(address => uint) public balanceOf; uint public totalSupply; uint private constant MULTIPLIER = 1e18; uint private rewardIndex; mapping(address => uint) private rewardIndexOf; mapping(address => uint) private earned; constructor(address _stakingToken, address _rewardToken) { stakingToken = IERC20(_stakingToken); rewardToken = IERC20(_rewardToken); } function updateRewardIndex(uint reward) external { rewardToken.transferFrom(msg.sender, address(this), reward); rewardIndex += (reward * MULTIPLIER) / totalSupply; } function _calculateRewards(address account) private view returns (uint) { uint shares = balanceOf[account]; return (shares * (rewardIndex - rewardIndexOf[account])) / MULTIPLIER; } function calculateRewardsEarned(address account) external view returns (uint) { return earned[account] + _calculateRewards(account); } function _updateRewards(address account) private { earned[account] += _calculateRewards(account); rewardIndexOf[account] = rewardIndex; } function stake(uint amount) external { _updateRewards(msg.sender); balanceOf[msg.sender] += amount; totalSupply += amount; stakingToken.transferFrom(msg.sender, address(this), amount); } function unstake(uint amount) external { _updateRewards(msg.sender); balanceOf[msg.sender] -= amount; totalSupply -= amount; stakingToken.transfer(msg.sender, amount); } function claim() external returns (uint) { _updateRewards(msg.sender); uint reward = earned[msg.sender]; if (reward > 0) { earned[msg.sender] = 0; rewardToken.transfer(msg.sender, reward); } return reward; } } interface IERC20 { function totalSupply() external view returns (uint); function balanceOf(address account) external view returns (uint); function transfer(address recipient, uint amount) external returns (bool); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint amount) external returns (bool); function transferFrom( address sender, address recipient, uint amount ) external returns (bool); event Transfer(address indexed from, address indexed to, uint value); event Approval(address indexed owner, address indexed spender, uint value); }

Get Cookin'
share iconShare

copy iconNo-Code Deploy
copy iconDownload Source
copy iconnpx cookbookdev i discrete-staking-rewards-example
copy icon

Bytecode

Download

Verification

Download

Recent Use

🍞 0x1D34 saved
🥐 0x7015 deployed
🥖 0xbb00 deployed
🥨 0x5F68 deployed
🥯 0x5F68 deployed

Last Publish

12/12/2022

Solidity Compiler

v0.8.17+commit.8df45f5f

Version

1.0.0

Cookbook is free.
Any contract you deploy is yours.
Your contract is owned and controlled by you.