Skip to content

Commit

Permalink
let viem handle chain mistmatch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasrosario committed Nov 24, 2023
1 parent 17a802c commit 0966042
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 50 deletions.
14 changes: 4 additions & 10 deletions src/hooks/L1/useWriteDepositERC20.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { l1StandardBridgeABI } from '@eth-optimism/contracts-ts'
import { type Config } from '@wagmi/core'
import { type WriteDepositERC20Parameters as WriteDepositERC20ActionParameters } from 'op-viem/actions'
import { useChainId, useWriteContract } from 'wagmi'
import { useAccount, useWriteContract } from 'wagmi'
import type { OpConfig } from '../../types/OpConfig.js'
import type { UseWriteOPActionBaseParameters } from '../../types/UseWriteOPActionBaseParameters.js'
import type { UseWriteOPActionBaseReturnType } from '../../types/UseWriteOPActionBaseReturnType.js'
Expand Down Expand Up @@ -44,7 +44,7 @@ export function useWriteDepositERC20<config extends Config = OpConfig, context =
): UseWriteDepositERC20ReturnType<config, context> {
const config = useOpConfig(args)
const { writeContract, writeContractAsync, ...writeReturn } = useWriteContract()
const currentChainId = useChainId()
const account = useAccount(args)

return {
writeDepositERC20: ({ l2ChainId, args, ...rest }: WriteDepositERC20Parameters) => {
Expand All @@ -54,16 +54,13 @@ export function useWriteDepositERC20<config extends Config = OpConfig, context =
throw new Error('L2 chain not configured')
}

if (currentChainId !== l2Chain.l1ChainId) {
throw new Error(`Chain mismatch. Expected ${l2Chain.l1ChainId}, got ${currentChainId}.`)
}

return writeContract({
chainId: l2Chain.l1ChainId,
address: l2Chain.l1Addresses.l1StandardBridge.address,
abi: ABI,
functionName: FUNCTION,
args: [args.l1Token, args.l2Token, args.to, args.amount, args.minGasLimit ?? 0, args.extraData ?? '0x'],
account: account.address,
...rest,
})
},
Expand All @@ -74,16 +71,13 @@ export function useWriteDepositERC20<config extends Config = OpConfig, context =
throw new Error('L2 chain not configured')
}

if (currentChainId !== l2Chain.l1ChainId) {
throw new Error(`Chain mismatch. Expected ${l2Chain.l1ChainId}, got ${currentChainId}.`)
}

