From dae6eb4eebd94d68ab416400eb64092689fbb892 Mon Sep 17 00:00:00 2001 From: makcandrov Date: Wed, 26 Jul 2023 06:09:28 -0700 Subject: [PATCH 1/3] refactor: skip interest accrual --- src/Blue.sol | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Blue.sol b/src/Blue.sol index 4480bf5cc..88e57db90 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -258,11 +258,15 @@ contract Blue { // Interests management. function _accrueInterests(Market memory market, Id id) internal { + uint256 elapsed = block.timestamp - lastUpdate[id]; + + if (elapsed == 0) return; + uint256 marketTotalBorrow = totalBorrow[id]; if (marketTotalBorrow != 0) { uint256 borrowRate = market.irm.borrowRate(market); - uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * (block.timestamp - lastUpdate[id])); + uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); totalBorrow[id] = marketTotalBorrow + accruedInterests; totalSupply[id] += accruedInterests; From 1e28a92805fb17af225aeb92b3afdd1188dd54f7 Mon Sep 17 00:00:00 2001 From: makcandrov Date: Mon, 31 Jul 2023 05:17:27 -0700 Subject: [PATCH 2/3] style: remove early return --- src/Blue.sol | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 0bd41c87e..1fd5ae259 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -290,26 +290,26 @@ contract Blue is IFlashLender { function _accrueInterests(Market memory market, Id id) internal { uint256 elapsed = block.timestamp - lastUpdate[id]; - if (elapsed == 0) return; - - uint256 marketTotalBorrow = totalBorrow[id]; - - if (marketTotalBorrow != 0) { - uint256 borrowRate = market.irm.borrowRate(market); - uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); - totalBorrow[id] = marketTotalBorrow + accruedInterests; - totalSupply[id] += accruedInterests; - - if (fee[id] != 0) { - uint256 feeAmount = accruedInterests.mulWadDown(fee[id]); - // The fee amount is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated. - uint256 feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount); - supplyShare[id][feeRecipient] += feeShares; - totalSupplyShares[id] += feeShares; + if (elapsed != 0) { + uint256 marketTotalBorrow = totalBorrow[id]; + + if (marketTotalBorrow != 0) { + uint256 borrowRate = market.irm.borrowRate(market); + uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); + totalBorrow[id] = marketTotalBorrow + accruedInterests; + totalSupply[id] += accruedInterests; + + if (fee[id] != 0) { + uint256 feeAmount = accruedInterests.mulWadDown(fee[id]); + // The fee amount is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated. + uint256 feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount); + supplyShare[id][feeRecipient] += feeShares; + totalSupplyShares[id] += feeShares; + } } - } - lastUpdate[id] = block.timestamp; + lastUpdate[id] = block.timestamp; + } } // Health check. From f5f40d2bae889b160c48790177c9025abed1f479 Mon Sep 17 00:00:00 2001 From: makcandrov Date: Mon, 31 Jul 2023 06:40:23 -0700 Subject: [PATCH 3/3] Revert "style: remove early return" This reverts commit 1e28a92805fb17af225aeb92b3afdd1188dd54f7. --- src/Blue.sol | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index e5fc6f33e..208e3f43f 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -290,26 +290,26 @@ contract Blue is IFlashLender { function _accrueInterests(Market memory market, Id id) internal { uint256 elapsed = block.timestamp - lastUpdate[id]; - if (elapsed != 0) { - uint256 marketTotalBorrow = totalBorrow[id]; - - if (marketTotalBorrow != 0) { - uint256 borrowRate = market.irm.borrowRate(market); - uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); - totalBorrow[id] = marketTotalBorrow + accruedInterests; - totalSupply[id] += accruedInterests; - - if (fee[id] != 0) { - uint256 feeAmount = accruedInterests.mulWadDown(fee[id]); - // The fee amount is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated. - uint256 feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount); - supplyShare[id][feeRecipient] += feeShares; - totalSupplyShares[id] += feeShares; - } + if (elapsed == 0) return; + + uint256 marketTotalBorrow = totalBorrow[id]; + + if (marketTotalBorrow != 0) { + uint256 borrowRate = market.irm.borrowRate(market); + uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); + totalBorrow[id] = marketTotalBorrow + accruedInterests; + totalSupply[id] += accruedInterests; + + if (fee[id] != 0) { + uint256 feeAmount = accruedInterests.mulWadDown(fee[id]); + // The fee amount is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated. + uint256 feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount); + supplyShare[id][feeRecipient] += feeShares; + totalSupplyShares[id] += feeShares; } - - lastUpdate[id] = block.timestamp; } + + lastUpdate[id] = block.timestamp; } // Health check.