Skip to content

Commit

Permalink
⚡️ irm: optimize legacy functions
Browse files Browse the repository at this point in the history
  • Loading branch information
itofarina committed Mar 15, 2024
1 parent 13edd68 commit 08619c3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 68 deletions.
72 changes: 36 additions & 36 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ EscrowedEXATest:testWithdrawFromUnknownStream() (gas: 886169)
EscrowedEXATest:testWithdrawMaxFromMultipleStreams() (gas: 1014203)
EscrowedEXATest:testWithdrawMaxShouldGiveReserveBackWhenDepleted() (gas: 329124)
EscrowedEXATest:testWithdrawMaxWithInvalidSender() (gas: 342347)
InterestRateModelTest:testFixedBorrowRate() (gas: 1891802)
InterestRateModelTest:testFixedRateRevertAlreadyMatured() (gas: 1905701)
InterestRateModelTest:testFixedRateRevertUtilizationExceeded() (gas: 1913063)
InterestRateModelTest:testFloatingBorrowRate() (gas: 1885059)
InterestRateModelTest:testMinTimeToMaturity() (gas: 1923229)
InterestRateModelTest:testRevertMaxUtilizationLowerThanWad() (gas: 40391)
InterestRateModelTest:testFixedBorrowRate() (gas: 1843854)
InterestRateModelTest:testFixedRateRevertAlreadyMatured() (gas: 1857753)
InterestRateModelTest:testFixedRateRevertUtilizationExceeded() (gas: 1865115)
InterestRateModelTest:testFloatingBorrowRate() (gas: 1837111)
InterestRateModelTest:testMinTimeToMaturity() (gas: 1875281)
InterestRateModelTest:testRevertMaxUtilizationLowerThanWad() (gas: 40324)
MarketTest:testAccountLiquidityAdjustedDebt() (gas: 383457)
MarketTest:testAnotherUserRedeemWhenOwnerHasShortfall() (gas: 488834)
MarketTest:testAnotherUserWithdrawWhenOwnerHasShortfall() (gas: 476496)
Expand Down Expand Up @@ -165,7 +165,7 @@ MarketTest:testDepositShouldUpdateFlexibleBorrowVariables() (gas: 447512)
MarketTest:testDepositToSmartPool() (gas: 160031)
MarketTest:testDistributeMultipleAccumulatedEarnings() (gas: 756517)
MarketTest:testDistributionOfLossesShouldReduceFromFloatingBackupBorrowedAccordingly() (gas: 2192309)
MarketTest:testEarlyRepaymentWithExcessiveAmountOfFees() (gas: 2664955)
MarketTest:testEarlyRepaymentWithExcessiveAmountOfFees() (gas: 2617024)
MarketTest:testEarlyWithdrawFromFreeLunchShouldNotRevertWithFloatingFullUtilization() (gas: 616705)
MarketTest:testFixedBorrowFailingWhenFlexibleBorrowAccruesDebt() (gas: 790382)
MarketTest:testFixedBorrowRateToMaturity() (gas: 410076)
Expand Down Expand Up @@ -215,8 +215,8 @@ MarketTest:testRoundingUpAllowanceWhenBorrowingAtMaturity() (gas: 502969)
MarketTest:testRoundingUpAllowanceWhenWithdrawingAtMaturity() (gas: 526476)
MarketTest:testSetDampSpeedFactorShouldUpdateFloatingAssetsAverage() (gas: 243656)
MarketTest:testSetEarningsAccumulatorSmoothFactorShouldDistributeEarnings() (gas: 498257)
MarketTest:testSetInterestRateModelShouldUpdateFloatingDebt() (gas: 2393276)
MarketTest:testSetInterestRateModelWithAddressZeroShouldNotUpdateFloatingDebt() (gas: 2225605)
MarketTest:testSetInterestRateModelShouldUpdateFloatingDebt() (gas: 2345345)
MarketTest:testSetInterestRateModelWithAddressZeroShouldNotUpdateFloatingDebt() (gas: 2177674)
MarketTest:testShareValueNotDecreasingAfterDeposit() (gas: 473858)
MarketTest:testShareValueNotDecreasingWhenMintingToTreasury() (gas: 479677)
MarketTest:testSingleFloatingBorrow() (gas: 328444)
Expand Down Expand Up @@ -249,36 +249,36 @@ PoolLibTest:testMaturityRangeTooWide() (gas: 6831)
PreviewerTest:testAccountsReturningAccurateAmounts() (gas: 1296347)
PreviewerTest:testAccountsReturningUtilizationForDifferentMaturities() (gas: 3650720)
PreviewerTest:testAccountsWithAccountOnlyDeposit() (gas: 829257)
PreviewerTest:testAccountsWithAccountThatHasBalances() (gas: 1790555)
PreviewerTest:testAccountsWithAccountThatHasBalances() (gas: 1790509)
PreviewerTest:testAccountsWithEmptyAccount() (gas: 678796)
PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 15755663)
PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 15707764)
PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 7049137)
PreviewerTest:testEmptyExactly() (gas: 5098692)
PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 677746)
PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 11477125)
PreviewerTest:testFixedPoolsA() (gas: 16773585)
PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 11429226)
PreviewerTest:testFixedPoolsA() (gas: 16744235)
PreviewerTest:testFixedPoolsChangingMaturityInTime() (gas: 1613611)
PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 13263686)
PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 13747954)
PreviewerTest:testFlexibleAvailableLiquidity() (gas: 14982932)
PreviewerTest:testFixedPoolsRatesAndUtilizations() (gas: 13215787)
PreviewerTest:testFixedPoolsWithFloatingAssetsAverage() (gas: 13700055)
PreviewerTest:testFlexibleAvailableLiquidity() (gas: 14944250)
PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 3906199)
PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 10996305)
PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 10948406)
PreviewerTest:testFloatingRateAndUtilization() (gas: 996740)
PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 6163444)
PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2125761)
PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 9627246)
PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 11111938)
PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 9153532)
PreviewerTest:testPreviewBorrowAtAllMaturitiesReturningAccurateAmount() (gas: 3094736)
PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmount() (gas: 523812)
PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 1187511)
PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateUtilization() (gas: 379563)
PreviewerTest:testPreviewBorrowAtMaturityWithFiveUnits() (gas: 319411)
PreviewerTest:testPreviewBorrowAtMaturityWithInvalidMaturity() (gas: 213019)
PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 9579347)
PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 11064039)
PreviewerTest:testOraclePriceReturningAccurateValues() (gas: 9105633)
PreviewerTest:testPreviewBorrowAtAllMaturitiesReturningAccurateAmount() (gas: 3093908)
PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmount() (gas: 523789)
PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 1187442)
PreviewerTest:testPreviewBorrowAtMaturityReturningAccurateUtilization() (gas: 379517)
PreviewerTest:testPreviewBorrowAtMaturityWithFiveUnits() (gas: 319388)
PreviewerTest:testPreviewBorrowAtMaturityWithInvalidMaturity() (gas: 212996)
PreviewerTest:testPreviewBorrowAtMaturityWithMaturedMaturity() (gas: 50123)
PreviewerTest:testPreviewBorrowAtMaturityWithOneUnit() (gas: 319390)
PreviewerTest:testPreviewBorrowAtMaturityWithOneUnit() (gas: 319367)
PreviewerTest:testPreviewBorrowAtMaturityWithSameTimestamp() (gas: 50041)
PreviewerTest:testPreviewBorrowAtMaturityWithZeroAmount() (gas: 201887)
PreviewerTest:testPreviewBorrowAtMaturityWithZeroAmount() (gas: 201864)
PreviewerTest:testPreviewDepositAtAllMaturitiesReturningAccurateAmounts() (gas: 1554561)
PreviewerTest:testPreviewDepositAtMaturityReturningAccurateAmount() (gas: 584837)
PreviewerTest:testPreviewDepositAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 858194)
Expand All @@ -298,22 +298,22 @@ PreviewerTest:testPreviewRepayAtMaturityWithMaturedMaturity() (gas: 496857)
PreviewerTest:testPreviewRepayAtMaturityWithOneUnit() (gas: 500038)
PreviewerTest:testPreviewRepayAtMaturityWithSameTimestamp() (gas: 495257)
PreviewerTest:testPreviewRepayAtMaturityWithZeroAmount() (gas: 500104)
PreviewerTest:testPreviewValueInFixedOperations() (gas: 1994048)
PreviewerTest:testPreviewWithdrawAtMaturityReturningAccurateAmount() (gas: 213016)
PreviewerTest:testPreviewWithdrawAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 770920)
PreviewerTest:testPreviewValueInFixedOperations() (gas: 1993933)
PreviewerTest:testPreviewWithdrawAtMaturityReturningAccurateAmount() (gas: 212995)
PreviewerTest:testPreviewWithdrawAtMaturityReturningAccurateAmountWithIntermediateOperations() (gas: 770851)
PreviewerTest:testPreviewWithdrawAtMaturityWithEmptyMaturity() (gas: 35229)
PreviewerTest:testPreviewWithdrawAtMaturityWithEmptyMaturityAndZeroAmount() (gas: 35272)
PreviewerTest:testPreviewWithdrawAtMaturityWithFiveUnits() (gas: 220114)
PreviewerTest:testPreviewWithdrawAtMaturityWithFiveUnits() (gas: 220088)
PreviewerTest:testPreviewWithdrawAtMaturityWithInvalidMaturity() (gas: 35293)
PreviewerTest:testPreviewWithdrawAtMaturityWithMaturedMaturity() (gas: 197469)
PreviewerTest:testPreviewWithdrawAtMaturityWithOneUnit() (gas: 220113)
PreviewerTest:testPreviewWithdrawAtMaturityWithOneUnit() (gas: 220087)
PreviewerTest:testPreviewWithdrawAtMaturityWithSameTimestamp() (gas: 197485)
PreviewerTest:testPreviewWithdrawAtMaturityWithZeroAmount() (gas: 220159)
PreviewerTest:testPreviewWithdrawAtMaturityWithZeroAmount() (gas: 220133)
PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6119994)
PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 6325080)
PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6153289)
PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 17163539)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 16354622)
PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 17115640)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 16306723)
PreviewerTest:testRewardsRateX() (gas: 7204946)
PriceFeedDoubleTest:testPriceFeedDoubleReturningAccurateDecimals() (gas: 428034)
PriceFeedDoubleTest:testPriceFeedDoubleReturningPrice() (gas: 26138)
Expand Down
36 changes: 4 additions & 32 deletions contracts/InterestRateModel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,7 @@ contract InterestRateModel {
) external view returns (uint256) {
if (block.timestamp >= maturity) revert AlreadyMatured();
uint256 floatingAssets = previewFloatingAssetsAverage(maturity);
uint256 backupBorrowed = market.floatingBackupBorrowed();
uint256 floatingDebt = totalFloatingBorrowAssets(
floatingAssets,
market.floatingDebt(),
backupBorrowed,
market.lastFloatingDebtUpdate()
);
uint256 floatingDebt = market.totalFloatingBorrowAssets();
uint256 newBorrowed = borrowed + amount;
uint256 backupDebtAddition = newBorrowed - Math.min(Math.max(borrowed, supplied), newBorrowed);

Expand All @@ -219,21 +213,15 @@ contract InterestRateModel {
market.maxFuturePools(),
fixedUtilization(supplied, newBorrowed, floatingAssets),
floatingAssets != 0 ? floatingDebt.divWadUp(floatingAssets) : 0,
globalUtilization(floatingAssets, floatingDebt, backupBorrowed + backupDebtAddition)
globalUtilization(floatingAssets, floatingDebt, market.floatingBackupBorrowed() + backupDebtAddition)
).mulDivDown(maturity - block.timestamp, 365 days);
}

