Skip to content

Commit

Permalink
Fix event emission of AToken and DebtToken events (#682)
Browse files Browse the repository at this point in the history
* fix: Initial fix for emitting whole balance in Transfer events

* fix: Initial fix for emitting accumulating Mint events in transfers

* fix: Fix the amount of BalanceTransfer event

* test: Add tests for event emission in tokens

* fix: Refactor old test cases for AToken events

* fix: Fix errors on tests

* test: Add failing tests of AToken events due to onBehalfOf mechanism

* fix: Fix tests

* fix: Fix order of emission of events in AToken ttransfer

* fix: Enhance natspec docs of main events

* fix: Rename `amount` field in IStableDebtToken to `value`

* fix: Add some little refactor to tests

* fix: Add emission of Transfer events for accruals in transfer function

* fix: Fix caller of Mint event when transferFrom

* docs: Fix missing docs in burnScaled function

* fix: Add more docs to Burn event

* fix: Resolve merge error in tests

* fix: Revert changes on event param names of StableDebtToken
  • Loading branch information
miguelmtzinf authored Nov 15, 2022
1 parent f6817ff commit ab07a4e
Show file tree
Hide file tree
Showing 14 changed files with 2,408 additions and 184 deletions.
2 changes: 1 addition & 1 deletion contracts/interfaces/IAToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface IAToken is IERC20, IScaledBalanceToken, IInitializableAToken {
* @dev Emitted during the transfer action
* @param from The user whose tokens are being transferred
* @param to The recipient
* @param value The amount being transferred
* @param value The scaled amount being transferred
* @param index The next liquidity index of the reserve
**/
event BalanceTransfer(address indexed from, address indexed to, uint256 value, uint256 index);
Expand Down
11 changes: 6 additions & 5 deletions contracts/interfaces/IScaledBalanceToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pragma solidity ^0.8.0;
/**
* @title IScaledBalanceToken
* @author Aave
* @notice Defines the basic interface for a scaledbalance token.
* @notice Defines the basic interface for a scaled-balance token.
**/
interface IScaledBalanceToken {
/**
* @dev Emitted after the mint action
* @param caller The address performing the mint
* @param onBehalfOf The address of the user that will receive the minted scaled balance tokens
* @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 value The scaled amount being minted (based on user entered amount and balance increase from interest)
* @param balanceIncrease The increase in scaled balance since the last action of 'onBehalfOf'
* @param index The next liquidity index of the reserve
**/
event Mint(
Expand All @@ -25,10 +25,11 @@ interface IScaledBalanceToken {

/**
* @dev Emitted after scaled balance tokens are burned
* @dev If the burn function does not involve a transfer of the underlying asset, the target defaults to zero address
* @param from The address from which the scaled tokens will be burned
* @param target The address that will receive the underlying, if any
* @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 value The scaled amount being burned (user entered amount - balance increase from interest)
* @param balanceIncrease The increase in scaled balance since the last action of 'from'
* @param index The next liquidity index of the reserve
**/
event Burn(
Expand Down
8 changes: 4 additions & 4 deletions contracts/interfaces/IStableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ interface IStableDebtToken is IInitializableDebtToken {
* @param user The address of the user who triggered the minting
* @param onBehalfOf The recipient of stable debt tokens
* @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 currentBalance The balance of the user based on the previous balance and balance increase from interest
* @param balanceIncrease The increase in balance since the last action of the user 'onBehalfOf'
* @param newRate The rate of the debt after the minting
* @param avgStableRate The next average stable rate after the minting
* @param newTotalSupply The next total supply of the stable debt token after the action
Expand All @@ -36,8 +36,8 @@ interface IStableDebtToken is IInitializableDebtToken {
* @dev Emitted when new stable debt is burned
* @param from The address from which the debt will be 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 currentBalance The balance of the user based on the previous balance and balance increase from interest
* @param balanceIncrease The increase in balance since the last action of 'from'
* @param avgStableRate The next average stable rate after the burning
* @param newTotalSupply The next total supply of the stable debt token after the action
**/
Expand Down
6 changes: 2 additions & 4 deletions contracts/protocol/tokenization/AToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ contract AToken is VersionedInitializable, ScaledBalanceTokenBase, EIP712Base, I
// 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);
}

/// @inheritdoc IERC20
Expand Down Expand Up @@ -216,13 +214,13 @@ contract AToken is VersionedInitializable, ScaledBalanceTokenBase, EIP712Base, I
uint256 fromBalanceBefore = super.balanceOf(from).rayMul(index);
uint256 toBalanceBefore = super.balanceOf(to).rayMul(index);

super._transfer(from, to, amount.rayDiv(index).toUint128());
super._transfer(from, to, amount, index);

if (validate) {
POOL.finalizeTransfer(underlyingAsset, from, to, amount, fromBalanceBefore, toBalanceBefore);
}

emit BalanceTransfer(from, to, amount, index);
emit BalanceTransfer(from, to, amount.rayDiv(index), index);
}

/**
Expand Down
1 change: 0 additions & 1 deletion contracts/protocol/tokenization/base/IncentivizedERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ abstract contract IncentivizedERC20 is Context, IERC20Detailed {
incentivesControllerLocal.handleAction(recipient, currentTotalSupply, oldRecipientBalance);
}
}
emit Transfer(sender, recipient, amount);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions contracts/protocol/tokenization/base/ScaledBalanceTokenBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,42 @@ abstract contract ScaledBalanceTokenBase is MintableIncentivizedERC20, IScaledBa
emit Burn(user, target, amountToBurn, balanceIncrease, index);
}
}

/**
* @notice Implements the basic logic to transfer scaled balance tokens between two users
* @dev It emits a mint event with the interest accrued per user
* @param sender The source address
* @param recipient The destination address
* @param amount The amount getting transferred
* @param index The next liquidity index of the reserve
**/
function _transfer(
address sender,
address recipient,
uint256 amount,
uint256 index
) internal {
uint256 senderScaledBalance = super.balanceOf(sender);
uint256 senderBalanceIncrease = senderScaledBalance.rayMul(index) -
senderScaledBalance.rayMul(_userState[sender].additionalData);

uint256 recipientScaledBalance = super.balanceOf(recipient);
uint256 recipientBalanceIncrease = recipientScaledBalance.rayMul(index) -
recipientScaledBalance.rayMul(_userState[recipient].additionalData);

_userState[sender].additionalData = index.toUint128();
_userState[recipient].additionalData = index.toUint128();

super._transfer(sender, recipient, amount.rayDiv(index).toUint128());

emit Transfer(address(0), sender, senderBalanceIncrease);
emit Mint(_msgSender(), sender, senderBalanceIncrease, senderBalanceIncrease, index);

if (recipientBalanceIncrease > 0) {
emit Transfer(address(0), recipient, recipientBalanceIncrease);
emit Mint(_msgSender(), recipient, recipientBalanceIncrease, recipientBalanceIncrease, index);
}

emit Transfer(sender, recipient, amount);
}
}
Loading

0 comments on commit ab07a4e

Please sign in to comment.