Skip to content

Commit

Permalink
StakingRewards
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Jan 7, 2022
1 parent fcd4de4 commit 5bd00f3
Show file tree
Hide file tree
Showing 10 changed files with 45,010 additions and 0 deletions.
2 changes: 2 additions & 0 deletions staking_rewards/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALCHEMY_API_KEY=
ROPSTEN_PRIVATE_KEY=
8 changes: 8 additions & 0 deletions staking_rewards/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
.env

#Hardhat files
cache
artifacts
dist
typechain
22 changes: 22 additions & 0 deletions staking_rewards/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 120,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false,
"explicitTypes": "always",
"semi": true
}
},
{
"files": "*.ts",
"options": {
"printWidth": 120
}
}
]
}
68 changes: 68 additions & 0 deletions staking_rewards/contracts/StakingRewards.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract StakingRewards {
IERC20 public rewardsToken;
IERC20 public stakingToken;

uint256 public rewardRate = 100;
uint256 public lastUpdateTime;
uint256 public rewardPerTokenStored;

mapping(address => uint256) public userRewardPerTokenPaid;
mapping(address => uint256) public rewards;

uint256 private _totalSupply;
mapping(address => uint256) private _balances;

constructor(address _stakingToken, address _rewardsToken) {
stakingToken = IERC20(_stakingToken);
rewardsToken = IERC20(_rewardsToken);
}

function rewardPerToken() public view returns (uint256) {
if (_totalSupply == 0) {
return 0;
} else {
uint256 elapsed = block.timestamp - lastUpdateTime;
return rewardPerTokenStored + (elapsed * rewardRate * 1e18) / _totalSupply;
}
}

function earned(address account) public view returns (uint256) {
uint256 accountBalance = _balances[account];
uint256 finalRewardPerToken = rewardPerToken() - userRewardPerTokenPaid[account];
uint256 accountRewards = rewards[account];

return (accountBalance * finalRewardPerToken) / 1e18 + accountRewards;
}

modifier updateReward(address account) {
rewardPerTokenStored = rewardPerToken();
lastUpdateTime = block.timestamp;

rewards[account] = earned(account);
userRewardPerTokenPaid[account] = rewardPerTokenStored;
_;
}

function stake(uint256 amount) external updateReward(msg.sender) {
_totalSupply += amount;
_balances[msg.sender] += amount;
stakingToken.transferFrom(msg.sender, address(this), amount);
}

function withdraw(uint256 amount) external updateReward(msg.sender) {
_totalSupply -= amount;
_balances[msg.sender] -= amount;
stakingToken.transfer(msg.sender, amount);
}

function getReward() external updateReward(msg.sender) {
uint256 reward = rewards[msg.sender];
rewards[msg.sender] = 0;
rewardsToken.transfer(msg.sender, reward);
}
}
56 changes: 56 additions & 0 deletions staking_rewards/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { config as dotenvConfig } from "dotenv";
import "@typechain/hardhat";
import "@nomiclabs/hardhat-ethers";
import "@nomiclabs/hardhat-waffle";
import "@nomiclabs/hardhat-etherscan";
import "solidity-coverage";
import "hardhat-gas-reporter";
import { HardhatUserConfig } from "hardhat/types/config";

dotenvConfig();

const gasPrice = parseInt(process.env.GAS_PRICE || "1000000000");

const config: HardhatUserConfig = {
solidity: {
compilers: [
{
version: "0.8.10",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
],
},
networks: {
hardhat: {
initialBaseFeePerGas: 0,
},
rinkeby: {
url: process.env.RINKEBY_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
gasPrice,
},
mainnet: {
url: process.env.MAINNET_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
gasPrice,
},
},
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: "USD",
gasPrice: 120,
coinmarketcap: process.env.COINMARKETCAP_API_KEY,
},
etherscan: {
apiKey: process.env.ETHERSCAN_API_KEY,
},
};

export default config;
Loading

0 comments on commit 5bd00f3

Please sign in to comment.