Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
fe69ed6
add eoa detection logic
0xTxbi Aug 12, 2025
2b6dc39
add eoa flag to adapted wallet
0xTxbi Aug 12, 2025
7b8f325
feat: changeset
0xTxbi Aug 12, 2025
fe76ef6
improved eoa detection logic
0xTxbi Aug 12, 2025
6347c47
update getQuote
0xTxbi Aug 12, 2025
36bd8e4
eoa detection hook
0xTxbi Aug 12, 2025
8b84310
update widget component
0xTxbi Aug 12, 2025
78b3cea
clean up
0xTxbi Aug 12, 2025
84f5ce0
refactor
0xTxbi Aug 13, 2025
fbf1312
add debug logs
0xTxbi Aug 14, 2025
640f714
fix double quote triggers
0xTxbi Aug 14, 2025
229c878
update eoa detection hook
0xTxbi Aug 15, 2025
11fdea5
fix malformed quote generation
0xTxbi Aug 15, 2025
0017d91
improve debug logs
0xTxbi Aug 15, 2025
28282f1
improved logging
0xTxbi Aug 19, 2025
769ba15
clean up
0xTxbi Aug 19, 2025
c97fcc7
fix package usage
0xTxbi Aug 19, 2025
9800263
Merge branch 'main' into joseph/fe-7821-for-eoas-using-protocol-v2-se…
0xTxbi Aug 20, 2025
e376ce2
fix code check
0xTxbi Aug 20, 2025
ef51c29
fix eoa detection hook
0xTxbi Sep 2, 2025
fc1f5f9
clean up
0xTxbi Sep 2, 2025
0cff8aa
Merge branch 'main' into joseph/fe-7821-for-eoas-using-protocol-v2-se…
0xTxbi Sep 2, 2025
2a07f14
address vm type switches
0xTxbi Sep 2, 2025
c8c03b7
refactor
0xTxbi Sep 2, 2025
9fcb052
clean up logs
0xTxbi Sep 3, 2025
3fc7503
improved wallet type detection and logging
0xTxbi Sep 4, 2025
2fd876e
temp debug logs
0xTxbi Sep 4, 2025
9f5c57b
clean up
0xTxbi Sep 4, 2025
ced3461
cleanup
0xTxbi Sep 4, 2025
1252cb2
more logging
0xTxbi Sep 5, 2025
11215b3
Merge branch 'main' into joseph/fe-7821-for-eoas-using-protocol-v2-se…
0xTxbi Sep 5, 2025
edd8700
clean up logs
0xTxbi Sep 5, 2025
f6f2241
clean up
0xTxbi Sep 5, 2025
ae704f2
Improve readability of eoa check
pedromcunha Sep 11, 2025
c3abdb0
Improve readability of eoa check
pedromcunha Sep 11, 2025
6ea3e13
Resolve conflicts
pedromcunha Sep 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-tigers-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@reservoir0x/relay-sdk': patch
---

Add EOA detection
1 change: 1 addition & 0 deletions packages/sdk/src/actions/getQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export async function getQuote(
throw new Error('Recipient is required')
}


const query: QuoteBody = {
user: includeDefaultParameters
? (defaultParameters?.user as string)
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/types/AdaptedWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ export type AdaptedWallet = {
chainId: number,
items: TransactionStepItem[]
) => Promise<string | undefined>
// detect if wallet is an EOA (externally owned account)
isEOA?: (chainId: number) => Promise<{ isEOA: boolean; isEIP7702Delegated: boolean }>
}
58 changes: 58 additions & 0 deletions packages/sdk/src/utils/viemWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,64 @@ export const adaptViemWallet = (wallet: WalletClient): AdaptedWallet => {
})

return id
},
isEOA: async (
chainId: number
): Promise<{ isEOA: boolean; isEIP7702Delegated: boolean }> => {
if (!wallet.account) {
return { isEOA: false, isEIP7702Delegated: false }
}

try {
let hasSmartWalletCapabilities = false
try {
const capabilities = await wallet.getCapabilities({
account: wallet.account,
chainId
})

hasSmartWalletCapabilities = Boolean(
capabilities?.atomicBatch?.supported ||
capabilities?.paymasterService?.supported ||
capabilities?.auxiliaryFunds?.supported ||
capabilities?.sessionKeys?.supported
)
} catch (capabilitiesError) {}

const client = getClient()
const chain = client.chains.find((chain) => chain.id === chainId)
const rpcUrl = chain?.httpRpcUrl

if (!chain) {
throw new Error(`Chain ${chainId} not found in relay client`)
}

const viemClient = createPublicClient({
chain: chain?.viemChain,
transport: rpcUrl ? http(rpcUrl) : http()
})

let code
try {
code = await viemClient.getCode({
address: wallet.account.address
})
} catch (getCodeError) {
throw getCodeError
}

const hasCode = Boolean(code && code !== '0x')
const isEIP7702Delegated = Boolean(
code && code.toLowerCase().startsWith('0xef01')
)
const isSmartWallet =
hasSmartWalletCapabilities || hasCode || isEIP7702Delegated
const isEOA = !isSmartWallet

return { isEOA, isEIP7702Delegated }
} catch (error) {
return { isEOA: false, isEIP7702Delegated: false }
}
}
}
}
37 changes: 25 additions & 12 deletions packages/ui/src/components/widgets/SwapWidgetRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type {
ComponentPropsWithoutRef,
Dispatch,
FC,
ReactNode,
SetStateAction
} from 'react'
import type { Dispatch, FC, ReactNode, SetStateAction } from 'react'
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
import {
useCurrencyBalance,
Expand All @@ -16,7 +10,8 @@ import {
usePreviousValueChange,
useIsWalletCompatible,
useFallbackState,
useGasTopUpRequired
useGasTopUpRequired,
useEOADetection
} from '../../hooks/index.js'
import type { Address, WalletClient } from 'viem'
import { formatUnits, parseUnits } from 'viem'
Expand Down Expand Up @@ -565,12 +560,19 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
isLoadingFromTokenPrice,
debouncedInputAmountValue,
tradeType,
originChainSupportsProtocolv2
originChainSupportsProtocolv2,
fromChain?.id
])

