Skip to main content

OrderBook ABI

Each trading pair has its own OrderBook contract (also called a “market”). Use these view functions to query market state.
Market addresses can be found via the Markets GraphQL Query or the Tokens page.

Market Info

[
  {
    "name": "quoteToken",
    "type": "function",
    "stateMutability": "view",
    "inputs": [],
    "outputs": [{ "name": "", "type": "address" }]
  },
  {
    "name": "baseToken",
    "type": "function",
    "stateMutability": "view",
    "inputs": [],
    "outputs": [{ "name": "", "type": "address" }]
  },
  {
    "name": "quoteUnit",
    "type": "function",
    "stateMutability": "view",
    "inputs": [],
    "outputs": [{ "name": "", "type": "uint256" }]
  },
  {
    "name": "makerFee",
    "type": "function",
    "stateMutability": "view",
    "inputs": [],
    "outputs": [{ "name": "", "type": "int24" }]
  },
  {
    "name": "takerFee",
    "type": "function",
    "stateMutability": "view",
    "inputs": [],
    "outputs": [{ "name": "", "type": "uint24" }]
  },
  {
    "name": "orderToken",
    "type": "function",
    "stateMutability": "view",
    "inputs": [],
    "outputs": [{ "name": "", "type": "address" }]
  },
  {
    "name": "priceBook",
    "type": "function",
    "stateMutability": "view",
    "inputs": [],
    "outputs": [{ "name": "", "type": "address" }]
  }
]
FunctionReturnsDescription
quoteTokenaddressThe quote token contract address
baseTokenaddressThe base token contract address
quoteUnituint256Amount that one raw unit represents in quote tokens
makerFeeint24Maker fee (100 = 1bp). Negative = rebate
takerFeeuint24Taker fee (100 = 1bp)
orderTokenaddressThe Order NFT contract address
priceBookaddressThe PriceBook contract address

Order Book State

[
  {
    "name": "isEmpty",
    "type": "function",
    "stateMutability": "view",
    "inputs": [{ "name": "isBid", "type": "bool" }],
    "outputs": [{ "name": "", "type": "bool" }]
  },
  {
    "name": "bestPriceIndex",
    "type": "function",
    "stateMutability": "view",
    "inputs": [{ "name": "isBid", "type": "bool" }],
    "outputs": [{ "name": "", "type": "uint16" }]
  },
  {
    "name": "getDepth",
    "type": "function",
    "stateMutability": "view",
    "inputs": [
      { "name": "isBid", "type": "bool" },
      { "name": "priceIndex", "type": "uint16" }
    ],
    "outputs": [{ "name": "", "type": "uint64" }]
  },
  {
    "name": "getOrder",
    "type": "function",
    "stateMutability": "view",
    "inputs": [{
      "name": "orderKey",
      "type": "tuple",
      "components": [
        { "name": "isBid", "type": "bool" },
        { "name": "priceIndex", "type": "uint16" },
        { "name": "orderIndex", "type": "uint256" }
      ]
    }],
    "outputs": [{
      "name": "",
      "type": "tuple",
      "components": [
        { "name": "amount", "type": "uint64" },
        { "name": "claimBounty", "type": "uint32" },
        { "name": "owner", "type": "address" }
      ]
    }]
  },
  {
    "name": "getClaimable",
    "type": "function",
    "stateMutability": "view",
    "inputs": [{
      "name": "orderKey",
      "type": "tuple",
      "components": [
        { "name": "isBid", "type": "bool" },
        { "name": "priceIndex", "type": "uint16" },
        { "name": "orderIndex", "type": "uint256" }
      ]
    }],
    "outputs": [
      { "name": "claimableRawAmount", "type": "uint64" },
      { "name": "claimableAmount", "type": "uint256" },
      { "name": "feeAmount", "type": "uint256" },
      { "name": "rebateAmount", "type": "uint256" }
    ]
  }
]
FunctionDescription
isEmpty(isBid)Returns true if the specified side has no orders
bestPriceIndex(isBid)Returns the best price index (highest bid or lowest ask)
getDepth(isBid, priceIndex)Returns total open amount at a price level
getOrder(orderKey)Returns order details (amount, claimBounty, owner)
getClaimable(orderKey)Returns claimable amounts and fees for a filled order

Price Conversion

