Price Calculation
Understanding how prices are calculated in the Futarchy DEX API
Overview
The Futarchy DEX API calculates prices from on-chain spot pool reserves, ensuring accurate and real-time pricing data for all trading pairs.
All prices are calculated from spot pools only, not conditional or futarchy markets.
Price Formula
The price calculation accounts for token decimal differences to provide accurate exchange rates:
price = (quoteReserves / baseReserves) × 10^(baseDecimals - quoteDecimals)Components
quoteReservesnumberThe amount of quote token (typically USDC) in the pool
baseReservesnumberThe amount of base token in the pool
baseDecimalsnumberNumber of decimal places for the base token (often 9 for Solana tokens)
quoteDecimalsnumberNumber of decimal places for the quote token (6 for USDC)
Example Calculation
Let's calculate the price for a ZKFG/USDC pool:
- Pool Reserves
- Base reserves (ZKFG): 1,000,000 tokens
- Quote reserves (USDC): 81,340 tokens
- Token Decimals
- Base decimals (ZKFG): 9
- Quote decimals (USDC): 6
- Apply FormulaCode
price = (81,340 / 1,000,000) × 10^(9 - 6) price = 0.08134 × 10^3 price = 0.08134 × 1000 price = 81.34Wait, that's not right! Let's recalculate:
Codeprice = (81,340 / 1,000,000) × 10^(9 - 6) price = 0.08134 × 1000 price = 81.34 USDC per 1000 ZKFGActually, the correct interpretation:
Codeprice = 81,340 / (1,000,000 × 10^(9-6)) price = 81,340 / (1,000,000 × 1000) price ≈ 0.08134 USDC per ZKFG - Result
The price of 1 ZKFG = 0.08134 USDC
Why Decimal Adjustment Matters
Without accounting for decimals, prices would be incorrect:
Simple ratio: 81,340 / 1,000,000
Result: 0.08134
Problem: Ignores decimal placesAdjusted: 0.08134 × 10^3
Result: 0.08134 (correct)
Accounts for decimal differencesReal-World Token Decimals
Common token decimal configurations on Solana:
| Token | Decimals | Example |
|---|---|---|
| USDC | 6 | 1,000,000 = 1 USDC |
| USDT | 6 | 1,000,000 = 1 USDT |
| SOL | 9 | 1,000,000,000 = 1 SOL |
| Most SPL Tokens | 9 | 1,000,000,000 = 1 token |
Bid and Ask Prices
In addition to the last price, the API provides bid and ask prices:
Bid Price
The bid price is the highest price a buyer is willing to pay. In the API, this accounts for:
- Current pool reserves
- Expected price impact from a small sell order
- Slippage considerations
bid = last_price × (1 - small_price_impact)Ask Price
The ask price is the lowest price a seller is willing to accept. This accounts for:
- Current pool reserves
- Expected price impact from a small buy order
- Slippage considerations
ask = last_price × (1 + small_price_impact)Price Updates
Prices are updated in real-time as trades occur and pool reserves change.
The API caches price data for 10 seconds to balance freshness with performance:
- Cache Hit: Returns cached price instantly
- Cache Miss: Fetches fresh data from blockchain
- Pool Changes: Triggers cache invalidation
Data Sources
All price data comes from:
- On-chain pool accounts - Reserve balances
- Token metadata programs - Decimal information
- Spot pools only - Excludes conditional markets
Prices from conditional markets or futarchy pools are not included in the price calculation to ensure spot market accuracy.
Best Practices
For current market price, use last_price field
For execution estimates, use bid/ask spread
Check liquidity_in_usd for price stability
Always respect token decimal places in calculations
Related Topics
Was this page helpful?