Skip to content
Business logic Audit finding Intermediate

Fee not collected in _forceRepay when should

In the _forceRepay function, the protocol only deducts and transfers its fee when the account's collateral balance is strictly greater than the fee, so when the two are exactly equal the fee is skipped. This means the protocol can lose its expected commission in that edge case.

dldLambda Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

57771 sc medium fee not collected in forcerepay when should

Submitted on Oct 28th 2025 at 19:44:26 UTC by @dldLambda for Audit Comp | Alchemix V3

Description

Brief/Intro

In _forceRepay, after subtracting the repayment amount from collateral, the protocol calculates protocolFeeTotal and checks if (account.collateralBalance > protocolFeeTotal) before deducting and transferring the fee to protocolFeeReceiver. Because of the strict > comparison, the fee is not deducted when account.collateralBalance == protocolFeeTotal. Other parts of the code (e.g., repay) allow equality — this leads to inconsistent behavior and potential loss of protocol revenue in edge cases.

Vulnerability Details

Let's look at the code:

  1. _forceRepay:
        if (account.collateralBalance > protocolFeeTotal) {
            account.collateralBalance -= protocolFeeTotal;
            // Transfer the protocol fee to the protocol fee receiver
            TokenUtils.safeTransfer(myt, protocolFeeReceiver, protocolFeeTotal);
        }
  1. repay: (correct)
        // Debt is subject to protocol fee similar to redemptions
        uint256 feeAmount = creditToYield * protocolFee / BPS;
        if (feeAmount > account.collateralBalance) {
            revert("Not enough collateral to pay for debt fee");
        } else {
            account.collateralBalance -= creditToYield * protocolFee / BPS;
        }
  1. _doLiquidation: (correct)
        // send base fee to liquidator if available
        if (feeInYield > 0 && account.collateralBalance >= feeInYield) {
            TokenUtils.safeTransfer(myt, msg.sender, feeInYield);
        }

As you can see, in functions repay and _doLiquidation fee is written off if fee<=account.collateralBalance.

However, function _forceRepay, when fee = account.collateralBalance, considers that the fee cannot be written off.

This is an unjustified inconsistency and may result in loss of fee.

Impact Details

The protocol does not receive the expected protocol fee in edge cases => economic damage (lost commission)

References

https://github.com/alchemix-finance/v3-poc/blob/a192ab313c81ba3ab621d9ca1ee000110fbdd1e9/src/AlchemistV3.sol#L852 - _doLiquidation

https://github.com/alchemix-finance/v3-poc/blob/a192ab313c81ba3ab621d9ca1ee000110fbdd1e9/src/AlchemistV3.sol#L498 - repay

https://github.com/alchemix-finance/v3-poc/blob/a192ab313c81ba3ab621d9ca1ee000110fbdd1e9/src/AlchemistV3.sol#L738 - _forceRepay

Proof of Concept

Proof of Concept

Run this simple script-simulation:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import "forge-std/Script.sol";
import "forge-std/console.sol";

contract ForceRepaySimulation is Script {

    struct ForceRepayAccount {
        uint256 collateralBalance;
        uint256 protocolFeeTotal;
    }

    function simulateForceRepay(ForceRepayAccount memory account) internal pure returns (bool commissionPaid) {
        if (account.collateralBalance > account.protocolFeeTotal) {
            commissionPaid = true; 
        } else {
            commissionPaid = false; 
        }
    }

    function run() external {
        console.log("=== ForceRepay Bug Simulation ===");

        ForceRepayAccount memory a1 = ForceRepayAccount({collateralBalance: 100, protocolFeeTotal: 100});
        ForceRepayAccount memory a2 = ForceRepayAccount({collateralBalance: 101, protocolFeeTotal: 100});
        ForceRepayAccount memory a3 = ForceRepayAccount({collateralBalance: 50, protocolFeeTotal: 100});

        console.log("a1 commissionPaid:", simulateForceRepay(a1)); // false
        console.log("a2 commissionPaid:", simulateForceRepay(a2)); // true
        console.log("a3 commissionPaid:", simulateForceRepay(a3)); // false
    }
}

commands:

  1. forge init poc
  2. cd poc
  3. add this script to script/ folder
  4. forge build
  5. forge script script/ForceRepaySimulation.s.sol:ForceRepaySimulation --broadcast

And you will see:

== Logs ==
  === ForceRepay Bug Simulation ===
  a1 commissionPaid: false
  a2 commissionPaid: true
  a3 commissionPaid: false

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

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 a strict greater-than comparison in _forceRepay where the other fee paths use greater-than-or-equal, so the equality case is mishandled.

Impact

The protocol may not receive its expected fee in the edge case where collateral equals the fee, causing a small loss of revenue.

What the attacker needed

Collateral balance exactly equals fee

The fix

The comparison in _forceRepay should be changed to allow equality, matching the behaviour of repay and _doLiquidation.

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.

A smart contract is a program that runs on a blockchain and moves digital tokens around. Here, when the system force-closes a loan, it is supposed to take a small fee, but a comparison written with 'greater than' instead of 'greater than or equal to' means the fee is silently skipped when the leftover collateral exactly matches the fee. The result is the protocol quietly loses money in that one situation.

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 dldLambda

View all

8 more disclosed reports, best first.

More from Alchemix V3

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.