Skip to content

Commit

Permalink
fix: disable refuel if destination address is smart account
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Nov 1, 2024
1 parent 92e3f4b commit b448b70
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/widget/src/hooks/useGasRefuel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useAccount } from '@lifi/wallet-management'
import { useMemo } from 'react'
import { useFieldValues } from '../stores/form/useFieldValues.js'
import { useAvailableChains } from './useAvailableChains.js'
import { useGasRecommendation } from './useGasRecommendation.js'
import { useIsContractAddress } from './useIsContractAddress.js'
import { useTokenBalance } from './useTokenBalance.js'

export const useGasRefuel = () => {
Expand All @@ -17,6 +19,19 @@ export const useGasRefuel = () => {
const toChain = getChainById(toChainId)
const fromChain = getChainById(fromChainId)

const { account } = useAccount({ chainType: fromChain?.chainType })

const isFromContractAddress = useIsContractAddress(
account.address,
fromChainId,
account.chainType
)
const isToContractAddress = useIsContractAddress(
toAddress,
toChainId,
toChain?.chainType
)

const { token: nativeToken } = useTokenBalance(
toAddress,
toChainId ? toChain?.nativeToken : undefined,
Expand All @@ -33,6 +48,10 @@ export const useGasRefuel = () => {
const isChainTypeSatisfied =
fromChain?.chainType !== toChain?.chainType ? Boolean(toAddress) : true

const isToAddressSatisfied = isFromContractAddress
? toAddress && toAddress !== account.address && !isToContractAddress
: true

const enabled = useMemo(() => {
if (
// We don't allow same chain refuel.
Expand All @@ -41,7 +60,8 @@ export const useGasRefuel = () => {
!gasRecommendation?.available ||
!gasRecommendation?.recommended ||
!nativeToken ||
!isChainTypeSatisfied
!isChainTypeSatisfied ||
!isToAddressSatisfied
) {
return false
}
Expand All @@ -56,6 +76,7 @@ export const useGasRefuel = () => {
fromChainId,
gasRecommendation,
isChainTypeSatisfied,
isToAddressSatisfied,
nativeToken,
toChainId,
])
Expand Down

0 comments on commit b448b70

Please sign in to comment.