@@ -14,7 +14,6 @@ import {
1414} from '../../../../hooks/index.js'
1515import type { Address , WalletClient } from 'viem'
1616import { formatUnits , parseUnits } from 'viem'
17- import { normalizeTokenAddress } from '../../../../utils/tokens.js'
1817import { useAccount , useWalletClient } from 'wagmi'
1918import { useCapabilities } from 'wagmi/experimental'
2019import type { Token } from '../../../../types/index.js'
@@ -35,7 +34,10 @@ import { ProviderOptionsContext } from '../../../../providers/RelayKitProvider.j
3534import type { DebouncedState } from 'usehooks-ts'
3635import type { AdaptedWallet } from '@relayprotocol/relay-sdk'
3736import type { LinkedWallet } from '../../../../types/index.js'
38- import { addressWithFallback , isValidAddress } from '../../../../utils/address.js'
37+ import {
38+ addressWithFallback ,
39+ isValidAddress
40+ } from '../../../../utils/address.js'
3941import { adaptViemWallet , getDeadAddress } from '@relayprotocol/relay-sdk'
4042import { errorToJSON } from '../../../../utils/errors.js'
4143import { useSwapButtonCta } from '../../../../hooks/widget/useSwapButtonCta.js'
@@ -263,19 +265,28 @@ const TokenWidgetRenderer: FC<TokenWidgetRendererProps> = ({
263265 toToken ?. chainId !== undefined &&
264266 fromToken . symbol === toToken . symbol
265267
266- const toChain = relayClient ?. chains . find (
267- ( chain ) => chain . id === toToken ?. chainId
268+ const toChain = useMemo (
269+ ( ) => relayClient ?. chains ?. find ( ( chain ) => chain . id === toToken ?. chainId ) ,
270+ [ relayClient ?. chains , toToken ?. chainId ]
268271 )
269- const fromChain = relayClient ?. chains ?. find (
270- ( chain ) => chain . id === fromToken ?. chainId
272+
273+ const fromChain = useMemo (
274+ ( ) => relayClient ?. chains ?. find ( ( chain ) => chain . id === fromToken ?. chainId ) ,
275+ [ relayClient ?. chains , fromToken ?. chainId ]
271276 )
272277
273- const fromChainWalletVMSupported =
274- ! fromChain ?. vmType ||
275- supportedWalletVMs . includes ( fromChain ?. vmType ) ||
276- fromChain ?. id === 1337
277- const toChainWalletVMSupported =
278- ! toChain ?. vmType || supportedWalletVMs . includes ( toChain ?. vmType )
278+ const fromChainWalletVMSupported = useMemo (
279+ ( ) =>
280+ ! fromChain ?. vmType ||
281+ supportedWalletVMs . includes ( fromChain ?. vmType ) ||
282+ fromChain ?. id === 1337 ,
283+ [ fromChain ?. vmType , fromChain ?. id , supportedWalletVMs ]
284+ )
285+
286+ const toChainWalletVMSupported = useMemo (
287+ ( ) => ! toChain ?. vmType || supportedWalletVMs . includes ( toChain ?. vmType ) ,
288+ [ toChain ?. vmType , supportedWalletVMs ]
289+ )
279290
280291 // Automatically select the correct wallet address based on fromToken's chain VM
281292 // In sell mode (no fromChain), use toChain to find compatible wallet
@@ -338,6 +349,7 @@ const TokenWidgetRenderer: FC<TokenWidgetRendererProps> = ({
338349 : undefined ,
339350 connectorKeyOverrides
340351 )
352+
341353 if (
342354 multiWalletSupportEnabled &&
343355 toChain &&
@@ -359,6 +371,8 @@ const TokenWidgetRenderer: FC<TokenWidgetRendererProps> = ({
359371
360372 return supportedWallet ?. address
361373 }
374+
375+ return undefined
362376 } , [
363377 multiWalletSupportEnabled ,
364378 toChain ,
@@ -369,9 +383,11 @@ const TokenWidgetRenderer: FC<TokenWidgetRendererProps> = ({
369383 connectorKeyOverrides
370384 ] )
371385
372- // Don't default to origin address as recipient (prevents selling to yourself)
373386 const recipient =
374- destinationAddressOverride ?? customToAddress ?? defaultRecipient
387+ destinationAddressOverride ??
388+ customToAddress ??
389+ defaultRecipient ??
390+ ( ! allowUnsupportedRecipient && address ? address : undefined )
375391
376392 const {
377393 value : fromBalance ,
@@ -399,7 +415,8 @@ const TokenWidgetRenderer: FC<TokenWidgetRendererProps> = ({
399415 chain : toChain ,
400416 address : recipient ,
401417 currency : toToken ?. address ? ( toToken . address as Address ) : undefined ,
402- enabled : toToken !== undefined ,
418+ enabled :
419+ toToken !== undefined && recipient !== undefined && recipient !== '' ,
403420 refreshInterval : undefined ,
404421 wallet
405422 } )
@@ -472,12 +489,22 @@ const TokenWidgetRenderer: FC<TokenWidgetRendererProps> = ({
472489 ? linkedWallets ?. find ( ( wallet ) => wallet . address === recipient )
473490 : undefined ) !== undefined
474491
475- const isValidFromAddress = isValidAddress (
476- fromChain ?. vmType ,
477- address ?? '' ,
478- fromChain ?. id ,
479- linkedWallet ?. connector ,
480- connectorKeyOverrides
492+ const isValidFromAddress = useMemo (
493+ ( ) =>
494+ isValidAddress (
495+ fromChain ?. vmType ,
496+ address ?? '' ,
497+ fromChain ?. id ,
498+ linkedWallet ?. connector ,
499+ connectorKeyOverrides
500+ ) ,
501+ [
502+ fromChain ?. vmType ,
503+ address ,
504+ fromChain ?. id ,
505+ linkedWallet ?. connector ,
506+ connectorKeyOverrides
507+ ]
481508 )
482509 const fromAddressWithFallback = addressWithFallback (
483510 fromChain ?. vmType ,
@@ -487,10 +514,9 @@ const TokenWidgetRenderer: FC<TokenWidgetRendererProps> = ({
487514 connectorKeyOverrides
488515 )
489516
490- const isValidToAddress = isValidAddress (
491- toChain ?. vmType ,
492- recipient ?? '' ,
493- toChain ?. id
517+ const isValidToAddress = useMemo (
518+ ( ) => isValidAddress ( toChain ?. vmType , recipient ?? '' , toChain ?. id ) ,
519+ [ toChain ?. vmType , recipient , toChain ?. id ]
494520 )
495521
496522 const toAddressWithFallback = addressWithFallback (
@@ -599,8 +625,6 @@ const TokenWidgetRenderer: FC<TokenWidgetRendererProps> = ({
599625 isFromNative
600626 )
601627
602-
603-
604628 const shouldSetQuoteParameters =
605629 fromToken &&
606630 toToken &&
@@ -760,7 +784,12 @@ const TokenWidgetRenderer: FC<TokenWidgetRendererProps> = ({
760784 queryClient . invalidateQueries ( { queryKey : quoteQueryKey } )
761785 } , [ queryClient , quoteQueryKey ] )
762786
763- const derivedError = quote || ( isFetchingQuote && Boolean ( quoteFetchingEnabled && quoteParameters !== undefined ) ) ? null : quoteError
787+ const derivedError =
788+ quote ||
789+ ( isFetchingQuote &&
790+ Boolean ( quoteFetchingEnabled && quoteParameters !== undefined ) )
791+ ? null
792+ : quoteError
764793 const error = derivedError
765794
766795 useDisconnected ( address , ( ) => {
0 commit comments