Skip to content

Commit

Permalink
Simple implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dulguun-staderlabs committed Nov 1, 2023
1 parent d7a81d4 commit fbc21a7
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 146 deletions.
9 changes: 9 additions & 0 deletions contracts/StaderConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ contract StaderConfig is IStaderConfig, AccessControlUpgradeable {
keccak256('NODE_EL_REWARD_VAULT_IMPLEMENTATION');
bytes32 public constant override VALIDATOR_WITHDRAWAL_VAULT_IMPLEMENTATION =
keccak256('VALIDATOR_WITHDRAWAL_VAULT_IMPLEMENTATION');
bytes32 public constant override LENDING_POOL_CONTRACT = keccak256('LENDING_POOL_CONTRACT');

//POR Feed Proxy
bytes32 public constant override ETH_BALANCE_POR_FEED = keccak256('ETH_BALANCE_POR_FEED');
Expand Down Expand Up @@ -284,6 +285,10 @@ contract StaderConfig is IStaderConfig, AccessControlUpgradeable {
setContract(ETHX_SUPPLY_POR_FEED, _ethXSupplyProxy);
}

function updateLendingPoolProxy(address _lendingPoolProxy) external onlyRole(DEFAULT_ADMIN_ROLE) {
setContract(LENDING_POOL_CONTRACT, _lendingPoolProxy);
}

function updateStaderToken(address _staderToken) external onlyRole(DEFAULT_ADMIN_ROLE) {
setToken(SD, _staderToken);
}
Expand Down Expand Up @@ -448,6 +453,10 @@ contract StaderConfig is IStaderConfig, AccessControlUpgradeable {
return contractsMap[VALIDATOR_WITHDRAWAL_VAULT_IMPLEMENTATION];
}

function getLendingPool() external view override returns (address) {
return contractsMap[LENDING_POOL_CONTRACT];
}

//POR Feed Proxy Getters
function getETHBalancePORFeedProxy() external view override returns (address) {
return contractsMap[ETH_BALANCE_POR_FEED];
Expand Down
4 changes: 4 additions & 0 deletions contracts/interfaces/IStaderConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ interface IStaderConfig {

function VALIDATOR_WITHDRAWAL_VAULT_IMPLEMENTATION() external view returns (bytes32);

function LENDING_POOL_CONTRACT() external view returns (bytes32);

//POR Feed Proxy
function ETH_BALANCE_POR_FEED() external view returns (bytes32);

Expand Down Expand Up @@ -149,6 +151,8 @@ interface IStaderConfig {
function getETHBalancePORFeedProxy() external view returns (address);

function getETHXSupplyPORFeedProxy() external view returns (address);

function getLendingPool() external view returns (address);

// Tokens
function getStaderToken() external view returns (address);
Expand Down
118 changes: 118 additions & 0 deletions contracts/lendingpool/IncentiveController.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// SPDX-License-Identifier: GPL-3.0-or-later

Check failure on line 1 in contracts/lendingpool/IncentiveController.sol

View workflow job for this annotation

GitHub Actions / Prettier

contracts/lendingpool/IncentiveController.sol#L1

There are issues with this file's formatting, please run Prettier to fix the errors
pragma solidity 0.8.16;

import '@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';

import '../library/UtilLib.sol';
import '../interfaces/IStaderConfig.sol';

/// @title IncentiveController
/// @notice This contract handles the distribution of reward tokens for a lending pool.
contract IncentiveController is AccessControlUpgradeable {
// The emission rate of the reward tokens per second.
uint256 public emissionPerSecond;

// The timestamp of the last reward calculation.
uint256 public lastUpdateTimestamp;

// The stored value of the reward per token, used to calculate rewards.
uint256 public rewardPerTokenStored;

// Reference to the lending pool token contract.
IERC20 public lendingPoolToken;

// Reference to the reward token contract.
IERC20 public rewardToken;

// Reference to the Stader configuration contract.
IStaderConfig public staderConfig;

// A mapping of accounts to their pending reward amounts.
mapping(address => uint256) public rewards;

// A mapping of accounts to the reward per token value at their last update.
mapping(address => uint256) public userRewardPerTokenPaid;

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

/// @notice Initializes the contract with necessary addresses.
/// @param _lendingPoolToken The address of the lending pool token contract.
/// @param _staderConfig The address of the Stader configuration contract.
/// @param _rewardToken The address of the reward token contract.
function initialize(address _lendingPoolToken, address _staderConfig, address _rewardToken) external initializer {
UtilLib.checkNonZeroAddress(_lendingPoolToken);
UtilLib.checkNonZeroAddress(_staderConfig);
UtilLib.checkNonZeroAddress(_rewardToken);

lendingPoolToken = IERC20(_lendingPoolToken);
staderConfig = IStaderConfig(_staderConfig);
rewardToken = IERC20(_rewardToken);

__AccessControl_init();
}

/// @notice Claims the accrued rewards for an account.
/// @param account The address of the account claiming rewards.
function claim(address account) external {
UtilLib.onlyStaderContract(msg.sender, staderConfig, staderConfig.LENDING_POOL_CONTRACT());

updateReward(account);

uint256 reward = rewards[account];
require(reward > 0, "No rewards to claim.");
rewards[account] = 0;
rewardToken.transfer(account, reward);

emit RewardClaimed(account, reward);
}

/// @notice Updates the reward on deposit in the lending pool.
/// @param account The account that made a deposit.
function onDeposit(address account) external {
UtilLib.onlyStaderContract(msg.sender, staderConfig, staderConfig.LENDING_POOL_CONTRACT());

updateReward(account);
}

/// @notice Calculates the current reward per token.
/// @return The calculated reward per token.
function rewardPerToken() public view returns (uint256) {
if (lendingPoolToken.totalSupply() == 0) {
return rewardPerTokenStored;
}
return rewardPerTokenStored + (
(block.timestamp - lastUpdateTimestamp) * emissionPerSecond * 1e18 / lendingPoolToken.totalSupply()
);
}

/// @notice Calculates the total accrued reward for an account.
/// @param account The account to calculate rewards for.
/// @return The total accrued reward for the account.
function earned(address account) public view returns (uint256) {
uint256 currentBalance = lendingPoolToken.balanceOf(account);
uint256 currentRewardPerToken = rewardPerToken();

return (currentBalance * (currentRewardPerToken - userRewardPerTokenPaid[account]) / 1e18) + rewards[account];
}

/// @dev Internal function to update the reward state for an account.
/// @param account The account to update the reward for.
function updateReward(address account) internal {
rewardPerTokenStored = rewardPerToken();
lastUpdateTimestamp = block.timestamp;

if(account != address(0)) {
rewards[account] = earned(account);
userRewardPerTokenPaid[account] = rewardPerTokenStored;
}
}

/// @dev Emitted when a reward is claimed.
/// @param user The user who claimed the reward.
/// @param reward The amount of reward claimed.
event RewardClaimed(address indexed user, uint256 reward);
}
146 changes: 0 additions & 146 deletions contracts/lendingpool/LendingPoolRewards.sol

This file was deleted.

0 comments on commit fbc21a7

Please sign in to comment.