@@ -315,16 +315,53 @@ export const getLastSelectedChainId = createSelector(
315315) ;
316316
317317// This returns undefined if the selected chain is not supported by swap/bridge (i.e, testnets)
318- export const getFromChain = createDeepEqualSelector (
319- [ getFromChains , getMultichainProviderConfig ] ,
320- ( fromChains , providerConfig ) => {
318+ export const getFromToken = createSelector (
319+ [
320+ ( state : BridgeAppState ) => state . bridge . fromToken ,
321+ getFromChains ,
322+ getMultichainProviderConfig ,
323+ ] ,
324+ ( fromToken , fromChains , providerConfig ) => {
321325 // When the page loads the global network always matches the network filter
322326 // Because useBridging checks whether the lastSelectedNetwork matches the provider config
323327 // Then useBridgeQueryParams sets the global network to lastSelectedNetwork as needed
324328 // TODO remove providerConfig references and just use getLastSelectedChainId
325- return fromChains . find (
329+ const fromChain = fromChains . find (
326330 ( { chainId } ) => chainId === providerConfig ?. chainId ,
327331 ) ;
332+ if ( ! fromChain ) {
333+ return null ;
334+ }
335+ const fromChainId = fromChain . chainId ;
336+ if (
337+ fromToken &&
338+ fromToken . address &&
339+ ! isCrossChain ( fromToken . chainId , fromChainId )
340+ ) {
341+ return fromToken ;
342+ }
343+ const { iconUrl, ...nativeAsset } = getNativeAssetForChainId ( fromChainId ) ;
344+ const newToToken = toBridgeToken ( nativeAsset ) ;
345+ return newToToken
346+ ? {
347+ ...newToToken ,
348+ chainId : formatChainIdToCaip ( fromChainId ) ,
349+ }
350+ : newToToken ;
351+ } ,
352+ ) ;
353+
354+ // Return's the chain matching the fromToken's chainId
355+ export const getFromChain = createSelector (
356+ [ getFromToken , getFromChains ] ,
357+ ( fromToken , fromChains ) => {
358+ if ( ! fromToken ) {
359+ return undefined ;
360+ }
361+ return fromChains . find (
362+ ( { chainId } ) =>
363+ formatChainIdToCaip ( chainId ) === formatChainIdToCaip ( fromToken . chainId ) ,
364+ ) ;
328365 } ,
329366) ;
330367
@@ -418,29 +455,6 @@ export const getToChain = createSelector(
418455 } ,
419456) ;
420457
421- export const getFromToken = createSelector (
422- [
423- ( state : BridgeAppState ) => state . bridge . fromToken ,
424- ( state ) => getFromChain ( state ) ?. chainId ,
425- ] ,
426- ( fromToken , fromChainId ) => {
427- if ( ! fromChainId ) {
428- return null ;
429- }
430- if ( fromToken ?. address ) {
431- return fromToken ;
432- }
433- const { iconUrl, ...nativeAsset } = getNativeAssetForChainId ( fromChainId ) ;
434- const newToToken = toBridgeToken ( nativeAsset ) ;
435- return newToToken
436- ? {
437- ...newToToken ,
438- chainId : formatChainIdToCaip ( fromChainId ) ,
439- }
440- : newToToken ;
441- } ,
442- ) ;
443-
444458export const getToToken = createSelector (
445459 [ getFromToken , getToChain , ( state ) => state . bridge . toToken ] ,
446460 ( fromToken , toChain , toToken ) => {
@@ -727,10 +741,7 @@ export const getToTokenConversionRate = createDeepEqualSelector(
727741 }
728742 if ( toChain ?. chainId && toToken ) {
729743 const nativeAssetId = getNativeAssetForChainId ( toChain . chainId ) ?. assetId ;
730- const tokenAssetId = toAssetId (
731- toToken . address ,
732- formatChainIdToCaip ( toChain . chainId ) ,
733- ) ;
744+ const tokenAssetId = toToken . assetId ;
734745
735746 // For non-EVM tokens (Solana, Bitcoin, Tron), we use the conversion rates provided by the multichain rates controller
736747 if ( isNonEvmChain ( toChain . chainId ) && nativeAssetId && tokenAssetId ) {
0 commit comments