Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
akshatmittal committed Apr 25, 2024
1 parent 434bdf9 commit e2a282c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 41 deletions.
66 changes: 38 additions & 28 deletions contracts/rewards/GenericMultiRewardsVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";

import { RewardInfo, Errors, Events } from "./definitions.sol";

/*
/**
* @title GenericMultiRewardsVault
* @notice Non-transferrable ERC4626 vault that allows streaming of rewards in multiple tokens
* @dev Reward tokens transferred by accident without using fundReward() will be lost!
*
* Registering new reward tokens is permissioned, but adding funds is permissionless
*
* No appreciation; exchange rate is always 1:1 with underlying
*
* @notice Non-transferrable ERC4626 vault that allows streaming of rewards in multiple other tokens
* @dev Reward tokens transferred by accident without using `fundReward()` will be lost!
* Registering new reward tokens is permissioned, but adding funds is permissionless.
* No appreciation; exchange rate is always 1:1 with underlying.
* Unit notation
* - {qRewardTok} = Reward token quanta
* - {qAsset} = Asset token quanta
Expand All @@ -34,9 +32,12 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
address initialOwner
) ERC4626(_underlying) ERC20(_name, _symbol) Ownable(initialOwner) {}

/*
** Core Vault Functionality
/**
* Core Vault Functionality
*/
function decimals() public view override returns (uint8) {
return IERC20Metadata(asset()).decimals();
}

function _convertToShares(uint256 assets, Math.Rounding) internal pure override returns (uint256) {
return assets;
Expand Down Expand Up @@ -65,7 +66,9 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
super._withdraw(caller, receiver, owner_, assets, shares);
}

