Skip to content
LogoLogo

Watch DEX Order Cancellations

dex.watchOrderCancelled

Watches OrderCancelled events on the DEX.

Usage

import {  } from './viem.config'
 
const  = ..()
 
.(() => {
  for (const  of ) .(.)
{  orderId: 123n,}
})
 
// Later, tear down the watcher.
.()

Also callable standalone: Actions.dex.watchOrderCancelled(client, options), with Actions imported from 'viem/tempo'.

Recipes

Detect Cancellations You Did Not Initiate

Filter by your tracked order IDs to keep an open-orders store accurate: anyone can clear stale orders with dex.cancelStale, so cancellations can happen without your involvement.

import {  } from './viem.config'
 
const  = [123n, 456n]
 
const  = ..({
  : { :  },
})
 
.(() => {
  for (const  of ) .('Remove from store:', ..)
Remove from store: 123n
})

Return Value

Returns a watcher handle. Watching starts when the first listener (or async iterator) attaches, and stops when off is called.

type ReturnType = {
  /** Registers a log listener. Starts the watcher on first registration. Returns an unregister function. */
  onLogs: (fn: (logs: readonly Log[]) => void) => () => void
  /** Registers an error listener. Returns an unregister function. */
  onError: (fn: (error: Error) => void) => () => void
  /** Tears down the watcher: removes listeners, ends iterators, stops the poll or subscription. */
  off: () => void
  /** Async-iterates emitted log batches (latest-only stream). */
  [Symbol.asyncIterator]: () => AsyncIterableIterator<{ logs: readonly Log[] }>
}

Each log is decoded, with the event arguments on args:

type Log = {
  /** Decoded `OrderCancelled` event arguments. */
  args: {
    /** Cancelled order ID. */
    orderId: bigint
  }
  /** Name of the emitted event. */
  eventName: 'OrderCancelled'
  // ...standard log fields (address, blockHash, blockNumber, logIndex, transactionHash, ...)
}

Parameters

args

  • Type: object
type Args = {
  /** Filter by order ID. */
  orderId?: bigint | readonly bigint[] | null
}

Indexed argument values to filter logs by.

batch

  • Type: boolean
  • Default: true

Whether to batch the logs found within a poll interval into a single emission. When false, each log is emitted on its own.

fromBlock

  • Type: bigint

Block number from which to start watching for logs.

poll

  • Type: boolean

Whether to poll for new logs instead of using a subscription. Defaults to true when the transport cannot subscribe or fromBlock is provided.

pollingInterval

  • Type: number
  • Default: client.pollingInterval

Polling frequency (in ms).