diff --git a/packages/wallet-management/src/components/SVMListItemButton.tsx b/packages/wallet-management/src/components/SVMListItemButton.tsx index 9591a00c3..d8848bae1 100644 --- a/packages/wallet-management/src/components/SVMListItemButton.tsx +++ b/packages/wallet-management/src/components/SVMListItemButton.tsx @@ -2,7 +2,6 @@ import { ChainId, ChainType } from '@lifi/sdk' import { Avatar, ListItemAvatar } from '@mui/material' import type { WalletAdapter } from '@solana/wallet-adapter-base' import { useWallet } from '@solana/wallet-adapter-react' -import { BPF_LOADER_DEPRECATED_PROGRAM_ID } from '@solana/web3.js' import { ListItemButton } from '../components/ListItemButton.js' import { ListItemText } from '../components/ListItemText.js' import { useLastConnectedAccount } from '../hooks/useAccount.js' diff --git a/packages/widget/src/hooks/useGasSufficiency.ts b/packages/widget/src/hooks/useGasSufficiency.ts index 4661b7ae8..cc09d5e3f 100644 --- a/packages/widget/src/hooks/useGasSufficiency.ts +++ b/packages/widget/src/hooks/useGasSufficiency.ts @@ -1,14 +1,8 @@ -import { - ChainType, - type EVMChain, - type RouteExtended, - type Token, -} from '@lifi/sdk' +import type { EVMChain, RouteExtended, Token } from '@lifi/sdk' import { useAccount } from '@lifi/wallet-management' import { useQuery } from '@tanstack/react-query' -import type { Address } from 'viem' -import { type Connector, useBytecode } from 'wagmi' import { useAvailableChains } from './useAvailableChains.js' +import { useIsContractAddress } from './useIsContractAddress.js' import { getTokenBalancesWithRetry } from './useTokenBalance.js' export interface GasSufficiency { @@ -27,18 +21,12 @@ export const useGasSufficiency = (route?: RouteExtended) => { const { account } = useAccount({ chainType: getChainById(route?.fromChainId)?.chainType, }) - const { data: contractCode } = useBytecode({ - address: - account.chainType === ChainType.EVM - ? (account.address as Address) - : undefined, - query: { - refetchInterval: 300_000, - staleTime: 300_000, - }, - }) - const isContractAddress = !!contractCode + const isContractAddress = useIsContractAddress( + account.address, + route?.fromChainId, + account.chainType + ) const { data: insufficientGas, isLoading } = useQuery({ queryKey: ['gas-sufficiency-check', account.address, route?.id], diff --git a/packages/widget/src/hooks/useIsContractAddress.ts b/packages/widget/src/hooks/useIsContractAddress.ts new file mode 100644 index 000000000..36def167a --- /dev/null +++ b/packages/widget/src/hooks/useIsContractAddress.ts @@ -0,0 +1,21 @@ +import { ChainType } from '@lifi/sdk' +import type { Address } from 'viem' +import { useBytecode } from 'wagmi' + +export const useIsContractAddress = ( + address?: string, + chainId?: number, + chainType?: ChainType +) => { + const { data: contractCode } = useBytecode({ + address: address as Address, + chainId: chainId, + query: { + refetchInterval: 300_000, + staleTime: 300_000, + enabled: Boolean(chainType === ChainType.EVM && chainId), + }, + }) + const isContractAddress = !!contractCode + return isContractAddress +} diff --git a/packages/widget/src/hooks/useToAddressRequirements.ts b/packages/widget/src/hooks/useToAddressRequirements.ts index 8e7330db4..3cc6186a1 100644 --- a/packages/widget/src/hooks/useToAddressRequirements.ts +++ b/packages/widget/src/hooks/useToAddressRequirements.ts @@ -1,20 +1,31 @@ +import { useAccount } from '@lifi/wallet-management' import { useChain } from '../hooks/useChain.js' import { useWidgetConfig } from '../providers/WidgetProvider/WidgetProvider.js' import { useFieldValues } from '../stores/form/useFieldValues.js' import { RequiredUI } from '../types/widget.js' +import { useIsContractAddress } from './useIsContractAddress.js' export const useToAddressRequirements = () => { const { requiredUI } = useWidgetConfig() const [fromChainId, toChainId] = useFieldValues('fromChain', 'toChain') - const { chain: fromChain } = useChain(fromChainId) const { chain: toChain } = useChain(toChainId) + const { account } = useAccount({ + chainType: fromChain?.chainType, + }) + const isFromContractAddress = useIsContractAddress( + account.address, + fromChainId, + account.chainType + ) const isDifferentChainType = fromChain && toChain && fromChain.chainType !== toChain.chainType const requiredToAddress = - requiredUI?.includes(RequiredUI.ToAddress) || isDifferentChainType + requiredUI?.includes(RequiredUI.ToAddress) || + isDifferentChainType || + isFromContractAddress return { requiredToAddress, diff --git a/packages/widget/src/stores/header/useHeaderStore.tsx b/packages/widget/src/stores/header/useHeaderStore.tsx index ed74f995f..6403d4118 100644 --- a/packages/widget/src/stores/header/useHeaderStore.tsx +++ b/packages/widget/src/stores/header/useHeaderStore.tsx @@ -1,7 +1,7 @@ import { createContext, useContext, useRef } from 'react' import { shallow } from 'zustand/shallow' import { createWithEqualityFn } from 'zustand/traditional' -import type { PersistStoreProps, PersistStoreProviderProps } from '../types.js' +import type { PersistStoreProviderProps } from '../types.js' import type { HeaderState, HeaderStore } from './types.js' export const HeaderStoreContext = createContext(null)