Skip to content

Commit

Permalink
Fix network issues (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaut authored Aug 28, 2024
1 parent 550ae66 commit 01ed8ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 64 deletions.
26 changes: 13 additions & 13 deletions src/assets/networks.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
[
{
{
"polkadot": {
"name": "Polkadot",
"specName": "polkadot",
"addressPrefix": 0,
"chainId": "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3",
"icon": "https://raw.githubusercontent.com/novasamatech/nova-spektr-utils/main/icons/v1/chains/Polkadot.svg",
"options": ["multisig"],
"nodes": [
{
"url": "wss://rpc.ibp.network/polkadot",
"name": "IBP network node"
},
{
"url": "wss://polkadot.public.curie.radiumblock.co/ws",
"name": "Radium node"
Expand All @@ -27,10 +31,6 @@
"url": "wss://rpc-polkadot.luckyfriday.io",
"name": "LuckyFriday node"
},
{
"url": "wss://rpc.ibp.network/polkadot",
"name": "IBP network node"
},
{
"url": "wss://rpc.dotters.network/polkadot",
"name": "Dotters Net node"
Expand Down Expand Up @@ -84,14 +84,18 @@
]
}
},
{
"kusama": {
"name": "Kusama",
"specName": "kusama",
"addressPrefix": 2,
"chainId": "0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe",
"icon": "https://raw.githubusercontent.com/novasamatech/nova-spektr-utils/main/icons/v1/chains/Kusama.svg",
"options": ["multisig"],
"nodes": [
{
"url": "wss://rpc.ibp.network/kusama",
"name": "IBP network node"
},
{
"url": "wss://kusama-rpc.dwellir.com",
"name": "Dwellir node"
Expand All @@ -104,10 +108,6 @@
"url": "wss://1rpc.io/ksm",
"name": "Automata 1RPC node"
},
{
"url": "wss://rpc.ibp.network/kusama",
"name": "IBP network node"
},
{
"url": "wss://rpc.dotters.network/kusama",
"name": "Dotters Net node"
Expand Down Expand Up @@ -169,7 +169,7 @@
]
}
},
{
"westend": {
"name": "Westend",
"specName": "westend",
"addressPrefix": 42,
Expand Down Expand Up @@ -231,4 +231,4 @@
]
}
}
]
}
30 changes: 11 additions & 19 deletions src/contexts/NetworkContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { getWsProvider } from 'polkadot-api/ws-provider/web'
import { getSmProvider } from 'polkadot-api/sm-provider'
import SmWorker from 'polkadot-api/smoldot/worker?worker'
import { startFromWorker } from 'polkadot-api/smoldot/from-worker'
import { supportedNetworksChainIds } from '@/lib/constants'
import { getChainInformation } from '@/lib/utils'
import { AssetType } from '@/lib/types'

Expand Down Expand Up @@ -46,19 +45,16 @@ const NetworkContextProvider = ({ children }: NetworkContextProps) => {
switch (network) {
case 'polkadot':
{
const [wsProv, assetInformation] = getChainInformation('polkadot')
setAssetInfo(assetInformation)
const { assetInfo, wsEndpoint } = getChainInformation('polkadot')
setAssetInfo(assetInfo)
setIsLight(false)
if (!wsProv) return
cl = createClient(getWsProvider(wsProv))
cl = createClient(getWsProvider(wsEndpoint))
typedApi = cl.getTypedApi(dot)
}
break
case 'polkadot-lc': {
const [, assetInformation] = getChainInformation(
supportedNetworksChainIds.polkadot,
)
setAssetInfo(assetInformation)
const { assetInfo } = getChainInformation('polkadot')
setAssetInfo(assetInfo)
setIsLight(true)
const smoldot = startFromWorker(new SmWorker())
const dotRelayChain = import('polkadot-api/chains/polkadot').then(
Expand All @@ -70,21 +66,17 @@ const NetworkContextProvider = ({ children }: NetworkContextProps) => {
}
case 'kusama':
{
const [wsProv, assetInformation] = getChainInformation(
supportedNetworksChainIds.kusama,
)
setAssetInfo(assetInformation)
const { assetInfo, wsEndpoint } = getChainInformation('kusama')
setAssetInfo(assetInfo)
setIsLight(false)
if (!wsProv) return
cl = createClient(getWsProvider(wsProv))

cl = createClient(getWsProvider(wsEndpoint))
typedApi = cl.getTypedApi(ksm)
}
break
case 'kusama-lc': {
const [, assetInformation] = getChainInformation(
supportedNetworksChainIds.kusama,
)
setAssetInfo(assetInformation)
const { assetInfo } = getChainInformation('kusama')
setAssetInfo(assetInfo)
setIsLight(true)
const smoldot = startFromWorker(new SmWorker())
const ksmRelayChain = import('polkadot-api/chains/ksmcc3').then(
Expand Down
7 changes: 0 additions & 7 deletions src/lib/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ const msgs: Record<string, MsgType> = {
},
}

const supportedNetworksChainIds = {
polkadot:
'0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3',
kusama: '0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe',
}

// Exports
export {
// Site specifics
Expand All @@ -67,7 +61,6 @@ export {
DelegeeListPolkadot,
DelegeeListKusama,
GithubOwner,
supportedNetworksChainIds,
// Alert messsages
msgs,
}
33 changes: 8 additions & 25 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,20 @@ import { House } from 'lucide-react'
import networks from '@/assets/networks.json'

import { twMerge } from 'tailwind-merge'
import type { AssetType, NameUrl, NetworkType, RouterType } from './types'
import { supportedNetworksChainIds } from './constants'
import type { NetworkType, RouterType } from './types'

const cn = (...inputs: ClassValue[]) => {
return twMerge(clsx(inputs))
}

const routes: RouterType[] = [{ link: 'home', name: 'Home', icon: House }]

const getSupportedNetworkInfo = (chainId: string): NetworkType => {
const network = networks.filter((n: { chainId: string }) => {
if (n.chainId === chainId) {
return n
}
})
return network as unknown as NetworkType
const getChainInformation = (networkName: keyof typeof networks) => {
const network: NetworkType = networks[networkName]
return {
assetInfo: network.assets[0],
wsEndpoint: network.nodes[0].url,
}
}

const getChainInformation = (nw: string): [string | null, AssetType] => {
const network: NetworkType = getSupportedNetworkInfo(
nw === 'polkadot'
? supportedNetworksChainIds.polkadot
: supportedNetworksChainIds.kusama,
)
const ws = network?.nodes?.map((n: NameUrl) => n.url)
if (!ws) return [null, {} as AssetType]

const randomWs = Math.floor(Math.random() * ws.length)

// return 2 values - a random wss (or null) and asset info
return [ws[randomWs] || null, network.assets[0] || ({} as AssetType)]
}

export { cn, routes, getSupportedNetworkInfo, getChainInformation }
export { cn, routes, getChainInformation }

0 comments on commit 01ed8ab

Please sign in to comment.