/// @dev deprecated in favor of `fixedRate(maturity, maxPools, uFixed, uFloating, uGlobal)`
function minFixedRate(uint256, uint256, uint256) external view returns (uint256 rate, uint256 utilization) {
uint256 floatingAssets = market.floatingAssetsAverage();
uint256 backupBorrowed = market.floatingBackupBorrowed();
uint256 floatingDebt = totalFloatingBorrowAssets(
floatingAssets,
market.floatingDebt(),
backupBorrowed,
market.lastFloatingDebtUpdate()
);
utilization = globalUtilization(floatingAssets, floatingDebt, backupBorrowed);
uint256 floatingDebt = market.totalFloatingBorrowAssets();
utilization = globalUtilization(floatingAssets, floatingDebt, market.floatingBackupBorrowed());
rate = baseRate(floatingAssets != 0 ? floatingDebt.divWadUp(floatingAssets) : 0, utilization);
}

Expand All @@ -254,22 +242,6 @@ contract InterestRateModel {
return memFloatingAssetsAverage.mulWadDown(1e18 - averageFactor) + averageFactor.mulWadDown(memFloatingAssets);
}

function totalFloatingBorrowAssets(
uint256 floatingAssets,
uint256 floatingDebt,
uint256 backupBorrowed,
uint256 lastFloatingDebtUpdate
) internal view returns (uint256) {
return
floatingDebt +
floatingDebt.mulWadDown(
floatingRate(
floatingAssets != 0 ? floatingDebt.divWadUp(floatingAssets) : 0,
globalUtilization(floatingAssets, floatingDebt, backupBorrowed)
).mulDivDown(block.timestamp - lastFloatingDebtUpdate, 365 days)
);
}

function globalUtilization(
uint256 floatingAssets,
uint256 floatingDebt,
Expand Down

0 comments on commit 08619c3

Please sign in to comment.