RewardsManager

Git Source

Inherits: IRewardsManager, Initializable

Author: Morpho Labs

Contract managing Aave's protocol rewards.

State Variables

_REWARDS_CONTROLLER

The address of Aave's rewards controller.

The rewards controller is, in theory, specific to an asset. In practice, it is the same for all assets and it is supposed to be true for future assets as well.

IRewardsController internal immutable _REWARDS_CONTROLLER;

_MORPHO

The address of the Morpho protocol.

IMorpho internal immutable _MORPHO;

_localAssetData

The local data related to a given asset (either aToken or debt token). asset -> reward -> RewardData

mapping(address => mapping(address => RewardData)) internal _localAssetData;

Functions

onlyMorpho

Prevents a user to call function allowed for the main Morpho contract only.

modifier onlyMorpho();

constructor

Contract constructor.

The implementation contract disables initialization upon deployment to avoid being hijacked.

constructor(address rewardsController, address morpho);

Parameters

NameTypeDescription
rewardsControlleraddressThe address of the Aave rewards controller.
morphoaddressThe address of the main Morpho contract.

claimRewards

Accrues unclaimed rewards for the given assets and returns the total unclaimed rewards.

function claimRewards(address[] calldata assets, address user)
    external
    onlyMorpho
    returns (address[] memory rewardsList, uint256[] memory claimedAmounts);

Parameters

NameTypeDescription
assetsaddress[]The assets for which to accrue rewards (aToken or variable debt token).
useraddressThe address of the user.

Returns

NameTypeDescription
rewardsListaddress[]The list of reward tokens.
claimedAmountsuint256[]The list of claimed reward amounts.

updateUserRewards

Updates the unclaimed rewards of a user.

Only called by Morpho at positions updates in the data structure.

function updateUserRewards(address user, address asset, uint256 userBalance) external onlyMorpho;

Parameters

NameTypeDescription
useraddressThe address of the user.
assetaddressThe address of the reference asset of the distribution (aToken or variable debt token).
userBalanceuint256The current user asset balance.

MORPHO

Returns the Morpho protocol address.

function MORPHO() external view returns (address);

REWARDS_CONTROLLER

Returns the rewards controller address.

function REWARDS_CONTROLLER() external view returns (address);

getRewardData

Returns the last updated index and timestamp for a specific asset and reward token.

function getRewardData(address asset, address reward)
    external
    view
    returns (uint256 startingIndex, uint256 index, uint256 lastUpdateTimestamp);

Parameters

NameTypeDescription
assetaddressThe address of the asset.
rewardaddressThe address of the reward token.

Returns

NameTypeDescription
startingIndexuint256The index from which the rewards manager begins tracking the rewards controller.
indexuint256The last updated index.
lastUpdateTimestampuint256The last updated timestamp.

getUserData

Returns the user's index and accrued rewards for a specific asset and rewards pair.

function getUserData(address asset, address reward, address user)
    external
    view
    returns (uint256 index, uint256 accrued);

Parameters

NameTypeDescription
assetaddressThe address of the asset.
rewardaddressThe address of the reward token.
useraddressThe address of the user.

Returns

NameTypeDescription
indexuint256The user's index.
accrueduint256The user's accrued rewards.

getUserAccruedRewards

Returns user's accrued rewards for the specified assets and reward token

function getUserAccruedRewards(address[] calldata assets, address user, address reward)
    external
    view
    returns (uint256 totalAccrued);

Parameters

NameTypeDescription
assetsaddress[]The list of assets to retrieve accrued rewards.
useraddressThe address of the user.
rewardaddressThe address of the reward token.

Returns

NameTypeDescription
totalAccrueduint256The total amount of accrued rewards.

getAllUserRewards

Returns user's rewards for the specified assets and for all reward tokens.

function getAllUserRewards(address[] calldata assets, address user)
    external
    view
    returns (address[] memory rewardsList, uint256[] memory unclaimedAmounts);

Parameters

NameTypeDescription
assetsaddress[]The list of assets to retrieve rewards.
useraddressThe address of the user.

Returns

NameTypeDescription
rewardsListaddress[]The list of reward tokens.
unclaimedAmountsuint256[]The list of unclaimed reward amounts.

getUserRewards

Returns user's rewards for the specified assets and reward token.

function getUserRewards(address[] calldata assets, address user, address reward) external view returns (uint256);

Parameters

NameTypeDescription
assetsaddress[]The list of assets to retrieve rewards.
useraddressThe address of the user.
rewardaddressThe address of the reward token

Returns

NameTypeDescription
<none>uint256The user's rewards in reward token.

getUserAssetIndex

Returns the user's index for the specified asset and reward token.

If an already listed AaveV3 reward token is not yet tracked (startingIndex == 0), this view function ignores that it will get updated upon interaction.

function getUserAssetIndex(address user, address asset, address reward) external view returns (uint256);

Parameters

NameTypeDescription
useraddressThe address of the user.
assetaddressThe address of the reference asset of the distribution (aToken or variable debt token).
rewardaddressThe address of the reward token.

Returns

NameTypeDescription
<none>uint256The user's index.

getAssetIndex

Returns the virtually updated asset index for the specified asset and reward token.

function getAssetIndex(address asset, address reward) external view returns (uint256 assetIndex);

Parameters

NameTypeDescription
assetaddressThe address of the reference asset of the distribution (aToken or variable debt token).
rewardaddressThe address of the reward token.

