- Gas optimization on _withdraw() on StaticAToken

This commit is contained in:
eboado 2021-04-13 14:01:52 +02:00
parent 57ab78d1cb
commit ac9a124e77

View File

@ -65,7 +65,6 @@ contract StaticAToken is IStaticAToken, ReentrancyGuard, ERC20 {
ASSET.safeApprove(address(LENDING_POOL), type(uint256).max); ASSET.safeApprove(address(LENDING_POOL), type(uint256).max);
} }
/// @inheritdoc IStaticAToken /// @inheritdoc IStaticAToken
function deposit( function deposit(
address recipient, address recipient,
@ -258,6 +257,10 @@ contract StaticAToken is IStaticAToken, ReentrancyGuard, ERC20 {
return amountToMint; return amountToMint;
} }
/**
* @dev only one of `staticAmount` or `dynamicAmount` can be > 0 at a time. For gas optimization that
* is verified not at the beginning, but in the conditional blocks before the tokens' burning
**/
function _withdraw( function _withdraw(
address owner, address owner,
address recipient, address recipient,
@ -266,7 +269,6 @@ contract StaticAToken is IStaticAToken, ReentrancyGuard, ERC20 {
bool toUnderlying bool toUnderlying
) internal returns (uint256, uint256) { ) internal returns (uint256, uint256) {
require(recipient != address(0), 'INVALID_RECIPIENT'); require(recipient != address(0), 'INVALID_RECIPIENT');
require(staticAmount == 0 || dynamicAmount == 0, 'ONLY_ONE_AMOUNT_FORMAT_ALLOWED');
uint256 userBalance = balanceOf(owner); uint256 userBalance = balanceOf(owner);
@ -274,11 +276,17 @@ contract StaticAToken is IStaticAToken, ReentrancyGuard, ERC20 {
uint256 amountToBurn; uint256 amountToBurn;
uint256 currentRate = rate(); uint256 currentRate = rate();
if (staticAmount > 0) { if (staticAmount > 0) {
amountToBurn = (staticAmount > userBalance) ? userBalance : staticAmount; require(dynamicAmount == 0, 'ONLY_ONE_AMOUNT_INPUT_ALLOWED');
amountToWithdraw = (staticAmount > userBalance)
? _staticToDynamicAmount(userBalance, currentRate) if (staticAmount > userBalance) {
: _staticToDynamicAmount(staticAmount, currentRate); amountToBurn = userBalance;
amountToWithdraw = _staticToDynamicAmount(userBalance, currentRate);
} else {
amountToBurn = staticAmount;
amountToWithdraw = _staticToDynamicAmount(staticAmount, currentRate);
}
} else { } else {
uint256 dynamicUserBalance = _staticToDynamicAmount(userBalance, currentRate); uint256 dynamicUserBalance = _staticToDynamicAmount(userBalance, currentRate);
amountToWithdraw = (dynamicAmount > dynamicUserBalance) ? dynamicUserBalance : dynamicAmount; amountToWithdraw = (dynamicAmount > dynamicUserBalance) ? dynamicUserBalance : dynamicAmount;