Skip to content

Commit

Permalink
fix: added assertion to check for existence of a signature on `topics…
Browse files Browse the repository at this point in the history
…` for `decodeEventLog` (#205)

* fix: added assertion to check for existence of event signature on topics

* fix: log type

* tests: update snapshots
  • Loading branch information
jxom authored Mar 13, 2023
1 parent 65ddf50 commit 36fa97a
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-gifts-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added an assertion to check for existence of an event signature on `topics` for `decodeEventLog`
12 changes: 12 additions & 0 deletions src/errors/abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AbiDecodingDataSizeInvalidError,
AbiEncodingArrayLengthMismatchError,
AbiEncodingLengthMismatchError,
AbiEventSignatureEmptyTopicsError,
DecodeLogTopicsMismatch,
InvalidAbiDecodingTypeError,
InvalidAbiEncodingTypeError,
Expand Down Expand Up @@ -61,6 +62,17 @@ test('AbiEncodingLengthMismatchError', () => {
`)
})

test('AbiEventSignatureEmptyTopicsError', () => {
expect(
new AbiEventSignatureEmptyTopicsError({ docsPath: '/test' }),
).toMatchInlineSnapshot(`
[AbiEventSignatureEmptyTopicsError: Cannot extract event signature from empty topics.
Docs: https://viem.sh/test.html
Version: [email protected]]
`)
})

describe('DecodeLogTopicsMismatch', () => {
test('default', () => {
expect(
Expand Down
9 changes: 9 additions & 0 deletions src/errors/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ export class AbiErrorSignatureNotFoundError extends BaseError {
}
}

export class AbiEventSignatureEmptyTopicsError extends BaseError {
name = 'AbiEventSignatureEmptyTopicsError'
constructor({ docsPath }: { docsPath: string }) {
super('Cannot extract event signature from empty topics.', {
docsPath,
})
}
}

export class AbiEventSignatureNotFoundError extends BaseError {
name = 'AbiEventSignatureNotFoundError'
constructor(signature: Hex, { docsPath }: { docsPath: string }) {
Expand Down
1 change: 1 addition & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
AbiErrorInputsNotFoundError,
AbiErrorNotFoundError,
AbiErrorSignatureNotFoundError,
AbiEventSignatureEmptyTopicsError,
AbiEventSignatureNotFoundError,
AbiEventNotFoundError,
AbiFunctionNotFoundError,
Expand Down
1 change: 1 addition & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test('exports actions', () => {
"AbiErrorNotFoundError": [Function],
"AbiErrorSignatureNotFoundError": [Function],
"AbiEventNotFoundError": [Function],
"AbiEventSignatureEmptyTopicsError": [Function],
"AbiEventSignatureNotFoundError": [Function],
"AbiFunctionNotFoundError": [Function],
"AbiFunctionOutputsNotFoundError": [Function],
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export {
AbiErrorNotFoundError,
AbiErrorSignatureNotFoundError,
AbiEventNotFoundError,
AbiEventSignatureEmptyTopicsError,
AbiEventSignatureNotFoundError,
AbiFunctionNotFoundError,
AbiFunctionOutputsNotFoundError,
Expand Down
2 changes: 1 addition & 1 deletion src/types/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type Log<
/** Index of the transaction that created this log or `null` if pending */
transactionIndex: TIndex | null
/** List of order-dependent topics */
topics: Hex[]
topics: [Hex, ...Hex[]] | []
/** `true` if this filter has been destroyed and is invalid */
removed: boolean
} & DecodedAbiEvent<TAbiEvent, TAbi, TEventName>
26 changes: 26 additions & 0 deletions src/utils/abi/decodeEventLog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,29 @@ test("errors: event doesn't exist", () => {
Version: [email protected]"
`)
})

test('errors: no topics', () => {
expect(() =>
decodeEventLog({
abi: [
{
inputs: [
{
indexed: true,
name: 'message',
type: 'string',
},
],
name: 'Bar',
type: 'event',
},
],
topics: [],
}),
).toThrowErrorMatchingInlineSnapshot(`
"Cannot extract event signature from empty topics.
Docs: https://viem.sh/docs/contract/decodeEventLog.html
Version: [email protected]"
`)
})
11 changes: 9 additions & 2 deletions src/utils/abi/decodeEventLog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Abi, AbiParameter, Narrow } from 'abitype'
import {
AbiEventSignatureEmptyTopicsError,
AbiEventSignatureNotFoundError,
DecodeLogTopicsMismatch,
} from '../../errors'
Expand All @@ -22,7 +23,7 @@ export type DecodeEventLogParameters<
abi: Narrow<TAbi>
data?: TData
eventName?: ExtractEventNameFromAbi<TAbi, TEventName>
topics: [signature: Hex, ...args: TTopics]
topics: [signature: Hex, ...args: TTopics] | []
}

export type DecodeEventLogReturnType<
Expand All @@ -34,6 +35,8 @@ export type DecodeEventLogReturnType<
eventName: TEventName
} & ExtractEventArgsFromTopics<TAbi, TEventName, TTopics, TData>

const docsPath = '/docs/contract/decodeEventLog'

export function decodeEventLog<
TAbi extends Abi | readonly unknown[],
TEventName extends string,
Expand All @@ -50,14 +53,18 @@ export function decodeEventLog<
TData
>): DecodeEventLogReturnType<TAbi, TEventName, TTopics, TData> {
const [signature, ...argTopics] = topics
if (!signature)
throw new AbiEventSignatureEmptyTopicsError({
docsPath,
})
const abiItem = (abi as Abi).find(
(x) =>
x.type === 'event' &&
signature === getEventSelector(formatAbiItem(x) as EventDefinition),
)
if (!(abiItem && 'name' in abiItem))
throw new AbiEventSignatureNotFoundError(signature, {
docsPath: '/docs/contract/decodeEventLog',
docsPath,
})

const { name, inputs } = abiItem
Expand Down

2 comments on commit 36fa97a

@vercel
Copy link

@vercel vercel bot commented on 36fa97a Mar 13, 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-wagmi-dev.vercel.app
viem-playground.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 36fa97a Mar 13, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.