Returns

NameTypeDescription
assetIndexuint256The reward token's virtually updated asset index.

_updateRewardData

Updates the state of the distribution for the specified reward.

function _updateRewardData(
    RewardData storage localRewardData,
    address asset,
    address reward,
    uint256 scaledTotalSupply,
    uint256 assetUnit
) internal returns (uint256 newIndex, bool indexUpdated);

Parameters

NameTypeDescription
localRewardDataRewardDataThe local reward's data.
assetaddressThe asset being rewarded.
rewardaddressThe address of the reward token.
scaledTotalSupplyuint256The current scaled total supply of underlying assets for this distribution.
assetUnituint256The asset's unit (10**decimals).

Returns

NameTypeDescription
newIndexuint256The new distribution index.
indexUpdatedboolTrue if the index was updated, false otherwise.

_updateUserData

Updates the state of the distribution for the specific user.

function _updateUserData(
    RewardData storage localRewardData,
    address user,
    uint256 userBalance,
    uint256 newAssetIndex,
    uint256 assetUnit
) internal returns (uint256 rewardsAccrued, bool dataUpdated);

Parameters

NameTypeDescription
localRewardDataRewardDataThe local reward's data
useraddressThe address of the user.
userBalanceuint256The current user asset balance.
newAssetIndexuint256The new index of the asset distribution.
assetUnituint256The asset's unit (10**decimals).

Returns

NameTypeDescription
rewardsAccrueduint256The rewards accrued since the last update.
dataUpdatedboolTrue if the data was updated, false otherwise.

_updateData

Iterates and accrues all the rewards for asset of the specific user.

function _updateData(address user, address asset, uint256 userBalance, uint256 scaledTotalSupply) internal;

Parameters

NameTypeDescription
useraddressThe user address.
assetaddressThe address of the reference asset of the distribution.
userBalanceuint256The current user asset balance.
scaledTotalSupplyuint256The current scaled total supply for this distribution.

_updateDataMultiple

Accrues all the rewards of the assets specified in the userAssetBalances list.

function _updateDataMultiple(address user, UserAssetBalance[] memory userAssetBalances) internal;

Parameters

NameTypeDescription
useraddressThe address of the user.
userAssetBalancesUserAssetBalance[]The list of structs with the user balance and total supply of a set of assets.

_getUserReward

Returns the accrued unclaimed amount of a reward from a user over a list of distribution.

function _getUserReward(address user, address reward, UserAssetBalance[] memory userAssetBalances)
    internal
    view
    returns (uint256 unclaimedRewards);

Parameters

NameTypeDescription
useraddressThe address of the user.
rewardaddressThe address of the reward token.
userAssetBalancesUserAssetBalance[]List of structs with the user balance and total supply of a set of assets.

Returns

NameTypeDescription
unclaimedRewardsuint256The accrued rewards for the user until the moment.

_getPendingRewards

Computes the pending (not yet accrued) rewards since the last user action.

function _getPendingRewards(address user, address reward, UserAssetBalance memory userAssetBalance)
    internal
    view
    returns (uint256);

Parameters

NameTypeDescription
useraddressThe address of the user.
rewardaddressThe address of the reward token.
userAssetBalanceUserAssetBalanceThe struct with the user balance and total supply of the incentivized asset.

Returns

NameTypeDescription
<none>uint256The pending rewards for the user since the last user action.

_getRewards

Computes user's accrued rewards on a distribution.

function _getRewards(uint256 userBalance, uint256 reserveIndex, uint256 userIndex, uint256 assetUnit)
    internal
    pure
    returns (uint256 rewards);

Parameters

NameTypeDescription
userBalanceuint256The current user asset balance.
reserveIndexuint256The current index of the distribution.
userIndexuint256The index stored for the user, representing its staking moment.
assetUnituint256The asset's unit (10**decimals).

Returns

NameTypeDescription
rewardsuint256The rewards accrued.

_getAssetIndex

Computes the next value of an specific distribution index, with validations.

function _getAssetIndex(
    RewardData storage localRewardData,
    address asset,
    address reward,
    uint256 scaledTotalSupply,
    uint256 assetUnit
) internal view returns (uint256, uint256);

Parameters

NameTypeDescription
localRewardDataRewardDataThe local reward's data.
assetaddressThe asset being rewarded.
rewardaddressThe address of the reward token.
scaledTotalSupplyuint256The current total supply of underlying assets for this distribution.
assetUnituint256The asset's unit (10**decimals).

Returns

NameTypeDescription
<none>uint256The former index and the new index in this order.
<none>uint256

_getUserAssetBalances

Returns user balances and total supply of all the assets specified by the assets parameter.

function _getUserAssetBalances(address[] calldata assets, address user)
    internal
    view
    returns (UserAssetBalance[] memory userAssetBalances);

Parameters

NameTypeDescription
assetsaddress[]List of assets to retrieve user balance and total supply.
useraddressThe address of the user.

Returns

NameTypeDescription
userAssetBalancesUserAssetBalance[]The list of structs with user balance and total supply of the given assets.

_computeUserIndex

Computes the index of a user for a specific reward distribution.

function _computeUserIndex(RewardData storage localRewardData, address user) internal view returns (uint256);

Parameters

NameTypeDescription
localRewardDataRewardDataThe local reward's data.
useraddressThe address of the user.

Returns

NameTypeDescription
<none>uint256The index of the user for the distribution.