Skip to content
Business logic Audit finding Intermediate

LiquidateWithReplacement does not charge swap fees on the borrower

The LiquidateWithReplacement function does not charge a swap fee to the borrower when it buys credit, unlike the equivalent BuyCreditMarket function. This means borrowers whose credit is purchased via liquidation avoid a fee that others pay, creating an unfair advantage.

pkqs90 Follow with 0xRobocop, BenRai and 11 others

Originally disclosed on Code4rena

Submitted by pkqs90, also found by 0xRobocop, BenRai, Brenzee, 0xStalin, alix40, 3n0ch, Honour, silver_eth, trachev, Nyx, KupiaSec, ether_sky, and VAD37

When a user places a credit sell limit order, it may be either:

  1. Bought by other users.
  2. Bought by the LiquidateWithReplacement action.

However, for case number 2, it does not charge a swap fee for the borrower, which contradicts to the swap fee definition.

Bug Description

Let’s first see how LiquidateWithReplacement works. There are two steps:

  1. Liquidation: Liquidator sends the amount of debt debt.futureValue to the protocol, and receive collateral tokens as liquidation reward.
  2. Buy credit: Use the original lender’s debt.futureValue as credit to buy credit from the borrower. This way the original lender can still get the same amount of credit at the same dueDate. The liquidator can then take away the difference between debt.futureValue and the amount sent to the borrower.

Note that step 2 is the same as buying credit using the BuyCreditMarket function.

However, according to the docs, all cash and credit swaps should charge swap fees on the borrower. The issue here is the swap in LiquidateWithReplacement does not charge swap fees.

Protocol fee; charged to the recipient of USDC on cash->credit and credit->cash swaps. Prorated based on credit tenor (the fee is annualized).

    function executeLiquidateWithReplacement(State storage state, LiquidateWithReplacementParams calldata params)
        external
        returns (uint256 issuanceValue, uint256 liquidatorProfitCollateralToken, uint256 liquidatorProfitBorrowToken)
    {
        emit Events.LiquidateWithReplacement(params.debtPositionId, params.borrower, params.minimumCollateralProfit);

        DebtPosition storage debtPosition = state.getDebtPosition(params.debtPositionId);
        DebtPosition memory debtPositionCopy = debtPosition;
        BorrowOffer storage borrowOffer = state.data.users[params.borrower].borrowOffer;
        uint256 tenor = debtPositionCopy.dueDate - block.timestamp;

        liquidatorProfitCollateralToken = state.executeLiquidate(
            LiquidateParams({
                debtPositionId: params.debtPositionId,
                minimumCollateralProfit: params.minimumCollateralProfit
            })
        );

        uint256 ratePerTenor = borrowOffer.getRatePerTenor(
            VariablePoolBorrowRateParams({
                variablePoolBorrowRate: state.oracle.variablePoolBorrowRate,
                variablePoolBorrowRateUpdatedAt: state.oracle.variablePoolBorrowRateUpdatedAt,
                variablePoolBorrowRateStaleRateInterval: state.oracle.variablePoolBorrowRateStaleRateInterval
            }),
            tenor
        );
>       issuanceValue = Math.mulDivDown(debtPositionCopy.futureValue, PERCENT, PERCENT + ratePerTenor);
        liquidatorProfitBorrowToken = debtPositionCopy.futureValue - issuanceValue;

        debtPosition.borrower = params.borrower;
        debtPosition.futureValue = debtPositionCopy.futureValue;
        debtPosition.liquidityIndexAtRepayment = 0;

        emit Events.UpdateDebtPosition(
            params.debtPositionId,
            debtPosition.borrower,
            debtPosition.futureValue,
            debtPosition.liquidityIndexAtRepayment
        );

        state.data.debtToken.mint(params.borrower, debtPosition.futureValue);
>       state.data.borrowAToken.transferFrom(address(this), params.borrower, issuanceValue);
        state.data.borrowAToken.transferFrom(address(this), state.feeConfig.feeRecipient, liquidatorProfitBorrowToken);
    }

Proof of Concept

Say two users setup the same sell limit order.

The first user’s order was bought by a user via BuyCreditMarket, and the second user’s order was bought by LiquidateWithReplacement. However, the swap fee is only charged to the first user. This is unfair and is not mentioned anywhere expected in the documents.

Recommended Mitigation Steps

Also charge swap fees during executeLiquidateWithReplacement.

aviggiano (Size) acknowledged and commented:

Feedback from the team:

  • Technically valid observation.
  • Whether we fix it or not is a product decision.
  • Technically this is a valid one since it is a credit for cash operation, since the chosen borrower sells his credit for cash.
  • Just imagine what would happen if that credit sell limit order would have been filled by a credit buy market order: the swap fee would have been charged, and liquidateWithReplacement() is essentially not different from doing a credit buy limit order filling a credit sell limit order.
  • From a product perspective it is not clear if it was decided to treat this in a special way, i.e., not to charge the swap fee on the credit seller, as it should be, as a form of incentive since it is highly desirable from the protocol perspective to have many such offers as the protocol earns a lot from a liquidation with replacement.
  • The liquidateWithReplacement() leads to the same outcome of the original loan for the lender and it should be indistinguishable from a standard credit sell limit order being filled for the borrower.
  • So regarding the latter, for consistency, the borrower should pay the swap fee; however, it is fair to observe the protocol already makes a big profit from liquidateWithReplacement() since it takes the full time value of money that otherwise in case of a standard liquidation would have been earned entirely by the lender, and since atm liquidateWithReplacement() atm has some limitations (see the auditors remarks about the problem of running liquidateWithReplacement() on large loans, due to the inability of fractionalize debt) then it could make sense to incentivize it.
  • I say “could” because that’s a product decision, not relevant for the protocol accounting.

hansfriese (judge) commented:

Thanks for the detailed feedback. Will keep as a valid Medium.


The original report as published on Code4rena, by pkqs90. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the executeLiquidateWithReplacement function omits the swap fee calculation that is present in the standard credit purchase path.

Impact

Borrowers whose credit is purchased via liquidation avoid the swap fee, giving them an unfair advantage over those who pay it through normal credit purchases.

What the attacker needed

A credit sell limit order exists; a liquidator uses LiquidateWithReplacement

The fix

The swap fee should also be charged during executeLiquidateWithReplacement, though the team has not committed to fixing it.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

In this protocol, when someone buys credit (a loan) from a borrower, a small fee is supposed to be charged to the borrower. The LiquidateWithReplacement function also buys credit from a borrower during a liquidation, but it forgets to charge that fee. So borrowers whose credit is bought this way pay less than those whose credit is bought normally.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by pkqs90

View all

25 more disclosed reports, best first.

More from Size

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.