Skip to content

Commit

Permalink
fix configs, add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasrosario committed Nov 24, 2023
1 parent 3c9e79d commit 91b81bc
Show file tree
Hide file tree
Showing 12 changed files with 3,007 additions and 1,276 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"@wagmi/core": "beta",
"op-viem": "1.3.0-alpha",
"typescript": ">=5.0.4",
"viem": "2.0.0-beta.0",
"viem": "beta",
"wagmi": "beta"
},
"peerDependenciesMeta": {
Expand Down
3,177 changes: 1,996 additions & 1,181 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

45 changes: 33 additions & 12 deletions src/_test/config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { createConfig, http } from '@wagmi/core'
import { mock } from '@wagmi/core/internal'
import { opStackL2ChainContracts } from 'op-viem'
import { baseAddresses } from 'op-viem/chains'
import { base as viem_base, mainnet } from 'viem/chains'
import { base as viem_base, mainnet as viem_mainnet } from 'viem/chains'
import { createConfig, http } from 'wagmi'
import { base as opWagmiBase } from '../constants/chains/base.js'
import { type OpConfig } from '../types/OpConfig.js'
import { accounts } from './constants.js'
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',
},
}

const base = {
chainId: viem_base.id,
l1ChaindId: mainnet.id,
l1Addresses: baseAddresses,
l2Addresses: opStackL2ChainContracts,
...getRpcUrls({ port: 8547 }),
...viem_base,
...getRpcUrls({ port: 8546 }),
fork: {
blockNumber: process.env.VITE_OPTIMISM_FORK_BLOCK_NUMBER
? BigInt(Number(process.env.VITE_OPTIMISM_FORK_BLOCK_NUMBER))
Expand All @@ -24,17 +30,32 @@ const base = {

export const config: OpConfig = {
l2chains: {
// @ts-expect-error this seems to have a mismatch that is likely a bug
[base.id]: base,
[opWagmiBase.chainId]: opWagmiBase,
},

...createConfig({
chains: [base],
chains: [mainnet, base],
connectors: [mock({ accounts })],
pollingInterval: 100,
storage: null,
transports: {
[base.id]: http(),
[mainnet.id]: http(),
},
}),
}

// Hacky way to mock a connected wallet
export const connectedConfig: OpConfig = {
...config,
state: {
chainId: 1,
connections: new Map().set(config.connectors[0].uid, {
accounts,
chainId: 1,
connector: config.connectors[0],
}),
current: config.connectors[0].uid,
status: 'connected',
},
}
17 changes: 9 additions & 8 deletions src/_test/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ export default async function() {
// handled in `setup.ts` but may require additional resetting (e.g. via `afterAll`), in case of
// any custom per-test adjustments that persist beyond `anvil_reset`.
await startProxy({
port: 8555,
options: {
forkUrl: rollupForkUrl,
forkBlockNumber: rollupForkBlockNumber,
blockTime: rollupBlockTime,
},
})
return await startProxy({
port: 8545,
options: {
forkUrl,
forkBlockNumber,
blockTime,
},
})
await startProxy({
port: 8546,
options: {
forkUrl: rollupForkUrl,
forkBlockNumber: rollupForkBlockNumber,
blockTime: rollupBlockTime,
},
})
}
4 changes: 2 additions & 2 deletions src/_test/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createElement } from 'react'
import { WagmiProvider } from 'wagmi'
export { act, cleanup } from '@testing-library/react'

import { config } from './config.js'
import { connectedConfig } from './config.js'

export const queryClient = new QueryClient()

Expand All @@ -34,7 +34,7 @@ export function renderHook<Result, Props>(
): RenderHookResult<Result, Props> {
queryClient.clear()
return rtl_renderHook(render, {
wrapper: createWrapper(WagmiProvider, { config, reconnectOnMount: false }),
wrapper: createWrapper(WagmiProvider, { config: connectedConfig, reconnectOnMount: false }),
...options,
})
}
Expand Down
130 changes: 130 additions & 0 deletions src/hooks/L1/useSimulateDepositERC20.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { expect, test } from 'vitest'
import { accounts } from '../../_test/constants.js'
import { renderHook, waitFor } from '../../_test/react.js'
import { useSimulateDepositERC20 } from './useSimulateDepositERC20.js'

test('useSimulateDepositERC20', async () => {
const { result } = renderHook(() =>
useSimulateDepositERC20({
args: {
l1Token: '0xbe9895146f7af43049ca1c1ae358b0541ea49704',
l2Token: '0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22',
to: accounts[0],
amount: 0n,
extraData: '0x',
},
l2ChainId: 8453,
dataSuffix: '0x1234',
})
)

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

expect(result.current).toMatchInlineSnapshot(`
{
"data": {
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [
{
"internalType": "address",
"name": "_l1Token",
"type": "address",
},
{
"internalType": "address",
"name": "_l2Token",
"type": "address",
},
{
"internalType": "address",
"name": "_to",
"type": "address",
},
{
"internalType": "uint256",
"name": "_amount",
"type": "uint256",
},
{
"internalType": "uint32",
"name": "_minGasLimit",
"type": "uint32",
},
{
"internalType": "bytes",
"name": "_extraData",
"type": "bytes",
},
],
"name": "depositERC20To",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function",
},
],
"account": {
"address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"type": "json-rpc",
},
"address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35",
"args": [
"0xbe9895146f7af43049ca1c1ae358b0541ea49704",
"0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22",
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
0n,
0,
"0x",
],
"chainId": 1,
"dataSuffix": "0x1234",
"functionName": "depositERC20To",
},
"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",
"address": "0x3154Cf16ccdb4C6d922629664174b904d80F2C35",
"args": [
"0xbe9895146f7af43049ca1c1ae358b0541ea49704",
"0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22",
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
0n,
0,
"0x",
],
"chainId": 1,
"dataSuffix": "0x1234",
"functionName": "depositERC20To",
},
],
"refetch": [Function],
"status": "success",
}
`)
})
4 changes: 3 additions & 1 deletion src/hooks/L1/useSimulateDepositERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { l1StandardBridgeABI } from '@eth-optimism/contracts-ts'
import { type SimulateDepositERC20Parameters } from 'op-viem/actions'
import { type Config, useSimulateContract, type UseSimulateContractParameters } from 'wagmi'
import { type Config, useAccount, useSimulateContract, type UseSimulateContractParameters } from 'wagmi'
import type { OpConfig } from '../../types/OpConfig.js'
import type { UseSimulateOPActionBaseParameters } from '../../types/UseSimulateOPActionBaseParameters.js'
import type { UseSimulateOPActionBaseReturnType } from '../../types/UseSimulateOPActionBaseReturnType.js'
Expand Down Expand Up @@ -38,6 +38,7 @@ export function useSimulateDepositERC20<
): UseSimulateDepositERC20ReturnType<config, chainId> {
const opConfig = useOpConfig(rest)
const l2Chain = opConfig.l2chains[l2ChainId]
const account = useAccount(rest)

if (!l2Chain) {
throw new Error('L2 chain not configured')
Expand All @@ -50,6 +51,7 @@ export function useSimulateDepositERC20<
args: [args.l1Token, args.l2Token, args.to, args.amount, args.minGasLimit ?? 0, args.extraData ?? '0x'],
chainId: l2Chain.l1ChainId,
query: query as UseSimulateContractParameters['query'],
account: account.address,
...rest,
}) as unknown as UseSimulateDepositERC20ReturnType<config, chainId>
}
38 changes: 17 additions & 21 deletions src/hooks/L1/useSimulateDepositETH.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { connect, disconnect } from '@wagmi/core'
import { expect, test } from 'vitest'
import { config } from '../../_test/config.js'
import { accounts } from '../../_test/constants.js'
import { renderHook, waitFor } from '../../_test/react.js'
import { useSimulateDepositETH } from './useSimulateDepositETH.js'