/// @dev Prevent transfer
/**
* @dev Prevent transfer
*/
function _update(address from, address to, uint256 amount) internal virtual override {
if (from != address(0) && to != address(0)) {
revert Errors.NotAllowed();
Expand All @@ -74,8 +77,8 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
super._update(from, to, amount);
}

/*
** Rewards: Claim Logic
/**
* Rewards: Claim Logic
*/

/**
Expand All @@ -99,10 +102,9 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
}
}

/*
** Rewards: Management
/**
* Rewards: Management
*/

IERC20[] public rewardTokens;
mapping(IERC20 rewardToken => RewardInfo rewardInfo) public rewardInfos;
mapping(IERC20 rewardToken => address distributor) public distributorInfo;
Expand All @@ -118,6 +120,7 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
* @param amount {qRewardTok} Initial funding amount for this reward.
* @dev The `rewardsEndTimestamp` gets calculated based on `rewardsPerSecond` and `amount`.
* @dev If `rewardsPerSecond` is 0 the rewards will be paid out instantly. In this case `amount` must be 0.
* This is useful for dynamic rewards, where the rewards are controlled by an external contract.
*/
function addRewardToken(
IERC20Metadata rewardToken,
Expand All @@ -134,7 +137,7 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
revert Errors.RewardTokenAlreadyExist(rewardToken);
}

if (amount > 0) {
if (amount != 0) {
if (rewardsPerSecond == 0) {
revert Errors.ZeroRewardsSpeed();
}
Expand Down Expand Up @@ -165,7 +168,7 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
}

/**
* @notice Updates distributor for the rewardToken
* @notice Updates distributor for `rewardToken`
* @param rewardToken Token that can be earned by staking.
* @param distributor Distributor with the ability to control rewards for this token.
* @dev Callable by owner or distributor themselves.
Expand Down Expand Up @@ -252,10 +255,12 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
emit Events.RewardInfoUpdate(rewardToken, rewards.rewardsPerSecond, rewardsEndTimestamp);
}

/// @param rewardsEndTimestamp {s}
/// @param rewardsPerSecond {qRewardTok/s}
/// @param amount {qRewardTok}
/// @return {s}
/**
* @param rewardsEndTimestamp {s}
* @param rewardsPerSecond {qRewardTok/s}
* @param amount {qRewardTok}
* @return {s}
*/
function _calcRewardsEnd(
uint48 rewardsEndTimestamp,
uint256 rewardsPerSecond,
Expand Down Expand Up @@ -295,8 +300,10 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
_;
}

/// @notice Accrue rewards over time.
/// @return accrued {qRewardTok}
/**
* @notice Accrue rewards over time.
* @return accrued {qRewardTok}
*/
function _accrueStatic(RewardInfo memory rewards) internal view returns (uint256 accrued) {
uint256 elapsed;
if (rewards.rewardsEndTimestamp > block.timestamp) {
Expand All @@ -309,8 +316,10 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
accrued = uint256(rewards.rewardsPerSecond * elapsed);
}

/// @notice Accrue global rewards for a rewardToken
/// @param accrued {qRewardTok}
/**
* @notice Accrue global rewards for a rewardToken
* @param accrued {qRewardTok}
*/
function _accrueRewards(IERC20 _rewardToken, uint256 accrued) internal {
uint256 supplyTokens = totalSupply();
uint256 deltaIndex;
Expand All @@ -325,7 +334,9 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
rewardInfos[_rewardToken].lastUpdatedTimestamp = SafeCast.toUint48(block.timestamp);
}

/// @notice Sync a user's rewards for a rewardToken with the global reward index for that token
/**
* @notice Sync a user's rewards for a rewardToken with the global reward index for that token
*/
function _accrueUser(address _user, IERC20 _rewardToken) internal {
RewardInfo memory rewardIndex = rewardInfos[_rewardToken];

Expand All @@ -343,7 +354,6 @@ contract GenericMultiRewardsVault is ERC4626, Ownable {
// {qRewardTok} = {qShare} * {qRewardTok} / {qShare}
uint256 supplierDelta = (balanceOf(_user) * deltaIndex) / uint256(10 ** decimals());


// {qRewardTok} += {qRewardTok}
accruedRewards[_user][_rewardToken] += supplierDelta;
userIndex[_user][_rewardToken] = rewardIndex.index;
Expand Down
1 change: 0 additions & 1 deletion contracts/rewards/definitions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ abstract contract Errors {
error RewardTokenDoesNotExist(IERC20 rewardToken);
error RewardTokenCanNotBeStakingToken();
error ZeroAmount();
error NotSubmitter(address submitter);
error RewardsAreDynamic(IERC20 rewardToken);
error ZeroRewardsSpeed();
error InvalidConfig();
Expand Down
32 changes: 20 additions & 12 deletions contracts/staking/GenericStakedAppreciatingVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ struct RewardTracker {

/*
* @title GenericMultiRewardsVault
* @notice Transferrable ERC4626 vault with linear reward streaming in the vault's asset token
*
* The only reward token is the asset itself. Adding rewards is permisionless.
*
* Asset tokens accidentally transferred into the contract will be picked up as part of
* the next week distribution period.
*
* Unit notation
* @notice Transferrable ERC4626 vault with linear reward streaming in the vault's asset token.
* The only reward token is the asset itself. Adding rewards is permisionless.
* @dev Asset tokens transferred into the contract without `deposit` will be picked up as part of
* the next week distribution period. Any token transferred into the contract that is not
* the asset token will be lost forever.
*
* Unit Notation
* - {qAsset} = Asset token quanta
* - {qShare} = Share token quanta
* - {s} = Seconds
Expand All @@ -35,7 +34,12 @@ contract GenericStakedAppreciatingVault is ERC4626 {
event RewardAdded(uint256 reward, uint256 periodStart, uint256 periodEnd);
event RewardDistributionUpdated(uint256 periodStart, uint256 periodEnd, uint256 amount);

/// @param _distributionPeriod {s} Distribution Period for Accumulated Rewards
/**
* @param _name Vault Name
* @param _symbol Vault Symbol
* @param _underlying {qAsset} Asset Token
* @param _distributionPeriod {s} Distribution Period for Accumulated Rewards
*/
constructor(
string memory _name,
string memory _symbol,
Expand Down Expand Up @@ -78,7 +82,9 @@ contract GenericStakedAppreciatingVault is ERC4626 {
);
}

/// @return {qAsset}
/**
* @return {qAsset}
*/
function totalAssets() public view override returns (uint256) {
// {qAsset} = {qAsset} + {qAsset}
return totalDeposited + _currentAccountedRewards();
Expand All @@ -96,7 +102,9 @@ contract GenericStakedAppreciatingVault is ERC4626 {
emit RewardAdded(rewardTracker.rewardAmount, rewardTracker.rewardPeriodStart, rewardTracker.rewardPeriodEnd);
}

/// @return {qAsset}
/**
* @return {qAsset}
*/
function _currentAccountedRewards() internal view returns (uint256) {
if (block.timestamp >= rewardTracker.rewardPeriodEnd) {
return rewardTracker.rewardAmount;
Expand Down Expand Up @@ -134,6 +142,6 @@ contract GenericStakedAppreciatingVault is ERC4626 {
}

function _decimalsOffset() internal pure override returns (uint8) {
return 3; // TODO: Change this?
return 3;
}
}

0 comments on commit e2a282c

Please sign in to comment.