Carry teleport info per chain (#5430)

* Carry teleport info per chain

* Always has teleports
This commit is contained in:
Jaco 2021-06-03 14:21:21 +03:00 committed by GitHub
parent 72bbe7d1c0
commit 3208fe117e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 30 deletions

View File

@ -27,18 +27,19 @@ export function createWestend (t: TFunction): EndpointOption {
'Patract Elara': 'wss://westend.elara.patract.io',
OnFinality: 'wss://westend.api.onfinality.io/public-ws'
},
teleport: [1000], // statemint only
linked: [
// (1) system parachains (none available yet)
// ...
// (2) common good, leave as second group
{
info: 'westmint',
allowTeleport: true,
paraId: 1000,
text: t('rpc.westend.shell', 'Westmint', { ns: 'apps-config' }),
providers: {
Parity: 'wss://westmint-rpc.polkadot.io'
}
},
teleport: [-1] // relay only
},
// (3) parachains with id, see Rococo (info here maps to the actual "named icon")
//

View File

@ -4,7 +4,6 @@
import type { Option } from '../settings/types';
export interface EndpointOption {
allowTeleport?: boolean;
dnslink?: string;
genesisHash?: string;
isChild?: boolean;
@ -14,11 +13,11 @@ export interface EndpointOption {
info?: string;
paraId?: number;
providers: Record<string, string>;
teleport?: number[];
text: React.ReactNode;
}
export interface LinkOption extends Option {
allowTeleport?: boolean;
dnslink?: string;
genesisHash?: string;
genesisHashRelay?: string;
@ -28,5 +27,6 @@ export interface LinkOption extends Option {
isSpaced?: boolean;
linked?: LinkOption[];
paraId?: number;
teleport?: number[];
textBy: string;
}

View File

@ -21,14 +21,14 @@ export function expandLinked (input: LinkOption[]): LinkOption[] {
}, []);
}
export function expandEndpoint (t: TFunction, { allowTeleport, dnslink, genesisHash, info, isChild, isDisabled, linked, paraId, providers, text }: EndpointOption): LinkOption[] {
export function expandEndpoint (t: TFunction, { dnslink, genesisHash, info, isChild, isDisabled, linked, paraId, providers, teleport, text }: EndpointOption): LinkOption[] {
const base = {
allowTeleport,
genesisHash,
info,
isChild,
isDisabled,
paraId,
teleport,
text
};
@ -47,13 +47,6 @@ export function expandEndpoint (t: TFunction, { allowTeleport, dnslink, genesisH
linked.forEach((o) => options.push(...expandEndpoint(t, o)));
last.isRelay = true;
last.linked = options;
// if one of the children allows teleport, add it to the root as well
const allowTeleport = options.some(({ allowTeleport }) => allowTeleport);
result.forEach((r): void => {
r.allowTeleport = allowTeleport;
});
}
return expandLinked(result);

View File

@ -7,6 +7,7 @@ import type { ParaId } from '@polkadot/types/interfaces';
import { useEffect, useState } from 'react';
import { createWsEndpoints } from '@polkadot/apps-config';
import { isNumber } from '@polkadot/util';
import { useApi } from './useApi';
import { useCall } from './useCall';
@ -18,9 +19,22 @@ interface Teleport {
paraId?: ParaId;
}
const endpoints = createWsEndpoints((k: string, v: string | undefined) => v || k).filter(({ allowTeleport }) => allowTeleport);
interface ExtLinkOption extends LinkOption {
teleport: number[];
}
function extractRelayDestinations (relayGenesis: string, filter: (l: LinkOption) => boolean): LinkOption[] {
const DEFAULT_STATE: Teleport = {
allowTeleport: false,
destinations: [],
isParachain: false
};
const endpoints = createWsEndpoints((k: string, v: string | undefined) => v || k).filter((v): v is ExtLinkOption =>
!!v.teleport &&
!!v.teleport.length
);
function extractRelayDestinations (relayGenesis: string, filter: (l: ExtLinkOption) => boolean): ExtLinkOption[] {
return endpoints
.filter((l) =>
(
@ -28,7 +42,7 @@ function extractRelayDestinations (relayGenesis: string, filter: (l: LinkOption)
l.genesisHash === relayGenesis
) && filter(l)
)
.reduce((result: LinkOption[], curr): LinkOption[] => {
.reduce((result: ExtLinkOption[], curr): ExtLinkOption[] => {
if (!result.some(({ genesisHash, paraId }) => paraId === curr.paraId || genesisHash === curr.genesisHash)) {
result.push(curr);
}
@ -47,24 +61,29 @@ function extractRelayDestinations (relayGenesis: string, filter: (l: LinkOption)
export function useTeleport (): Teleport {
const { api, apiUrl, isApiReady } = useApi();
const chainParaId = useCall<ParaId>(isApiReady && api.query.parachainInfo?.parachainId);
const [state, setState] = useState<Teleport>(() => ({
allowTeleport: false,
destinations: [],
isParachain: false
}));
const [state, setState] = useState<Teleport>(() => ({ ...DEFAULT_STATE }));
useEffect((): void => {
if (isApiReady) {
const relayGenesis = api.genesisHash.toHex();
const destinations = extractRelayDestinations(relayGenesis, ({ genesisHash }) => relayGenesis !== genesisHash);
const endpoint = endpoints.find(({ value }) =>
value === apiUrl
);
setState((prev) => ({
...prev,
allowTeleport: destinations.length !== 0,
destinations
}));
if (endpoint) {
const destinations = extractRelayDestinations(relayGenesis, ({ paraId }) =>
isNumber(paraId) &&
endpoint.teleport.includes(paraId)
);
setState((prev) => ({
...prev,
allowTeleport: destinations.length !== 0,
destinations
}));
}
}
}, [api, isApiReady]);
}, [api, apiUrl, isApiReady]);
useEffect((): void => {
if (chainParaId) {
@ -78,8 +97,9 @@ export function useTeleport (): Teleport {
// FIXME Cannot quite get this working...
allowTeleport: false, // !!endpoint,
destinations: endpoint && endpoint.genesisHashRelay
// FIXME we probably just want to check for !chainParaId.eq(paraId) || !!genesisHash (would affect Teleport modal)
? extractRelayDestinations(endpoint.genesisHashRelay, ({ genesisHash }) => !!genesisHash)
? extractRelayDestinations(endpoint.genesisHashRelay, ({ paraId }) =>
endpoint.teleport.includes(isNumber(paraId) ? paraId : -1)
)
: [],
isParachain: true,
paraId: chainParaId