Skip to content
Audit finding Advanced

BalancerPriceOracle::validatePrice uses stale TWAP

The validatePrice function in BalancerPriceOracle calculates a time-weighted average price from snapshot data that is only refreshed when updatePrice is called, and it never triggers that refresh itself. An attacker could exploit stale price data to either block legitimate buyback or liquidity operations or to pass validation with a manipulated spot price, potentially enabling sandwich attacks.

santipu_ Follow with LinKenji, M4v3r1ck and 1 other

Originally disclosed on Code4rena

Submitted by santipu_, also found by LinKenji, M4v3r1ck, and PlayboiEvokid

autonolas-tokenomics/../contracts/oracles/BalancerPriceOracle.sol #L132

The BalancerPriceOracle contract’s validatePrice() function calculates the time-weighted average price using potentially outdated snapshot data because it never calls updatePrice() to refresh the oracle state before validation.

The root cause is that validatePrice() is a view function that only reads from storage without ensuring the underlying TWAP data has been recently updated current:

    function validatePrice(uint256 slippage) external view returns (bool) {
        // ...

        // Compute time-weighted average price
>>      uint256 timeWeightedAverage = (snapshot.cumulativePrice + (snapshot.averagePrice * elapsedTime)) /
            ((snapshot.cumulativePrice / snapshot.averagePrice) + elapsedTime);

        uint256 tradePrice = getPrice();

        // ...
    }

The TWAP calculation uses snapshot.averagePrice and snapshot.cumulativePrice, which are only updated when updatePrice() is explicitly called. If no one has called updatePrice() for an extended period, the formula extrapolates from stale data:

  • snapshot.averagePrice reflects the price from the last successful update, not the current market.
  • snapshot.cumulativePrice hasn’t accumulated price data for the intervening period.
  • The elapsedTime grows unboundedly, causing the stale averagePrice to dominate the TWAP calculation.

For example, if the oracle was last updated 24 hours ago at price X, and the current spot price is Y (significantly different from X), the TWAP calculation will be heavily weighted toward the outdated price X. This produces a TWAP that doesn’t accurately reflect recent market activity.

The problem is exacerbated because updatePrice() is a public function with no incentive mechanism—there’s no guarantee anyone will call it regularly. The contracts that depend on validatePrice() (BuyBackBurner and LiquidityManagerOptimism) don’t call updatePrice() before validation either:

    function _buyOLAS(address secondToken, uint256 secondTokenAmount) internal virtual returns (uint256 olasAmount) {
        // Get oracle address
        address poolOracle = mapV2Oracles[secondToken];

        // Check for zero address
        require(poolOracle != address(0), "Zero oracle address");

        // Apply slippage protection
>>      require(IOracle(poolOracle).validatePrice(maxSlippage), "Before swap slippage limit is breached");

        // ...
    }
    function _checkTokensAndRemoveLiquidityV2(address[] memory tokens, bytes32 v2Pool)
        internal
        virtual
        override
        returns (uint256[] memory amounts)
    {
        // ...

        // Apply slippage protection via V2 oracle: transform BPS into % as required by the function
>>      if (!IOracle(oracleV2).validatePrice(maxSlippage / 100)) {
            revert SlippageLimitBreached();
        }

        // ...
    }

This leads to two failure modes:

  1. False negatives: Legitimate operations are blocked because the stale TWAP doesn’t reflect the current market price, causing validatePrice() to return false even when the spot price is fair.
  2. False positives: If the current spot price happens to align with the stale TWAP (potentially through manipulation), validatePrice() returns true even though the TWAP is based on outdated data and doesn’t represent actual recent market activity.

Given that validatePrice() is the core protection mechanism for both BuyBackBurner and LiquidityManagerOptimism:

  1. BuyBackBurner: Buyback operations may be incorrectly blocked or incorrectly allowed based on stale price data, enabling either DoS or sandwich attacks.
  2. LiquidityManagerOptimism: Liquidity withdrawals face the same risk—legitimate withdrawals blocked, or manipulated withdrawals allowed through.

So the impact on both contracts are either a DoS under normal conditions, or allowed price manipulation that causes a loss of funds for the protocol due to sandwich attacks.

Recommended mitigation steps

There are different approaches to mitigate this issue:

  • Modify validatePrice() to call updatePrice() internally before performing the TWAP calculation. This requires changing validatePrice() from a view function to a state-modifying function.
  • Update the functions calling validatePrice() so they always call updatePrice().
  • Update validatePrice() so that it returns false if the TWAP hasn’t been updated in a long time.

The original report as published on Code4rena, by santipu_. 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 validatePrice view function reads stored TWAP snapshot values without ensuring they have been refreshed by updatePrice, so elapsed time grows and stale averagePrice dominates the calculation.

Impact

Legitimate buyback or liquidity operations could be blocked (DoS), or manipulated operations could pass validation and suffer sandwich attacks causing loss of funds.

What the attacker needed

UpdatePrice not called for an extended period; attacker can influence spot price

The fix

validatePrice should refresh the TWAP before validating, or callers should call updatePrice first, or validatePrice should return false when the TWAP is stale.

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 price oracle is a contract that tells other contracts what an asset is worth. This oracle uses an average price that only updates when someone explicitly calls a refresh function, but the check that uses the average never calls refresh first. So the check can rely on an old average that no longer matches the market, which can wrongly block fair trades or wrongly allow unfair ones.

More by santipu_

View all

13 more disclosed reports, best first.

More from Olas

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.