[
  {
    "name": "indexToPrice",
    "type": "function",
    "stateMutability": "view",
    "inputs": [{ "name": "priceIndex", "type": "uint16" }],
    "outputs": [{ "name": "price", "type": "uint256" }]
  },
  {
    "name": "priceToIndex",
    "type": "function",
    "stateMutability": "view",
    "inputs": [
      { "name": "price", "type": "uint256" },
      { "name": "roundingUp", "type": "bool" }
    ],
    "outputs": [
      { "name": "index", "type": "uint16" },
      { "name": "correctedPrice", "type": "uint256" }
    ]
  },
  {
    "name": "rawToBase",
    "type": "function",
    "stateMutability": "view",
    "inputs": [
      { "name": "rawAmount", "type": "uint64" },
      { "name": "priceIndex", "type": "uint16" },
      { "name": "roundingUp", "type": "bool" }
    ],
    "outputs": [{ "name": "", "type": "uint256" }]
  },
  {
    "name": "rawToQuote",
    "type": "function",
    "stateMutability": "view",
    "inputs": [{ "name": "rawAmount", "type": "uint64" }],
    "outputs": [{ "name": "", "type": "uint256" }]
  },
  {
    "name": "baseToRaw",
    "type": "function",
    "stateMutability": "view",
    "inputs": [
      { "name": "baseAmount", "type": "uint256" },
      { "name": "priceIndex", "type": "uint16" },
      { "name": "roundingUp", "type": "bool" }
    ],
    "outputs": [{ "name": "", "type": "uint64" }]
  },
  {
    "name": "quoteToRaw",
    "type": "function",
    "stateMutability": "view",
    "inputs": [
      { "name": "quoteAmount", "type": "uint256" },
      { "name": "roundingUp", "type": "bool" }
    ],
    "outputs": [{ "name": "", "type": "uint64" }]
  }
]
FunctionDescription
indexToPrice(priceIndex)Converts a price index to the actual price
priceToIndex(price, roundingUp)Converts a price to the nearest index
rawToBase(rawAmount, priceIndex, roundingUp)Converts raw amount to base token amount
rawToQuote(rawAmount)Converts raw amount to quote token amount
baseToRaw(baseAmount, priceIndex, roundingUp)Converts base token amount to raw amount
quoteToRaw(quoteAmount, roundingUp)Converts quote token amount to raw amount

ERC20 Token ABI

For token approvals before trading:
[
  {
    "name": "approve",
    "type": "function",
    "inputs": [
      { "name": "spender", "type": "address" },
      { "name": "amount", "type": "uint256" }
    ],
    "outputs": [{ "name": "", "type": "bool" }]
  },
  {
    "name": "allowance",
    "type": "function",
    "stateMutability": "view",
    "inputs": [
      { "name": "owner", "type": "address" },
      { "name": "spender", "type": "address" }
    ],
    "outputs": [{ "name": "", "type": "uint256" }]
  },
  {
    "name": "balanceOf",
    "type": "function",
    "stateMutability": "view",
    "inputs": [{ "name": "account", "type": "address" }],
    "outputs": [{ "name": "", "type": "uint256" }]
  },
  {
    "name": "decimals",
    "type": "function",
    "stateMutability": "view",
    "inputs": [],
    "outputs": [{ "name": "", "type": "uint8" }]
  },
  {
    "name": "symbol",
    "type": "function",
    "stateMutability": "view",
    "inputs": [],
    "outputs": [{ "name": "", "type": "string" }]
  }
]

Usage Example

import { ethers } from 'ethers';

const ORDERBOOK_ABI = [
  {
    "name": "bestPriceIndex",
    "type": "function",
    "stateMutability": "view",
    "inputs": [{ "name": "isBid", "type": "bool" }],
    "outputs": [{ "name": "", "type": "uint16" }]
  },
  {
    "name": "indexToPrice",
    "type": "function",
    "stateMutability": "view",
    "inputs": [{ "name": "priceIndex", "type": "uint16" }],
    "outputs": [{ "name": "price", "type": "uint256" }]
  },
  {
    "name": "getDepth",
    "type": "function",
    "stateMutability": "view",
    "inputs": [
      { "name": "isBid", "type": "bool" },
      { "name": "priceIndex", "type": "uint16" }
    ],
    "outputs": [{ "name": "", "type": "uint64" }]
  }
];

const MARKET_ADDRESS = '0x2e4a11c7711c6a69ac973cbc40a9b16d14f9aa7e';  // EURC/XSGD

const provider = new ethers.JsonRpcProvider('https://0xrpc.io/sep');
const market = new ethers.Contract(MARKET_ADDRESS, ORDERBOOK_ABI, provider);

// Get best bid price
const bestBidIndex = await market.bestPriceIndex(true);
const bestBidPrice = await market.indexToPrice(bestBidIndex);
console.log(`Best bid: ${ethers.formatUnits(bestBidPrice, 18)}`);

// Get depth at best bid
const depth = await market.getDepth(true, bestBidIndex);
console.log(`Depth at best bid: ${depth}`);
For most use cases, the GraphQL API provides an easier way to query order book depth.