const loadingProtocolVersion =
fromChain?.id && originChainSupportsProtocolv2 && isLoadingFromTokenPrice

const explicitDeposit = useEOADetection(
wallet,
quoteProtocol,
fromToken?.chainId,
fromChain?.vmType
)
const normalizedSponsoredTokens = useMemo(() => {
const chainVms = relayClient?.chains.reduce(
(chains, chain) => {
Expand Down Expand Up @@ -605,8 +607,15 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
normalizedSponsoredTokens.includes(normalizedToToken) &&
normalizedSponsoredTokens.includes(normalizedFromToken)

const shouldSetQuoteParameters =
fromToken &&
toToken &&
(quoteProtocol !== 'preferV2' ||
fromChain?.vmType !== 'evm' ||
explicitDeposit !== undefined)

const quoteParameters: Parameters<typeof useQuote>['2'] =
fromToken && toToken
shouldSetQuoteParameters
? {
user: fromAddressWithFallback,
originChainId: fromToken.chainId,
Expand All @@ -633,7 +642,11 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
refundTo: fromToken?.chainId === 1337 ? address : undefined,
slippageTolerance: slippageTolerance,
topupGas: gasTopUpEnabled && gasTopUpRequired,
protocolVersion: quoteProtocol
protocolVersion: quoteProtocol,
...(quoteProtocol === 'preferV2' &&
explicitDeposit !== undefined && {
explicitDeposit: explicitDeposit
})
}
: undefined

Expand Down Expand Up @@ -711,7 +724,7 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
onQuoteReceived,
{
refetchOnWindowFocus: false,
enabled: quoteFetchingEnabled,
enabled: quoteFetchingEnabled && quoteParameters !== undefined,
refetchInterval:
!transactionModalOpen &&
!depositAddressModalOpen &&
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import useMoonPayTransaction from './useMoonPayTransaction.js'
import { useInternalRelayChains } from './useInternalRelayChains.js'
import useGasTopUpRequired from './useGasTopUpRequired.js'
import useHyperliquidUsdcBalance from './useHyperliquidUsdcBalance.js'
import useEOADetection from './useEOADetection.js'

export {
useMounted,
Expand All @@ -45,5 +46,6 @@ export {
useMoonPayTransaction,
useInternalRelayChains,
useGasTopUpRequired,
useHyperliquidUsdcBalance
useHyperliquidUsdcBalance,
useEOADetection
}
88 changes: 88 additions & 0 deletions packages/ui/src/hooks/useEOADetection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { useMemo, useEffect, useState, useRef } from 'react'
import type { AdaptedWallet } from '@relayprotocol/relay-sdk'

/**
* Hook to detect if a wallet is an EOA and return the appropriate explicitDeposit flag
* Only runs detection when protocol version is 'preferV2' and wallet supports EOA detection
*/
const useEOADetection = (
wallet?: AdaptedWallet,
protocolVersion?: string,
chainId?: number,
chainVmType?: string
): boolean | undefined => {
const [detectionState, setDetectionState] = useState<{
value: boolean | undefined
conditionKey: string
}>({ value: undefined, conditionKey: '' })

const walletRef = useRef<AdaptedWallet | undefined>(wallet)
const walletId = useRef<number>(0)

if (walletRef.current !== wallet) {
walletRef.current = wallet
walletId.current += 1
}

const conditionKey = `${wallet?.vmType}:${chainVmType}:${!!wallet?.isEOA}:${protocolVersion}:${chainId}:${walletId.current}`

const shouldDetect = useMemo(() => {
return (
wallet !== undefined &&
protocolVersion === 'preferV2' &&
chainId !== undefined &&
wallet?.vmType === 'evm' &&
chainVmType === 'evm'
)
}, [wallet?.vmType, protocolVersion, chainId, chainVmType])

// Synchronously return undefined when conditions change
const explicitDeposit = useMemo(() => {
return detectionState.conditionKey !== conditionKey || !shouldDetect
? undefined
: detectionState.value
}, [conditionKey, shouldDetect, detectionState])

useEffect(() => {
setDetectionState({ value: undefined, conditionKey })

if (!shouldDetect) {
return
}

const detectEOA = async () => {
try {
if (!wallet?.isEOA) {
setDetectionState((current) =>
current.conditionKey === conditionKey
? { value: false, conditionKey }
: current
)
return
}

const eoaResult = await wallet.isEOA(chainId!)
const { isEOA, isEIP7702Delegated } = eoaResult
const explicitDepositValue = !isEOA || isEIP7702Delegated

setDetectionState((current) =>
current.conditionKey === conditionKey
? { value: explicitDepositValue, conditionKey }
: current
)
} catch (error) {
setDetectionState((current) =>
current.conditionKey === conditionKey
? { value: undefined, conditionKey }
: current
)
}
}

detectEOA()
}, [conditionKey, shouldDetect, wallet, chainId])

return explicitDeposit
}

export default useEOADetection