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
- The pool has reserves
_pool.reserve1 = 199 * 10**18and_pool.reserve0 = 100 * 10**18. - A call to
priceALast()executes_pool.reserve1 / _pool.reserve0which is(199 * 10**18) / (100 * 10**18). - Due to integer division, the result is truncated from the actual price (1.99) to
1. - Alternatively, if
_pool.reserve1 = 99 * 10**18and_pool.reserve0 = 100 * 10**18, the calculation(99 * 10**18) / (100 * 10**18)results in0because integer division rounds down. The same logic applies symmetrically topriceBLastwhen_pool.reserve0is 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));
});
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
- Set pool reserves to 199e18 and 100e18.
- Call priceALast(), which computes reserve1 / reserve0.
- Observe the result is truncated to 1 instead of 1.99.
- 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.