Skip to content

Commit 2ceb3a2

Browse files
authored
add method to getWithdrawalMessages from a transaciton (#21)
1 parent 967eb83 commit 2ceb3a2

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

src/_test/index.test.ts renamed to src/actions/public/getL2HashesForDepositTx.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test, expect } from 'vitest'
22
import { createPublicClient, http } from 'viem'
33
import { mainnet } from '@wagmi/chains'
4-
import { publicOpStackActions } from '../decorators/publicOpStack'
4+
import { publicOpStackActions } from '../../decorators/publicOpStack'
55

66
test('correctly retrieves L2 hash', async () => {
77
const client = createPublicClient({
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { test, expect } from 'vitest'
2+
import { createPublicClient, http } from 'viem'
3+
import { base } from '@wagmi/chains'
4+
import { publicOpStackActions } from '../../decorators/publicOpStack'
5+
6+
test('correctly retrieves L2 hash', async () => {
7+
const client = createPublicClient({
8+
chain: base,
9+
transport: http(),
10+
}).extend(publicOpStackActions)
11+
12+
const messages = await client.getWithdrawalMessages({
13+
hash:
14+
'0x999bab960dbdf600c51371ae819957063337a50cec2eb8032412739defadabe7',
15+
})
16+
expect(messages.length).toEqual(1)
17+
expect(messages[0].nonce).toBeDefined()
18+
expect(messages[0].gasLimit).toBeDefined()
19+
expect(messages[0].data).toBeDefined()
20+
expect(messages[0].value).toBeDefined()
21+
expect(messages[0].sender).toBeDefined()
22+
expect(messages[0].target).toBeDefined()
23+
expect(messages[0].withdrawalHash).toBeDefined()
24+
})
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { type PublicClient, Chain, Transport, Hash, Address, Hex, decodeEventLog } from 'viem'
2+
import { l2ToL1MessagePasserABI } from '@eth-optimism/contracts-ts'
3+
4+
export type MessagePassedEvent = {
5+
nonce: bigint,
6+
sender: Address,
7+
target: Address,
8+
value: bigint,
9+
gasLimit: bigint,
10+
data: Hex,
11+
withdrawalHash: Hex
12+
}
13+
14+
export type GetWithdrawalMessagesParameters = {
15+
hash: Hash
16+
}
17+
18+
export type GetWithdrawalMessagesReturnType = MessagePassedEvent[]
19+
20+
/**
21+
* Retrieves all MessagePassed events from a withdrawal transaction
22+
*
23+
* @param client - Public client to use
24+
* @param parameters - {@link GetWithdrawalMessagesParameters}
25+
* @returns An array of all MessagePassed events emitted in this transaction. {@link GetWithdrawalMessagesReturnType}
26+
*/
27+
export async function getWithdrawalMessages<TChain extends Chain | undefined>(
28+
client: PublicClient<Transport, TChain>,
29+
{ hash }: GetWithdrawalMessagesParameters,
30+
): Promise<GetWithdrawalMessagesReturnType> {
31+
const receipt = await client.getTransactionReceipt({ hash })
32+
const events: MessagePassedEvent[] = []
33+
for (const log of receipt.logs) {
34+
/// These transactions will contain events from several contracts
35+
/// this decode will revert for events not from l2ToL1MessagePasserABI
36+
/// we are OK ignoring these events
37+
try {
38+
const event = decodeEventLog({
39+
abi: l2ToL1MessagePasserABI,
40+
data: log.data,
41+
topics: log.topics,
42+
})
43+
if (event.eventName === 'MessagePassed') {
44+
events.push(event.args)
45+
}
46+
} catch {}
47+
}
48+
return events;
49+
}

src/decorators/publicOpStack.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Account, Chain, Client, type PublicClient, Transport } from 'viem'
1+
import { Chain, type PublicClient, Transport } from 'viem'
22
import {
33
GetL2HashesForDepositTxParamters,
44
GetL2HashesForDepositTxReturnType,
55
getL2HashesForDepositTx,
66
} from '../actions/public/getL2HashesForDepositTx'
7+
import { GetWithdrawalMessagesParameters, GetWithdrawalMessagesReturnType, getWithdrawalMessages } from '../actions/public/getWithdrawalMessages'
78

89
/// NOTE We don't currently need account for exisiting actions but keeping in case
910
export type PublicOpStackActions<
@@ -13,6 +14,9 @@ export type PublicOpStackActions<
1314
getL2HashesForDepositTx: (
1415
args: GetL2HashesForDepositTxParamters,
1516
) => Promise<GetL2HashesForDepositTxReturnType>
17+
getWithdrawalMessages: (
18+
args: GetWithdrawalMessagesParameters,
19+
) => Promise<GetWithdrawalMessagesReturnType>
1620
}
1721

1822
export function publicOpStackActions<
@@ -23,5 +27,6 @@ export function publicOpStackActions<
2327
): PublicOpStackActions<TTransport, TChain> {
2428
return {
2529
getL2HashesForDepositTx: (args) => getL2HashesForDepositTx(client, args),
30+
getWithdrawalMessages: (args) => getWithdrawalMessages(client, args),
2631
}
2732
}

0 commit comments

Comments
 (0)