Skip to content

token.watchRole

Watches for role membership update events on TIP20 tokens.

Usage

example.ts
import { client } from './viem.config'
 
const unwatch = client.token.watchRole({
  token: '0x...', // Address or ID of the TIP20 token
  onRoleUpdated: (args, log) => {
    console.log('Account:', args.account)
    console.log('Has role:', args.hasRole)
    console.log('Role:', args.role)
    console.log('Type:', args.type) // 'granted' or 'revoked'
    console.log('Sender:', args.sender)
  },
})
 
// Later, stop watching
unwatch()

Return Type

type ReturnType = () => void

Returns a function to unsubscribe from the event.

Parameters

onRoleUpdated

  • Type: function
declare function onRoleUpdated(args: Args, log: Log): void
 
type Args = {
  /** Role being updated */
  role: Hex
  /** Account receiving or losing the role */
  account: Address
  /** Address that updated the role */
  sender: Address
  /** Whether the account has the role */
  hasRole: boolean
  /** Type of role update */
  type: 'granted' | 'revoked'
}

Callback to invoke when a role membership is updated.

token

  • Type: Address | bigint

Address or ID of the TIP20 token.

args (optional)

  • Type: object
type Args = {
  /** Filter by role */
  role?: Hex | Hex[] | null
  /** Filter by account */
  account?: Address | Address[] | null
  /** Filter by sender */
  sender?: Address | Address[] | null
}

Filter parameters for the watch subscription.

fromBlock (optional)

  • Type: bigint

Block to start listening from.

onError (optional)

  • Type: function
declare function onError(error: Error): void

The callback to call when an error occurred when trying to get for a new block.

poll (optional)

  • Type: true

Whether to use polling.

pollingInterval (optional)

  • Type: number

Polling frequency (in ms). Defaults to Client's pollingInterval config.