Skip to content

Commit

Permalink
Merge pull request #31 from base-org/lukas/more-tests
Browse files Browse the repository at this point in the history
more tests
  • Loading branch information
lukasrosario authored Nov 24, 2023
2 parents e557297 + c6f4036 commit 7b0643a
Show file tree
Hide file tree
Showing 27 changed files with 1,884 additions and 113 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 })
148 changes: 148 additions & 0 deletions src/hooks/L1/useSimulateFinalizeWithdrawalTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { expect, test } from 'vitest'
import { renderHook, waitFor } from '../../_test/react.js'
import { useSimulateFinalizeWithdrawalTransaction } from './useSimulateFinalizeWithdrawalTransaction.js'

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

await waitFor(() => expect(result.current.isSuccess).toBeTruthy())

expect(result.current).toMatchInlineSnapshot(`
{
"data": {
"request": {
"abi": [
{
"inputs": [
{
"components": [
{
"internalType": "uint256",
"name": "nonce",
"type": "uint256",
},
{
"internalType": "address",
"name": "sender",
"type": "address",
},
{
"internalType": "address",
"name": "target",
"type": "address",
},
{
"internalType": "uint256",
"name": "value",
"type": "uint256",
},
{
"internalType": "uint256",
"name": "gasLimit",
"type": "uint256",
},
{
"internalType": "bytes",
"name": "data",
"type": "bytes",
},
],
"internalType": "struct Types.WithdrawalTransaction",
"name": "_tx",
"type": "tuple",
},
],
"name": "finalizeWithdrawalTransaction",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function",
},
],
"account": {
"address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"type": "json-rpc",
},
"address": "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e",
"args": [
{
"data": "0xd764ad0b000100000000000000000000000000000000000000000000000000000000072800000000000000000000000042000000000000000000000000000000000000100000000000000000000000003154cf16ccdb4c6d922629664174b904d80f2c35000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000186a000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001040166a07a000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000d9aaec86b65d86f6a7b5b1b0c42ffa531710b6ca000000000000000000000000b85c1a4062c877145b5cbf623d3625158611b1500000000000000000000000007aa3fca04e32189529f8b24f6d3cf4a8ada68ca6000000000000000000000000000000000000000000000000000000000000138800000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000001010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": 390747n,
"nonce": 1766847064778384329583297500742918515827483896875618958121606201292645266n,
"sender": "0x4200000000000000000000000000000000000007",
"target": "0x866E82a600A1414e583f7F13623F1aC5d58b0Afa",
"value": 0n,
"withdrawalHash": "0x72ad06241743ba6acbe9f54279780f39140f14f111aa0cb4a34b8bffaa0ce389",
},
],
"dataSuffix": undefined,
"functionName": "finalizeWithdrawalTransaction",
"l1CrossDomainMessenger": {
"address": "0x866E82a600A1414e583f7F13623F1aC5d58b0Afa",
"blockCreated": 17482143,
"chainId": 1,
},
"l1Erc721Bridge": {
"address": "0x608d94945A64503E642E6370Ec598e519a2C1E53",
"blockCreated": 17482143,
"chainId": 1,
},
"l1StandardBridge": {
"address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35",
"blockCreated": 17482143,
"chainId": 1,
},
"l2OutputOracle": {
"address": "0x56315b90c40730925ec5485cf004d835058518A0",
"blockCreated": 17482143,
"chainId": 1,
},
},
"result": undefined,
},
"dataUpdatedAt": 1700438400000,
"error": null,
"errorUpdateCount": 0,
"errorUpdatedAt": 0,
"failureCount": 0,
"failureReason": null,
"fetchStatus": "idle",
"isError": false,
"isFetched": true,
"isFetchedAfterMount": true,
"isFetching": false,
"isInitialLoading": false,
"isLoading": false,
"isLoadingError": false,
"isPaused": false,
"isPending": false,
"isPlaceholderData": false,
"isRefetchError": false,
"isRefetching": false,
"isStale": true,
"isSuccess": true,
"queryKey": [
"simulateContract",
{
"account": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"action": "finalizeWithdrawalTransaction",
"blockNumber": undefined,
"chainId": 1,
"gasPrice": undefined,
"type": undefined,
"value": undefined,
"withdrawalTxHash": "0xe602bbed1f47b362d5dfde374f4a390d89a282d27772fc16db162f1b1f33df43",
},
],
"refetch": [Function],
"status": "success",
}
`)
}, { retry: 3 })
12 changes: 6 additions & 6 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 @@ -52,20 +52,20 @@ export function useSimulateFinalizeWithdrawalTransaction<
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,
})

return simulateFinalizeWithdrawalTransaction(l1PublicClient, {
withdrawal: withdrawalMessages.messages[0],
account: address,
account: account.address,
...l1Addresses,
})
},
Expand All @@ -80,13 +80,13 @@ export function useSimulateFinalizeWithdrawalTransaction<
value: undefined,
...args,
},
account: address,
account: account.address,
chainId: l2Chain.l1ChainId,
action: 'finalizeWithdrawalTransaction',
}),
}

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
Loading

0 comments on commit 7b0643a

Please sign in to comment.