Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: nest IscChains in StardusNetwork #2375

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DEFAULT_PERSISTED_PROFILE_OBJECT } from '@core/profile/constants'
import { IOnboardingProfile } from '../interfaces'
import { DEFAULT_L1_EVM_NETWORK_CONFIGURATION, SupportedNetworkId } from '@core/network/constants'
import features from '@features/features'
import { IPureEvmNetworkConfiguration, IStardustNetwork } from '@core/network/interfaces'
import { IPureEvmNetworkConfiguration } from '@core/network/interfaces'

export function convertOnboardingProfileToPersistedProfile(
onboardingProfile?: Partial<IOnboardingProfile>
Expand All @@ -26,7 +26,7 @@ export function convertOnboardingProfileToPersistedProfile(

return {
...structuredClone(DEFAULT_PERSISTED_PROFILE_OBJECT),
...{ network: onboardingProfile?.network as IStardustNetwork },
...{ network },
...(evmNetworks && { evmNetworks }),
...(onboardingProfile?.name && { name: onboardingProfile.name }),
...(onboardingProfile?.id && { id: onboardingProfile.id }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

import { get } from 'svelte/store'
import { IBaseToken } from '@core/token/interfaces'
import { activeProfile, updateActiveProfile } from '@core/profile/stores'
import { NetworkNamespace } from '../enums'
import {
Expand All @@ -10,10 +9,9 @@ import {
IStardustNetwork,
IStardustNetworkMetadata,
} from '../interfaces'
import { addNetwork, networkStatus } from '../stores'
import { NetworkId, StardustNetworkId } from '../types'

import { IBaseToken } from '@core/token'
import { IscChain } from '../classes'
import { addChain, networkStatus, removeChain } from '../stores'
import { EvmNetworkId, StardustNetworkId } from '../types'

export class StardustNetwork implements IStardustNetwork {
public readonly id: StardustNetworkId
Expand All @@ -24,7 +22,7 @@ export class StardustNetwork implements IStardustNetwork {
public readonly bech32Hrp: string
public readonly protocol: IProtocol
public readonly baseToken: IBaseToken
public readonly chainConfigurations: IIscChainConfiguration[]
public iscChains: IscChain[]

constructor(persistedNetwork: IStardustNetworkMetadata) {
this.id = persistedNetwork.id
Expand All @@ -35,7 +33,12 @@ export class StardustNetwork implements IStardustNetwork {
this.networkName = persistedNetwork.protocol.networkName
this.protocol = persistedNetwork.protocol
this.baseToken = persistedNetwork.baseToken
this.chainConfigurations = persistedNetwork.chainConfigurations

this.iscChains = persistedNetwork.chainConfigurations
.map((chainConfiguration) => {
return new IscChain(chainConfiguration)
})
.filter(Boolean)
}

getStatus(): INetworkStatus {
Expand All @@ -50,28 +53,27 @@ export class StardustNetwork implements IStardustNetwork {
network.chainConfigurations.push(chainConfiguration)
updateActiveProfile({ network })

this.chainConfigurations.push(chainConfiguration)
addNetwork(chainConfiguration)
const iscChain = new IscChain(chainConfiguration)
this.iscChains.push(iscChain)
addChain(iscChain)
}
}

private isChainAlreadyAdded(chainConfiguration: IIscChainConfiguration): boolean {
return this.chainConfigurations.some((evmNetwork) => {
const hasSameName = evmNetwork.name === chainConfiguration.name
const hasSameId = evmNetwork.id === chainConfiguration.id
return this.iscChains.some(({ name, id }) => {
const hasSameName = name === chainConfiguration.name
const hasSameId = id === chainConfiguration.id
return hasSameName || hasSameId
})
}

editChain(networkId: NetworkId, payload: Partial<IIscChainConfiguration>): Promise<void> {
return Promise.resolve()
}

removeChain(networkId: NetworkId): void {
removeChain(networkId: EvmNetworkId): void {
const network = get(activeProfile).network
const newChains = network.chainConfigurations.filter(
(chainConfiguration) => chainConfiguration.id !== networkId
)
this.iscChains = this.iscChains.filter((chain) => chain.id === networkId)
removeChain(networkId)
updateActiveProfile({ network: { ...network, chainConfigurations: newChains } })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { NetworkNamespace } from '../enums'
import { NetworkId } from '../types'
import { INetworkStatus } from './network-status.interface'
import { IIscChainConfiguration } from './evm-network-configuration.interface'
import { IStardustNetworkMetadata } from '@core/network'
import { IscChain, IStardustNetworkMetadata } from '@core/network'

export interface IStardustNetwork extends IStardustNetworkMetadata {
export interface IStardustNetwork extends Omit<IStardustNetworkMetadata, 'chainConfigurations'> {
namespace: NetworkNamespace.Stardust
bech32Hrp: string

iscChains: IscChain[]

getStatus(): INetworkStatus

addChain(chainConfiguration: IIscChainConfiguration): void
editChain(networkId: NetworkId, payload: Partial<IIscChainConfiguration>): Promise<void>
removeChain(networkId: NetworkId): void
}
29 changes: 15 additions & 14 deletions packages/shared/src/lib/core/network/stores/networks.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@ import { activeProfile } from '@core/profile/stores'
import features from '@features/features'

import { IscChain, EvmNetwork, StardustNetwork } from '../classes'
import { IEvmNetwork, IIscChainConfiguration, IStardustNetwork } from '../interfaces'
import { Network, NetworkId } from '../types'
import { IEvmNetwork, IIscChain, IStardustNetwork } from '../interfaces'
import { EvmNetworkId, Network, NetworkId } from '../types'
import { EvmNetworkType, NetworkNamespace } from '../enums'

export const networks: Writable<Network[]> = writable([])

export function initializeNetworks(): void {
const profile = get(activeProfile)
let _networks: Network[] = []
const _networks: Network[] = []
if (profile?.network) {
const stardustNetwork = new StardustNetwork(profile.network)
const chains = profile.network.chainConfigurations
.map((chainConfiguration) => {
return new IscChain(chainConfiguration)
})
.filter(Boolean) as IEvmNetwork[]
_networks = [stardustNetwork, ...chains]
_networks.push(stardustNetwork, ...stardustNetwork.iscChains)
}

if (features.network.evmNetworks.enabled) {
Expand All @@ -32,20 +27,26 @@ export function initializeNetworks(): void {
networks.set(_networks)
}

export function addNetwork(chainConfiguration: IIscChainConfiguration): void {
const network = getNetwork(chainConfiguration.id)
export function getNetwork(networkId: NetworkId): Network | undefined {
return get(networks)?.find((network) => network.id === networkId)
}

export function addChain(chain: IIscChain): void {
const network = getNetwork(chain.id)
if (network) {
return
}

networks.update((networks) => {
networks.push(new IscChain(chainConfiguration))
networks.push(chain)
return networks
})
}

export function getNetwork(networkId: NetworkId): Network | undefined {
return get(networks)?.find((network) => network.id === networkId)
export function removeChain(chainId: EvmNetworkId): void {
networks.update((networks) => {
return networks.filter(({ id }) => id !== chainId)
})
}

export function getL1Network(): IStardustNetwork {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export function buildPersistedNetworkFromNodeInfoResponse(
coinType: _coinType,
protocol: nodeInfoResponse?.nodeInfo?.protocol,
baseToken: { standard: TokenStandard.BaseToken, ...nodeInfoResponse?.nodeInfo?.baseToken },
chainConfigurations: chainConfigurations,
chainConfigurations,
}
}
Loading