Key Properties of the Price Data Structure
1. Data Type
UINT 256:
Price values are stored as a 256-bit unsigned integer, ensuring sufficient precision for complex computations.
This representation supports extremely large values while maintaining compatibility with Ethereum’s architecture.
Not Scaled Integers:
Prices are expressed as raw integers, avoiding scaling complexities common in floating-point arithmetic.
2. ERC-20 Scaling Standard
Scale Integer (10¹⁸):
Prices follow the same scale as ERC-20 tokens (e.g., 1 ETH = 10¹⁸ units). This ensures consistency across decentralized applications and simplifies integration with other DeFi protocols.
3. Binary Representation
Q128.128 Format:
Prices are stored using a binary fixed-point representation:
The first 128 bits represent the integer portion of the price.
The last 128 bits represent the fractional (decimal) portion of the price.
This format ensures precision for both whole and fractional values, avoiding rounding errors in calculations.
4. Optimized Storage
Single-Sided Price Storage:
The protocol only keeps one price (e.g., X) instead of storing both sides of a pair (e.g., X and Y).
Using a single price reduces storage requirements and computational overhead while enabling the calculation of the second value when needed.