Skip to main content

Core Concepts

This page explains the fundamental concepts you need to understand when working with Sera.

Price Indices

Sera uses an Arithmetic Price Book where prices are represented as indices rather than raw values. This provides:
  • Gas-efficient storage (uint16 vs uint256)
  • Predictable price levels
  • Easy price level iteration

Price Formula

price = minPrice + (tickSpace × priceIndex)
ParameterDescription
minPriceThe minimum price supported by the market
tickSpaceThe spacing between each price level
priceIndexAn integer from 0 to 65,535 (uint16)

Example

For a market with minPrice = 0.001 and tickSpace = 0.00001:
Price IndexActual Price
00.001000
1000.002000
5000.006000
10000.011000

Converting Between Price and Index

Price → Index:
// Get the index for a target price
const { index, correctedPrice } = await priceBook.priceToIndex(
  targetPrice,
  false  // roundingUp: false for bids, true for asks
);
Index → Price:
const price = await priceBook.indexToPrice(priceIndex);

Raw Amounts vs Token Amounts

Sera uses “raw amounts” internally for gas efficiency. Understanding the conversion is crucial.

Quote Unit

Each market has a quoteUnit that defines how many quote tokens equal one “raw unit”:
rawAmount = quoteAmount / quoteUnit
quoteAmount = rawAmount × quoteUnit

Conversions

ConversionFormula
Raw → QuotequoteAmount = rawAmount × quoteUnit
Quote → RawrawAmount = quoteAmount / quoteUnit
Raw → BasebaseAmount = rawAmount × quoteUnit / price
Base → RawrawAmount = baseAmount × price / quoteUnit

Example

For a market with quoteUnit = 1000000 (6 decimals):
// You want to buy 100 USDC worth
const quoteAmount = 100_000000n;  // 100 USDC (6 decimals)
const rawAmount = quoteAmount / 1000000n;  // = 100 raw units

Order Types

Limit Orders

Limit orders sit on the order book until filled or cancelled.
ParameterBid (Buy)Ask (Sell)
priceIndexMaximum price willing to payMinimum price willing to receive
rawAmountQuote tokens to spendNot used (set to 0)
baseAmountNot used (set to 0)Base tokens to sell
postOnlyIf true, order fails if it would fill immediatelySame

Market Orders

Market orders execute immediately against existing orders.
ParameterBid (Buy)Ask (Sell)
limitPriceIndexMaximum price willing to payMinimum price willing to receive
rawAmountMax quote to spend (expendInput) / Min quote to receive (!expendInput)Min quote to receive (expendInput) / Max quote to spend (!expendInput)
baseAmountMin base to receive (expendInput) / Max base to spend (!expendInput)Max base to spend (expendInput) / Min base to receive (!expendInput)
expendInputIf true, spend all input tokensSame

Order Lifecycle

┌──────────┐     ┌──────────┐     ┌──────────┐     ┌──────────┐
│  Create  │────▶│   Open   │────▶│  Filled  │────▶│ Claimed  │
└──────────┘     └──────────┘     └──────────┘     └──────────┘
                      │                                  ▲
                      │           ┌──────────┐          │
                      └──────────▶│ Cancelled │         │
                                  └──────────┘         │
                                       │               │
                                       └───────────────┘
StatusDescription
openOrder is on the book, waiting to be filled
filledOrder has been completely filled, proceeds ready to claim
partialOrder is partially filled
cancelledOrder was cancelled by the owner
claimedProceeds have been claimed

Fees

Sera uses a maker-taker fee model:
Fee TypeDescriptionTypical Value
Taker FeePaid by the party that takes liquidity0.1% (10 bps)
Maker FeePaid/received by the party that provides liquidity-0.05% (rebate)
Negative maker fees mean makers receive a rebate for providing liquidity. For testnet, all fees are set to 0.
Fee values are stored as basis points × 100:
  • 1000 = 0.1% = 10 bps
  • -500 = -0.05% = 5 bps rebate

Order NFTs

Each limit order creates an NFT representing ownership. This enables:
  • Transferability: Transfer order ownership to another address
  • Composability: Use orders in other DeFi protocols
  • Claim Delegation: Anyone holding the NFT can claim proceeds
// Get the NFT ID from an order
const nftId = order.nftId;

// Transfer order ownership
await orderNFT.transferFrom(owner, newOwner, nftId);

Next Steps