Skip to content
Race condition Audit finding Intermediate

A “FrontRunning attack” can be made to the initialize function

The initialize function of a new liquidity pool can be front-run by a bot that sees the pending transaction and calls initialize first with a different initial price. This lets the attacker set the pool's starting price, potentially causing the original creator to deposit at an unfavourable price.

0xSmartContract Follow

Originally disclosed on Code4rena

Submitted by 0xSmartContract, also found by 0xDecorativePineapple, 0xmatt, 0xNazgul, berndartmueller, brgltd, catchup, ch13fd357r0y3r, cryptonue, Jeiwan, ladboy233, minhtrng, neko_nyaa, rbserver, rvierdiiev, s3cunda, and Trust

AlgebraPool.sol#L193-L206

The initialize function in AlgebraPool.sol#L193-L206 is a very important function and sets the liquidity price at the beginning of the pool.

Performs some checks (For example, if the price is not 0).

However it is unprotected against running from the front, and a bot listening to Mempool will run from the front and cause its pool to start at too high or too low of a price.

It is very important that this function is not enabled for FrontRunning operation.

function initialize(uint160 initialPrice) external override {
    require(globalState.price == 0, 'AI');
    // getTickAtSqrtRatio checks validity of initialPrice inside
    int24 tick = TickMath.getTickAtSqrtRatio(initialPrice);

    uint32 timestamp = _blockTimestamp();
    IDataStorageOperator(dataStorageOperator).initialize(timestamp, tick);

    globalState.price = initialPrice;
    globalState.unlocked = true;
    globalState.tick = tick;

    emit Initialize(initialPrice, tick);
  }

Proof of Concept

1- Alice starts a new pool in Algebra and triggers the price determination transaction with initialize. 2- Bob listens to the mempool with the following code, which is a minimal example, and sees at what price the initialize function is triggered with the initialPrice argument, and starts the pool at the price he wants by pre-executing it and makes Alice deposit the wrong amount at the wrong price.

mempool.js
 var customWsProvider = new ethers.providers.WebSocketProvider(url);
 customWsProvider.on("pending", (tx) => {
 let pendingTx = await connect.provider.getTransaction(txHash);
        if (pendingTx) {
            // filter pendingTx.data
            if (pendingTx.data.indexOf("0xf637731d") !== -1) {      //  func. signature : f637731d  =>  initialize(uint160)
               console.log(pendingTx);
            }
        }

Recommended Mitigation Steps

Add a modifier that ensures that only the authorized person, that is, the first liquidator to the pool, initiates the initialize function.

Or, divide the process of determining the price of the pool into parts, first print the price amounts to the state variable, and then make the initialize run before this price can be changed.

0xean (judge) decreased severity to Medium and commented:

Downgrading to Medium. Alice shouldn’t be relying on this initial price to determine the “fair” market price. When there is very limited liquidity the price is extremely easy to move along the x*y=k curve in any case so even after the initialized call is made someone could manipulate the pools pricing very easily when liquidity is low.

sameepsi (QuickSwap & StellaSwap) disputed and commented:

I don’t think it’s a bug. Even if someone sets the wrong price initially then it will be arbitraged. That’s how AMMs work by design.

0xean (judge) commented:

Going to leave as Medium - even if for nothing else as to warn users in the future to not trust on-chain pricing for new pools or pools with low liquidity.


The original report as published on Code4rena, by 0xSmartContract. 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 initialize function lacks access control, allowing any address to call it first and set the initial price.

Impact

The attacker could set the pool's initial price to an arbitrary value, causing the original creator to deposit at a wrong price; however, the judge noted that with low liquidity the price is easy to manipulate anyway and arbitrage would correct it.

What the attacker needed

Attacker can monitor the mempool; victim creates a new pool

The fix

The function should be restricted so only an authorised address can call it, or the price should be set in a separate step before initialization.

How it was found

  1. Alice starts a new pool and sends a transaction calling initialize with her chosen initial price.
  2. Bob monitors the mempool for pending transactions that call the initialize function.
  3. Bob sees Alice's transaction and the initialPrice argument.
  4. Bob submits his own initialize transaction with a different price and gets it mined before Alice's.

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.

When a new trading pool is created, a function sets its starting price. Because this function is public and the transaction is visible before it is confirmed, someone else can copy it and submit their own version first with a different price. The original creator then gets a pool with the attacker's price instead of their own.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by 0xSmartContract

View all

6 more disclosed reports, best first.

More from QuickSwap and StellaSwap contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.