Skip to content
LogoLogo

Watch DEX Order Fills

dex.watchOrderFilled

Watches OrderFilled events on the DEX.

Usage

import {  } from './viem.config'
 
const  = ..()
 
.(() => {
  for (const  of ) .(.)
{  orderId: 123n,  maker: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',  taker: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',  amountFilled: 100000000n,  partialFill: false,}
})
 
// Later, tear down the watcher.
.()

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

Recipes

Update Order Status as Fills Land

Pass a maker filter to receive only fills of your own orders, and branch on partialFill to mark them partially or fully filled in a status store.

import {  } from './viem.config'
 
const  = ..({
  : { : .. },
})
 
.(() => {
  for (const  of ) {
    const { , ,  } = .
    if () .(`Order ${}: filled ${}`)
    else .(`Order ${}: fully filled`)
  }
})

Reconcile Fills After Downtime

Pass fromBlock with the order IDs you track to replay fills that landed while your service was offline, then continue streaming live fills.

import {  } from './viem.config'
 
const  = [123n, 456n, 789n]
const  = 1_234_567n
 
const  = ..({
  : { :  },
  :  + 1n,
})
 
for await (const {  } of ) {
  for (const  of ) .(.., ..)
}

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 `OrderFilled` event arguments. */
  args: {
    /** Order ID. */
    orderId: bigint
    /** Order maker. */
    maker: Address
    /** Order taker. */
    taker: Address
    /** Amount filled. */
    amountFilled: bigint
    /** Whether the fill was partial. */
    partialFill: boolean
  }
  /** Name of the emitted event. */
  eventName: 'OrderFilled'
  // ...standard log fields (address, blockHash, blockNumber, logIndex, transactionHash, ...)
}

Parameters

args

  • Type: object
type Args = {
  /** Filter by order ID. */
  orderId?: bigint | readonly bigint[] | null
  /** Filter by order maker. */
  maker?: Address | readonly Address[] | null
  /** Filter by order taker. */
  taker?: Address | readonly Address[] | 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).