Skip to content

Commit

Permalink
simplify ppl api
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaut committed Sep 25, 2024
1 parent 7eeb699 commit 2b84662
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 117 deletions.
72 changes: 72 additions & 0 deletions src/assets/networks.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@
"name": "Dotters Net node"
}
],
"peopleNodes": [
{
"url": "wss://sys.ibp.network/people-polkadot",
"name": "IBP1"
},
{
"url": "wss://sys.dotters.network/people-polkadot",
"name": "IBP2"
},
{
"url": "wss://rpc-people-polkadot.luckyfriday.io",
"name": "LuckyFriday"
},
{
"url": "wss://polkadot-people-rpc.polkadot.io",
"name": "Parity"
},
{
"url": "wss://people-polkadot.public.curie.radiumblock.co/ws",
"name": "RadiumBlock"
}
],
"assets": [
{
"assetId": 0,
Expand Down Expand Up @@ -121,6 +143,32 @@
"name": "OnFinality node"
}
],
"peopleNodes": [
{
"url": "wss://people-kusama-rpc.dwellir.com",
"name": "Dwellir"
},
{
"url": "wss://sys.ibp.network/people-kusama",
"name": "IBP1"
},
{
"url": "wss://sys.dotters.network/people-kusama",
"name": "IBP2"
},
{
"url": "wss://rpc-people-kusama.luckyfriday.io",
"name": "LuckyFriday"
},
{
"url": "wss://kusama-people-rpc.polkadot.io",
"name": "Parity"
},
{
"url": "wss://ksm-rpc.stakeworld.io/people",
"name": "Stakeworld"
}
],
"assets": [
{
"assetId": 0,
Expand Down Expand Up @@ -198,6 +246,24 @@
"name": "OnFinality node"
}
],
"peopleNodes": [
{
"url": "wss://sys.ibp.network/people-westend",
"name": "IBP1"
},
{
"url": "wss://people-westend-rpc.dwellir.com",
"name": "Dwellir"
},
{
"url": "wss://sys.dotters.network/people-westend",
"name": "IBP2"
},
{
"url": "wss://westend-people-rpc.polkadot.io",
"name": "Parity"
}
],
"assets": [
{
"assetId": 0,
Expand Down Expand Up @@ -244,6 +310,12 @@
"name": "Development node"
}
],
"peopleNodes": [
{
"url": "wss://sys.ibp.network/people-westend",
"name": "IBP1"
}
],
"assets": [
{
"assetId": 0,
Expand Down
74 changes: 0 additions & 74 deletions src/assets/peopleNetworks.json

This file was deleted.

6 changes: 1 addition & 5 deletions src/contexts/AccountsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export interface IAccountContext {
selectAccount: (account: InjectedPolkadotAccount | undefined) => void
}

type AccountAddressType = {
address: string
}

const AccountContext = createContext<IAccountContext | undefined>(undefined)

const AccountContextProvider = ({ children }: AccountContextProps) => {
Expand Down Expand Up @@ -54,7 +50,7 @@ const AccountContextProvider = ({ children }: AccountContextProps) => {
useEffect(() => {
if (localStorageAccount) {
const account = accounts.find(
({ address }: AccountAddressType) => address === localStorageAccount,
({ address }) => address === localStorageAccount,
)
if (account) {
selectAccount(account)
Expand Down
51 changes: 15 additions & 36 deletions src/contexts/NetworkContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { startFromWorker } from 'polkadot-api/smoldot/from-worker'
import { getChainInformation } from '@/lib/utils'
import { AssetType } from '@/lib/types'
import networks from '@/assets/networks.json'
import peopleNetworks from '@/assets/peopleNetworks.json'
import { DEFAULT_NETWORK, SELECTED_NETWORK_KEY } from '@/lib/constants'
import { useLocalStorage } from 'usehooks-ts'
import { useSearchParams } from 'react-router-dom'
Expand All @@ -39,7 +38,6 @@ type NetworkContextProps = {
children: React.ReactNode | React.ReactNode[]
}
export type NetworksFromConfig = keyof typeof networks
export type SupportedPeopleNetworkNames = keyof typeof peopleNetworks

export type SupportedNetworkNames =
| 'polkadot-lc'
Expand All @@ -50,21 +48,18 @@ export type PeopleApiType = TypedApi<
typeof dotPeople | typeof ksmPeople | typeof westendPeople
>

export const descriptorName: Record<SupportedNetworkNames, ChainDefinition> = {
polkadot: dot,
'polkadot-lc': dot,
kusama: ksm,
'kusama-lc': ksm,
westend: westend,
'fast-westend': fastWestend,
export interface ChainDefinitions {
main: ChainDefinition
people: ChainDefinition
}
export const descriptorPeopleName: Record<
SupportedPeopleNetworkNames,
ChainDefinition
> = {
polkadotPeople: dotPeople,
kusamaPeople: ksmPeople,
westendPeople: westendPeople,

export const descriptorName: Record<SupportedNetworkNames, ChainDefinitions> = {
polkadot: { main: dot, people: dotPeople },
'polkadot-lc': { main: dot, people: dotPeople },
kusama: { main: ksm, people: ksmPeople },
'kusama-lc': { main: ksm, people: ksmPeople },
westend: { main: westend, people: westendPeople },
'fast-westend': { main: fastWestend, people: westendPeople },
}

export type TrackList = Record<number, string>
Expand All @@ -78,7 +73,6 @@ export interface INetworkContext {
peopleApi?: PeopleApiType
peopleClient?: PolkadotClient
network?: SupportedNetworkNames
peopleNetwork?: SupportedPeopleNetworkNames
assetInfo: AssetType
trackList: TrackList
}
Expand All @@ -88,12 +82,6 @@ export const isSupportedNetwork = (
): network is SupportedNetworkNames =>
!!descriptorName[network as SupportedNetworkNames]

const extractPeopleFromNetwork = (network: SupportedNetworkNames) =>
network
.replace('-lc', '')
.replace('fast-', '')
.concat('People') as SupportedPeopleNetworkNames

const NetworkContext = createContext<INetworkContext | undefined>(undefined)

const NetworkContextProvider = ({ children }: NetworkContextProps) => {
Expand All @@ -112,23 +100,17 @@ const NetworkContextProvider = ({ children }: NetworkContextProps) => {

const [assetInfo, setAssetInfo] = useState<AssetType>({} as AssetType)
const [network, setNetwork] = useState<SupportedNetworkNames | undefined>()
const [peopleNetwork, setPeopleNetwork] =
useState<SupportedPeopleNetworkNames>(
extractPeopleFromNetwork(DEFAULT_NETWORK),
)
const [searchParams, setSearchParams] = useSearchParams({ network: '' })

const selectNetwork = useCallback(
(network: string) => {
if (!isSupportedNetwork(network)) {
console.error('This network is not supported', network)
selectNetwork(DEFAULT_NETWORK)
setPeopleNetwork(extractPeopleFromNetwork(DEFAULT_NETWORK))
return
}

setNetwork(network)
setPeopleNetwork(extractPeopleFromNetwork(network))

setSearchParams((prev) => {
prev.set('network', network)
Expand All @@ -150,7 +132,6 @@ const NetworkContextProvider = ({ children }: NetworkContextProps) => {
DEFAULT_NETWORK) as SupportedNetworkNames

selectNetwork(selected)
setPeopleNetwork(extractPeopleFromNetwork(selected))
}
}, [localStorageNetwork, network, searchParams, selectNetwork])

Expand Down Expand Up @@ -212,17 +193,15 @@ const NetworkContextProvider = ({ children }: NetworkContextProps) => {
peopleClient = createClient(getWsProvider(wss))
}

const descriptor = descriptorName[network]
const typedApi = client.getTypedApi(descriptor)

const descriptorPeople = descriptorPeopleName[peopleNetwork]
const typedPeopleApi = peopleClient.getTypedApi(descriptorPeople)
const descriptors = descriptorName[network]
const typedApi = client.getTypedApi(descriptors.main)
const typedPeopleApi = peopleClient.getTypedApi(descriptors.people)

setClient(client)
setPeopleClient(peopleClient)
setApi(typedApi)
setPeopleApi(typedPeopleApi)
}, [network, peopleNetwork])
}, [network])

useEffect(() => {
if (isLight) {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { House } from 'lucide-react'
import networks from '@/assets/networks.json'

import { twMerge } from 'tailwind-merge'
import type { NetworkType, RouterType, Vote } from './types'
import type { RouterType, Vote } from './types'
import { ApiType, NetworksFromConfig } from '@/contexts/NetworkContext'
import { DEFAULT_TIME, lockPeriod, ONE_DAY, THRESHOLD } from './constants'
import { bnMin } from './bnMin'
Expand All @@ -17,10 +17,11 @@ export const routes: RouterType[] = [
]

export const getChainInformation = (networkName: NetworksFromConfig) => {
const network: NetworkType = networks[networkName]
const network = networks[networkName]
return {
assetInfo: network.assets[0],
wsEndpoint: network.nodes[0].url,
peopleEndpoint: network.peopleNodes[0].url,
}
}

Expand Down

0 comments on commit 2b84662

Please sign in to comment.