From 0966042e8ac493621383f4eef6ad853be35294fe Mon Sep 17 00:00:00 2001 From: Lukas Rosario <36800180+lukasrosario@users.noreply.github.com> Date: Fri, 24 Nov 2023 18:21:46 -0500 Subject: [PATCH] let viem handle chain mistmatch errors --- src/hooks/L1/useWriteDepositERC20.ts | 14 ++++---------- src/hooks/L1/useWriteDepositETH.ts | 6 ------ .../L1/useWriteFinalizeWithdrawalTransaction.ts | 7 +------ src/hooks/L1/useWriteProveWithdrawalTransaction.ts | 7 +------ src/hooks/L2/useSimulateWithdrawERC20.ts | 4 +++- src/hooks/L2/useSimulateWithdrawETH.ts | 4 +++- src/hooks/L2/useWriteWithdrawERC20.ts | 11 +---------- src/hooks/L2/useWriteWithdrawETH.ts | 14 ++++---------- 8 files changed, 17 insertions(+), 50 deletions(-) diff --git a/src/hooks/L1/useWriteDepositERC20.ts b/src/hooks/L1/useWriteDepositERC20.ts index 5b6375d..24b5802 100644 --- a/src/hooks/L1/useWriteDepositERC20.ts +++ b/src/hooks/L1/useWriteDepositERC20.ts @@ -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' @@ -44,7 +44,7 @@ export function useWriteDepositERC20 { const config = useOpConfig(args) const { writeContract, writeContractAsync, ...writeReturn } = useWriteContract() - const currentChainId = useChainId() + const account = useAccount(args) return { writeDepositERC20: ({ l2ChainId, args, ...rest }: WriteDepositERC20Parameters) => { @@ -54,16 +54,13 @@ export function useWriteDepositERC20 = {}, ): UseWriteDepositETHReturnType { const opConfig = useOpConfig(args) - const currentChainId = useChainId() const mutation = { mutationFn({ l2ChainId, args, ...rest }: WriteDepositETHParameters) { @@ -95,10 +93,6 @@ export function useWriteDepositETH = {}, ): UseWriteFinalizeWithdrawalTransactionReturnType { const opConfig = useOpConfig(args) - const currentChainId = useChainId() const mutation = { mutationFn({ l2ChainId, args, ...rest }: WriteFinalizeWithdrawalTransactionParameters) { @@ -98,10 +97,6 @@ export function useWriteFinalizeWithdrawalTransaction = {}, ): UseWriteProveWithdrawalTransactionReturnType { const opConfig = useOpConfig(args) - const currentChainId = useChainId() const mutation = { mutationFn({ l2ChainId, args, ...rest }: WriteProveWithdrawalTransactionParameters) { @@ -115,10 +114,6 @@ export function useWriteProveWithdrawalTransaction { const opConfig = useOpConfig(rest) const l2Chain = opConfig.l2chains[chainId] + const account = useAccount(rest) if (!l2Chain) { throw new Error('L2 chain not configured') @@ -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 } diff --git a/src/hooks/L2/useSimulateWithdrawETH.ts b/src/hooks/L2/useSimulateWithdrawETH.ts index da86257..9fb371d 100644 --- a/src/hooks/L2/useSimulateWithdrawETH.ts +++ b/src/hooks/L2/useSimulateWithdrawETH.ts @@ -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' @@ -40,6 +40,7 @@ export function useSimulateWithdrawETH< ): UseSimulateWithdrawETHReturnType { const opConfig = useOpConfig(rest) const l2Chain = opConfig.l2chains[chainId] + const account = useAccount(rest) if (!l2Chain) { throw new Error('L2 chain not configured') @@ -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 } diff --git a/src/hooks/L2/useWriteWithdrawERC20.ts b/src/hooks/L2/useWriteWithdrawERC20.ts index 31baea9..689ab4f 100644 --- a/src/hooks/L2/useWriteWithdrawERC20.ts +++ b/src/hooks/L2/useWriteWithdrawERC20.ts @@ -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' @@ -44,14 +44,9 @@ export function useWriteWithdrawERC20 { 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) { @@ -68,10 +63,6 @@ export function useWriteWithdrawERC20 { - if (currentChainId !== chainId) { - throw new Error(`Chain mismatch. Expected ${chainId}, got ${currentChainId}.`) - } - const l2Chain = config.l2chains[chainId] if (!l2Chain) { diff --git a/src/hooks/L2/useWriteWithdrawETH.ts b/src/hooks/L2/useWriteWithdrawETH.ts index 4465f68..bfc48e6 100644 --- a/src/hooks/L2/useWriteWithdrawETH.ts +++ b/src/hooks/L2/useWriteWithdrawETH.ts @@ -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' @@ -45,14 +45,10 @@ export function useWriteWithdrawETH { 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) { @@ -66,14 +62,11 @@ export function useWriteWithdrawETH { - if (currentChainId !== chainId) { - throw new Error(`Chain mismatch. Expected ${chainId}, got ${currentChainId}.`) - } - const l2Chain = config.l2chains[chainId] if (!l2Chain) { @@ -87,6 +80,7 @@ export function useWriteWithdrawETH