numotrade / numo2

🤖 Leverage on long-tail tokens.
Other
0 stars 0 forks source link

Precision of invariant() is inconsistent #8

Open robertleifke opened 1 week ago

robertleifke commented 1 week ago

Description:

The invariant function of the Pair.sol contract does not function as intended due to a mismatch in fixed-point precision. As shown in figure 6.1, the invariant method multiplies and divides values by 1e18 to manage precision. For example, the scale0 and scale1 values are multiplied by 1e18 before dividing by the value of the liquidity variable to maintain a precision of 1e18, the same as that of the function’s input values.

The a, b, and c terms of the core invariant logic increase the precision to 1e36, which is appropriate for arithmetic that is sensitive to rounding errors. The d term, however, uses native exponentiation to raise a value with 1e18 precision to the fourth power, yielding a number with 1e72 precision. The underlying value of the d term is therefore orders of magnitude larger than that of the others, preventing the invariant from being satisfied under expected circumstances (e.g., the arithmetic operations can easily revert due to overflow protection).

Action items: