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

[RewardCalculation] Store period number instead of block number #33

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions contracts/interfaces/IRewardPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ interface IRewardPool {
uint256 debited;
// The amount rewards that user have already earned.
uint256 credited;
// Last block number that the info updated.
uint256 lastSyncedBlock;
// Last period number that the info updated.
uint256 lastSyncedPeriod;
}

struct SettledRewardFields {
Expand All @@ -36,8 +36,8 @@ interface IRewardPool {
}

struct SettledPool {
// Last block number that the info updated.
uint256 lastSyncedBlock;
// Last period number that the info updated.
uint256 lastSyncedPeriod;
// Accumulated of the amount rewards per share (one unit staking).
uint256 accumulatedRps;
}
Expand Down
16 changes: 8 additions & 8 deletions contracts/ronin/staking/RewardCalculation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract contract RewardCalculation is IRewardPool {
PendingPool memory _pool = _pendingPool[_poolAddr];

uint256 _balance = balanceOf(_poolAddr, _user);
if (_rewardSinked(_poolAddr, _periodOf(_reward.lastSyncedBlock))) {
if (_rewardSinked(_poolAddr, _reward.lastSyncedPeriod)) {
SettledRewardFields memory _sReward = _sUserReward[_poolAddr][_user];
uint256 _diffRps = _pool.accumulatedRps - _sReward.accumulatedRps;
return (_balance * _diffRps) / 1e18 + _sReward.debited;
Expand All @@ -45,10 +45,10 @@ abstract contract RewardCalculation is IRewardPool {
SettledPool memory _sPool = _settledPool[_poolAddr];

uint256 _diffRps = _sPool.accumulatedRps - _sReward.accumulatedRps;
if (_reward.lastSyncedBlock <= _sPool.lastSyncedBlock) {
if (_reward.lastSyncedPeriod <= _sPool.lastSyncedPeriod) {
uint256 _currentBalance = balanceOf(_poolAddr, _user);

if (_rewardSinked(_poolAddr, _periodOf(_reward.lastSyncedBlock))) {
if (_rewardSinked(_poolAddr, _reward.lastSyncedPeriod)) {
return (_currentBalance * _diffRps) / 1e18 + _sReward.debited;
}

Expand Down Expand Up @@ -93,7 +93,7 @@ abstract contract RewardCalculation is IRewardPool {
SettledPool memory _sPool = _settledPool[_poolAddr];

// Syncs the reward once the last sync is settled.
if (_reward.lastSyncedBlock <= _sPool.lastSyncedBlock) {
if (_reward.lastSyncedPeriod <= _sPool.lastSyncedPeriod) {
uint256 _claimableReward = getClaimableReward(_poolAddr, _user);

SettledRewardFields storage _sReward = _sUserReward[_poolAddr][_user];
Expand All @@ -108,7 +108,7 @@ abstract contract RewardCalculation is IRewardPool {

_reward.debited = _debited;
_reward.credited = _credited;
_reward.lastSyncedBlock = block.number;
_reward.lastSyncedPeriod = _periodOf(block.number);
emit PendingRewardUpdated(_poolAddr, _user, _debited, _credited);
}

Expand All @@ -129,13 +129,13 @@ abstract contract RewardCalculation is IRewardPool {
SettledRewardFields storage _sReward = _sUserReward[_poolAddr][_user];

_sReward.debited = 0;
if (_reward.lastSyncedBlock <= _sPool.lastSyncedBlock) {
if (_reward.lastSyncedPeriod <= _sPool.lastSyncedPeriod) {
_sReward.accumulatedRps = _sPool.accumulatedRps;
}
emit SettledRewardUpdated(_poolAddr, _user, 0, _sReward.accumulatedRps);

_reward.credited += _amount;
_reward.lastSyncedBlock = block.number;
_reward.lastSyncedPeriod = _periodOf(block.number);
emit PendingRewardUpdated(_poolAddr, _user, _reward.debited, _reward.credited);
}

Expand Down Expand Up @@ -187,7 +187,7 @@ abstract contract RewardCalculation is IRewardPool {
SettledPool storage _sPool = _settledPool[_poolAddr];
if (_accumulatedRpsList[_i] != _sPool.accumulatedRps) {
_sPool.accumulatedRps = _accumulatedRpsList[_i];
_sPool.lastSyncedBlock = block.number;
_sPool.lastSyncedPeriod = _periodOf(block.number);
}
}
emit SettledPoolsUpdated(_poolList, _accumulatedRpsList);
Expand Down