Skip to content

Commit

Permalink
Refactor VaultsListView and earn-network-tools for improved network h…
Browse files Browse the repository at this point in the history
…andling and clarity
  • Loading branch information
marcinciarka committed Nov 14, 2024
1 parent 7a95d97 commit c2c75c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ import {
type SDKNetwork,
type SDKVaultsListType,
} from '@summerfi/app-types'
import {
formatCryptoBalance,
humanNetworktoSDKNetwork,
sdkNetworkToHumanNetwork,
zero,
} from '@summerfi/app-utils'
import { formatCryptoBalance, sdkNetworkToHumanNetwork, zero } from '@summerfi/app-utils'
import { capitalize } from 'lodash-es'

import { networkIconByNetworkName } from '@/constants/networkIcons'
Expand Down Expand Up @@ -50,9 +45,7 @@ export const VaultsListView = ({ selectedNetwork, vaultsList }: VaultsListViewPr
const networkFilteredVaults = useMemo(
() =>
localVaultNetwork && localVaultNetwork !== 'all-networks'
? vaultsList.filter(
(vault) => sdkNetworkToHumanNetwork(vault.protocol.network) === localVaultNetwork,
)
? vaultsList.filter(({ protocol }) => protocol.network === localVaultNetwork)
: vaultsList,
[localVaultNetwork, vaultsList],
)
Expand All @@ -63,10 +56,8 @@ export const VaultsListView = ({ selectedNetwork, vaultsList }: VaultsListViewPr
() =>
localVaultNetwork && localVaultNetwork !== 'all-networks'
? {
iconName: networkIconByNetworkName[
humanNetworktoSDKNetwork(localVaultNetwork)
] as IconNamesList,
value: sdkNetworkToHumanNetwork(localVaultNetwork),
iconName: networkIconByNetworkName[localVaultNetwork] as IconNamesList,
value: localVaultNetwork,
label: capitalize(sdkNetworkToHumanNetwork(localVaultNetwork)),
}
: allNetworksOption,
Expand All @@ -76,7 +67,7 @@ export const VaultsListView = ({ selectedNetwork, vaultsList }: VaultsListViewPr
() => [
...[...new Set(vaultsList.map(({ protocol }) => protocol.network))].map((network) => ({
iconName: networkIconByNetworkName[network] as IconNamesList,
value: sdkNetworkToHumanNetwork(network),
value: network,
label: capitalize(sdkNetworkToHumanNetwork(network)),
})),
allNetworksOption,
Expand All @@ -101,7 +92,7 @@ export const VaultsListView = ({ selectedNetwork, vaultsList }: VaultsListViewPr
if (selectedVaultData && selectedVaultData.protocol.network !== selected.value) {
setVaultId(undefined)
}
softRouterPush(`/earn/${selected.value}`)
softRouterPush(`/earn/${sdkNetworkToHumanNetwork(selected.value as SDKNetwork)}`)

break
}
Expand Down
23 changes: 13 additions & 10 deletions packages/app-utils/src/helpers/earn-network-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ export const isSupportedSDKChain = (
): chainId is SDKChainId.ARBITRUM | SDKChainId.BASE =>
typeof chainId === 'number' && sdkSupportedChains.includes(chainId)

export const sdkNetworkToHumanNetwork = (network: SDKNetwork): string => {
const humanReadableNetworkMap = {
[SDKNetwork.ArbitrumOne]: 'arbitrum',
[SDKNetwork.Base]: 'base',
}
const humanReadableNetworkMap = {
[SDKNetwork.ArbitrumOne]: 'arbitrum',
[SDKNetwork.Base]: 'base',
}

const sdkNetworkMap = {
arbitrum: SDKNetwork.ArbitrumOne,
base: SDKNetwork.Base,
}

export const sdkNetworkToHumanNetwork = (network: SDKNetwork): string => {
if (!humanReadableNetworkMap[network as SDKSupportedNetwork]) {
// eslint-disable-next-line no-console
console.error('sdkNetworkToHumanNetwork: Network needs mapping', network)
Expand All @@ -30,12 +35,10 @@ export const sdkNetworkToHumanNetwork = (network: SDKNetwork): string => {
}

export const humanNetworktoSDKNetwork = (network: string): SDKNetwork => {
const sdkNetworkMap = {
arbitrum: SDKNetwork.ArbitrumOne,
base: SDKNetwork.Base,
if (network in humanReadableNetworkMap) {
return network.toLowerCase() as SDKNetwork
}

const sdkNetwork = sdkNetworkMap[network.toLowerCase() as Partial<keyof typeof sdkNetworkMap>]
const sdkNetwork = sdkNetworkMap[network.toLowerCase() as keyof typeof sdkNetworkMap]

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!sdkNetwork) {
Expand Down

0 comments on commit c2c75c5

Please sign in to comment.