/
AToken.sol
532 lines (475 loc) · 17.2 KB
/
AToken.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.8.19;
import {IERC20} from "../../dependencies/openzeppelin/contracts/IERC20.sol";
import {IERC20Detailed} from "../../dependencies/openzeppelin/contracts/IERC20Detailed.sol";
import {SafeERC20} from "../../dependencies/openzeppelin/contracts/SafeERC20.sol";
import {ILendingPool} from "../../interfaces/ILendingPool.sol";
import {IAToken} from "../../interfaces/IAToken.sol";
import {WadRayMath} from "../libraries/math/WadRayMath.sol";
import {Errors} from "../libraries/helpers/Errors.sol";
import {Helpers} from "../libraries/helpers/Helpers.sol";
import {VersionedInitializable} from "../../dependencies/aave-upgradeability/VersionedInitializable.sol";
import {IncentivizedERC20} from "./IncentivizedERC20.sol";
import {IIncentivesController} from "../../interfaces/IIncentivesController.sol";
import {SafeMath} from "../../dependencies/openzeppelin/contracts/SafeMath.sol";
import {ReserveConfiguration} from "../libraries/configuration/ReserveConfiguration.sol";
import {PercentageMath} from "../libraries/math/PercentageMath.sol";
import {ILendingPoolAddressesProvider} from "../../interfaces/ILendingPoolAddressesProvider.sol";
import {ILendingPoolConfigurator} from "../../interfaces/ILendingPoolConfigurator.sol";
import "../../dependencies/openzeppelin/contracts/utils/Strings.sol";
/**
* @title VMEX ERC20 AToken (vToken)
* @dev Implementation of the interest bearing token for the Aave protocol
* @author Aave and VMEX
*/
contract AToken is
VersionedInitializable,
IncentivizedERC20("ATOKEN_IMPL", "ATOKEN_IMPL", 0),
IAToken
{
using WadRayMath for uint256;
using SafeERC20 for IERC20;
using SafeMath for uint256;
using ReserveConfiguration for *;
using PercentageMath for uint256;
using Helpers for address;
bytes public constant EIP712_REVISION = bytes("1");
bytes32 internal constant EIP712_DOMAIN =
keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
);
bytes32 public constant PERMIT_TYPEHASH =
keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
);
uint256 public constant ATOKEN_REVISION = 0x1;
/// @dev owner => next valid nonce to submit with permit()
mapping(address => uint256) public _nonces;
ILendingPoolAddressesProvider public _addressesProvider;
ILendingPool public _pool;
address public _lendingPoolConfigurator;
address public _underlyingAsset;
uint64 public _tranche;
modifier onlyLendingPool() {
require(
_msgSender() == address(_pool),
Errors.CT_CALLER_MUST_BE_LENDING_POOL
);
_;
}
modifier onlyLendingPoolConfigurator() {
require(
_msgSender() == _lendingPoolConfigurator,
Errors.LP_CALLER_NOT_LENDING_POOL_CONFIGURATOR
);
_;
}
function approveIncentivesController() internal {
address incentivesController = address(_getIncentivesController());
if (incentivesController != address(0) &&
IERC20(_underlyingAsset).allowance(address(this), incentivesController) == 0) {
IERC20(_underlyingAsset).approve(incentivesController, type(uint).max);
}
}
function getRevision() internal pure virtual override returns (uint256) {
return ATOKEN_REVISION;
}
function DOMAIN_SEPARATOR() public view returns (bytes32) {
return keccak256(
abi.encode(
EIP712_DOMAIN,
keccak256(bytes("VMEX vToken")),
keccak256(EIP712_REVISION),
block.chainid,
address(this)
)
);
}
/**
* @dev Initializes the aToken
* @param pool The address of the lending pool where this aToken will be used
* @param vars Stores treasury vars to fix stack too deep
*/
function initialize(
ILendingPool pool,
InitializeTreasuryVars memory vars
) external override initializer {
uint8 aTokenDecimals = IERC20Detailed(vars.underlyingAsset).decimals();
string memory aTokenName = string(
abi.encodePacked(
"Vmex interest bearing ",
vars.underlyingAsset.getName(),
Strings.toString(vars.trancheId)
)
);
string memory aTokenSymbol = string(
abi.encodePacked(
"v",
vars.underlyingAsset.getSymbol(),
Strings.toString(vars.trancheId)
)
);
_setName(aTokenName);
_setSymbol(aTokenSymbol);
_setDecimals(aTokenDecimals);
_pool = pool;
_lendingPoolConfigurator = vars.lendingPoolConfigurator;
_addressesProvider = ILendingPoolAddressesProvider(vars.addressesProvider);
_underlyingAsset = vars.underlyingAsset;
_tranche = vars.trancheId;
emit Initialized(
vars.underlyingAsset,
vars.trancheId,
address(pool),
aTokenDecimals,
aTokenName,
aTokenSymbol
);
}
/**
* @dev Burns aTokens from `user` and sends the equivalent amount of underlying to `receiverOfUnderlying`
* - Only callable by the LendingPool, as extra state updates there need to be managed
* @param user The owner of the aTokens, getting them burned
* @param receiverOfUnderlying The address that will receive the underlying
* @param amount The amount being burned
* @param index The new liquidity index of the reserve
**/
function burn(
address user,
address receiverOfUnderlying,
uint256 amount,
uint256 index
) external override onlyLendingPool {
uint256 amountScaled = amount.rayDiv(index);
require(amountScaled != 0, Errors.CT_INVALID_BURN_AMOUNT);
_burn(user, amountScaled);
IERC20(_underlyingAsset).safeTransfer(receiverOfUnderlying, amount);
emit Transfer(user, address(0), amount); // note: this is amount user receives, not amount user requests
emit Burn(user, receiverOfUnderlying, amount, index);
}
/**
* @dev Mints `amount` aTokens to `user`
* - Only callable by the LendingPool, as extra state updates there need to be managed
* @param user The address receiving the minted tokens
* @param amount The amount of tokens being deposited
* @param index The new liquidity index of the reserve
* @return `true` if the the previous balance of the user was 0
*/
function mint(
address user,
uint256 amount,
uint256 index
) external override onlyLendingPool returns (bool) {
approveIncentivesController();
uint256 previousBalance = super.balanceOf(user);
uint256 amountScaled = amount.rayDiv(index);
require(amountScaled != 0, Errors.CT_INVALID_MINT_AMOUNT);
_mint(user, amountScaled);
emit Transfer(address(0), user, amount);
emit Mint(user, amount, index);
return previousBalance == 0;
}
/**
* @dev Mints aTokens to the reserve treasury
* - Only callable by the LendingPool
* @param amount The amount of tokens getting minted
* @param index The new liquidity index of the reserve
*/
function mintToTreasury(uint256 amount, uint256 index)
external
override
onlyLendingPool
{
approveIncentivesController();
if (amount == 0) {
return;
}
address treasury = ILendingPoolConfigurator(_lendingPoolConfigurator).trancheAdminTreasuryAddresses(_tranche);
// Compared to the normal mint, we don't check for rounding errors.
// The amount to mint can easily be very small since it is a fraction of the interest ccrued.
// In that case, the treasury will experience a (very small) loss, but it
// wont cause potentially valid transactions to fail.
_mint(treasury, amount.rayDiv(index));
emit Transfer(address(0), treasury, amount);
emit Mint(treasury, amount, index);
}
/**
* @dev Mints aTokens to the reserve treasury
* - Only callable by the LendingPool
* @param amount The amount of tokens getting minted
* @param index The new liquidity index of the reserve
*/
function mintToVMEXTreasury(uint256 amount, uint256 index)
external
override
onlyLendingPool
{
approveIncentivesController();
if (amount == 0) {
return;
}
address treasury = _addressesProvider.getVMEXTreasury();
// Compared to the normal mint, we don't check for rounding errors.
// The amount to mint can easily be very small since it is a fraction of the interest ccrued.
// In that case, the treasury will experience a (very small) loss, but it
// wont cause potentially valid transactions to fail.
_mint(treasury, amount.rayDiv(index));
emit Transfer(address(0), treasury, amount);
emit Mint(treasury, amount, index);
}
/**
* @dev Transfers aTokens in the event of a borrow being liquidated, in case the liquidators reclaims the aToken
* - Only callable by the LendingPool
* @param from The address getting liquidated, current owner of the aTokens
* @param to The recipient
* @param value The amount of tokens getting transferred
**/
function transferOnLiquidation(
address from,
address to,
uint256 value
) external override onlyLendingPool {
// Being a normal transfer, the Transfer() and BalanceTransfer() are emitted
// so no need to emit a specific event here
_transfer(from, to, value, false);
emit Transfer(from, to, value);
}
/**
* @dev Calculates the balance of the user: principal balance + interest generated by the principal
* @param user The user whose balance is calculated
* @return The balance of the user
**/
function balanceOf(address user)
public
view
override(IncentivizedERC20, IERC20)
returns (uint256)
{
return
super.balanceOf(user).rayMul(
_pool.getReserveNormalizedIncome(_underlyingAsset, _tranche)
);
}
/**
* @dev Returns the scaled balance of the user. The scaled balance is the sum of all the
* updated stored balance divided by the reserve's liquidity index at the moment of the update
* @param user The user whose balance is calculated
* @return The scaled balance of the user
**/
function scaledBalanceOf(address user)
external
view
override
returns (uint256)
{
return super.balanceOf(user);
}
/**
* @dev Returns the scaled balance of the user and the scaled total supply.
* @param user The address of the user
* @return The scaled balance of the user
* @return The scaled balance and the scaled total supply
**/
function getScaledUserBalanceAndSupply(address user)
external
view
override
returns (uint256, uint256)
{
return (super.balanceOf(user), super.totalSupply());
}
/**
* @dev calculates the total supply of the specific aToken
* since the balance of every single user increases over time, the total supply
* does that too.
* @return the current total supply
**/
function totalSupply()
public
view
override(IncentivizedERC20, IERC20)
returns (uint256)
{
uint256 currentSupplyScaled = super.totalSupply();
if (currentSupplyScaled == 0) {
return 0;
}
return
currentSupplyScaled.rayMul(
_pool.getReserveNormalizedIncome(_underlyingAsset, _tranche)
);
}
/**
* @dev Returns the scaled total supply of the variable debt token. Represents sum(debt/index)
* @return the scaled total supply
**/
function scaledTotalSupply()
public
view
virtual
override
returns (uint256)
{
return super.totalSupply();
}
/**
* @dev Returns the address of the underlying asset of this aToken (E.g. WETH for aWETH)
**/
function UNDERLYING_ASSET_ADDRESS() public view override returns (address) {
return _underlyingAsset;
}
/**
* @dev Returns the address of the lending pool where this aToken is used
**/
function POOL() public view returns (ILendingPool) {
return _pool;
}
/**
* @dev For internal usage in the logic of the parent contract IncentivizedERC20
**/
function _getIncentivesController()
internal
view
override
returns (IIncentivesController)
{
return IIncentivesController(_addressesProvider.getIncentivesController());
}
/**
* @dev Returns the address of the incentives controller contract
**/
function getIncentivesController()
external
view
override
returns (IIncentivesController)
{
return _getIncentivesController();
}
/**
* @dev Transfers the underlying asset to `target`. Used by the LendingPool to transfer
* assets in borrow() without burning atokens
* @param target The recipient of the aTokens
* @param amount The amount getting transferred
* @return The amount transferred
**/
function transferUnderlyingTo(address target, uint256 amount)
external
override
onlyLendingPool
returns (uint256)
{
IERC20(_underlyingAsset).safeTransfer(target, amount);
return amount;
}
/**
* @dev Invoked to execute actions on the aToken side after a repayment.
* @param user The user executing the repayment
* @param amount The amount getting repaid
**/
function handleRepayment(address user, uint256 amount)
external
override
onlyLendingPool
{}
/**
* @dev implements the permit function as for
* https://github.com/ethereum/EIPs/blob/8a34d644aacf0f9f8f00815307fd7dd5da07655f/EIPS/eip-2612.md
* @param owner The owner of the funds
* @param spender The spender
* @param value The amount
* @param deadline The deadline timestamp, type(uint256).max for max deadline
* @param v Signature param
* @param s Signature param
* @param r Signature param
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external {
require(owner != address(0), "INVALID_OWNER");
//solium-disable-next-line
require(block.timestamp <= deadline, "INVALID_EXPIRATION");
uint256 currentValidNonce = _nonces[owner];
bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
PERMIT_TYPEHASH,
owner,
spender,
value,
currentValidNonce,
deadline
)
)
)
);
require(owner == ecrecover(digest, v, r, s), "INVALID_SIGNATURE");
_nonces[owner] = currentValidNonce.add(1);
_approve(owner, spender, value);
}
/**
* @dev Transfers the aTokens between two users. Validates the transfer
* (ie checks for valid HF after the transfer) if required
* @param from The source address
* @param to The destination address
* @param amount The amount getting transferred
* @param validate `true` if the transfer needs to be validated
**/
function _transfer(
address from,
address to,
uint256 amount,
bool validate
) internal {
address underlyingAsset = _underlyingAsset;
ILendingPool pool = _pool;
uint256 index = pool.getReserveNormalizedIncome(
underlyingAsset,
_tranche
);
uint256 fromBalanceBefore = super.balanceOf(from).rayMul(index);
uint256 toBalanceBefore = super.balanceOf(to).rayMul(index);
super._transfer(from, to, amount.rayDiv(index));
if (validate) {
pool.finalizeTransfer(
underlyingAsset,
_tranche,
from,
to,
amount,
fromBalanceBefore,
toBalanceBefore
);
}
emit BalanceTransfer(from, to, amount, index);
}
/**
* @dev Overrides the parent _transfer to force validated transfer() and transferFrom()
* @param from The source address
* @param to The destination address
* @param amount The amount getting transferred
**/
function _transfer(
address from,
address to,
uint256 amount
) internal override {
_transfer(from, to, amount, true);
}
function getStakedAmount() external view override returns (uint256) {
IIncentivesController ic = _getIncentivesController();
if (address(ic) == address(0)) {
return 0;
}
return ic.totalStaked();
}
}