Skip to content
LogoLogo

Watch Token Creation

token.watchCreate

Watches TIP-20 TokenCreated events from the token factory.

Usage

import {  } from './viem.config'
 
const  = ..({})
 
.(() => {
  for (const  of ) .(.)
{  token: '0x20c0000000000000000000000000000000000042',  name: 'My Token',  symbol: 'MYT',  currency: 'USD',  quoteToken: '0x20C0000000000000000000000000000000000000',  admin: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',  salt: '0x9d3a4e2f1b8c65d0a7e4f2c9b1d8360e5a2c7f41d9b06e8352c1a4f7e0b6d914'}
})
 
// Later, tear down the watcher.
.()

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

Recipes

Screen New Tokens Quoted in Your Stablecoin

Watch factory creations and keep only tokens whose quoteToken matches your stablecoin, comparing addresses with Address.isEqual, for example to seed a listing pipeline.

import {  } from 'viem/utils'
import {  } from './viem.config'
 
const  = '0x20c0000000000000000000000000000000000000'
 
const  = ..({})
 
.(() => {
  for (const  of ) {
    if (!.(.., )) continue
    .('Listing candidate:', .., ..)
Listing candidate: 0x20c0000000000000000000000000000000000042 MYT
  }
})

Keep a Token Registry Current After Restarts

Pass fromBlock when your indexer restarts, so tokens created while it was offline are replayed into the registry before live events.

import {  } from './viem.config'
 
// Last block the registry indexed, loaded from its store.
const  = 1_204_320n
 
const  = ..({
  :  + 1n,
})
 
.(() => {
  for (const  of ) {
    // Upsert into the registry keyed by token address.
    .(.., .., .., ..)
  }
})

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 `TokenCreated` event arguments. */
  args: {
    /** Address of the created token. */
    token: Address
    /** Name of the token. */
    name: string
    /** Symbol of the token. */
    symbol: string
    /** Currency of the token. */
    currency: string
    /** Quote token address. */
    quoteToken: Address
    /** Admin address. */
    admin: Address
    /** Salt used to derive the token address. */
    salt: Hex
  }
  /** Name of the emitted event. */
  eventName: 'TokenCreated'
  // ...standard log fields (address, blockHash, blockNumber, logIndex, transactionHash, ...)
}

Parameters

args

  • Type: object
type Args = {
  /** Filter by created token address. */
  token?: Address | 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).