-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add method to getWithdrawalMessages from a transaciton (#21)
- Loading branch information
1 parent
967eb83
commit 2ceb3a2
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/_test/index.test.ts → ...ns/public/getL2HashesForDepositTx.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { test, expect } from 'vitest' | ||
import { createPublicClient, http } from 'viem' | ||
import { base } from '@wagmi/chains' | ||
import { publicOpStackActions } from '../../decorators/publicOpStack' | ||
|
||
test('correctly retrieves L2 hash', async () => { | ||
const client = createPublicClient({ | ||
chain: base, | ||
transport: http(), | ||
}).extend(publicOpStackActions) | ||
|
||
const messages = await client.getWithdrawalMessages({ | ||
hash: | ||
'0x999bab960dbdf600c51371ae819957063337a50cec2eb8032412739defadabe7', | ||
}) | ||
expect(messages.length).toEqual(1) | ||
expect(messages[0].nonce).toBeDefined() | ||
expect(messages[0].gasLimit).toBeDefined() | ||
expect(messages[0].data).toBeDefined() | ||
expect(messages[0].value).toBeDefined() | ||
expect(messages[0].sender).toBeDefined() | ||
expect(messages[0].target).toBeDefined() | ||
expect(messages[0].withdrawalHash).toBeDefined() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { type PublicClient, Chain, Transport, Hash, Address, Hex, decodeEventLog } from 'viem' | ||
import { l2ToL1MessagePasserABI } from '@eth-optimism/contracts-ts' | ||
|
||
export type MessagePassedEvent = { | ||
nonce: bigint, | ||
sender: Address, | ||
target: Address, | ||
value: bigint, | ||
gasLimit: bigint, | ||
data: Hex, | ||
withdrawalHash: Hex | ||
} | ||
|
||
export type GetWithdrawalMessagesParameters = { | ||
hash: Hash | ||
} | ||
|
||
export type GetWithdrawalMessagesReturnType = MessagePassedEvent[] | ||
|
||
/** | ||
* Retrieves all MessagePassed events from a withdrawal transaction | ||
* | ||
* @param client - Public client to use | ||
* @param parameters - {@link GetWithdrawalMessagesParameters} | ||
* @returns An array of all MessagePassed events emitted in this transaction. {@link GetWithdrawalMessagesReturnType} | ||
*/ | ||
export async function getWithdrawalMessages<TChain extends Chain | undefined>( | ||
client: PublicClient<Transport, TChain>, | ||
{ hash }: GetWithdrawalMessagesParameters, | ||
): Promise<GetWithdrawalMessagesReturnType> { | ||
const receipt = await client.getTransactionReceipt({ hash }) | ||
const events: MessagePassedEvent[] = [] | ||
for (const log of receipt.logs) { | ||
/// These transactions will contain events from several contracts | ||
/// this decode will revert for events not from l2ToL1MessagePasserABI | ||
/// we are OK ignoring these events | ||
try { | ||
const event = decodeEventLog({ | ||
abi: l2ToL1MessagePasserABI, | ||
data: log.data, | ||
topics: log.topics, | ||
}) | ||
if (event.eventName === 'MessagePassed') { | ||
events.push(event.args) | ||
} | ||
} catch {} | ||
} | ||
return events; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters