Skip to content

Commit

Permalink
implement toggle networks
Browse files Browse the repository at this point in the history
  • Loading branch information
onghwan committed Sep 18, 2024
1 parent a8ab299 commit 1547ec1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 62 deletions.
94 changes: 38 additions & 56 deletions packages/core-mobile/app/screens/bridge/BridgeUniversal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 ?? [])
)

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-mobile/app/temp/caip2ChainIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const getChainIdFromCaip2 = (
: Number(caip2ChainId.split(':')[1])
}

export const useNetworksByCaip2ChainIds = (
export const useNetworksFromCaip2ChainIds = (
caip2ChainIds: string[]
): Network[] => {
const { networks } = useNetworks()
Expand Down

0 comments on commit 1547ec1

Please sign in to comment.