From 9d8e60c28c980768dbbf7da7f4c0dca573684410 Mon Sep 17 00:00:00 2001 From: julien Date: Tue, 8 Aug 2023 00:04:11 +0200 Subject: [PATCH 1/3] refactor: use early return in `_accrueInterests` --- src/Blue.sol | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 660ffd91c..741bf503f 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -371,26 +371,25 @@ contract Blue is IBlue { if (elapsed == 0) return; uint256 marketTotalBorrow = totalBorrow[id]; + lastUpdate[id] = block.timestamp; - if (marketTotalBorrow != 0) { - uint256 borrowRate = IIrm(market.irm).borrowRate(market); - uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); - totalBorrow[id] = marketTotalBorrow + accruedInterests; - totalSupply[id] += accruedInterests; - - uint256 feeShares; - 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. - feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount); - supplyShares[id][feeRecipient] += feeShares; - totalSupplyShares[id] += feeShares; - } + if (marketTotalBorrow == 0) return; - emit AccrueInterests(id, borrowRate, accruedInterests, feeShares); + uint256 borrowRate = IIrm(market.irm).borrowRate(market); + uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); + totalBorrow[id] = marketTotalBorrow + accruedInterests; + totalSupply[id] += accruedInterests; + + uint256 feeShares; + 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. + feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount); + supplyShares[id][feeRecipient] += feeShares; + totalSupplyShares[id] += feeShares; } - lastUpdate[id] = block.timestamp; + emit AccrueInterests(id, borrowRate, accruedInterests, feeShares); } // Health check. From 485bec0629d8ec396f2b03dc8fa3c436cf196943 Mon Sep 17 00:00:00 2001 From: julien Date: Tue, 8 Aug 2023 14:44:41 +0200 Subject: [PATCH 2/3] refactor: remove `marketTotalBorrow` variable in `_accrueInterests` --- src/Blue.sol | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 741bf503f..f77d8f019 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -370,14 +370,13 @@ contract Blue is IBlue { if (elapsed == 0) return; - uint256 marketTotalBorrow = totalBorrow[id]; lastUpdate[id] = block.timestamp; - if (marketTotalBorrow == 0) return; + if (totalBorrow[id] == 0) return; uint256 borrowRate = IIrm(market.irm).borrowRate(market); - uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed); - totalBorrow[id] = marketTotalBorrow + accruedInterests; + uint256 accruedInterests = totalBorrow[id].mulWadDown(borrowRate * elapsed); + totalBorrow[id] += accruedInterests; totalSupply[id] += accruedInterests; uint256 feeShares; From 2200d9814aadbb0b8c738fd4dbbfbe5c6630b2bb Mon Sep 17 00:00:00 2001 From: julien Date: Thu, 10 Aug 2023 15:24:30 +0200 Subject: [PATCH 3/3] fix: remove useless early return Signed-off-by: julien --- src/Blue.sol | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index d978d4673..e08e799ff 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -387,10 +387,6 @@ contract Blue is IBlue { if (elapsed == 0) return; - lastUpdate[id] = block.timestamp; - - if (totalBorrow[id] == 0) return; - uint256 borrowRate = IIrm(market.irm).borrowRate(market); uint256 accruedInterests = totalBorrow[id].mulWadDown(borrowRate.wTaylorCompounded(elapsed)); totalBorrow[id] += accruedInterests; @@ -405,6 +401,8 @@ contract Blue is IBlue { totalSupplyShares[id] += feeShares; } + lastUpdate[id] = block.timestamp; + emit AccrueInterests(id, borrowRate, accruedInterests, feeShares); }