8000 Remove bad debt gas saving comment by MathisGD · Pull Request #584 · morpho-org/morpho-blue · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove bad debt gas saving comment #584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2023

Conversation

MathisGD
Copy link
Contributor
@MathisGD MathisGD commented Nov 8, 2023

No description provided.

@MathisGD MathisGD linked an issue Nov 8, 2023 that may be closed by this pull request
@MathisGD MathisGD self-assigned this Nov 8, 2023
@MathisGD MathisGD requested a review from QGarchery November 8, 2023 12:16
Rubilmax
Rubilmax previously approved these changes Nov 11, 2023
MerlinEgalite
MerlinEgalite previously approved these changes Nov 11, 2023
@MerlinEgalite MerlinEgalite changed the base branch from cantina-review to main November 12, 2023 17:02
@MerlinEgalite MerlinEgalite dismissed stale reviews from Rubilmax and themself November 12, 2023 17:02

The base branch was changed.

Copy link
Contributor
@QGarchery QGarchery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested using forge, see the following POC

Gas test POC
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import "../BaseTest.sol";

contract LiquidationGas is BaseTest {
    uint256 constant ASSETS = 12 ether;

    function setUp() public override {
        super.setUp();

        loanToken.setBalance(SUPPLIER, ASSETS);
        collateralToken.setBalance(SUPPLIER, ASSETS);
        vm.startPrank(SUPPLIER);
        morpho.supply(marketParams, ASSETS, 0, SUPPLIER, "");
        morpho.supplyCollateral(marketParams, ASSETS, SUPPLIER, "");
        morpho.borrow(marketParams, ASSETS / 2, 0, SUPPLIER, SUPPLIER);
        vm.stopPrank();

        loanToken.setBalance(BORROWER, ASSETS);
        collateralToken.setBalance(BORROWER, ASSETS);
        vm.startPrank(BORROWER);
        morpho.supply(marketParams, ASSETS, 0, BORROWER, "");
        morpho.supplyCollateral(marketParams, ASSETS, BORROWER, "");
        morpho.borrow(marketParams, ASSETS / 2, 0, BORROWER, BORROWER);
        vm.stopPrank();

        oracle.setPrice(ORACLE_PRICE_SCALE / 1000);
    }

    function testGasBadDebt() public {
        vm.warp(block.timestamp + 30 days);
        vm.startPrank(LIQUIDATOR);
        morpho.liquidate(marketParams, BORROWER, ASSETS, 0, "");
        vm.stopPrank();
    }
}

From this test, 2 factors stand out:

  • it costs a lot in solidity to change slots, especially with packing because of the management of the slots. In assembly we could have written something that would be much more efficient, and it would make for more efficient bad debt realization. Note that I only tested without via-IR, so it may be more efficient in practice
  • the blatant case where it does not save gas is if interest were already accrued (this is why there is the vm.warp in the POC), because at this point the slot totalSupplyAssets gets changed.

So in the end, I still think it saves gas (approximately 1.3k) in most cases. But I'm ok to remove that comment because it is more nuanced than what is currently written

@MerlinEgalite MerlinEgalite merged commit 67b5448 into main Nov 13, 2023
@MerlinEgalite MerlinEgalite deleted the docs/remove-bad-debt-comment-1 branch November 13, 2023 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Comment about bad debt gas saving is outdated
5 participants
0