Skip to content

Commit 5309280

Browse files
committed
fix: resolves #3705
1 parent 29660de commit 5309280

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

.changeset/twelve-shirts-kneel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"viem": patch
3+
---
4+
5+
Fixed issue where `decodeEventLog` would not check validity of the topic against the signature.

src/utils/abi/decodeEventLog.test.ts

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { assertType, describe, expect, test } from 'vitest'
22

3+
import { waitForTransactionReceipt } from '../../actions/public/waitForTransactionReceipt.js'
4+
import { base } from '../../chains/index.js'
5+
import { createClient } from '../../clients/createClient.js'
6+
import { http } from '../../clients/transports/http.js'
37
import { getAddress } from '../address/getAddress.js'
48
import { toEventSelector } from '../hash/toEventSelector.js'
5-
69
import { decodeEventLog } from './decodeEventLog.js'
710

811
test('Transfer()', () => {
@@ -193,7 +196,7 @@ test('unnamed args: mixed ordering of indexed args', () => {
193196
],
194197
data: '0x0000000000000000000000000000000000000000000000000000000000000001',
195198
topics: [
196-
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
199+
'0x138dbc8474f748db86063dcef24cef1495bc73385a946f8d691128085e5ebec2',
197200
'0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
198201
'0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
199202
],
@@ -908,6 +911,69 @@ describe('GitHub repros', () => {
908911
)
909912
})
910913
})
914+
915+
test('https://github.com/wevm/viem/issues/3705', async () => {
916+
const client = createClient({
917+
chain: base,
918+
transport: http(),
919+
})
920+
921+
const receipt = await waitForTransactionReceipt(client, {
922+
hash: '0xbee3f576c43a29d3c924cd46ff1ff42489eceae5e8e31afdbba8f982c8291a76',
923+
})
924+
925+
const decoded = receipt.logs
926+
.map((log) => {
927+
try {
928+
return decodeEventLog({
929+
...log,
930+
abi: [
931+
{
932+
type: 'event',
933+
name: 'PetRegistered',
934+
inputs: [
935+
{
936+
name: 'petId',
937+
type: 'uint256',
938+
indexed: true,
939+
internalType: 'uint256',
940+
},
941+
{
942+
name: 'user',
943+
type: 'address',
944+
indexed: true,
945+
internalType: 'address',
946+
},
947+
],
948+
anonymous: false,
949+
},
950+
],
951+
})
952+
} catch {
953+
return null
954+
}
955+
})
956+
.filter((log) => log?.eventName === 'PetRegistered')
957+
958+
expect(decoded).toMatchInlineSnapshot(`
959+
[
960+
{
961+
"args": {
962+
"petId": 75574n,
963+
"user": "0x03998b821AB247677a034762CA7F99fACF227975",
964+
},
965+
"eventName": "PetRegistered",
966+
},
967+
{
968+
"args": {
969+
"petId": 75575n,
970+
"user": "0x03998b821AB247677a034762CA7F99fACF227975",
971+
},
972+
"eventName": "PetRegistered",
973+
},
974+
]
975+
`)
976+
})
911977
})
912978

913979
test("errors: event doesn't exist", () => {

src/utils/abi/decodeEventLog.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,11 @@ export function decodeEventLog<
116116
const [signature, ...argTopics] = topics
117117
if (!signature) throw new AbiEventSignatureEmptyTopicsError({ docsPath })
118118

119-
const abiItem = (() => {
120-
if (abi.length === 1) return abi[0]
121-
return abi.find(
122-
(x) =>
123-
x.type === 'event' &&
124-
signature === toEventSelector(formatAbiItem(x) as EventDefinition),
125-
)
126-
})()
119+
const abiItem = abi.find(
120+
(x) =>
121+
x.type === 'event' &&
122+
signature === toEventSelector(formatAbiItem(x) as EventDefinition),
123+
)
127124

128125
if (!(abiItem && 'name' in abiItem) || abiItem.type !== 'event')
129126
throw new AbiEventSignatureNotFoundError(signature, { docsPath })

0 commit comments

Comments
 (0)