Skip to content

Commit

Permalink
prove withdrawa test
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasrosario committed Nov 24, 2023
1 parent e557297 commit 460e800
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 59 deletions.
4 changes: 2 additions & 2 deletions example/components/FinalizeWithdrawalTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function FinalizeWithdrawalTransaction({ selectedChainId }: FinalizeWithd
useSimulateFinalizeWithdrawalTransaction(
{
args: {
l1WithdrawalTxHash: withdrawalTxHash as Hash,
withdrawalTxHash: withdrawalTxHash as Hash,
},
l2ChainId: selectedChainId,
query: { enabled: false, retry: false },
Expand All @@ -33,7 +33,7 @@ export function FinalizeWithdrawalTransaction({ selectedChainId }: FinalizeWithd
} else {
await writeFinalizeWithdrawalTransactionAsync({
args: {
l1WithdrawalTxHash: withdrawalTxHash as Hash,
withdrawalTxHash: withdrawalTxHash as Hash,
},
l2ChainId: selectedChainId,
})
Expand Down
4 changes: 2 additions & 2 deletions example/components/ProveWithdrawalTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function ProveWithdrawalTransaction({ selectedChainId }: ProveWithdrawalT
const { status: simulateStatus, refetch: simulateProveWithdrawalTransaction } = useSimulateProveWithdrawalTransaction(
{
args: {
l1WithdrawalTxHash: withdrawalTxHash as Hash,
withdrawalTxHash: withdrawalTxHash as Hash,
},
l2ChainId: selectedChainId,
query: { enabled: false, retry: false },
Expand All @@ -32,7 +32,7 @@ export function ProveWithdrawalTransaction({ selectedChainId }: ProveWithdrawalT
} else {
await writeProveWithdrawalTransactionAsync({
args: {
l1WithdrawalTxHash: withdrawalTxHash as Hash,
withdrawalTxHash: withdrawalTxHash as Hash,
},
l2ChainId: selectedChainId,
})
Expand Down
24 changes: 12 additions & 12 deletions src/_test/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import { getRpcUrls } from './utils.js'
const mainnet = {
...viem_mainnet,
...getRpcUrls({ port: 8545 }),
fork: {
blockNumber: process.env.VITE_MAINNET_FORK_BLOCK_NUMBER
? BigInt(Number(process.env.VITE_MAINNET_FORK_BLOCK_NUMBER))
: 18136086n,
url: process.env.VITE_MAINNET_FORK_URL ?? 'https://cloudflare-eth.com',
},
// fork: {
// blockNumber: process.env.VITE_MAINNET_FORK_BLOCK_NUMBER
// ? BigInt(Number(process.env.VITE_MAINNET_FORK_BLOCK_NUMBER))
// : 18136086n,
// url: process.env.VITE_MAINNET_FORK_URL ?? 'https://cloudflare-eth.com',
// },
}

const base = {
...viem_base,
...getRpcUrls({ port: 8546 }),
fork: {
blockNumber: process.env.VITE_OPTIMISM_FORK_BLOCK_NUMBER
? BigInt(Number(process.env.VITE_OPTIMISM_FORK_BLOCK_NUMBER))
: 5940037n,
url: process.env.VITE_OPTIMISM_FORK_URL ?? 'https://mainnet.base.org',
},
// fork: {
// blockNumber: process.env.VITE_OPTIMISM_FORK_BLOCK_NUMBER
// ? BigInt(Number(process.env.VITE_OPTIMISM_FORK_BLOCK_NUMBER))
// : 3960000n,
// url: process.env.VITE_OPTIMISM_FORK_URL ?? 'https://mainnet.base.org',
// },
}

export const config: OpConfig = {
Expand Down
40 changes: 20 additions & 20 deletions src/_test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ function warn(message: string) {
}
}

export let forkBlockNumber: bigint
if (process.env.VITE_ANVIL_BLOCK_NUMBER) {
forkBlockNumber = BigInt(Number(process.env.VITE_ANVIL_BLOCK_NUMBER))
} else {
forkBlockNumber = 18136086n
warn(
`\`VITE_ANVIL_BLOCK_NUMBER\` not found. Falling back to \`${forkBlockNumber}\`.`,
)
}
// export let forkBlockNumber: bigint
// if (process.env.VITE_ANVIL_BLOCK_NUMBER) {
// forkBlockNumber = BigInt(Number(process.env.VITE_ANVIL_BLOCK_NUMBER))
// } else {
// forkBlockNumber = 18136086n
// warn(
// `\`VITE_ANVIL_BLOCK_NUMBER\` not found. Falling back to \`${forkBlockNumber}\`.`,
// )
// }

export let forkUrl: string
if (process.env.VITE_ANVIL_FORK_URL) {
Expand All @@ -42,17 +42,17 @@ if (process.env.VITE_ANVIL_BLOCK_TIME) {
warn(`\`VITE_ANVIL_BLOCK_TIME\` not found. Falling back to \`${blockTime}\`.`)
}

export let rollupForkBlockNumber: bigint
if (process.env.VITE_ANVIL_ROLLUP_BLOCK_NUMBER) {
rollupForkBlockNumber = BigInt(
Number(process.env.VITE_ANVIL_ROLLUP_BLOCK_NUMBER),
)
} else {
rollupForkBlockNumber = 3709321n
warn(
`\`VITE_ANVIL_ROLLUP_BLOCK_NUMBER\` not found. Falling back to \`${rollupForkBlockNumber}\`.`,
)
}
// export let rollupForkBlockNumber: bigint
// if (process.env.VITE_ANVIL_ROLLUP_BLOCK_NUMBER) {
// rollupForkBlockNumber = BigInt(
// Number(process.env.VITE_ANVIL_ROLLUP_BLOCK_NUMBER),
// )
// } else {
// rollupForkBlockNumber = 3709321n
// warn(
// `\`VITE_ANVIL_ROLLUP_BLOCK_NUMBER\` not found. Falling back to \`${rollupForkBlockNumber}\`.`,
// )
// }

export let rollupForkUrl: string
if (process.env.VITE_ANVIL_ROLLUP_FORK_URL) {
Expand Down
10 changes: 6 additions & 4 deletions src/_test/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { startProxy } from '@viem/anvil'

import {
blockTime,
forkBlockNumber,
// forkBlockNumber,
forkUrl,
rollupBlockTime,
rollupForkBlockNumber,
// rollupForkBlockNumber,
rollupForkUrl,
} from './constants.js'

Expand Down Expand Up @@ -37,15 +37,17 @@ export default async function() {
port: 8545,
options: {
forkUrl,
forkBlockNumber,
// TODO: Figure out forking / archive node issues
// forkBlockNumber,
blockTime,
},
})
await startProxy({
port: 8546,
options: {
forkUrl: rollupForkUrl,
forkBlockNumber: rollupForkBlockNumber,
// TODO: Figure out forking / archive node issues
// forkBlockNumber: rollupForkBlockNumber,
blockTime: rollupBlockTime,
},
})
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/L1/useProveWithdrawalArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { useGetL2OutputIndexAfter } from './useGetL2OutputIndexAfter.js'
export function useProveWithdrawalArgs({
l2ChainId,
config,
l1WithdrawalTxHash,
withdrawalTxHash,
}: {
l1WithdrawalTxHash: Hash
withdrawalTxHash: Hash
l2ChainId: number
config?: Config
}) {
Expand Down Expand Up @@ -47,7 +47,7 @@ export function useProveWithdrawalArgs({
})

const { data: withdrawalReceipt } = useWaitForTransactionReceipt({
hash: l1WithdrawalTxHash,
hash: withdrawalTxHash,
chainId: l2Chain.chainId,
})

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/L1/useSimulateDepositERC20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ test('useSimulateDepositERC20', async () => {
"status": "success",
}
`)
})
}, { retry: 3 })
4 changes: 2 additions & 2 deletions src/hooks/L1/useSimulateFinalizeWithdrawalTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type UseSimulateFinalizeWithdrawalTransactionParameters<
& UseSimulateOPActionBaseParameters<typeof ABI, typeof FUNCTION, config, chainId>
& {
args: {
l1WithdrawalTxHash: Hash
withdrawalTxHash: Hash
}
l2ChainId: number
}
Expand Down Expand Up @@ -60,7 +60,7 @@ export function useSimulateFinalizeWithdrawalTransaction<
const query = {
async queryFn() {
const withdrawalMessages = await getWithdrawalMessages(l2PublicClient, {
hash: args.l1WithdrawalTxHash,
hash: args.withdrawalTxHash,
})

return simulateFinalizeWithdrawalTransaction(l1PublicClient, {
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/L1/useSimulateProveWithdrawalTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, test } from 'vitest'
import { renderHook, waitFor } from '../../_test/react.js'
import { useSimulateProveWithdrawalTransaction } from './useSimulateProveWithdrawalTransaction.js'

test('useSimulateProveWithdrawalTransaction', async () => {
const { result } = renderHook(() =>
useSimulateProveWithdrawalTransaction({
args: {
withdrawalTxHash: '0xf735fdde9e33a002dcfe8d3fb2ca059c585d538a0e3ddc561d6fdcc303cff408',
},
l2ChainId: 8453,
query: { retry: false },
})
)

// We're only checking that we're able to successfully call the contract. The snapshot
// will continue to change as blocks get procuded, and this check is sufficient.
await waitFor(() => expect(result.current.isSuccess).toBeTruthy())
}, { retry: 3 })
12 changes: 6 additions & 6 deletions src/hooks/L1/useSimulateProveWithdrawalTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type UseSimulateProveWithdrawalTransactionParameters<
& UseSimulateOPActionBaseParameters<typeof ABI, typeof FUNCTION, config, chainId>
& {
args: {
l1WithdrawalTxHash: Hash
withdrawalTxHash: Hash
}
l2ChainId: number
}
Expand Down Expand Up @@ -55,15 +55,15 @@ export function useSimulateProveWithdrawalTransaction<
throw new Error('L2 chain not configured')
}

const { address } = useAccount()
const account = useAccount(rest)
const l1PublicClient = usePublicClient({ chainId: l2Chain.l1ChainId })
const l2PublicClient = usePublicClient({ chainId: l2ChainId })
const l1Addresses = opConfig.l2chains[l2ChainId].l1Addresses

const query = {
async queryFn() {
const withdrawalMessages = await getWithdrawalMessages(l2PublicClient, {
hash: args.l1WithdrawalTxHash,
hash: args.withdrawalTxHash,
})

const { l2BlockNumber } = await getLatestProposedL2BlockNumber(l1PublicClient, {
Expand All @@ -82,7 +82,7 @@ export function useSimulateProveWithdrawalTransaction<

return simulateProveWithdrawalTransaction(l1PublicClient, {
args: simulateProveWithdrawalTransactionArgs,
account: address,
account: account.address,
...l1Addresses,
})
},
Expand All @@ -97,13 +97,13 @@ export function useSimulateProveWithdrawalTransaction<
value: undefined,
...args,
},
account: address,
account: account.address,
chainId: l2Chain.l1ChainId,
action: 'proveWithdrawalTransaction',
}),
}

const enabled = Boolean(address) && (queryOverride?.enabled ?? true)
const enabled = Boolean(account.address) && (queryOverride?.enabled ?? true)
return {
...useQuery({ ...query, queryKeyHashFn: hashFn, enabled }),
queryKey: query.queryKey,
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/L1/useWriteDepositERC20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { accounts } from '../../_test/constants.js'
import { renderHook, waitFor } from '../../_test/react.js'
import { useWriteDepositERC20 } from './useWriteDepositERC20.js'

test(useWriteDepositERC20.name, async () => {
test('useWriteDepositERC20', async () => {
const { result } = renderHook(() => useWriteDepositERC20())

expect(result.current.writeDepositERC20).toBeDefined()
Expand Down Expand Up @@ -791,4 +791,4 @@ test(useWriteDepositERC20.name, async () => {
"writeDepositERC20Async": [Function],
}
`)
})
}, { retry: 3 })
2 changes: 1 addition & 1 deletion src/hooks/L1/useWriteDepositETH.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { accounts } from '../../_test/constants.js'
import { renderHook, waitFor } from '../../_test/react.js'
import { useWriteDepositETH } from './useWriteDepositETH.js'

test(useWriteDepositETH.name, async () => {
test('useWriteDepositETH', async () => {
const { result } = renderHook(() => useWriteDepositETH())

// write contract lazily writes
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/L1/useWriteFinalizeWithdrawalTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type WriteFinalizeWithdrawalTransactionParameters<
chainId extends config['chains'][number]['id'] = number,
> = WriteOPContractBaseParameters<typeof ABI, typeof FUNCTION, config, chainId> & {
args: {
l1WithdrawalTxHash: Hash
withdrawalTxHash: Hash
}
l2ChainId: number
}
Expand Down Expand Up @@ -62,7 +62,7 @@ async function writeMutation(
const l1Addresses = config.l2chains[l2ChainId].l1Addresses

const withdrawalMessages = await getWithdrawalMessages(l2PublicClient, {
hash: args.l1WithdrawalTxHash,
hash: args.withdrawalTxHash,
})

await simulateFinalizeWithdrawalTransaction(l1PublicClient, {
Expand Down
26 changes: 26 additions & 0 deletions src/hooks/L1/useWriteProveWithdrawalTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, test } from 'vitest'
import { renderHook, waitFor } from '../../_test/react.js'
import { useWriteProveWithdrawalTransaction } from './useWriteProveWithdrawalTransaction.js'

test('useWriteProveWithdrawalTransaction', async () => {
const { result } = renderHook(() => useWriteProveWithdrawalTransaction())

expect(result.current.writeProveWithdrawalTransaction).toBeDefined()
expect(result.current.writeProveWithdrawalTransaction).toBeDefined()
expect(result.current.data).toBeUndefined()
expect(result.current.isIdle).toBe(true)

result.current.writeProveWithdrawalTransaction({
args: {
withdrawalTxHash: '0xf735fdde9e33a002dcfe8d3fb2ca059c585d538a0e3ddc561d6fdcc303cff408',
},
l2ChainId: 8453,
})

await waitFor(() => {
expect(result.current.error).toBeNull()
// We're only checking that we're able to successfully call the contract. The snapshot
// will continue to change as blocks get procuded, and this check is sufficient.
expect(result.current.isSuccess).toBeTruthy()
})
}, { retry: 3 })
4 changes: 2 additions & 2 deletions src/hooks/L1/useWriteProveWithdrawalTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type WriteProveWithdrawalTransactionParameters<
chainId extends config['chains'][number]['id'] = number,
> = WriteOPContractBaseParameters<typeof ABI, typeof FUNCTION, config, chainId> & {
args: {
l1WithdrawalTxHash: Hash
withdrawalTxHash: Hash
}
l2ChainId: number
}
Expand Down Expand Up @@ -65,7 +65,7 @@ async function writeMutation(
const l1Addresses = config.l2chains[l2ChainId].l1Addresses

const withdrawalMessages = await getWithdrawalMessages(l2PublicClient, {
hash: args.l1WithdrawalTxHash,
hash: args.withdrawalTxHash,
})

const { l2BlockNumber } = await getLatestProposedL2BlockNumber(l1PublicClient, {
Expand Down

0 comments on commit 460e800

Please sign in to comment.