Skip to content
LogoLogo

Get DEX Order

dex.getOrder

Gets an order by ID.

Usage

import {  } from './viem.config'
 
const  = await ..({
  : 123n,
})
 
.('Order:', )
Order: { orderId: 123n, maker: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', bookKey: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', isBid: true, tick: 100, amount: 100000000n, remaining: 100000000n, prev: 0n, next: 0n, isFlip: false, flipTick: 0 }

Recipes

Show Fill Progress in an Order Status UI

Use amount and remaining to derive how much of a resting order has filled, and Tick.toPrice to render its price for display.

import {  } from 'viem/tempo'
import {  } from './viem.config'
 
const  = await ..({ : 123n })
 
const  = . - .
const  = (( * 100n) / .)
 
.(
  `${. ? 'Buy' : 'Sell'} @ ${.(.)}: ${}% filled`,
)
Buy @ 1.001: 40% filled

Reprice an Order by Re-Posting the Remainder

Combine dex.getOrder with dex.cancelSync and dex.placeSync to pull a stale order and re-post its unfilled remainder at a new price.

import {  } from 'viem/tempo'
import {  } from './viem.config'
 
const  = await ..({ : 123n })
 
if (. > 0n) {
  await ..({ : . })
  await ..({
    : .,
    : .('0.999'),
    : '0x20c0000000000000000000000000000000000001',
    : . ? 'buy' : 'sell',
  })
}

Return Value

type ReturnType = {
  /** Order ID. */
  orderId: bigint
  /** Order maker. */
  maker: Address
  /** Orderbook key. */
  bookKey: Hex
  /** Whether the order is on the bid side. */
  isBid: boolean
  /** Price tick. */
  tick: number
  /** Original order amount. */
  amount: bigint
  /** Remaining order amount. */
  remaining: bigint
  /** Previous order ID at this tick. */
  prev: bigint
  /** Next order ID at this tick. */
  next: bigint
  /** Whether the order flips when filled. */
  isFlip: boolean
  /** Target tick for the flipped order. */
  flipTick: number
}

The decoded order state.

Parameters

orderId

  • Type: bigint

Order ID to query.

blockNumber (optional)

  • Type: bigint

Block number to read the state from.

blockOverrides (optional)

  • Type: BlockOverrides

Block overrides to apply to the state.

blockTag (optional)

  • Type: BlockTag

Block tag to read the state from.

stateOverride (optional)

  • Type: StateOverride

State override to apply.