-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathERC20AaveLMUpgradeable.sol
309 lines (271 loc) · 11.2 KB
/
ERC20AaveLMUpgradeable.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.17;
import {ERC20Upgradeable} from 'openzeppelin-contracts-upgradeable/contracts/token/ERC20/ERC20Upgradeable.sol';
import {IERC20} from 'openzeppelin-contracts/contracts/interfaces/IERC20.sol';
import {SafeERC20} from 'openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol';
import {SafeCast} from 'solidity-utils/contracts/oz-common/SafeCast.sol';
import {IRewardsController} from '../../rewards/interfaces/IRewardsController.sol';
import {IERC20AaveLM} from './interfaces/IERC20AaveLM.sol';
/**
* @title ERC20AaveLMUpgradeable.sol
* @notice Wrapper smart contract that supports tracking and claiming liquidity mining rewards from the Aave system
* @dev ERC20 extension, so ERC20 initialization should be done by the children contract/s
* @author BGD labs
*/
abstract contract ERC20AaveLMUpgradeable is ERC20Upgradeable, IERC20AaveLM {
using SafeCast for uint256;
/// @custom:storage-location erc7201:aave-dao.storage.ERC20AaveLM
struct ERC20AaveLMStorage {
address _referenceAsset; // a/v token to track rewards on INCENTIVES_CONTROLLER
address[] _rewardTokens;
mapping(address reward => RewardIndexCache cache) _startIndex;
mapping(address user => mapping(address reward => UserRewardsData cache)) _userRewardsData;
}
// keccak256(abi.encode(uint256(keccak256("aave-dao.storage.ERC20AaveLM")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant ERC20AaveLMStorageLocation =
0x4fad66563f105be0bff96185c9058c4934b504d3ba15ca31e86294f0b01fd200;
function _getERC20AaveLMStorage() private pure returns (ERC20AaveLMStorage storage $) {
assembly {
$.slot := ERC20AaveLMStorageLocation
}
}
IRewardsController public immutable INCENTIVES_CONTROLLER;
constructor(IRewardsController rewardsController) {
if (address(rewardsController) == address(0)) {
revert ZeroIncentivesControllerIsForbidden();
}
INCENTIVES_CONTROLLER = rewardsController;
}
function __ERC20AaveLM_init(address referenceAsset_) internal onlyInitializing {
__ERC20AaveLM_init_unchained(referenceAsset_);
}
function __ERC20AaveLM_init_unchained(address referenceAsset_) internal onlyInitializing {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
$._referenceAsset = referenceAsset_;
if (INCENTIVES_CONTROLLER != IRewardsController(address(0))) {
refreshRewardTokens();
}
}
///@inheritdoc IERC20AaveLM
function claimRewardsOnBehalf(
address onBehalfOf,
address receiver,
address[] memory rewards
) external {
address msgSender = _msgSender();
if (msgSender != onBehalfOf && msgSender != INCENTIVES_CONTROLLER.getClaimer(onBehalfOf)) {
revert InvalidClaimer(msgSender);
}
_claimRewardsOnBehalf(onBehalfOf, receiver, rewards);
}
///@inheritdoc IERC20AaveLM
function claimRewards(address receiver, address[] memory rewards) external {
_claimRewardsOnBehalf(_msgSender(), receiver, rewards);
}
///@inheritdoc IERC20AaveLM
function claimRewardsToSelf(address[] memory rewards) external {
_claimRewardsOnBehalf(_msgSender(), _msgSender(), rewards);
}
///@inheritdoc IERC20AaveLM
function refreshRewardTokens() public override {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
address[] memory rewards = INCENTIVES_CONTROLLER.getRewardsByAsset($._referenceAsset);
for (uint256 i = 0; i < rewards.length; i++) {
_registerRewardToken(rewards[i]);
}
}
///@inheritdoc IERC20AaveLM
function collectAndUpdateRewards(address reward) public returns (uint256) {
if (reward == address(0)) {
return 0;
}
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
address[] memory assets = new address[](1);
assets[0] = address($._referenceAsset);
return INCENTIVES_CONTROLLER.claimRewards(assets, type(uint256).max, address(this), reward);
}
///@inheritdoc IERC20AaveLM
function isRegisteredRewardToken(address reward) public view override returns (bool) {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
return $._startIndex[reward].isRegistered;
}
///@inheritdoc IERC20AaveLM
function getCurrentRewardsIndex(address reward) public view returns (uint256) {
if (address(reward) == address(0)) {
return 0;
}
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
(, uint256 nextIndex) = INCENTIVES_CONTROLLER.getAssetIndex($._referenceAsset, reward);
return nextIndex;
}
///@inheritdoc IERC20AaveLM
function getTotalClaimableRewards(address reward) external view returns (uint256) {
if (reward == address(0)) {
return 0;
}
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
address[] memory assets = new address[](1);
assets[0] = $._referenceAsset;
uint256 freshRewards = INCENTIVES_CONTROLLER.getUserRewards(assets, address(this), reward);
return IERC20(reward).balanceOf(address(this)) + freshRewards;
}
///@inheritdoc IERC20AaveLM
function getClaimableRewards(address user, address reward) external view returns (uint256) {
return _getClaimableRewards(user, reward, balanceOf(user), getCurrentRewardsIndex(reward));
}
///@inheritdoc IERC20AaveLM
function getUnclaimedRewards(address user, address reward) external view returns (uint256) {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
return $._userRewardsData[user][reward].unclaimedRewards;
}
///@inheritdoc IERC20AaveLM
function getReferenceAsset() external view returns (address) {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
return $._referenceAsset;
}
///@inheritdoc IERC20AaveLM
function rewardTokens() external view returns (address[] memory) {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
return $._rewardTokens;
}
/**
* @notice Updates rewards for senders and receiver in a transfer (not updating rewards for address(0))
* @param from The address of the sender of tokens
* @param to The address of the receiver of tokens
*/
function _update(address from, address to, uint256 amount) internal virtual override {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
for (uint256 i = 0; i < $._rewardTokens.length; i++) {
address rewardToken = address($._rewardTokens[i]);
uint256 rewardsIndex = getCurrentRewardsIndex(rewardToken);
if (from != address(0)) {
_updateUser(from, rewardsIndex, rewardToken);
}
if (to != address(0) && from != to) {
_updateUser(to, rewardsIndex, rewardToken);
}
}
super._update(from, to, amount);
}
/**
* @notice Adding the pending rewards to the unclaimed for specific user and updating user index
* @param user The address of the user to update
* @param currentRewardsIndex The current rewardIndex
* @param rewardToken The address of the reward token
*/
function _updateUser(address user, uint256 currentRewardsIndex, address rewardToken) internal {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
uint256 balance = balanceOf(user);
if (balance > 0) {
$._userRewardsData[user][rewardToken].unclaimedRewards = _getClaimableRewards(
user,
rewardToken,
balance,
currentRewardsIndex
).toUint128();
}
$._userRewardsData[user][rewardToken].rewardsIndexOnLastInteraction = currentRewardsIndex
.toUint128();
}
/**
* @notice Compute the pending in asset decimals. Pending is the amount to add (not yet unclaimed) rewards in asset decimals.
* @param balance The balance of the user
* @param rewardsIndexOnLastInteraction The index which was on the last interaction of the user
* @param currentRewardsIndex The current rewards index in the system
* @return The amount of pending rewards in asset decimals
*/
function _getPendingRewards(
uint256 balance,
uint256 rewardsIndexOnLastInteraction,
uint256 currentRewardsIndex
) internal view returns (uint256) {
if (balance == 0) {
return 0;
}
return (balance * (currentRewardsIndex - rewardsIndexOnLastInteraction)) / 10 ** decimals();
}
/**
* @notice Compute the claimable rewards for a user
* @param user The address of the user
* @param reward The address of the reward
* @param balance The balance of the user in asset decimals
* @param currentRewardsIndex The current rewards index
* @return The total rewards that can be claimed by the user (if `fresh` flag true, after updating rewards)
*/
function _getClaimableRewards(
address user,
address reward,
uint256 balance,
uint256 currentRewardsIndex
) internal view returns (uint256) {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
RewardIndexCache memory rewardsIndexCache = $._startIndex[reward];
if (!rewardsIndexCache.isRegistered) {
revert RewardNotInitialized(reward);
}
UserRewardsData memory currentUserRewardsData = $._userRewardsData[user][reward];
return
currentUserRewardsData.unclaimedRewards +
_getPendingRewards(
balance,
currentUserRewardsData.rewardsIndexOnLastInteraction == 0
? rewardsIndexCache.lastUpdatedIndex
: currentUserRewardsData.rewardsIndexOnLastInteraction,
currentRewardsIndex
);
}
/**
* @notice Claim rewards on behalf of a user and send them to a receiver
* @param onBehalfOf The address to claim on behalf of
* @param rewards The addresses of the rewards
* @param receiver The address to receive the rewards
*/
function _claimRewardsOnBehalf(
address onBehalfOf,
address receiver,
address[] memory rewards
) internal virtual {
for (uint256 i = 0; i < rewards.length; i++) {
if (address(rewards[i]) == address(0)) {
continue;
}
uint256 currentRewardsIndex = getCurrentRewardsIndex(rewards[i]);
uint256 balance = balanceOf(onBehalfOf);
uint256 userReward = _getClaimableRewards(
onBehalfOf,
rewards[i],
balance,
currentRewardsIndex
);
uint256 totalRewardTokenBalance = IERC20(rewards[i]).balanceOf(address(this));
uint256 unclaimedReward = 0;
if (userReward > totalRewardTokenBalance) {
totalRewardTokenBalance += collectAndUpdateRewards(address(rewards[i]));
}
if (userReward > totalRewardTokenBalance) {
unclaimedReward = userReward - totalRewardTokenBalance;
userReward = totalRewardTokenBalance;
}
if (userReward > 0) {
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
$._userRewardsData[onBehalfOf][rewards[i]].unclaimedRewards = unclaimedReward.toUint128();
$
._userRewardsData[onBehalfOf][rewards[i]]
.rewardsIndexOnLastInteraction = currentRewardsIndex.toUint128();
SafeERC20.safeTransfer(IERC20(rewards[i]), receiver, userReward);
}
}
}
/**
* @notice Initializes a new rewardToken
* @param reward The reward token to be registered
*/
function _registerRewardToken(address reward) internal {
if (isRegisteredRewardToken(reward)) return;
uint256 startIndex = getCurrentRewardsIndex(reward);
ERC20AaveLMStorage storage $ = _getERC20AaveLMStorage();
$._rewardTokens.push(reward);
$._startIndex[reward] = RewardIndexCache(true, startIndex.toUint248());
emit RewardTokenRegistered(reward, startIndex);
}
}