From 2b8466215b2b7559b4ae0cb8176aa34d91fa5286 Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Wed, 25 Sep 2024 15:13:23 +0100 Subject: [PATCH] simplify ppl api --- src/assets/networks.json | 72 +++++++++++++++++++++++++++++++ src/assets/peopleNetworks.json | 74 -------------------------------- src/contexts/AccountsContext.tsx | 6 +-- src/contexts/NetworkContext.tsx | 51 +++++++--------------- src/lib/utils.ts | 5 ++- 5 files changed, 91 insertions(+), 117 deletions(-) delete mode 100644 src/assets/peopleNetworks.json diff --git a/src/assets/networks.json b/src/assets/networks.json index bda765b..bb93c20 100644 --- a/src/assets/networks.json +++ b/src/assets/networks.json @@ -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, @@ -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, @@ -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, @@ -244,6 +310,12 @@ "name": "Development node" } ], + "peopleNodes": [ + { + "url": "wss://sys.ibp.network/people-westend", + "name": "IBP1" + } + ], "assets": [ { "assetId": 0, diff --git a/src/assets/peopleNetworks.json b/src/assets/peopleNetworks.json deleted file mode 100644 index 3d8b0c6..0000000 --- a/src/assets/peopleNetworks.json +++ /dev/null @@ -1,74 +0,0 @@ -{ - "polkadotPeople": { - "nodes": [ - { - "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" - } - ] - }, - "kusamaPeople": { - "nodes": [ - { - "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" - } - ] - }, - "westendPeople": { - "nodes": [ - { - "url": "wss://people-westend-rpc.dwellir.com", - "name": "Dwellir" - }, - { - "url": "wss://sys.ibp.network/people-westend", - "name": "IBP1" - }, - { - "url": "wss://sys.dotters.network/people-westend", - "name": "IBP2" - }, - { - "url": "wss://westend-people-rpc.polkadot.io", - "name": "Parity" - } - ] - } -} diff --git a/src/contexts/AccountsContext.tsx b/src/contexts/AccountsContext.tsx index 22713d1..3facf21 100644 --- a/src/contexts/AccountsContext.tsx +++ b/src/contexts/AccountsContext.tsx @@ -21,10 +21,6 @@ export interface IAccountContext { selectAccount: (account: InjectedPolkadotAccount | undefined) => void } -type AccountAddressType = { - address: string -} - const AccountContext = createContext(undefined) const AccountContextProvider = ({ children }: AccountContextProps) => { @@ -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) diff --git a/src/contexts/NetworkContext.tsx b/src/contexts/NetworkContext.tsx index 4b18b87..2011e25 100644 --- a/src/contexts/NetworkContext.tsx +++ b/src/contexts/NetworkContext.tsx @@ -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' @@ -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' @@ -50,21 +48,18 @@ export type PeopleApiType = TypedApi< typeof dotPeople | typeof ksmPeople | typeof westendPeople > -export const descriptorName: Record = { - 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 = { + 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 @@ -78,7 +73,6 @@ export interface INetworkContext { peopleApi?: PeopleApiType peopleClient?: PolkadotClient network?: SupportedNetworkNames - peopleNetwork?: SupportedPeopleNetworkNames assetInfo: AssetType trackList: TrackList } @@ -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(undefined) const NetworkContextProvider = ({ children }: NetworkContextProps) => { @@ -112,10 +100,6 @@ const NetworkContextProvider = ({ children }: NetworkContextProps) => { const [assetInfo, setAssetInfo] = useState({} as AssetType) const [network, setNetwork] = useState() - const [peopleNetwork, setPeopleNetwork] = - useState( - extractPeopleFromNetwork(DEFAULT_NETWORK), - ) const [searchParams, setSearchParams] = useSearchParams({ network: '' }) const selectNetwork = useCallback( @@ -123,12 +107,10 @@ const NetworkContextProvider = ({ children }: NetworkContextProps) => { 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) @@ -150,7 +132,6 @@ const NetworkContextProvider = ({ children }: NetworkContextProps) => { DEFAULT_NETWORK) as SupportedNetworkNames selectNetwork(selected) - setPeopleNetwork(extractPeopleFromNetwork(selected)) } }, [localStorageNetwork, network, searchParams, selectNetwork]) @@ -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) { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 0164138..9be1d57 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -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' @@ -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, } }