-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementa Staking Rewards #2
Open
Kayaba-Attribution
wants to merge
5
commits into
martriay:main
Choose a base branch
from
Kayaba-Attribution:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a061799
implementar staking rewards
Kayaba-Attribution cf24357
Code Clean Up
Kayaba-Attribution 79adad1
Staking Rewards PR fixes
Kayaba-Attribution 1d82609
Merge branch 'main' into main
Kayaba-Attribution ed03a86
Update exchange-test.js
Kayaba-Attribution File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
import "hardhat/console.sol"; | ||
|
||
contract StakingRewards { | ||
IERC20 public rewardsToken; | ||
IERC20 public stakingToken; | ||
|
||
uint public rewardRate = 100; | ||
uint public lastUpdateTime; | ||
uint public rewardPerTokenStored; | ||
|
||
mapping(address => uint) public userRewardPerTokenPaid; | ||
mapping(address => uint) public rewards; | ||
|
||
uint private _totalSupply; | ||
mapping(address => uint) private _balances; | ||
|
||
/* ========== CONSTRUCTOR ========== */ | ||
constructor(address _stakingToken, address _rewardsToken) { | ||
stakingToken = IERC20(_stakingToken); | ||
rewardsToken = IERC20(_rewardsToken); | ||
} | ||
|
||
/* ========== VIEWS ========== */ | ||
function totalSupply() external view returns (uint256) { | ||
return _totalSupply; | ||
} | ||
|
||
function balanceOf(address account) external view returns (uint256) { | ||
return _balances[account]; | ||
} | ||
|
||
function rewardPerToken() public view returns (uint) { | ||
//console.log("RewardsPerToken total supply: %s rewardPerTokenStored %s",_totalSupply,rewardPerTokenStored); | ||
if (_totalSupply == 0) { | ||
return rewardPerTokenStored; | ||
} | ||
return | ||
rewardPerTokenStored + | ||
(((block.timestamp - lastUpdateTime) * rewardRate * 1e18) / _totalSupply); | ||
} | ||
|
||
function earned(address account) public view returns (uint) { | ||
return | ||
((_balances[account] * | ||
(rewardPerToken() - userRewardPerTokenPaid[account])) / 1e18) + | ||
rewards[account]; | ||
} | ||
|
||
/* ========== MUTATIVE FUNCTIONS ========== */ | ||
|
||
function stake(uint _amount) external updateReward(msg.sender) { | ||
require(_amount > 0, "Cannot stake 0"); | ||
_totalSupply += _amount; | ||
_balances[msg.sender] += _amount; | ||
stakingToken.transferFrom(msg.sender, address(this), _amount); | ||
|
||
//console.log("Stake %s tokens", _amount); | ||
} | ||
|
||
function withdraw(uint _amount) public updateReward(msg.sender) { | ||
require(_amount > 0, "Cannot withdraw 0"); | ||
_totalSupply -= _amount; | ||
_balances[msg.sender] -= _amount; | ||
stakingToken.transfer(msg.sender, _amount); | ||
|
||
//console.log("withdraw %s tokens \ntotal supply %s", _amount,_totalSupply); | ||
|
||
} | ||
|
||
function getReward() public updateReward(msg.sender) { | ||
//console.log("Rewards on get", rewards[msg.sender],rewardPerTokenStored); | ||
uint reward = rewards[msg.sender]; | ||
if (reward > 0) { | ||
rewards[msg.sender] = 0; | ||
rewardsToken.transferFrom(address(this), msg.sender, reward); | ||
} | ||
|
||
} | ||
|
||
function exit() external { | ||
withdraw(_balances[msg.sender]); | ||
getReward(); | ||
} | ||
|
||
/// @notice add the tokens to be distributed as rewards | ||
/// @param _amount token amount (token address is the one on constructor) | ||
function depositRewardsTokens(uint256 _amount) external { | ||
rewardsToken.transferFrom(msg.sender, address(this), _amount); | ||
rewardsToken.approve(address(this), _amount); | ||
} | ||
|
||
/* ========== MODIFIERS ========== */ | ||
modifier updateReward(address account) { | ||
rewardPerTokenStored = rewardPerToken(); | ||
lastUpdateTime = block.timestamp; | ||
|
||
rewards[account] = earned(account); | ||
userRewardPerTokenPaid[account] = rewardPerTokenStored; | ||
_; | ||
} | ||
|
||
} | ||
|
||
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); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mejor utilizar la de OpenZepelling acá.