Skip to content

Commit

Permalink
fix: avoid gas check for smart contract wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Oct 24, 2024
1 parent 25eb04d commit 1bd956d
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions packages/widget/src/hooks/useGasSufficiency.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { EVMChain, RouteExtended, Token } from '@lifi/sdk'
import {
ChainType,
type EVMChain,
type RouteExtended,
type Token,
} from '@lifi/sdk'
import { useAccount } from '@lifi/wallet-management'
import { useQuery } from '@tanstack/react-query'
import type { Connector } from 'wagmi'
import type { Address } from 'viem'
import { type Connector, useBytecode } from 'wagmi'
import { useAvailableChains } from './useAvailableChains.js'
import { getTokenBalancesWithRetry } from './useTokenBalance.js'

Expand All @@ -21,13 +27,26 @@ 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 { data: insufficientGas, isLoading } = useQuery({
queryKey: ['gas-sufficiency-check', account.address, route?.id],
queryFn: async ({ queryKey: [, accountAddress] }) => {
if (!route) {
return
}

// We assume that LI.Fuel protocol always refuels the destination chain
const hasRefuelStep = route.steps
.flatMap((step) => step.includedSteps)
Expand All @@ -40,11 +59,7 @@ export const useGasSufficiency = (route?: RouteExtended) => {
// We need to avoid destination chain step sufficiency check if we have LI.Fuel protocol sub-step
const skipDueToRefuel =
step.action.fromChainId === route.toChainId && hasRefuelStep
if (
step.estimate.gasCosts &&
(account.connector as Connector)?.id !== 'safe' &&
!skipDueToRefuel
) {
if (step.estimate.gasCosts && !skipDueToRefuel) {
const { token } = step.estimate.gasCosts[0]
const gasCostAmount = step.estimate.gasCosts.reduce(
(amount, gasCost) =>
Expand Down Expand Up @@ -96,7 +111,7 @@ export const useGasSufficiency = (route?: RouteExtended) => {
)

if (!tokenBalances?.length) {
return []
return
}
;[route.fromChainId, route.toChainId].forEach((chainId) => {
if (gasCosts[chainId]) {
Expand Down Expand Up @@ -133,7 +148,7 @@ export const useGasSufficiency = (route?: RouteExtended) => {
return gasCostResult
},

enabled: Boolean(account.address && route),
enabled: Boolean(!isContractAddress && account.address && route),
refetchInterval,
staleTime: refetchInterval,
})
Expand Down

0 comments on commit 1bd956d

Please sign in to comment.