> **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.

# Get Event Logs \[contract.getLogs]

Returns a list of event logs emitted by a contract.

## Usage

This example returns a list of event logs emitted by a contract.

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

const logs = await client.contract.getLogs({
  abi: Abis.erc20,
  eventName: 'Transfer',
})
// @log: [
// @log:   {
// @log:     address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2',
// @log:     args: { from: '0xd8da...', to: '0xa5cc...', value: 420n },
// @log:     blockHash: '0xabe6...01a4',
// @log:     blockNumber: 19760236n,
// @log:     data: '0x0000...01a4',
// @log:     eventName: 'Transfer',
// @log:     logIndex: 271,
// @log:     removed: false,
// @log:     topics: ['0xddf2...b3ef', '0x0000...d8da', '0x0000...a5cc'],
// @log:     transactionHash: '0xcfa5...6e93',
// @log:     transactionIndex: 145,
// @log:   },
// @log: ]
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Client, http, publicActions } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  chain: mainnet,
  transport: http(),
}).extend(publicActions())
```
:::

### Standalone Action

Call `Actions.contract.getLogs` directly by passing the Client as the first argument.

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

const logs = await Actions.contract.getLogs(client, {
  abi: Abis.erc20,
  eventName: 'Transfer',
})
// @log: [
// @log:   {
// @log:     address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2',
// @log:     args: { from: '0xd8da...', to: '0xa5cc...', value: 420n },
// @log:     blockHash: '0xabe6...01a4',
// @log:     blockNumber: 19760236n,
// @log:     data: '0x0000...01a4',
// @log:     eventName: 'Transfer',
// @log:     logIndex: 271,
// @log:     removed: false,
// @log:     topics: ['0xddf2...b3ef', '0x0000...d8da', '0x0000...a5cc'],
// @log:     transactionHash: '0xcfa5...6e93',
// @log:     transactionIndex: 145,
// @log:   },
// @log: ]
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Client, http } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  chain: mainnet,
  transport: http(),
})
```
:::

## Recipes

Use a Client method or pass the Client to the standalone action.

### Fetching All Events for an ABI

Omit `eventName` to fetch and decode logs for every event in the `abi`.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const logs = await Actions.contract.getLogs(client, {
  abi: Abis.erc20, // [!code focus]
})
```

### Fetching Events for a Contract

Restrict logs to a single contract by passing its `address`.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
const logs = await Actions.contract.getLogs(client, {
  abi,
  address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', // [!code focus]
})
```

### Fetching a Single Event

Pass `eventName` to filter and decode logs by one event.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
const logs = await Actions.contract.getLogs(client, {
  abi,
  eventName: 'Transfer', // [!code focus]
})
```

### Filtering by Indexed Arguments

Pass `args` to filter logs by the indexed parameters of the named event.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
const logs = await Actions.contract.getLogs(client, {
  abi,
  eventName: 'Transfer',
  args: { // [!code focus]
    from: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', // [!code focus]
  }, // [!code focus]
})
```

### Fetching Events Within a Block Range

Pass `fromBlock` and `toBlock` to scope logs to a range of blocks.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
const logs = await Actions.contract.getLogs(client, {
  abi,
  fromBlock: 19760235n, // [!code focus]
  toBlock: 19760240n, // [!code focus]
})
```

### Fetching Events for a Single Block

Pass `blockHash` to fetch logs from one block.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
const logs = await Actions.contract.getLogs(client, {
  abi,
  blockHash: '0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4', // [!code focus]
})
```

### Strictly Decoding Logs

Pass `strict: true` to only include logs whose topics and data conform exactly to the event.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
const logs = await Actions.contract.getLogs(client, {
  abi,
  eventName: 'Transfer',
  strict: true, // [!code focus]
})
```

## Return Value

`readonly Log[]`

A list of event logs decoded against the `abi`. Each log includes its `args` and `eventName`.

## Parameters

### abi

* **Type:** `Abi`

The contract ABI to filter and decode logs by.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const logs = await Actions.contract.getLogs(client, {
  abi: Abis.erc20, // [!code focus]
})
```

### address

* **Type:** `Address | readonly Address[]`

One or more addresses from which the logs originated.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
// ---cut---
const logs = await Actions.contract.getLogs(client, {
  abi,
  address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', // [!code focus]
})
```

### args

* **Type:** Inferred from `abi` and `eventName`.

The indexed argument values to filter the logs by. Pass an array of values for a single argument to match any of them (logical OR).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
// ---cut---
const logs = await Actions.contract.getLogs(client, {
  abi,
  eventName: 'Transfer',
  args: { // [!code focus]
    from: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', // [!code focus]
  }, // [!code focus]
})
```

### blockHash

* **Type:** `Hex`

The hash of the block to include logs from. Mutually exclusive with `fromBlock`/`toBlock`.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
// ---cut---
const logs = await Actions.contract.getLogs(client, {
  abi,
  blockHash: '0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4', // [!code focus]
})
```

### eventName

* **Type:** `string`

The name of the event to filter and decode the logs by. When omitted, logs for every event in the `abi` are returned.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
// ---cut---
const logs = await Actions.contract.getLogs(client, {
  abi,
  eventName: 'Transfer', // [!code focus]
})
```

### fromBlock

* **Type:** `bigint | 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'`

The block number or tag after which to include logs (inclusive).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
// ---cut---
const logs = await Actions.contract.getLogs(client, {
  abi,
  fromBlock: 19760235n, // [!code focus]
  toBlock: 19760240n,
})
```

### strict

* **Type:** `boolean`
* **Default:** `false`

Whether the logs must conform exactly to the indexed and non-indexed arguments on the event. When `false`, logs that partially conform are included (and their non-conforming `args` are omitted).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
// ---cut---
const logs = await Actions.contract.getLogs(client, {
  abi,
  eventName: 'Transfer',
  strict: true, // [!code focus]
})
```

### toBlock

* **Type:** `bigint | 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'`

The block number or tag before which to include logs (inclusive).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Abis } from 'viem/utils'

const client = Client.create({ chain: mainnet, transport: http() })
const abi = Abis.erc20
// ---cut---
const logs = await Actions.contract.getLogs(client, {
  abi,
  fromBlock: 19760235n,
  toBlock: 19760240n, // [!code focus]
})
```

## Errors

| Error | Description |
| --- | --- |
| `AbiItem.NotFoundError` | The selected event is missing from the runtime ABI. |
| `AbiEvent.FilterTypeNotSupportedError` | `args` includes an indexed tuple or array, which event topic filters do not support. |
| `AbiParameters.InvalidTypeError` | `args` includes an indexed ABI type that the topic encoder does not support. |
| `Address.InvalidAddressError` | `args` includes an invalid indexed address. |
| `Hex.IntegerOutOfRangeError` | `args` includes an indexed integer outside the supported 256-bit range. |
| `Hex.SizeExceedsPaddingSizeError` | An indexed fixed-bytes value exceeds the 32-byte topic size. |
