> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://v3.viem.sh/api/mcp` to find what you need.

# Watch Token Approvals \[token.watchApprove]

Watches TIP-20 `Approval` events for a token.

## Usage

:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'

const watcher = client.token.watchApprove({
  token: '0x20c0000000000000000000000000000000000000',
})

watcher.onLogs((logs) => {
  for (const log of logs) console.log(log.args)
  // @log: {
  // @log:   owner: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  // @log:   spender: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8',
  // @log:   amount: 100000000n
  // @log: }
})

// Later, tear down the watcher.
watcher.off()
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
// [!include ~/snippets/tempo/viem.config.ts:setup]
```
:::

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

## Recipes

### Collect Funds When a Payer Approves Your Processor

Filter `args.spender` to your processor account, then pull the approved amount with [`token.transferSync`](/tempo/actions/token.transfer) and its `from` parameter.

:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'

const watcher = client.token.watchApprove({
  args: { spender: client.account.address }, // [!code focus]
  token: '0x20c0000000000000000000000000000000000000',
})

watcher.onLogs(async (logs) => {
  for (const log of logs) {
    // Collect the approved amount from the payer.
    const { receipt } = await client.token.transferSync({ // [!code focus]
      amount: log.args.amount, // [!code focus]
      from: log.args.owner, // [!code focus]
      to: client.account.address, // [!code focus]
      token: '0x20c0000000000000000000000000000000000000', // [!code focus]
    }) // [!code focus]
  }
})
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
// [!include ~/snippets/tempo/viem.config.ts:setup]
```
:::

### Alert on Approvals From Treasury Accounts

Filter `args.owner` to accounts you control and flag any approval that names a spender outside your allowlist, an early signal of a compromised key.

:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'

// Spenders authorized by your treasury policy.
const allowedSpenders = new Set<string>([
  '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
])

const watcher = client.token.watchApprove({
  args: { owner: '0x8ba1f109551bD432803012645Ac136ddd64DBA72' }, // [!code focus]
  token: '0x20c0000000000000000000000000000000000000',
})

watcher.onLogs((logs) => {
  for (const log of logs) {
    if (allowedSpenders.has(log.args.spender)) continue // [!code focus]
    console.log('Unexpected approval:', log.args.spender, log.args.amount) // [!code focus]
    // @log: Unexpected approval: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 100000000n
  }
})
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
// [!include ~/snippets/tempo/viem.config.ts:setup]
```
:::

## Return Value

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

```ts
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`:

```ts
type Log = {
  /** Decoded `Approval` event arguments. */
  args: {
    /** Address of the token owner. */
    owner: Address
    /** Address of the approved spender. */
    spender: Address
    /** Amount approved. */
    amount: bigint
  }
  /** Name of the emitted event. */
  eventName: 'Approval'
  // ...standard log fields (address, blockHash, blockNumber, logIndex, transactionHash, ...)
}
```

## Parameters

### args

* **Type:** `object`

```ts
type Args = {
  /** Filter by owner address. */
  owner?: Address | Address[] | null
  /** Filter by spender address. */
  spender?: 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.