const connector = config.connectors[0]!
const portal = '0xe93c8cD0D409341205A592f8c4Ac1A5fe5585cfA'

test('useSimulateDepositETH', async () => {
await connect(config, { connector })

const { result } = renderHook(() =>
useSimulateDepositETH({
args: {
to: accounts[0],
gasLimit: 100000,
amount: 1n,
},
portal,
value: BigInt(1),
l2ChainId: 8453,
dataSuffix: '0x1234',
})
)
Expand All @@ -29,6 +21,7 @@ test('useSimulateDepositETH', async () => {
{
"data": {
"request": {
"__mode": "prepared",
"abi": [
{
"inputs": [
Expand Down Expand Up @@ -68,14 +61,15 @@ test('useSimulateDepositETH', async () => {
"address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"type": "json-rpc",
},
"address": "0xe93c8cD0D409341205A592f8c4Ac1A5fe5585cfA",
"address": "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e",
"args": [
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
1n,
100000,
21000n,
false,
"0x",
],
"chainId": 1,
"dataSuffix": "0x1234",
"functionName": "depositTransaction",
"value": 1n,
Expand Down Expand Up @@ -107,20 +101,22 @@ test('useSimulateDepositETH', async () => {
"simulateContract",
{
"account": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"blockNumber": undefined,
"chainId": 8453,
"address": "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e",
"args": [
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
1n,
21000n,
false,
"0x",
],
"chainId": 1,
"dataSuffix": "0x1234",
"gasLimit": 100000,
"gasPrice": undefined,
"to": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"type": undefined,
"value": undefined,
"functionName": "depositTransaction",
"value": 1n,
},
],
"refetch": [Function],
"status": "success",
}
`)

await disconnect(config, { connector })
})
5 changes: 4 additions & 1 deletion src/hooks/L1/useSimulateDepositETH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { optimismPortalABI } from '@eth-optimism/contracts-ts'
import { type SimulateDepositETHParameters } from 'op-viem/actions'
import { type Config, useEstimateGas, useSimulateContract, type UseSimulateContractParameters } from 'wagmi'
import { type Config, useAccount, useEstimateGas, useSimulateContract, type UseSimulateContractParameters } from 'wagmi'
import type { OpConfig } from '../../types/OpConfig.js'
import type { UseSimulateOPActionBaseParameters } from '../../types/UseSimulateOPActionBaseParameters.js'
import type { UseSimulateOPActionBaseReturnType } from '../../types/UseSimulateOPActionBaseReturnType.js'
Expand Down Expand Up @@ -38,6 +38,7 @@ export function useSimulateDepositETH<
): UseSimulateDepositETHReturnType<config, chainId> {
const opConfig = useOpConfig(rest)
const l2Chain = opConfig.l2chains[l2ChainId]
const account = useAccount(rest)

if (!l2Chain) {
throw new Error('L2 chain not configured')
Expand All @@ -48,6 +49,7 @@ export function useSimulateDepositETH<
to: args.to,
value: args.amount,
data: args.data,
account: account.address,
})

const enabled = Boolean(args.gasLimit || l2GasEstimate) && (query?.enabled ?? true)
Expand All @@ -59,6 +61,7 @@ export function useSimulateDepositETH<
chainId: l2Chain.l1ChainId,
value: args.amount,
query: { ...query, enabled } as UseSimulateContractParameters['query'],
account: account.address,
...rest,
}) as unknown as UseSimulateDepositETHReturnType<config, chainId>
}
Loading

0 comments on commit 91b81bc

Please sign in to comment.