-
Notifications
You must be signed in to change notification settings - Fork 58
/
AaveDistributionManager.sol
245 lines (210 loc) · 7.86 KB
/
AaveDistributionManager.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;
pragma experimental ABIEncoderV2;
import {SafeMath} from '../lib/SafeMath.sol';
import {DistributionTypes} from '../lib/DistributionTypes.sol';
import {IAaveDistributionManager} from '../interfaces/IAaveDistributionManager.sol';
/**
* @title AaveDistributionManager
* @notice Accounting contract to manage multiple staking distributions
* @author Aave
**/
contract AaveDistributionManager is IAaveDistributionManager {
using SafeMath for uint256;
struct AssetData {
uint128 emissionPerSecond;
uint128 lastUpdateTimestamp;
uint256 index;
mapping(address => uint256) users;
}
uint256 public immutable DISTRIBUTION_END;
address public immutable EMISSION_MANAGER;
uint8 public constant PRECISION = 18;
mapping(address => AssetData) public assets;
event AssetConfigUpdated(address indexed asset, uint256 emission);
event AssetIndexUpdated(address indexed asset, uint256 index);
event UserIndexUpdated(address indexed user, address indexed asset, uint256 index);
constructor(address emissionManager, uint256 distributionDuration) public {
DISTRIBUTION_END = block.timestamp.add(distributionDuration);
EMISSION_MANAGER = emissionManager;
}
/**
* @dev Configures the distribution of rewards for a list of assets
* @param assetsConfigInput The list of configurations to apply
**/
function configureAssets(DistributionTypes.AssetConfigInput[] calldata assetsConfigInput)
external
override
{
require(msg.sender == EMISSION_MANAGER, 'ONLY_EMISSION_MANAGER');
for (uint256 i = 0; i < assetsConfigInput.length; i++) {
AssetData storage assetConfig = assets[assetsConfigInput[i].underlyingAsset];
_updateAssetStateInternal(
assetsConfigInput[i].underlyingAsset,
assetConfig,
assetsConfigInput[i].totalStaked
);
assetConfig.emissionPerSecond = assetsConfigInput[i].emissionPerSecond;
emit AssetConfigUpdated(
assetsConfigInput[i].underlyingAsset,
assetsConfigInput[i].emissionPerSecond
);
}
}
/**
* @dev Updates the state of one distribution, mainly rewards index and timestamp
* @param underlyingAsset The address used as key in the distribution, for example sAAVE or the aTokens addresses on Aave
* @param assetConfig Storage pointer to the distribution's config
* @param totalStaked Current total of staked assets for this distribution
* @return The new distribution index
**/
function _updateAssetStateInternal(
address underlyingAsset,
AssetData storage assetConfig,
uint256 totalStaked
) internal returns (uint256) {
uint256 oldIndex = assetConfig.index;
uint128 lastUpdateTimestamp = assetConfig.lastUpdateTimestamp;
if (block.timestamp == lastUpdateTimestamp) {
return oldIndex;
}
uint256 newIndex =
_getAssetIndex(oldIndex, assetConfig.emissionPerSecond, lastUpdateTimestamp, totalStaked);
if (newIndex != oldIndex) {
assetConfig.index = newIndex;
emit AssetIndexUpdated(underlyingAsset, newIndex);
}
assetConfig.lastUpdateTimestamp = uint128(block.timestamp);
return newIndex;
}
/**
* @dev Updates the state of an user in a distribution
* @param user The user's address
* @param asset The address of the reference asset of the distribution
* @param stakedByUser Amount of tokens staked by the user in the distribution at the moment
* @param totalStaked Total tokens staked in the distribution
* @return The accrued rewards for the user until the moment
**/
function _updateUserAssetInternal(
address user,
address asset,
uint256 stakedByUser,
uint256 totalStaked
) internal returns (uint256) {
AssetData storage assetData = assets[asset];
uint256 userIndex = assetData.users[user];
uint256 accruedRewards = 0;
uint256 newIndex = _updateAssetStateInternal(asset, assetData, totalStaked);
if (userIndex != newIndex) {
if (stakedByUser != 0) {
accruedRewards = _getRewards(stakedByUser, newIndex, userIndex);
}
assetData.users[user] = newIndex;
emit UserIndexUpdated(user, asset, newIndex);
}
return accruedRewards;
}
/**
* @dev Used by "frontend" stake contracts to update the data of an user when claiming rewards from there
* @param user The address of the user
* @param stakes List of structs of the user data related with his stake
* @return The accrued rewards for the user until the moment
**/
function _claimRewards(address user, DistributionTypes.UserStakeInput[] memory stakes)
internal
returns (uint256)
{
uint256 accruedRewards = 0;
for (uint256 i = 0; i < stakes.length; i++) {
accruedRewards = accruedRewards.add(
_updateUserAssetInternal(
user,
stakes[i].underlyingAsset,
stakes[i].stakedByUser,
stakes[i].totalStaked
)
);
}
return accruedRewards;
}
/**
* @dev Return the accrued rewards for an user over a list of distribution
* @param user The address of the user
* @param stakes List of structs of the user data related with his stake
* @return The accrued rewards for the user until the moment
**/
function _getUnclaimedRewards(address user, DistributionTypes.UserStakeInput[] memory stakes)
internal
view
returns (uint256)
{
uint256 accruedRewards = 0;
for (uint256 i = 0; i < stakes.length; i++) {
AssetData storage assetConfig = assets[stakes[i].underlyingAsset];
uint256 assetIndex =
_getAssetIndex(
assetConfig.index,
assetConfig.emissionPerSecond,
assetConfig.lastUpdateTimestamp,
stakes[i].totalStaked
);
accruedRewards = accruedRewards.add(
_getRewards(stakes[i].stakedByUser, assetIndex, assetConfig.users[user])
);
}
return accruedRewards;
}
/**
* @dev Internal function for the calculation of user's rewards on a distribution
* @param principalUserBalance Amount staked by the user on a distribution
* @param reserveIndex Current index of the distribution
* @param userIndex Index stored for the user, representation his staking moment
* @return The rewards
**/
function _getRewards(
uint256 principalUserBalance,
uint256 reserveIndex,
uint256 userIndex
) internal pure returns (uint256) {
return principalUserBalance.mul(reserveIndex.sub(userIndex)).div(10**uint256(PRECISION));
}
/**
* @dev Calculates the next value of an specific distribution index, with validations
* @param currentIndex Current index of the distribution
* @param emissionPerSecond Representing the total rewards distributed per second per asset unit, on the distribution
* @param lastUpdateTimestamp Last moment this distribution was updated
* @param totalBalance of tokens considered for the distribution
* @return The new index.
**/
function _getAssetIndex(
uint256 currentIndex,
uint256 emissionPerSecond,
uint128 lastUpdateTimestamp,
uint256 totalBalance
) internal view returns (uint256) {
if (
emissionPerSecond == 0 ||
totalBalance == 0 ||
lastUpdateTimestamp == block.timestamp ||
lastUpdateTimestamp >= DISTRIBUTION_END
) {
return currentIndex;
}
uint256 currentTimestamp =
block.timestamp > DISTRIBUTION_END ? DISTRIBUTION_END : block.timestamp;
uint256 timeDelta = currentTimestamp.sub(lastUpdateTimestamp);
return
emissionPerSecond.mul(timeDelta).mul(10**uint256(PRECISION)).div(totalBalance).add(
currentIndex
);
}
/**
* @dev Returns the data of an user on a distribution
* @param user Address of the user
* @param asset The address of the reference asset of the distribution
* @return The new index
**/
function getUserAssetData(address user, address asset) public view returns (uint256) {
return assets[asset].users[user];
}
}