Skip to content

Commit

Permalink
feat: add inference to getLogs event (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm authored Apr 12, 2023
1 parent 7d3ca19 commit ee1cb7f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-panthers-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added inference to `getLogs` `event` type.
96 changes: 96 additions & 0 deletions src/actions/public/getLogs.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { expectTypeOf, test } from 'vitest'

import { publicClient } from '../../_test/index.js'
import { getLogs } from './getLogs.js'
import type { AbiEvent } from 'abitype'

test('event: const assertion', async () => {
const event = {
inputs: [
{
indexed: true,
name: 'from',
type: 'address',
},
{
indexed: true,
name: 'to',
type: 'address',
},
{
indexed: false,
name: 'value',
type: 'uint256',
},
],
name: 'Transfer',
type: 'event',
} as const
const logs = await getLogs(publicClient, {
event,
})
expectTypeOf(logs[0]['args']).toEqualTypeOf<{
from: `0x${string}`
to: `0x${string}`
value: bigint
}>()
})

test('event: defined inline', async () => {
const logs = await getLogs(publicClient, {
event: {
inputs: [
{
indexed: true,
name: 'from',
type: 'address',
},
{
indexed: true,
name: 'to',
type: 'address',
},
{
indexed: false,
name: 'value',
type: 'uint256',
},
],
name: 'Transfer',
type: 'event',
},
})
expectTypeOf(logs[0]['args']).toEqualTypeOf<{
from: `0x${string}`
to: `0x${string}`
value: bigint
}>()
})

test('event: declared as `AbiEvent`', async () => {
const event: AbiEvent = {
inputs: [
{
indexed: true,
name: 'from',
type: 'address',
},
{
indexed: true,
name: 'to',
type: 'address',
},
{
indexed: false,
name: 'value',
type: 'uint256',
},
],
name: 'Transfer',
type: 'event',
}
const logs = await getLogs(publicClient, {
event,
})
expectTypeOf(logs[0]['args']).toEqualTypeOf<readonly unknown[]>()
})
6 changes: 3 additions & 3 deletions src/actions/public/getLogs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AbiEvent } from 'abitype'
import type { AbiEvent, Narrow } from 'abitype'
import type { PublicClient, Transport } from '../../clients/index.js'
import type {
Address,
Expand Down Expand Up @@ -28,7 +28,7 @@ export type GetLogsParameters<
address?: Address | Address[]
} & (
| {
event: TAbiEvent
event: Narrow<TAbiEvent>
args?: MaybeExtractEventArgsFromAbi<[TAbiEvent], TEventName>
}
| {
Expand Down Expand Up @@ -97,7 +97,7 @@ export async function getLogs<
if (event)
topics = encodeEventTopics({
abi: [event],
eventName: event.name,
eventName: (event as AbiEvent).name,
args,
} as EncodeEventTopicsParameters)

Expand Down

2 comments on commit ee1cb7f

@vercel
Copy link

@vercel vercel bot commented on ee1cb7f Apr 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on ee1cb7f Apr 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viem-playground – ./playgrounds/browser

viem-playground.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app
viem-playground-wagmi-dev.vercel.app

Please sign in to comment.