Skip to content
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

Draft Abstract Staking logic #83

Merged
merged 13 commits into from
Jan 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored AbstractStaking, AbstractValueDistributor and AbstractValu…
…eDistributorMock contracts
mllwchrry committed Jan 18, 2024
commit ca2e18ac2537f630d5d3067e73b46e58cc9cc46e
7 changes: 7 additions & 0 deletions contracts/mock/staking/AbstractValueDistributorMock.sol
Original file line number Diff line number Diff line change
@@ -26,4 +26,11 @@ contract AbstractValueDistributorMock is AbstractValueDistributor, Multicall {
function userOwedValue(address user_) external view returns (uint256) {
return userDistribution(user_).owedValue;
}

function _getValueToDistribute(
uint256 timeUpTo_,
uint256 timeLastUpdate_
) internal view virtual override returns (uint256) {
return DECIMAL * (timeUpTo_ - timeLastUpdate_);
}
}
13 changes: 8 additions & 5 deletions contracts/staking/AbstractStaking.sol
Original file line number Diff line number Diff line change
@@ -41,6 +41,9 @@ abstract contract AbstractStaking is AbstractValueDistributor, Initializable {

/**
* @notice Initializes the contract setting the values provided as shares token, rewards token, reward rate and staking start time.
*
* Warning: when shares and rewards tokens are the same, users may accidentally withdraw other users' shares as a reward.
mllwchrry marked this conversation as resolved.
Show resolved Hide resolved
*
* @param sharesToken_ The address of the shares token.
* @param rewardsToken_ The address of the rewards token.
* @param rate_ The reward rate.
@@ -187,14 +190,14 @@ abstract contract AbstractStaking is AbstractValueDistributor, Initializable {

/**
* @notice Gets the value to be distributed for a given time period.
* @param timeUpTo The end timestamp of the period.
* @param timeLastUpdate The start timestamp of the period.
* @param timeUpTo_ The end timestamp of the period.
* @param timeLastUpdate_ The start timestamp of the period.
* @return The value to be distributed for the period.
*/
function _getValueToDistribute(
uint256 timeUpTo,
uint256 timeLastUpdate
uint256 timeUpTo_,
uint256 timeLastUpdate_
) internal view virtual override returns (uint256) {
return _rate * (timeUpTo - timeLastUpdate);
return _rate * (timeUpTo_ - timeLastUpdate_);
}
}
20 changes: 9 additions & 11 deletions contracts/staking/AbstractValueDistributor.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import {PRECISION, DECIMAL} from "../utils/Globals.sol";
import {PRECISION} from "../utils/Globals.sol";

/**
* @notice The AbstractValueDistributor module
@@ -203,28 +203,26 @@ abstract contract AbstractValueDistributor {
*
* Note: It will usually be required to override this function to provide custom distribution mechanics.
*
* @param timeUpTo The end timestamp of the period.
* @param timeLastUpdate The start timestamp of the period.
* @param timeUpTo_ The end timestamp of the period.
* @param timeLastUpdate_ The start timestamp of the period.
* @return The value to be distributed for the period.
*/
function _getValueToDistribute(
uint256 timeUpTo,
uint256 timeLastUpdate
) internal view virtual returns (uint256) {
return DECIMAL * (timeUpTo - timeLastUpdate); // 1 token with 18 decimals per second
}
uint256 timeUpTo_,
uint256 timeLastUpdate_
) internal view virtual returns (uint256);

/**
* @notice Gets the expected cumulative sum of value per token staked distributed at a given timestamp.
* @param timeUpTo The timestamp up to which to calculate the value distribution.
* @param timeUpTo_ The timestamp up to which to calculate the value distribution.
* @return The future cumulative sum of value per token staked that has been distributed.
*/
function _getFutureCumulativeSum(uint256 timeUpTo) internal view returns (uint256) {
function _getFutureCumulativeSum(uint256 timeUpTo_) internal view returns (uint256) {
if (_totalShares == 0) {
return _cumulativeSum;
}

uint256 value_ = _getValueToDistribute(timeUpTo, _updatedAt);
uint256 value_ = _getValueToDistribute(timeUpTo_, _updatedAt);

return _cumulativeSum + (value_ * PRECISION) / _totalShares;
}