Skip to content
LogoLogo

Watch Token Burns

token.watchBurn

Watches TIP-20 Burn events for a token.

Usage

import {  } from './viem.config'
 
const  = ..({
  : '0x20c0000000000000000000000000000000000000',
})
 
.(() => {
  for (const  of ) .(.)
{ from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', amount: 50000000n }
})
 
// Later, tear down the watcher.
.()

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

Recipes

Reconcile Redemption Burns With Fiat Payouts

Filter args.from to your redemption wallet to confirm the burn behind each fiat payout, keeping issuance records aligned with the offchain ledger.

import {  } from './viem.config'
 
const  = ..({
  : { : '0x8ba1f109551bD432803012645Ac136ddd64DBA72' },
  : '0x20c0000000000000000000000000000000000000',
})
 
.(() => {
  for (const  of ) {
    // Match the burn to a pending payout by transaction hash.
    .('Redeemed:', .., .)
Redeemed: 250000000n 0x59a1c...
  }
})

Track Circulating Supply as Burns Land

Combine the watcher with token.getTotalSupply to keep a treasury dashboard's supply figure current after every burn.

import {  } from './viem.config'
 
const  = ..({
  : '0x20c0000000000000000000000000000000000000',
})
 
.(async () => {
  const {  } = await ..({
    : '0x20c0000000000000000000000000000000000000',
  })
  .('Circulating supply:', )
Circulating supply: 12500000
})

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 `Burn` event arguments. */
  args: {
    /** Address whose tokens were burned. */
    from: Address
    /** Amount burned. */
    amount: bigint
  }
  /** Name of the emitted event. */
  eventName: 'Burn'
  // ...standard log fields (address, blockHash, blockNumber, logIndex, transactionHash, ...)
}

Parameters

args

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

token

  • Type: Address | bigint

Token to operate on: a TIP-20 token id or a contract address.