From 3d1b2abd5bc20ba5e8d35151d5e456c872d799c6 Mon Sep 17 00:00:00 2001 From: Nikos Kontakis Date: Tue, 12 Apr 2022 11:57:05 +0200 Subject: [PATCH] Fix broken substrate connect light client (#7362) * Fix broken substrate connect light client * make code slicker * minor * Update packages/react-api/src/Api.tsx Co-authored-by: Jaco * Update packages/react-api/src/Api.tsx Co-authored-by: Jaco * Update packages/react-api/src/Api.tsx Co-authored-by: Jaco Co-authored-by: Jaco --- packages/react-api/src/Api.tsx | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/react-api/src/Api.tsx b/packages/react-api/src/Api.tsx index 1dc210ff6d..bd60220a6c 100644 --- a/packages/react-api/src/Api.tsx +++ b/packages/react-api/src/Api.tsx @@ -18,7 +18,7 @@ import { TokenUnit } from '@polkadot/react-components/InputNumber'; import { StatusContext } from '@polkadot/react-components/Status'; import { useApiUrl, useEndpoint } from '@polkadot/react-hooks'; import ApiSigner from '@polkadot/react-signer/signers/ApiSigner'; -import { ScProvider } from '@polkadot/rpc-provider/substrate-connect'; +import { ScProvider, WellKnownChain } from '@polkadot/rpc-provider/substrate-connect'; import { keyring } from '@polkadot/ui-keyring'; import { settings } from '@polkadot/ui-settings'; import { formatBalance, isNumber, isTestChain, objectSpread, stringify } from '@polkadot/util'; @@ -183,6 +183,21 @@ async function loadOnReady (api: ApiPromise, endpoint: LinkOption | null, inject }; } +function getWellKnownChain (chain = 'polkadot') { + switch (chain) { + case 'kusama': + return WellKnownChain.ksmcc3; + case 'polkadot': + return WellKnownChain.polkadot; + case 'rococo': + return WellKnownChain.rococo_v2_1; + case 'westend': + return WellKnownChain.westend2; + default: + throw new Error(`Unable to construct light chain ${chain}`); + } +} + // eslint-disable-next-line @typescript-eslint/require-await async function createApi (apiUrl: string, signer: ApiSigner, onError: (error: unknown) => void): Promise>> { const types = getDevTypes(); @@ -190,7 +205,7 @@ async function createApi (apiUrl: string, signer: ApiSigner, onError: (error: un try { const provider = isLight - ? new ScProvider(apiUrl.replace('light://substrate-connect/', '') as 'polkadot') + ? new ScProvider(getWellKnownChain(apiUrl.replace('light://substrate-connect/', ''))) : new WsProvider(apiUrl); api = new ApiPromise({