diff --git a/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useAllowance.tsx b/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useAllowance.tsx index 728d69c61d2..43db67c83c2 100644 --- a/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useAllowance.tsx +++ b/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useAllowance.tsx @@ -1,4 +1,6 @@ import type { AccountId } from '@shapeshiftoss/caip' +import type { EvmChainId } from '@shapeshiftoss/chain-adapters' +import { evmChainIds } from '@shapeshiftoss/chain-adapters' import type { TradeQuoteStep } from '@shapeshiftoss/swapper' import { Err, type Result } from '@sniptt/monads' import type { QueryFunction } from '@tanstack/react-query' @@ -11,7 +13,7 @@ import { } from 'components/MultiHopTrade/hooks/useAllowanceApproval/helpers' import { useWallet } from 'hooks/useWallet/useWallet' -import { useBlockNumber } from './useBlockNumber' +import { useEvmBlockNumber } from './useBlockNumber' type QueryKeyArgs = Partial> & { blockNumber: bigint | undefined } @@ -86,7 +88,13 @@ export function useAllowance( [wallet], ) - const blockNumber = useBlockNumber(chainId, watch) + const maybeEvmChainId = useMemo(() => { + const isEvmChainId = chainId && evmChainIds.includes(chainId as EvmChainId) + if (!isEvmChainId) return + return chainId as EvmChainId + }, [chainId]) + + const blockNumber = useEvmBlockNumber(maybeEvmChainId, watch) const allowanceQuery = useQuery({ queryKey: queryKey({ diff --git a/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useBlockNumber.tsx b/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useBlockNumber.tsx index 04b6467d746..39d3e55b595 100644 --- a/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useBlockNumber.tsx +++ b/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useBlockNumber.tsx @@ -1,8 +1,8 @@ -import type { ChainId } from '@shapeshiftoss/caip' +import type { EvmChainId } from '@shapeshiftoss/chain-adapters' import { useEffect, useState } from 'react' import { assertGetViemClient } from 'lib/viem-client' -export const useBlockNumber = (chainId: ChainId | undefined, enabled: boolean) => { +export const useEvmBlockNumber = (chainId: EvmChainId | undefined, enabled: boolean) => { const [blockNumber, setBlockNumber] = useState() useEffect(() => { diff --git a/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useIsApprovalNeeded.tsx b/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useIsApprovalNeeded.tsx index cf931c799ff..e2b3cf3583d 100644 --- a/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useIsApprovalNeeded.tsx +++ b/src/components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useIsApprovalNeeded.tsx @@ -1,4 +1,5 @@ import type { AccountId } from '@shapeshiftoss/caip' +import { type EvmChainId, evmChainIds } from '@shapeshiftoss/chain-adapters' import type { TradeQuoteStep } from '@shapeshiftoss/swapper' import { useMemo } from 'react' import { useAllowance } from 'components/MultiHopTrade/components/MultiHopTradeConfirm/hooks/useAllowance' @@ -17,6 +18,10 @@ export const useIsApprovalNeeded = ( const isApprovalNeeded = useMemo(() => { if (tradeQuoteStep === undefined) return undefined + const isEvmChainId = evmChainIds.includes(tradeQuoteStep.sellAsset.chainId as EvmChainId) + + if (!isEvmChainId) return false + if (data?.isErr()) { const error = data.unwrapErr() // the error type is a GetAllowanceErr enum so we can handle all cases with exhaustiveness diff --git a/src/components/MultiHopTrade/hooks/useAllowanceApproval/helpers.ts b/src/components/MultiHopTrade/hooks/useAllowanceApproval/helpers.ts index a0c71a78c23..f37f1aae6b7 100644 --- a/src/components/MultiHopTrade/hooks/useAllowanceApproval/helpers.ts +++ b/src/components/MultiHopTrade/hooks/useAllowanceApproval/helpers.ts @@ -7,9 +7,9 @@ import { isLedger } from '@shapeshiftoss/hdwallet-ledger' import type { TradeQuote } from '@shapeshiftoss/swapper' import type { Result } from '@sniptt/monads' import { Err, Ok } from '@sniptt/monads' -import { getChainAdapterManager } from 'context/PluginProvider/chainAdapterSingleton' import { bn } from 'lib/bignumber/bignumber' import { MAX_ALLOWANCE } from 'lib/swapper/swappers/utils/constants' +import { assertGetChainAdapter } from 'lib/utils' import { getApproveContractData, getErc20Allowance, getFees } from 'lib/utils/evm' export type GetAllowanceArgs = { @@ -35,10 +35,8 @@ export const getAllowance = async ({ wallet, accountId, }: GetAllowanceArgs): Promise> => { - const adapterManager = getChainAdapterManager() - const adapter = adapterManager.get(chainId) + const adapter = assertGetChainAdapter(chainId) - if (!adapter) throw Error(`no chain adapter found for chain Id: ${chainId}`) if (!wallet) throw new Error('no wallet available') // No approval needed for selling a non-EVM asset