return writeContractAsync({
chainId: l2Chain.l1ChainId,
address: l2Chain.l1Addresses.l1StandardBridge.address,
abi: ABI,
functionName: FUNCTION,
args: [args.l1Token, args.l2Token, args.to, args.amount, args.minGasLimit ?? 0, args.extraData ?? '0x'],
account: account.address,
...rest,
})
},
Expand Down
6 changes: 0 additions & 6 deletions src/hooks/L1/useWriteDepositETH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
writeDepositETH,
type WriteDepositETHParameters as WriteDepositETHActionParameters,
} from 'op-viem/actions'
import { useChainId } from 'wagmi'
import type { OpConfig } from '../../types/OpConfig.js'
import type { UseWriteOPActionBaseParameters } from '../../types/UseWriteOPActionBaseParameters.js'
import type { UseWriteOPActionBaseReturnType } from '../../types/UseWriteOPActionBaseReturnType.js'
Expand Down Expand Up @@ -85,7 +84,6 @@ export function useWriteDepositETH<config extends Config = OpConfig, context = u
args: UseWriteDepositETHParameters<config, context> = {},
): UseWriteDepositETHReturnType<config, context> {
const opConfig = useOpConfig(args)
const currentChainId = useChainId()

const mutation = {
mutationFn({ l2ChainId, args, ...rest }: WriteDepositETHParameters) {
Expand All @@ -95,10 +93,6 @@ export function useWriteDepositETH<config extends Config = OpConfig, context = u
throw new Error('L2 chain not configured')
}

if (currentChainId !== l2Chain.l1ChainId) {
throw new Error(`Chain mismatch. Expected ${l2Chain.l1ChainId}, got ${currentChainId}.`)
}

return writeMutation(opConfig, { args, l1ChainId: l2Chain.l1ChainId, l2ChainId: l2ChainId, ...rest })
},
mutationKey: ['writeContract'],
Expand Down
7 changes: 1 addition & 6 deletions src/hooks/L1/useWriteFinalizeWithdrawalTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
writeFinalizeWithdrawalTranasction,
} from 'op-viem/actions'
import type { Hash } from 'viem'
import { type Config, useChainId } from 'wagmi'
import { type Config } from 'wagmi'
import { getPublicClient, getWalletClient } from 'wagmi/actions'
import type { OpConfig } from '../../types/OpConfig.js'
import type { UseWriteOPActionBaseParameters } from '../../types/UseWriteOPActionBaseParameters.js'
Expand Down Expand Up @@ -88,7 +88,6 @@ export function useWriteFinalizeWithdrawalTransaction<config extends Config = Op
args: UseWriteFinalizeWithdrawalTransactionParameters<config, context> = {},
): UseWriteFinalizeWithdrawalTransactionReturnType<config, context> {
const opConfig = useOpConfig(args)
const currentChainId = useChainId()

const mutation = {
mutationFn({ l2ChainId, args, ...rest }: WriteFinalizeWithdrawalTransactionParameters) {
Expand All @@ -98,10 +97,6 @@ export function useWriteFinalizeWithdrawalTransaction<config extends Config = Op
throw new Error('L2 chain not configured')
}

if (currentChainId !== l2Chain.l1ChainId) {
throw new Error(`Chain mismatch. Expected ${l2Chain.l1ChainId}, got ${currentChainId}.`)
}

return writeMutation(opConfig, { args, l1ChainId: l2Chain.l1ChainId, l2ChainId: l2ChainId, ...rest })
},
mutationKey: ['writeContract'],
Expand Down
7 changes: 1 addition & 6 deletions src/hooks/L1/useWriteProveWithdrawalTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
writeProveWithdrawalTransaction,
} from 'op-viem/actions'
import type { Hash } from 'viem'
import { type Config, useChainId } from 'wagmi'
import { type Config } from 'wagmi'
import { getPublicClient, getWalletClient } from 'wagmi/actions'
import type { OpConfig } from '../../types/OpConfig.js'
import type { UseWriteOPActionBaseParameters } from '../../types/UseWriteOPActionBaseParameters.js'
Expand Down Expand Up @@ -105,7 +105,6 @@ export function useWriteProveWithdrawalTransaction<config extends Config = OpCon
args: UseWriteProveWithdrawalTransactionParameters<config, context> = {},
): UseWriteProveWithdrawalTransactionReturnType<config, context> {
const opConfig = useOpConfig(args)
const currentChainId = useChainId()

const mutation = {
mutationFn({ l2ChainId, args, ...rest }: WriteProveWithdrawalTransactionParameters) {
Expand All @@ -115,10 +114,6 @@ export function useWriteProveWithdrawalTransaction<config extends Config = OpCon
throw new Error('L2 chain not configured')
}

if (currentChainId !== l2Chain.l1ChainId) {
throw new Error(`Chain mismatch. Expected ${l2Chain.l1ChainId}, got ${currentChainId}.`)
}

return writeMutation(opConfig, { args, l1ChainId: l2Chain.l1ChainId, l2ChainId: l2ChainId, ...rest })
},
mutationKey: ['writeContract'],
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/L2/useSimulateWithdrawERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { l2StandardBridgeABI } from '@eth-optimism/contracts-ts'
import type { Config } from '@wagmi/core'
import { type SimulateWithdrawERC20Parameters } from 'op-viem/actions'
import { useSimulateContract, type UseSimulateContractParameters } from 'wagmi'
import { 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 @@ -39,6 +39,7 @@ export function useSimulateWithdrawERC20<
): UseSimulateWithdrawERC20ReturnType<config, chainId> {
const opConfig = useOpConfig(rest)
const l2Chain = opConfig.l2chains[chainId]
const account = useAccount(rest)

if (!l2Chain) {
throw new Error('L2 chain not configured')
Expand All @@ -51,6 +52,7 @@ export function useSimulateWithdrawERC20<
functionName: FUNCTION,
args: [args.l2Token, args.to, args.amount, args.minGasLimit ?? 0, args.extraData ?? '0x'],
query: query as UseSimulateContractParameters['query'],
account: account.address,
...rest,
}) as unknown as UseSimulateWithdrawERC20ReturnType<config, chainId>
}
4 changes: 3 additions & 1 deletion src/hooks/L2/useSimulateWithdrawETH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { l2StandardBridgeABI } from '@eth-optimism/contracts-ts'
import type { Config } from '@wagmi/core'
import { type SimulateWithdrawETHParameters } from 'op-viem/actions'
import { useSimulateContract, type UseSimulateContractParameters } from 'wagmi'
import { 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 @@ -40,6 +40,7 @@ export function useSimulateWithdrawETH<
): UseSimulateWithdrawETHReturnType<config, chainId> {
const opConfig = useOpConfig(rest)
const l2Chain = opConfig.l2chains[chainId]
const account = useAccount(rest)

if (!l2Chain) {
throw new Error('L2 chain not configured')
Expand All @@ -53,6 +54,7 @@ export function useSimulateWithdrawETH<
args: [OVM_ETH, args.to, args.amount, args.minGasLimit ?? 0, args.extraData ?? '0x'],
value: args.amount,
query: query as UseSimulateContractParameters['query'],
account: account.address,
...rest,
}) as unknown as UseSimulateWithdrawETHReturnType<config, chainId>
}
11 changes: 1 addition & 10 deletions src/hooks/L2/useWriteWithdrawERC20.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { l2StandardBridgeABI } from '@eth-optimism/contracts-ts'
import { type Config } from '@wagmi/core'
import { type WriteWithdrawERC20Parameters as WriteWithdrawERC20ActionParameters } from 'op-viem/actions'
import { useChainId, useWriteContract } from 'wagmi'
import { useWriteContract } from 'wagmi'
import type { OpConfig } from '../../types/OpConfig.js'
import type { UseWriteOPActionBaseParameters } from '../../types/UseWriteOPActionBaseParameters.js'
import type { UseWriteOPActionBaseReturnType } from '../../types/UseWriteOPActionBaseReturnType.js'
Expand Down Expand Up @@ -44,14 +44,9 @@ export function useWriteWithdrawERC20<config extends Config = OpConfig, context
): UseWriteWithdrawERC20ReturnType<config, context> {
const config = useOpConfig(args)
const { writeContract, writeContractAsync, ...writeReturn } = useWriteContract()
const currentChainId = useChainId()

return {
writeWithdrawERC20: ({ chainId, args, ...rest }: WriteWithdrawERC20Parameters) => {
if (currentChainId !== chainId) {
throw new Error(`Chain mismatch. Expected ${chainId}, got ${currentChainId}.`)
}

const l2Chain = config.l2chains[chainId]

if (!l2Chain) {
Expand All @@ -68,10 +63,6 @@ export function useWriteWithdrawERC20<config extends Config = OpConfig, context
})
},
writeWithdrawERC20Async: ({ chainId, args, ...rest }: WriteWithdrawERC20Parameters) => {
if (currentChainId !== chainId) {
throw new Error(`Chain mismatch. Expected ${chainId}, got ${currentChainId}.`)
}

const l2Chain = config.l2chains[chainId]

if (!l2Chain) {
Expand Down
14 changes: 4 additions & 10 deletions src/hooks/L2/useWriteWithdrawETH.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { l2StandardBridgeABI } from '@eth-optimism/contracts-ts'
import { type Config } from '@wagmi/core'
import { type WriteWithdrawETHParameters as WriteWithdrawETHActionParameters } from 'op-viem/actions'
import { useChainId, useWriteContract } from 'wagmi'
import { useAccount, useWriteContract } from 'wagmi'
import type { OpConfig } from '../../types/OpConfig.js'
import type { UseWriteOPActionBaseParameters } from '../../types/UseWriteOPActionBaseParameters.js'
import type { UseWriteOPActionBaseReturnType } from '../../types/UseWriteOPActionBaseReturnType.js'
Expand Down Expand Up @@ -45,14 +45,10 @@ export function useWriteWithdrawETH<config extends Config = OpConfig, context =
): UseWriteWithdrawETHReturnType<config, context> {
const config = useOpConfig(args)
const { writeContract, writeContractAsync, ...writeReturn } = useWriteContract()
const currentChainId = useChainId()
const account = useAccount(args)

return {
writeWithdrawETH: ({ chainId, args, ...rest }: WriteWithdrawETHParameters) => {
if (currentChainId !== chainId) {
throw new Error(`Chain mismatch. Expected ${chainId}, got ${currentChainId}.`)
}

const l2Chain = config.l2chains[chainId]

if (!l2Chain) {
Expand All @@ -66,14 +62,11 @@ export function useWriteWithdrawETH<config extends Config = OpConfig, context =
functionName: FUNCTION,
args: [OVM_ETH, args.to, args.amount, args.minGasLimit ?? 0, args.extraData ?? '0x'],
value: args.amount,
account: account.address,
...rest,
})
},
writeWithdrawETHAsync: ({ chainId, args, ...rest }: WriteWithdrawETHParameters) => {
if (currentChainId !== chainId) {
throw new Error(`Chain mismatch. Expected ${chainId}, got ${currentChainId}.`)
}

const l2Chain = config.l2chains[chainId]

if (!l2Chain) {
Expand All @@ -87,6 +80,7 @@ export function useWriteWithdrawETH<config extends Config = OpConfig, context =
functionName: FUNCTION,
args: [OVM_ETH, args.to, args.amount, args.minGasLimit ?? 0, args.extraData ?? '0x'],
value: args.amount,
account: account.address,
...rest,
})
},
Expand Down

0 comments on commit 0966042

Please sign in to comment.