From ac9a124e77739bc85956b71c8f26f29e78421d9c Mon Sep 17 00:00:00 2001 From: eboado Date: Tue, 13 Apr 2021 14:01:52 +0200 Subject: [PATCH] - Gas optimization on _withdraw() on StaticAToken --- .../protocol/tokenization/StaticAToken.sol | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/contracts/protocol/tokenization/StaticAToken.sol b/contracts/protocol/tokenization/StaticAToken.sol index 2a0fcf50..76b91699 100644 --- a/contracts/protocol/tokenization/StaticAToken.sol +++ b/contracts/protocol/tokenization/StaticAToken.sol @@ -65,7 +65,6 @@ contract StaticAToken is IStaticAToken, ReentrancyGuard, ERC20 { ASSET.safeApprove(address(LENDING_POOL), type(uint256).max); } - /// @inheritdoc IStaticAToken function deposit( address recipient, @@ -258,6 +257,10 @@ contract StaticAToken is IStaticAToken, ReentrancyGuard, ERC20 { 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( address owner, address recipient, @@ -266,7 +269,6 @@ contract StaticAToken is IStaticAToken, ReentrancyGuard, ERC20 { bool toUnderlying ) internal returns (uint256, uint256) { require(recipient != address(0), 'INVALID_RECIPIENT'); - require(staticAmount == 0 || dynamicAmount == 0, 'ONLY_ONE_AMOUNT_FORMAT_ALLOWED'); uint256 userBalance = balanceOf(owner); @@ -274,11 +276,17 @@ contract StaticAToken is IStaticAToken, ReentrancyGuard, ERC20 { uint256 amountToBurn; uint256 currentRate = rate(); + if (staticAmount > 0) { - amountToBurn = (staticAmount > userBalance) ? userBalance : staticAmount; - amountToWithdraw = (staticAmount > userBalance) - ? _staticToDynamicAmount(userBalance, currentRate) - : _staticToDynamicAmount(staticAmount, currentRate); + require(dynamicAmount == 0, 'ONLY_ONE_AMOUNT_INPUT_ALLOWED'); + + if (staticAmount > userBalance) { + amountToBurn = userBalance; + amountToWithdraw = _staticToDynamicAmount(userBalance, currentRate); + } else { + amountToBurn = staticAmount; + amountToWithdraw = _staticToDynamicAmount(staticAmount, currentRate); + } } else { uint256 dynamicUserBalance = _staticToDynamicAmount(userBalance, currentRate); amountToWithdraw = (dynamicAmount > dynamicUserBalance) ? dynamicUserBalance : dynamicAmount;