Skip to content
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
32 changes: 22 additions & 10 deletions src/compound/SupplyVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,40 @@ contract SupplyVault is ISupplyVault, SupplyVaultBase {
address to,
uint256 amount
) internal override {
_accrueUnclaimedRewards(from);
_accrueUnclaimedRewards(to);
uint256 newRewardsIndex = _claimRewards();
_accrueUnclaimedRewardsFromRewardsIndex(from, newRewardsIndex);
_accrueUnclaimedRewardsFromRewardsIndex(to, newRewardsIndex);

super._beforeTokenTransfer(from, to, amount);
}

function _accrueUnclaimedRewards(address _user) internal returns (uint256 unclaimed) {
uint256 supply = totalSupply();
uint256 rewardsIndexMem = rewardsIndex;
function _claimRewards() internal returns (uint256 newRewardsIndex) {
newRewardsIndex = rewardsIndex;

uint256 supply = totalSupply();
if (supply > 0) {
address[] memory poolTokens = new address[](1);
poolTokens[0] = poolToken;
rewardsIndexMem += morpho.claimRewards(poolTokens, false).divWadDown(supply);
rewardsIndex = rewardsIndexMem;

newRewardsIndex += morpho.claimRewards(poolTokens, false).divWadDown(supply);
rewardsIndex = newRewardsIndex;
}
}

function _accrueUnclaimedRewards(address _user) internal returns (uint256 unclaimed) {
return _accrueUnclaimedRewardsFromRewardsIndex(_user, _claimRewards());
}

function _accrueUnclaimedRewardsFromRewardsIndex(address _user, uint256 _newRewardsIndex)
internal
returns (uint256 unclaimed)
{
UserRewardsData storage userRewardsData = userRewards[_user];
uint256 rewardsIndexDiff;

// Safe because we always have `rewardsIndex` >= `userRewardsData.index`.
unchecked {
rewardsIndexDiff = rewardsIndexMem - userRewardsData.index;
rewardsIndexDiff = _newRewardsIndex - userRewardsData.index;
}

unclaimed = userRewardsData.unclaimed;
Expand All @@ -146,8 +158,8 @@ contract SupplyVault is ISupplyVault, SupplyVaultBase {
userRewardsData.unclaimed = unclaimed.safeCastTo128();
}

userRewardsData.index = rewardsIndexMem.safeCastTo128();
userRewardsData.index = _newRewardsIndex.safeCastTo128();

emit Accrued(_user, rewardsIndexMem, unclaimed);
emit Accrued(_user, _newRewardsIndex, unclaimed);
}
}