Skip to content
Audit finding Intermediate

Precision loss in priceALast and priceBLast

The price calculation functions priceALast and priceBLast use integer division on pool reserves, causing truncation of fractional parts. An attacker could exploit this precision loss to obtain incorrect prices, potentially leading to financial loss or manipulation.

hecker_trieu_tien Follow with 0xterrah, holtzzx and 2 others

Originally disclosed on Code4rena

Submitted by hecker_trieu_tien, also found by 0xterrah, holtzzx, PotEater, and rayss

https://github.com/code-423n4/2025-04-virtuals-protocol/blob/main/contracts/fun/FPair.sol#L103

Finding description and impact

The price calculation functions priceALast and priceBLast use integer division (/) directly on the pool reserves (_pool.reserve0, _pool.reserve1). Integer division in Solidity truncates any fractional part of the result, leading to significant precision loss.

Scenario

  1. The pool has reserves _pool.reserve1 = 199 * 10**18 and _pool.reserve0 = 100 * 10**18.
  2. A call to priceALast() executes _pool.reserve1 / _pool.reserve0 which is (199 * 10**18) / (100 * 10**18).
  3. Due to integer division, the result is truncated from the actual price (1.99) to 1.
  4. Alternatively, if _pool.reserve1 = 99 * 10**18 and _pool.reserve0 = 100 * 10**18, the calculation (99 * 10**18) / (100 * 10**18) results in 0 because integer division rounds down. The same logic applies symmetrically to priceBLast when _pool.reserve0 is divided by _pool.reserve1.

Recommended mitigation steps

To fix this issue, the contract should use a fixed-point arithmetic approach. Scale the numerator by a large factor (e.g., 10^18) before division.

Proof of Concept

describe("Price Calculation Precision Loss", function () {
    it("should demonstrate precision loss in priceALast function", async function () {
      const { fPair, router } = await loadFixture(deployFPairFixture);

      // Scenario 1: reserve1 = 199 * 10^18, reserve0 = 100 * 10^18
      // Expected price: 1.99, but due to integer division, it will be 1
      const reserve0 = parseEther("100");
      const reserve1 = parseEther("199");

      // Since only router can call mint, we need to impersonate it
      await fPair.connect(router).mint(reserve0, reserve1);

      // Check reserves were set correctly
      const [actualReserve0, actualReserve1] = await fPair.getReserves();
      expect(actualReserve0).to.equal(reserve0);
      expect(actualReserve1).to.equal(reserve1);

      // Check price calculation
      const priceA = await fPair.priceALast();
      console.log("Actual price should be 1.99, but priceALast() returns:", priceA.toString());

      // Verify the precision loss - price should be 1 instead of 1.99
      expect(priceA).to.equal(1);

      // Calculate the actual price with higher precision (using JavaScript)
      const actualPrice = Number(formatEther(reserve1)) / Number(formatEther(reserve0));
      console.log("Actual price calculated with JavaScript:", actualPrice);
      console.log("Precision loss:", actualPrice - Number(priceA));
    });

The original report as published on Code4rena, by hecker_trieu_tien. 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 contract performs integer division on the raw reserves without scaling the numerator to preserve precision.

Impact

The incorrect prices could lead to financial loss or manipulation, though the text does not specify a concrete exploit scenario.

What the attacker needed

None

The fix

The contract should scale the numerator by a large factor (e.g., 10^18) before division to preserve precision.

How it was found

  1. Set pool reserves to 199e18 and 100e18.
  2. Call priceALast(), which computes reserve1 / reserve0.
  3. Observe the result is truncated to 1 instead of 1.99.
  4. Alternatively, set reserves to 99e18 and 100e18, and the result becomes 0.

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.

Smart contracts often use whole numbers only, so dividing two whole numbers throws away any remainder. Here, the code divides two large reserve amounts directly, so a price like 1.99 becomes 1, and a price like 0.99 becomes 0. This means the contract reports wrong prices, which could be abused.

More by hecker_trieu_tien

View all

1 more disclosed report, best first.

More from Virtuals Protocol

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.