Skip to content

Commit

Permalink
Merge pull request #210 from aave/feat/fix-token-events
Browse files Browse the repository at this point in the history
fix: inconsistent token events
  • Loading branch information
The-3D authored Dec 3, 2021
2 parents fd308d0 + 78a6198 commit 8e236ec
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 130 deletions.
18 changes: 14 additions & 4 deletions contracts/interfaces/IAToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ interface IAToken is IERC20, IScaledBalanceToken, IInitializableAToken {
/**
* @notice Emitted after the mint action
* @param from The address performing the mint
* @param value The amount being
* @param value The amount being minted (user entered amount + balance increase from interest)
* @param balanceIncrease The increase in balance since the last action of the user
* @param index The next liquidity index of the reserve
**/
event Mint(address indexed from, uint256 value, uint256 index);
event Mint(address indexed from, uint256 value, uint256 balanceIncrease, uint256 index);

/**
* @notice Mints `amount` aTokens to `user`
Expand All @@ -37,10 +38,17 @@ interface IAToken is IERC20, IScaledBalanceToken, IInitializableAToken {
* @notice Emitted after aTokens are burned
* @param from The owner of the aTokens, getting them burned
* @param target The address that will receive the underlying
* @param value The amount being burned
* @param value The amount being burned (user entered amount - balance increase from interest)
* @param balanceIncrease The increase in balance since the last action of the user
* @param index The next liquidity index of the reserve
**/
event Burn(address indexed from, address indexed target, uint256 value, uint256 index);
event Burn(
address indexed from,
address indexed target,
uint256 value,
uint256 balanceIncrease,
uint256 index
);

/**
* @notice Emitted during the transfer action
Expand All @@ -53,6 +61,8 @@ interface IAToken is IERC20, IScaledBalanceToken, IInitializableAToken {

/**
* @notice Burns aTokens from `user` and sends the equivalent amount of underlying to `receiverOfUnderlying`
* @dev In some instances, the mint event could be emitted from a burn transaction
* if the amount to burn is less than the interest the user earned
* @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
Expand Down
6 changes: 4 additions & 2 deletions contracts/interfaces/IStableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface IStableDebtToken is IInitializableDebtToken {
* @notice Emitted when new stable debt is minted
* @param user The address of the user who triggered the minting
* @param onBehalfOf The recipient of stable debt tokens
* @param amount The amount minted
* @param amount The amount minted (user entered amount + balance increase from interest)
* @param currentBalance The current balance of the user
* @param balanceIncrease The increase in balance since the last action of the user
* @param newRate The rate of the debt after the minting
Expand All @@ -36,7 +36,7 @@ interface IStableDebtToken is IInitializableDebtToken {
/**
* @notice Emitted when new stable debt is burned
* @param user The address of the user
* @param amount The amount being burned
* @param amount The amount being burned (user entered amount - balance increase from interest)
* @param currentBalance The current balance of the user
* @param balanceIncrease The the increase in balance since the last action of the user
* @param avgStableRate The next average stable rate after the burning
Expand Down Expand Up @@ -81,6 +81,8 @@ interface IStableDebtToken is IInitializableDebtToken {
* @notice Burns debt of `user`
* @dev The resulting rate is the weighted average between the rate of the new debt
* and the rate of the previous debt
* @dev In some instances, a burn transaction will emit a mint event
* if the amount to burn is less than the interest the user earned
* @param user The address of the user getting his debt burned
* @param amount The amount of debt tokens getting burned
* @return The total stable debt
Expand Down
18 changes: 14 additions & 4 deletions contracts/interfaces/IVariableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ interface IVariableDebtToken is IScaledBalanceToken, IInitializableDebtToken {
* @notice Emitted after the mint action
* @param from The address performing the mint
* @param onBehalfOf The address of the user on which behalf minting has been performed
* @param value The amount to be minted
* @param value The amount to be minted (user entered amount + balance increase from interest)
* @param balanceIncrease The increase in balance since the last action of the user
* @param index The last index of the reserve
**/
event Mint(address indexed from, address indexed onBehalfOf, uint256 value, uint256 index);
event Mint(
address indexed from,
address indexed onBehalfOf,
uint256 value,
uint256 balanceIncrease,
uint256 index
);

/**
* @notice Mints debt token to the `onBehalfOf` address
Expand All @@ -40,13 +47,16 @@ interface IVariableDebtToken is IScaledBalanceToken, IInitializableDebtToken {
/**
* @notice Emitted when variable debt is burnt
* @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 (user entered amount - balance increase from interest)
* @param balanceIncrease The increase in balance since the last action of the user
* @param index The index of the user
**/
event Burn(address indexed user, uint256 amount, uint256 index);
event Burn(address indexed user, uint256 amount, uint256 balanceIncrease, uint256 index);

/**
* @notice Burns user variable debt
* @dev In some instances, a burn transaction will emit a mint event
* if the amount to burn is less than the interest the user earned
* @param user The user which debt is burnt
* @param amount The amount getting burned
* @param index The variable debt index of the reserve
Expand Down
20 changes: 12 additions & 8 deletions contracts/protocol/tokenization/AToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract AToken is VersionedInitializable, IncentivizedERC20, IAToken {
require(amountScaled != 0, Errors.CT_INVALID_BURN_AMOUNT);

uint256 scaledBalance = super.balanceOf(user);
uint256 accumulatedInterest = scaledBalance.rayMul(index) -
uint256 balanceIncrease = scaledBalance.rayMul(index) -
scaledBalance.rayMul(_userState[user].additionalData);

_userState[user].additionalData = Helpers.castUint128(index);
Expand All @@ -102,11 +102,14 @@ contract AToken is VersionedInitializable, IncentivizedERC20, IAToken {
IERC20(_underlyingAsset).safeTransfer(receiverOfUnderlying, amount);
}

emit Transfer(user, address(0), amount);
if (accumulatedInterest > amount) {
emit Mint(user, accumulatedInterest - amount, index);
if (balanceIncrease > amount) {
uint256 amountToMint = balanceIncrease - amount;
emit Transfer(address(0), user, amountToMint);
emit Mint(user, amountToMint, balanceIncrease, index);
} else {
emit Burn(user, receiverOfUnderlying, amount - accumulatedInterest, index);
uint256 amountToBurn = amount - balanceIncrease;
emit Transfer(user, address(0), amountToBurn);
emit Burn(user, receiverOfUnderlying, amountToBurn, balanceIncrease, index);
}
}

Expand All @@ -120,15 +123,16 @@ contract AToken is VersionedInitializable, IncentivizedERC20, IAToken {
require(amountScaled != 0, Errors.CT_INVALID_MINT_AMOUNT);

uint256 scaledBalance = super.balanceOf(user);
uint256 accumulatedInterest = scaledBalance.rayMul(index) -
uint256 balanceIncrease = scaledBalance.rayMul(index) -
scaledBalance.rayMul(_userState[user].additionalData);

_userState[user].additionalData = Helpers.castUint128(index);

_mint(user, Helpers.castUint128(amountScaled));

emit Transfer(address(0), user, amount);
emit Mint(user, amount + accumulatedInterest, index);
uint256 amountToMint = amount + balanceIncrease;
emit Transfer(address(0), user, amountToMint);
emit Mint(user, amountToMint, balanceIncrease, index);

return scaledBalance == 0;
}
Expand Down
11 changes: 6 additions & 5 deletions contracts/protocol/tokenization/StableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,14 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
rate.rayMul(vars.amountInRay)).rayDiv(vars.nextSupply.wadToRay())
);

_mint(onBehalfOf, amount + balanceIncrease, vars.previousSupply);
uint256 amountToMint = amount + balanceIncrease;
_mint(onBehalfOf, amountToMint, vars.previousSupply);

emit Transfer(address(0), onBehalfOf, amount + balanceIncrease);
emit Transfer(address(0), onBehalfOf, amountToMint);
emit Mint(
user,
onBehalfOf,
amount,
amountToMint,
currentBalance,
balanceIncrease,
vars.nextStableRate,
Expand Down Expand Up @@ -218,7 +219,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
if (balanceIncrease > amount) {
uint256 amountToMint = balanceIncrease - amount;
_mint(user, amountToMint, previousSupply);
emit Transfer(address(0), user, balanceIncrease - amount);
emit Transfer(address(0), user, amountToMint);
emit Mint(
user,
user,
Expand All @@ -232,7 +233,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
} else {
uint256 amountToBurn = amount - balanceIncrease;
_burn(user, amountToBurn, previousSupply);
emit Transfer(address(0), user, amount - balanceIncrease);
emit Transfer(user, address(0), amountToBurn);
emit Burn(user, amountToBurn, currentBalance, balanceIncrease, nextAvgStableRate, nextSupply);
}

Expand Down
21 changes: 12 additions & 9 deletions contracts/protocol/tokenization/VariableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
require(amountScaled != 0, Errors.CT_INVALID_MINT_AMOUNT);

uint256 scaledBalance = super.balanceOf(onBehalfOf);
uint256 accumulatedInterest = scaledBalance.rayMul(index) -
uint256 balanceIncrease = scaledBalance.rayMul(index) -
scaledBalance.rayMul(_userState[onBehalfOf].additionalData);

_userState[onBehalfOf].additionalData = Helpers.castUint128(index);

_mint(onBehalfOf, Helpers.castUint128(amountScaled));

emit Transfer(address(0), onBehalfOf, amount + accumulatedInterest);
emit Mint(user, onBehalfOf, amount + accumulatedInterest, index);
uint256 amountToMint = amount + balanceIncrease;
emit Transfer(address(0), onBehalfOf, amountToMint);
emit Mint(user, onBehalfOf, amountToMint, balanceIncrease, index);

return (scaledBalance == 0, scaledTotalSupply());
}
Expand All @@ -111,19 +112,21 @@ contract VariableDebtToken is DebtTokenBase, IVariableDebtToken {
require(amountScaled != 0, Errors.CT_INVALID_BURN_AMOUNT);

uint256 scaledBalance = super.balanceOf(user);
uint256 accumulatedInterest = scaledBalance.rayMul(index) -
uint256 balanceIncrease = scaledBalance.rayMul(index) -
scaledBalance.rayMul(_userState[user].additionalData);

_userState[user].additionalData = Helpers.castUint128(index);

_burn(user, Helpers.castUint128(amountScaled));

if (accumulatedInterest > amount) {
emit Transfer(address(0), user, accumulatedInterest - amount);
emit Mint(user, user, accumulatedInterest - amount, index);
if (balanceIncrease > amount) {
uint256 amountToMint = balanceIncrease - amount;
emit Transfer(address(0), user, amountToMint);
emit Mint(user, user, amountToMint, balanceIncrease, index);
} else {
emit Transfer(user, address(0), amount - accumulatedInterest);
emit Burn(user, amount - accumulatedInterest, index);
uint256 amountToBurn = amount - balanceIncrease;
emit Transfer(user, address(0), amountToBurn);
emit Burn(user, amountToBurn, balanceIncrease, index);
}
return scaledTotalSupply();
}
Expand Down
Loading

0 comments on commit 8e236ec

Please sign in to comment.