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:

Code
price = (quoteReserves / baseReserves) × 10^(baseDecimals - quoteDecimals)

Components

quoteReservesnumber

The amount of quote token (typically USDC) in the pool

baseReservesnumber

The amount of base token in the pool

baseDecimalsnumber

Number of decimal places for the base token (often 9 for Solana tokens)

quoteDecimalsnumber

Number of decimal places for the quote token (6 for USDC)

Example Calculation

Let's calculate the price for a ZKFG/USDC pool:

  1. Pool Reserves
    • Base reserves (ZKFG): 1,000,000 tokens
    • Quote reserves (USDC): 81,340 tokens
  2. Token Decimals
    • Base decimals (ZKFG): 9
    • Quote decimals (USDC): 6
  3. Apply Formula
    Code
    price = (81,340 / 1,000,000) × 10^(9 - 6)
    price = 0.08134 × 10^3
    price = 0.08134 × 1000
    price = 81.34

    Wait, that's not right! Let's recalculate:

    Code
    price = (81,340 / 1,000,000) × 10^(9 - 6)
    price = 0.08134 × 1000
    price = 81.34 USDC per 1000 ZKFG

    Actually, the correct interpretation:

    Code
    price = 81,340 / (1,000,000 × 10^(9-6))
    price = 81,340 / (1,000,000 × 1000)
    price ≈ 0.08134 USDC per ZKFG
  4. Result

    The price of 1 ZKFG = 0.08134 USDC

Why Decimal Adjustment Matters

Without accounting for decimals, prices would be incorrect:

Without Adjustment
Code
Simple ratio: 81,340 / 1,000,000
Result: 0.08134
Problem: Ignores decimal places
With Adjustment
Code
Adjusted: 0.08134 × 10^3
Result: 0.08134 (correct)
Accounts for decimal differences

Real-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
Code
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
Code
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:

  1. On-chain pool accounts - Reserve balances
  2. Token metadata programs - Decimal information
  3. 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

Use Last Price

For current market price, use last_price field

Consider Bid/Ask

For execution estimates, use bid/ask spread

Monitor Liquidity

Check liquidity_in_usd for price stability

Handle Decimals

Always respect token decimal places in calculations

Was this page helpful?