Skip to content
Business logic Audit finding Beginner

Fee amount is recomputed multiple times when the initial value has already been cached

The repay function in AlchemistV3 recomputes the fee amount three times instead of using the cached value, wasting gas and increasing the chance of errors. An attacker could not exploit this directly, but it makes the contract less efficient and more error-prone.

Josh4324 Follow

Originally disclosed on Immunefi

Informational View original
  • Disclosed after under an hour

56561 sc insight fee amount is recomputed multiple times when the initial value has already been cached

Submitted on Oct 17th 2025 at 16:49:54 UTC by @Josh4324 for Audit Comp | Alchemix V3

Description

Brief/Intro

The fee amount is being calculated in every place that it is used, despite it being cached earlier. This results in additional gas usage and is also prone to errors.

Vulnerability Details

Check the repay function in the Alchemix V3.

  1. The feeAmount is calculated and stored.
  2. The feeAmount variable is used.
    1. and 5. The feeAmount is recomputed 3 times, thus wasting gas and increasing the error surface.
Solidity
1)->  uint256 feeAmount = creditToYield * protocolFee / BPS;
2)->        if (feeAmount > account.collateralBalance) {
            revert("Not enough collateral to pay for debt fee");
        } else {
3)->            account.collateralBalance -= creditToYield * protocolFee / BPS; 
        }

        _subDebt(recipientTokenId, credit);

        // Transfer the repaid tokens to the transmuter.
        TokenUtils.safeTransferFrom(myt, msg.sender, transmuter, creditToYield); 
4)->        TokenUtils.safeTransfer(myt, protocolFeeReceiver, creditToYield * protocolFee / BPS); 
5)->         _mytSharesDeposited -= creditToYield * protocolFee / BPS; 

        emit Repay(msg.sender, amount, recipientTokenId, creditToYield);

Impact Details

  1. More gas is consumed.
  2. The error surface is increased, especially when updating the code.

References

https://github.com/alchemix-finance/v3-poc/blob/immunefi\\_audit/src/AlchemistV3.sol?utm\\_source=immunefi#L530C4-L541C66

Proof of Concept

Proof of Concept

Please run this on Remix.

It has two functions repeat and noRepeat. The repeat function repeats the fee calculation three times, the same as the above function, while the no repeat uses the cached value instead.

output repeat cost: 9759 to execute. noRepeat cost: 8204 to execute

Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;


contract Repeat {

    uint constant BPS = 10000;
    uint public protocolFee = 1000;
    uint _mytSharesDeposited = type(uint).max;
    
    function repeat() external  {
        uint collateralBalance = 1000 ether;
        uint creditToYield = 1000 ether;

        uint256 feeAmount = creditToYield * protocolFee / BPS;
        if (feeAmount > collateralBalance) {
            revert("Not enough collateral to pay for debt fee");
        } else {
            collateralBalance -= creditToYield * protocolFee / BPS; //@audit insight M feeAmount is computed multiple times
        }

        // Mock the transfer of tokens
        mock_transfer(creditToYield * protocolFee / BPS); //@audit M H-2 fee is paid from the Alchemist, not the user?
        _mytSharesDeposited -= creditToYield * protocolFee / BPS; //@audit M H-3 Credit without fee should be added here
        
    }


    function noRepeat() external  {
        uint collateralBalance = 1000 ether;
        uint creditToYield = 1000 ether;

        uint256 feeAmount = creditToYield * protocolFee / BPS;
        if (feeAmount > collateralBalance) {
            revert("Not enough collateral to pay for debt fee");
        } else {
            collateralBalance -= feeAmount; //@audit insight M feeAmount is computed multiple times
        }

        // Mock the transfer of tokens
        mock_transfer(feeAmount); //@audit M H-2 fee is paid from the Alchemist, not the user?
        _mytSharesDeposited -= feeAmount; //@audit M H-3 Credit without fee should be added here
        
    }



    function mock_transfer(uint amount) internal {

    }


}

The original report as published on Immunefi, by Josh4324. 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

The developer recomputed the fee amount instead of using the already cached variable feeAmount.

What the attacker needed

None

The fix

The code should use the cached feeAmount variable instead of recalculating it.

How it was found

  1. Call the repay function.
  2. Observe that feeAmount is calculated and stored.
  3. Observe that the same calculation is repeated three more times instead of using the stored value.

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 a smart contract, every calculation costs a small fee called gas. The code here calculates the same fee three extra times instead of reusing the first result, which wastes gas and makes mistakes more likely if the code is changed later. This is not a security hole that lets someone steal funds, but it is a code quality issue.

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 Josh4324

View all

10 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.