-
Notifications
You must be signed in to change notification settings - Fork 587
/
Copy pathIncentivizedERC20.sol
235 lines (208 loc) · 7.82 KB
/
IncentivizedERC20.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;
import {Context} from '../../../dependencies/openzeppelin/contracts/Context.sol';
import {IERC20} from '../../../dependencies/openzeppelin/contracts/IERC20.sol';
import {IERC20Detailed} from '../../../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
import {SafeCast} from '../../../dependencies/openzeppelin/contracts/SafeCast.sol';
import {WadRayMath} from '../../libraries/math/WadRayMath.sol';
import {Errors} from '../../libraries/helpers/Errors.sol';
import {IAaveIncentivesController} from '../../../interfaces/IAaveIncentivesController.sol';
import {IPoolAddressesProvider} from '../../../interfaces/IPoolAddressesProvider.sol';
import {IPool} from '../../../interfaces/IPool.sol';
import {IACLManager} from '../../../interfaces/IACLManager.sol';
/**
* @title IncentivizedERC20
* @author Aave, inspired by the Openzeppelin ERC20 implementation
* @notice Basic ERC20 implementation
*/
abstract contract IncentivizedERC20 is Context, IERC20Detailed {
using WadRayMath for uint256;
using SafeCast for uint256;
/**
* @dev Only pool admin can call functions marked by this modifier.
*/
modifier onlyPoolAdmin() {
IACLManager aclManager = IACLManager(_addressesProvider.getACLManager());
require(aclManager.isPoolAdmin(msg.sender), Errors.CALLER_NOT_POOL_ADMIN);
_;
}
/**
* @dev Only pool can call functions marked by this modifier.
*/
modifier onlyPool() {
require(_msgSender() == address(POOL), Errors.CALLER_MUST_BE_POOL);
_;
}
/**
* @dev UserState - additionalData is a flexible field.
* ATokens and VariableDebtTokens use this field store the index of the
* user's last supply/withdrawal/borrow/repayment. StableDebtTokens use
* this field to store the user's stable rate.
*/
struct UserState {
uint128 balance;
uint128 additionalData;
}
// Map of users address and their state data (userAddress => userStateData)
mapping(address => UserState) internal _userState;
// Map of allowances (delegator => delegatee => allowanceAmount)
mapping(address => mapping(address => uint256)) private _allowances;
uint256 internal _totalSupply;
string private _name;
string private _symbol;
uint8 private _decimals;
IAaveIncentivesController internal _incentivesController;
IPoolAddressesProvider internal immutable _addressesProvider;
IPool public immutable POOL;
/**
* @dev Constructor.
* @param pool The reference to the main Pool contract
* @param name The name of the token
* @param symbol The symbol of the token
* @param decimals The number of decimals of the token
*/
constructor(IPool pool, string memory name, string memory symbol, uint8 decimals) {
_addressesProvider = pool.ADDRESSES_PROVIDER();
_name = name;
_symbol = symbol;
_decimals = decimals;
POOL = pool;
}
/// @inheritdoc IERC20Detailed
function name() public view override returns (string memory) {
return _name;
}
/// @inheritdoc IERC20Detailed
function symbol() external view override returns (string memory) {
return _symbol;
}
/// @inheritdoc IERC20Detailed
function decimals() external view override returns (uint8) {
return _decimals;
}
/// @inheritdoc IERC20
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
/// @inheritdoc IERC20
function balanceOf(address account) public view virtual override returns (uint256) {
return _userState[account].balance;
}
/**
* @notice Returns the address of the Incentives Controller contract
* @return The address of the Incentives Controller
*/
function getIncentivesController() external view virtual returns (IAaveIncentivesController) {
return _incentivesController;
}
/**
* @notice Sets a new Incentives Controller
* @param controller the new Incentives controller
*/
function setIncentivesController(IAaveIncentivesController controller) external onlyPoolAdmin {
_incentivesController = controller;
}
/// @inheritdoc IERC20
function transfer(address recipient, uint256 amount) external virtual override returns (bool) {
uint128 castAmount = amount.toUint128();
_transfer(_msgSender(), recipient, castAmount);
return true;
}
/// @inheritdoc IERC20
function allowance(
address owner,
address spender
) external view virtual override returns (uint256) {
return _allowances[owner][spender];
}
/// @inheritdoc IERC20
function approve(address spender, uint256 amount) external virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
/// @inheritdoc IERC20
function transferFrom(
address sender,
address recipient,
uint256 amount
) external virtual override returns (bool) {
uint128 castAmount = amount.toUint128();
_approve(sender, _msgSender(), _allowances[sender][_msgSender()] - castAmount);
_transfer(sender, recipient, castAmount);
return true;
}
/**
* @notice Increases the allowance of spender to spend _msgSender() tokens
* @param spender The user allowed to spend on behalf of _msgSender()
* @param addedValue The amount being added to the allowance
* @return `true`
*/
function increaseAllowance(address spender, uint256 addedValue) external virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
return true;
}
/**
* @notice Decreases the allowance of spender to spend _msgSender() tokens
* @param spender The user allowed to spend on behalf of _msgSender()
* @param subtractedValue The amount being subtracted to the allowance
* @return `true`
*/
function decreaseAllowance(
address spender,
uint256 subtractedValue
) external virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] - subtractedValue);
return true;
}
/**
* @notice Transfers tokens between two users and apply incentives if defined.
* @param sender The source address
* @param recipient The destination address
* @param amount The amount getting transferred
*/
function _transfer(address sender, address recipient, uint128 amount) internal virtual {
uint128 oldSenderBalance = _userState[sender].balance;
_userState[sender].balance = oldSenderBalance - amount;
uint128 oldRecipientBalance = _userState[recipient].balance;
_userState[recipient].balance = oldRecipientBalance + amount;
IAaveIncentivesController incentivesControllerLocal = _incentivesController;
if (address(incentivesControllerLocal) != address(0)) {
uint256 currentTotalSupply = _totalSupply;
incentivesControllerLocal.handleAction(sender, currentTotalSupply, oldSenderBalance);
if (sender != recipient) {
incentivesControllerLocal.handleAction(recipient, currentTotalSupply, oldRecipientBalance);
}
}
}
/**
* @notice Approve `spender` to use `amount` of `owner`s balance
* @param owner The address owning the tokens
* @param spender The address approved for spending
* @param amount The amount of tokens to approve spending of
*/
function _approve(address owner, address spender, uint256 amount) internal virtual {
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @notice Update the name of the token
* @param newName The new name for the token
*/
function _setName(string memory newName) internal {
_name = newName;
}
/**
* @notice Update the symbol for the token
* @param newSymbol The new symbol for the token
*/
function _setSymbol(string memory newSymbol) internal {
_symbol = newSymbol;
}
/**
* @notice Update the number of decimals for the token
* @param newDecimals The new number of decimals for the token
*/
function _setDecimals(uint8 newDecimals) internal {
_decimals = newDecimals;
}
}