Skip to content

Commit

Permalink
Add tests for watching evm withdrawals
Browse files Browse the repository at this point in the history
  • Loading branch information
gndelia committed Jan 21, 2025
1 parent 4be48d7 commit db4cd92
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions webapp/test/utils/watch/evmWithdrawals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { MessageDirection, MessageStatus } from '@eth-optimism/sdk'
import { hemiSepolia } from 'hemi-viem'
import { ToEvmWithdrawOperation } from 'types/tunnel'
import { createQueuedCrossChainMessenger } from 'utils/crossChainMessenger'
import { getEvmBlock, getEvmTransactionReceipt } from 'utils/evmApi'
import { createPublicProvider } from 'utils/providers'
// import { watchEvmWithdrawal } from 'utils/watch/evmWithdrawals'
import { sepolia } from 'viem/chains'
import { beforeEach, describe, expect, it, vi } from 'vitest'

// @ts-expect-error Only adding the minimum required properties
const withdrawal: ToEvmWithdrawOperation = {
direction: MessageDirection.L2_TO_L1,
l1ChainId: sepolia.id,
l2ChainId: hemiSepolia.id,
transactionHash: '0x0000000000000000000000000000000000000004',
}

vi.mock('utils/crossChainMessenger', () => ({
createQueuedCrossChainMessenger: vi.fn(),
}))

vi.mock('utils/evmApi', () => ({
getEvmBlock: vi.fn(),
getEvmTransactionReceipt: vi.fn(),
}))

vi.mock('utils/providers', () => ({
createPublicProvider: vi.fn(),
}))

describe('utils/watch/evmWithdrawals', function () {
beforeEach(function () {
vi.clearAllMocks()
vi.resetAllMocks()
vi.resetModules()
})

describe('watchEvmWithdrawal', async function () {
it('should return no changes if the withdrawal is pending', async function () {
const { watchEvmWithdrawal } = await import('utils/watch/evmWithdrawals')
vi.mocked(createQueuedCrossChainMessenger).mockResolvedValue({})
vi.mocked(createPublicProvider).mockResolvedValue({})
vi.mocked(getEvmTransactionReceipt).mockResolvedValue(null)

const updates = await watchEvmWithdrawal(withdrawal)

expect(updates).toEqual({})
})

it('should return the updated fields with the new values', async function () {
const { watchEvmWithdrawal } = await import('utils/watch/evmWithdrawals')
const blockNumber = BigInt(123)
const newStatus = MessageStatus.READY_TO_PROVE
const timestamp = BigInt(new Date().getTime())
const getMessageStatus = vi.fn().mockResolvedValue(newStatus)
vi.mocked(createQueuedCrossChainMessenger).mockResolvedValue({
getMessageStatus,
})
vi.mocked(getEvmBlock).mockResolvedValue({ timestamp })
vi.mocked(getEvmTransactionReceipt).mockResolvedValue({
blockNumber,
})

const updates = await watchEvmWithdrawal({
...withdrawal,
status: MessageStatus.STATE_ROOT_NOT_PUBLISHED,
})

expect(updates).toEqual({
blockNumber: Number(blockNumber),
status: newStatus,
timestamp: Number(timestamp),
})
expect(getMessageStatus).toHaveBeenCalledOnce()
expect(getMessageStatus).toHaveBeenCalledWith(
withdrawal.transactionHash,
0,
withdrawal.direction,
)
})

it('should return no updates if the withdrawal has not changed', async function () {
const newWithdrawal: ToEvmWithdrawOperation = {
...withdrawal,
blockNumber: 789,
timestamp: new Date().getTime(),
}
const { watchEvmWithdrawal } = await import('utils/watch/evmWithdrawals')
vi.mocked(createQueuedCrossChainMessenger).mockResolvedValue({
getMessageStatus: vi.fn().mockResolvedValue(newWithdrawal.status),
})
vi.mocked(getEvmBlock).mockResolvedValue({
timestamp: BigInt(newWithdrawal.timestamp),
})
vi.mocked(getEvmTransactionReceipt).mockResolvedValue({
blockNumber: BigInt(newWithdrawal.blockNumber),
})

const updates = await watchEvmWithdrawal(newWithdrawal)

expect(updates).toEqual({})
})
})
})

0 comments on commit db4cd92

Please sign in to comment.