mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
Initial commit
This commit is contained in:
parent
23f99d30f0
commit
3df87a8e5d
|
|
@ -182,4 +182,29 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
|
||||||
|
|
||||||
emit BurnDebt(user, amount, previousBalance, currentBalance, balanceIncrease);
|
emit BurnDebt(user, amount, previousBalance, currentBalance, balanceIncrease);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Calculates the increase in balance since the last user interaction
|
||||||
|
* @param user The address of the user for which the interest is being accumulated
|
||||||
|
* @return The previous principal balance, the new principal balance, the balance increase
|
||||||
|
* and the new user index
|
||||||
|
**/
|
||||||
|
function _calculateBalanceIncrease(address user) internal view returns (uint256, uint256, uint256) {
|
||||||
|
uint256 previousPrincipalBalance = principalBalanceOf(user);
|
||||||
|
|
||||||
|
if (previousPrincipalBalance == 0) {
|
||||||
|
return (0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculation of the accrued interest since the last accumulation
|
||||||
|
uint256 balanceIncrease = balanceOf(user).sub(previousPrincipalBalance);
|
||||||
|
|
||||||
|
return (
|
||||||
|
previousPrincipalBalance,
|
||||||
|
previousPrincipalBalance.add(balanceIncrease),
|
||||||
|
balanceIncrease
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,27 +38,15 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
|
||||||
* @return the debt balance of the user
|
* @return the debt balance of the user
|
||||||
**/
|
**/
|
||||||
function balanceOf(address user) public virtual override view returns (uint256) {
|
function balanceOf(address user) public virtual override view returns (uint256) {
|
||||||
uint256 userBalance = principalBalanceOf(user);
|
uint256 scaledBalance = principalBalanceOf(user);
|
||||||
uint256 index = _usersData[user];
|
|
||||||
if (userBalance == 0) {
|
if (scaledBalance == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
userBalance
|
scaledBalance
|
||||||
.wadToRay()
|
.rayMul(POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET));
|
||||||
.rayMul(POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET))
|
|
||||||
.rayDiv(index)
|
|
||||||
.rayToWad();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev returns the index of the last user action
|
|
||||||
* @return the user index
|
|
||||||
**/
|
|
||||||
|
|
||||||
function getUserIndex(address user) external virtual override view returns (uint256) {
|
|
||||||
return _usersData[user];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -67,19 +55,11 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
|
||||||
* @param amount the amount of debt being minted
|
* @param amount the amount of debt being minted
|
||||||
**/
|
**/
|
||||||
function mint(address user, uint256 amount) external override onlyLendingPool {
|
function mint(address user, uint256 amount) external override onlyLendingPool {
|
||||||
(
|
|
||||||
uint256 previousBalance,
|
|
||||||
uint256 currentBalance,
|
|
||||||
uint256 balanceIncrease
|
|
||||||
) = _calculateBalanceIncrease(user);
|
|
||||||
|
|
||||||
_mint(user, amount.add(balanceIncrease));
|
uint256 index = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET);
|
||||||
|
|
||||||
uint256 newUserIndex = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET);
|
_mint(user, amount.rayDiv(index));
|
||||||
require(newUserIndex < (1 << 128), "Debt token: Index overflow");
|
emit MintDebt(user, amount, index);
|
||||||
_usersData[user] = newUserIndex;
|
|
||||||
|
|
||||||
emit MintDebt(user, amount, previousBalance, currentBalance, balanceIncrease, newUserIndex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -88,26 +68,10 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
|
||||||
* @param amount the amount of debt being burned
|
* @param amount the amount of debt being burned
|
||||||
**/
|
**/
|
||||||
function burn(address user, uint256 amount) external override onlyLendingPool {
|
function burn(address user, uint256 amount) external override onlyLendingPool {
|
||||||
(
|
|
||||||
uint256 previousBalance,
|
|
||||||
uint256 currentBalance,
|
|
||||||
uint256 balanceIncrease
|
|
||||||
) = _calculateBalanceIncrease(user);
|
|
||||||
|
|
||||||
if (balanceIncrease > amount) {
|
uint256 index = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET);
|
||||||
_mint(user, balanceIncrease.sub(amount));
|
_burn(user, amount.rayDiv(index));
|
||||||
} else {
|
|
||||||
_burn(user, amount.sub(balanceIncrease));
|
|
||||||
}
|
|
||||||
|
|
||||||
uint256 newUserIndex = 0;
|
emit BurnDebt(user, amount, index);
|
||||||
//if user not repaid everything
|
|
||||||
if (currentBalance != amount) {
|
|
||||||
newUserIndex = POOL.getReserveNormalizedVariableDebt(UNDERLYING_ASSET);
|
|
||||||
require(newUserIndex < (1 << 128), "Debt token: Index overflow");
|
|
||||||
}
|
|
||||||
_usersData[user] = newUserIndex;
|
|
||||||
|
|
||||||
emit BurnDebt(user, amount, previousBalance, currentBalance, balanceIncrease, newUserIndex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -104,27 +104,4 @@ abstract contract DebtTokenBase is ERC20, VersionedInitializable {
|
||||||
spender; subtractedValue;
|
spender; subtractedValue;
|
||||||
revert('ALLOWANCE_NOT_SUPPORTED');
|
revert('ALLOWANCE_NOT_SUPPORTED');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Calculates the increase in balance since the last user interaction
|
|
||||||
* @param user The address of the user for which the interest is being accumulated
|
|
||||||
* @return The previous principal balance, the new principal balance, the balance increase
|
|
||||||
* and the new user index
|
|
||||||
**/
|
|
||||||
function _calculateBalanceIncrease(address user) internal view returns (uint256, uint256, uint256) {
|
|
||||||
uint256 previousPrincipalBalance = principalBalanceOf(user);
|
|
||||||
|
|
||||||
if (previousPrincipalBalance == 0) {
|
|
||||||
return (0, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculation of the accrued interest since the last accumulation
|
|
||||||
uint256 balanceIncrease = balanceOf(user).sub(previousPrincipalBalance);
|
|
||||||
|
|
||||||
return (
|
|
||||||
previousPrincipalBalance,
|
|
||||||
previousPrincipalBalance.add(balanceIncrease),
|
|
||||||
balanceIncrease
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,17 +12,11 @@ interface IVariableDebtToken {
|
||||||
* @dev emitted when new variable debt is minted
|
* @dev emitted when new variable debt is minted
|
||||||
* @param user the user receiving the debt
|
* @param user the user receiving the debt
|
||||||
* @param amount the amount of debt being minted
|
* @param amount the amount of debt being minted
|
||||||
* @param previousBalance the previous balance of the user
|
|
||||||
* @param currentBalance the current balance of the user
|
|
||||||
* @param balanceIncrease the debt accumulated since the last action
|
|
||||||
* @param index the index of the user
|
* @param index the index of the user
|
||||||
**/
|
**/
|
||||||
event MintDebt(
|
event MintDebt(
|
||||||
address user,
|
address user,
|
||||||
uint256 amount,
|
uint256 amount,
|
||||||
uint256 previousBalance,
|
|
||||||
uint256 currentBalance,
|
|
||||||
uint256 balanceIncrease,
|
|
||||||
uint256 index
|
uint256 index
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -30,17 +24,11 @@ interface IVariableDebtToken {
|
||||||
* @dev emitted when variable debt is burnt
|
* @dev emitted when variable debt is burnt
|
||||||
* @param user the user which debt has been burned
|
* @param user the user which debt has been burned
|
||||||
* @param amount the amount of debt being burned
|
* @param amount the amount of debt being burned
|
||||||
* @param previousBalance the previous balance of the user
|
|
||||||
* @param currentBalance the current balance of the user
|
|
||||||
* @param balanceIncrease the debt accumulated since the last action
|
|
||||||
* @param index the index of the user
|
* @param index the index of the user
|
||||||
**/
|
**/
|
||||||
event BurnDebt(
|
event BurnDebt(
|
||||||
address user,
|
address user,
|
||||||
uint256 amount,
|
uint256 amount,
|
||||||
uint256 previousBalance,
|
|
||||||
uint256 currentBalance,
|
|
||||||
uint256 balanceIncrease,
|
|
||||||
uint256 index
|
uint256 index
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user