From 1547ec1f80f484c5c5ffdb0dd7d75d558402c492 Mon Sep 17 00:00:00 2001 From: onghwan Date: Mon, 16 Sep 2024 13:23:13 -0400 Subject: [PATCH] implement toggle networks --- .../app/screens/bridge/BridgeUniversal.tsx | 94 ++++++++----------- .../useUnifiedBridge/useUnifiedBridge.ts | 12 ++- .../core-mobile/app/temp/caip2ChainIds.ts | 2 +- 3 files changed, 46 insertions(+), 62 deletions(-) diff --git a/packages/core-mobile/app/screens/bridge/BridgeUniversal.tsx b/packages/core-mobile/app/screens/bridge/BridgeUniversal.tsx index 65e8470ba6..960c436420 100644 --- a/packages/core-mobile/app/screens/bridge/BridgeUniversal.tsx +++ b/packages/core-mobile/app/screens/bridge/BridgeUniversal.tsx @@ -52,6 +52,7 @@ import { isUserRejectedError } from 'store/rpc/providers/walletConnect/utils' import { Network } from '@avalabs/core-chains-sdk' import { NetworkLogo } from 'screens/network/NetworkLogo' import { BridgeAsset } from '@avalabs/bridge-unified' +import { isEthereumNetwork } from 'services/network/utils/isEthereumNetwork' import { AssetBalance, BridgeProvider } from './utils/types' const blockchainTitleMaxWidth = Dimensions.get('window').width * 0.5 @@ -162,41 +163,6 @@ const BridgeUniversal: FC = () => { BIG_ZERO.eq(amount) || hasInvalidReceiveAmount - // Update selected asset for unified bridge whenever currentBlockchain changes - // useEffect(() => { - // if (!selectedBridgeAsset) return - - // const correspondingAsset = assetsWithBalances?.find(asset => { - // // when selected asset is USDC.e and we are switching to Ethereum - // // we want to automatically select USDC - // // to do that, we need to compare by symbol (USDC) instead of symbolOnNetwork (USDC.e) - // if ( - // currentBlockchain === Blockchain.ETHEREUM && - // selectedBridgeAsset.symbolOnNetwork === 'USDC.e' - // ) { - // return asset.symbol === selectedBridgeAsset.symbol - // } - - // // for all other cases we just simply compare the real symbol on network - // return asset.symbolOnNetwork === selectedBridgeAsset.symbolOnNetwork - // }) - - // // if the found asset is not in the list of new assets with balances, clear the selection - // if (!correspondingAsset) { - // setSelectedBridgeAsset(undefined) - // return - // } - - // // if the found asset is a unified bridge asset and its value is different, set it as the current asset - // if ( - // isUnifiedBridgeAsset(correspondingAsset.asset) && - // JSON.stringify(correspondingAsset.asset) !== - // JSON.stringify(selectedAsset.asset) - // ) { - // setSelectedAsset(correspondingAsset) - // } - // }, [assetsWithBalances, currentBlockchain, selectedAsset]) - useEffect(() => { setSourceNetwork(activeNetwork) }, [activeNetwork, setSourceNetwork]) @@ -281,34 +247,50 @@ const BridgeUniversal: FC = () => { }>() useEffect(() => { - if (previousConfig) { - const oldChainId = previousConfig.sourceNetwork.chainId - + if (previousConfig?.bridgeAsset) { + let bridgeAssetSymbol = previousConfig.bridgeAsset.symbol if ( - targetNetworks.findIndex(network => network.chainId === oldChainId) !== - -1 + sourceNetwork && + isEthereumNetwork(sourceNetwork) && + bridgeAssetSymbol === 'USDC.e' ) { - setTargetNetwork(previousConfig.sourceNetwork) - if (previousConfig.bridgeAsset) { - const bridgeAssetSymbol = previousConfig.bridgeAsset.symbol - const bridgeAsset = bridgeAssets.find( - asset => asset.symbol === bridgeAssetSymbol - ) - if (bridgeAsset) { - setSelectedBridgeAsset(bridgeAsset) - } - } - setPreviousConfig(undefined) - setAmount(undefined) + bridgeAssetSymbol = 'USDC' + } + + const bridgeAsset = bridgeAssets.find( + asset => asset.symbol === bridgeAssetSymbol + ) + + if (bridgeAsset) { + setSelectedBridgeAsset(bridgeAsset) } } }, [ - sourceNetwork, - previousConfig, - targetNetworks, - setTargetNetwork, + previousConfig?.bridgeAsset, setSelectedBridgeAsset, bridgeAssets, + setAmount, + sourceNetwork + ]) + + useEffect(() => { + if ( + previousConfig?.sourceNetwork && + targetNetworks.findIndex( + network => network.chainId === previousConfig.sourceNetwork.chainId + ) !== -1 + ) { + setTargetNetwork(previousConfig.sourceNetwork) + + setPreviousConfig(undefined) + setAmount(undefined) + } + }, [ + selectedBridgeAsset, + previousConfig?.sourceNetwork, + targetNetworks, + setTargetNetwork, + setPreviousConfig, setAmount ]) diff --git a/packages/core-mobile/app/screens/bridge/hooks/useUnifiedBridge/useUnifiedBridge.ts b/packages/core-mobile/app/screens/bridge/hooks/useUnifiedBridge/useUnifiedBridge.ts index 5c2f078912..e2de1547b2 100644 --- a/packages/core-mobile/app/screens/bridge/hooks/useUnifiedBridge/useUnifiedBridge.ts +++ b/packages/core-mobile/app/screens/bridge/hooks/useUnifiedBridge/useUnifiedBridge.ts @@ -9,7 +9,7 @@ import { selectActiveAccount } from 'store/account/slice' import { setPendingTransfer } from 'store/unifiedBridge/slice' import AnalyticsService from 'services/analytics/AnalyticsService' import { useInAppRequest } from 'hooks/useInAppRequest' -import { useNetworksByCaip2ChainIds } from 'temp/caip2ChainIds' +import { useNetworksFromCaip2ChainIds } from 'temp/caip2ChainIds' import { Network } from '@avalabs/core-chains-sdk' import { isEthereumNetwork } from 'services/network/utils/isEthereumNetwork' import { BridgeAsset } from '@avalabs/bridge-unified' @@ -51,11 +51,11 @@ export const useUnifiedBridge = (amount: Big): UnifiedBridge => { [selectedBridgeAsset, assetsWithBalances] ) - const sourceNetworks = useNetworksByCaip2ChainIds( + const sourceNetworks = useNetworksFromCaip2ChainIds( Object.keys(chainAssetMap ?? []) ) - const targetNetworks = useNetworksByCaip2ChainIds( + const targetNetworks = useNetworksFromCaip2ChainIds( Object.keys(selectedBridgeAsset?.destinations ?? []) ) @@ -145,8 +145,10 @@ export const useUnifiedBridge = (amount: Big): UnifiedBridge => { ]) useEffect(() => { - setSelectedBridgeAsset(bridgeAssets[0]) - }, [bridgeAssets]) + if (!selectedBridgeAsset) { + setSelectedBridgeAsset(bridgeAssets[0]) + } + }, [selectedBridgeAsset, bridgeAssets]) useEffect(() => { if (targetNetworks.length === 0) { diff --git a/packages/core-mobile/app/temp/caip2ChainIds.ts b/packages/core-mobile/app/temp/caip2ChainIds.ts index 436614d184..e920c791c7 100644 --- a/packages/core-mobile/app/temp/caip2ChainIds.ts +++ b/packages/core-mobile/app/temp/caip2ChainIds.ts @@ -150,7 +150,7 @@ export const getChainIdFromCaip2 = ( : Number(caip2ChainId.split(':')[1]) } -export const useNetworksByCaip2ChainIds = ( +export const useNetworksFromCaip2ChainIds = ( caip2ChainIds: string[] ): Network[] => { const { networks } = useNetworks()