Skip to content

Commit

Permalink
Allow xcm asset teleport (polkadot-js#5413)
Browse files Browse the repository at this point in the history
* Allow xcm asset teleport

* Should be operational...

* lock cron

* Teleport from parachain to relay

* Only enable on relay

* Disable para teleport
  • Loading branch information
jacogr authored Jun 2, 2021
1 parent f35c0de commit 7148e7e
Show file tree
Hide file tree
Showing 23 changed files with 314 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'Lock Threads'

on:
schedule:
- cron: '4 4 * * *'
- cron: '9 9 * * *'

jobs:
lock:
Expand Down
2 changes: 1 addition & 1 deletion packages/apps-config/src/endpoints/development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { TFunction } from 'i18next';
import type { LinkOption } from '../settings/types';
import type { LinkOption } from './types';

export const CUSTOM_ENDPOINT_KEY = 'polkadot-app-custom-endpoints';

Expand Down
2 changes: 1 addition & 1 deletion packages/apps-config/src/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { TFunction } from 'i18next';
import type { LinkOption } from '../settings/types';
import type { LinkOption } from './types';

import { createCustom, createDev, createOwn } from './development';
import { createProduction } from './production';
Expand Down
2 changes: 1 addition & 1 deletion packages/apps-config/src/endpoints/production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { TFunction } from 'i18next';
import type { LinkOption } from '../settings/types';
import type { LinkOption } from './types';

import { expandEndpoints } from './util';

Expand Down
2 changes: 1 addition & 1 deletion packages/apps-config/src/endpoints/productionRelays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { TFunction } from 'i18next';
import type { LinkOption } from '../settings/types';
import type { LinkOption } from './types';

import { createKusama } from './productionRelayKusama';
import { createPolkadot } from './productionRelayPolkadot';
Expand Down
2 changes: 1 addition & 1 deletion packages/apps-config/src/endpoints/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { TFunction } from 'i18next';
import type { LinkOption } from '../settings/types';
import type { LinkOption } from './types';

import { expandEndpoints } from './util';

Expand Down
1 change: 1 addition & 0 deletions packages/apps-config/src/endpoints/testingRelayWestend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function createWestend (t: TFunction): EndpointOption {
// (2) common good, leave as second group
{
info: 'westmint',
allowTeleport: true,
paraId: 1000,
text: t('rpc.westend.shell', 'Westmint', { ns: 'apps-config' }),
providers: {
Expand Down
2 changes: 1 addition & 1 deletion packages/apps-config/src/endpoints/testingRelays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { TFunction } from 'i18next';
import type { LinkOption } from '../settings/types';
import type { LinkOption } from './types';

import { createRococo } from './testingRelayRococo';
import { createWestend } from './testingRelayWestend';
Expand Down
17 changes: 17 additions & 0 deletions packages/apps-config/src/endpoints/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2017-2021 @polkadot/apps-config authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Option } from '../settings/types';

export interface EndpointOption {
allowTeleport?: boolean;
dnslink?: string;
genesisHash?: string;
isChild?: boolean;
Expand All @@ -13,3 +16,17 @@ export interface EndpointOption {
providers: Record<string, string>;
text: React.ReactNode;
}

export interface LinkOption extends Option {
allowTeleport?: boolean;
dnslink?: string;
genesisHash?: string;
genesisHashRelay?: string;
isChild?: boolean;
isDevelopment?: boolean;
isRelay?: boolean;
isSpaced?: boolean;
linked?: LinkOption[];
paraId?: number;
textBy: string;
}
16 changes: 12 additions & 4 deletions packages/apps-config/src/endpoints/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { TFunction } from 'i18next';
import type { LinkOption } from '../settings/types';
import type { EndpointOption } from './types';
import type { EndpointOption, LinkOption } from './types';

export function expandLinked (input: LinkOption[]): LinkOption[] {
return input.reduce((result: LinkOption[], entry): LinkOption[] => {
Expand All @@ -22,9 +21,9 @@ export function expandLinked (input: LinkOption[]): LinkOption[] {
}, []);
}

export function expandEndpoint (t: TFunction, input: EndpointOption): LinkOption[] {
const { dnslink, genesisHash, info, isChild, isDisabled, linked, paraId, providers, text } = input;
export function expandEndpoint (t: TFunction, { allowTeleport, dnslink, genesisHash, info, isChild, isDisabled, linked, paraId, providers, text }: EndpointOption): LinkOption[] {
const base = {
allowTeleport,
genesisHash,
info,
isChild,
Expand All @@ -36,6 +35,7 @@ export function expandEndpoint (t: TFunction, input: EndpointOption): LinkOption
const result = Object.entries(providers).map(([host, value], index): LinkOption => ({
...base,
dnslink: index === 0 ? dnslink : undefined,
isRelay: false,
textBy: t('rpc.hosted.by', 'hosted by {{host}}', { ns: 'apps-config', replace: { host } }),
value
}));
Expand All @@ -45,7 +45,15 @@ export function expandEndpoint (t: TFunction, input: EndpointOption): LinkOption
const options: LinkOption[] = [];

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);
Expand Down
12 changes: 0 additions & 12 deletions packages/apps-config/src/settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,3 @@ export interface Option {
text: React.ReactNode;
value: string | number;
}

export interface LinkOption extends Option {
dnslink?: string;
genesisHash?: string;
genesisHashRelay?: string;
isChild?: boolean;
isDevelopment?: boolean;
isSpaced?: boolean;
linked?: LinkOption[];
paraId?: number;
textBy: string;
}
2 changes: 2 additions & 0 deletions packages/apps-routing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import staking from './staking';
import storage from './storage';
import sudo from './sudo';
import techcomm from './techcomm';
import teleport from './teleport';
import transfer from './transfer';
import treasury from './treasury';

Expand All @@ -39,6 +40,7 @@ export default function create (t: TFunction): Routes {
claims(t),
poll(t),
transfer(t),
teleport(t),
staking(t),
democracy(t),
council(t),
Expand Down
30 changes: 30 additions & 0 deletions packages/apps-routing/src/teleport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2017-2021 @polkadot/apps-routing authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { TFunction } from 'i18next';
import type { Route } from './types';

import Modal from '@polkadot/app-parachains/Teleport';

export default function create (t: TFunction): Route {
return {
Component: Modal,
Modal,
display: {
isHidden: false,
needsAccounts: true,
needsApi: [
[
'tx.xcm.teleportAssets',
'tx.xcmPallet.teleportAssets',
'tx.polkadotXcm.teleportAssets'
]
],
needsTeleport: true
},
group: 'accounts',
icon: 'share-square',
name: 'teleport',
text: t('nav.teleport', 'Teleport', { ns: 'apps-routing' })
};
}
6 changes: 4 additions & 2 deletions packages/apps-routing/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ export interface RouteProps extends AppProps, BareProps {
}

export interface Route {
Component: React.ComponentType<RouteProps>;
Modal?: React.ComponentType<any>;
// FIXME This is weird, we really expect the memo to be there...
Component: React.ComponentType<RouteProps> | React.MemoExoticComponent<any>;
Modal?: React.ComponentType<any> | React.MemoExoticComponent<any>;
display: {
isHidden?: boolean;
isModal?: boolean;
needsAccounts?: boolean;
needsApi?: (string | string[])[];
needsSudo?: boolean;
needsTeleport?: boolean;
};
group: RouteGroup;
icon: IconName;
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/src/Endpoints/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/apps authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { LinkOption } from '@polkadot/apps-config/settings/types';
import type { LinkOption } from '@polkadot/apps-config/endpoints/types';
import type { Group } from './types';

// ok, this seems to be an eslint bug, this _is_ a package import
Expand Down
15 changes: 9 additions & 6 deletions packages/apps/src/Menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useLocation } from 'react-router-dom';
import styled from 'styled-components';

import createRoutes from '@polkadot/apps-routing';
import { useAccounts, useApi, useCall } from '@polkadot/react-hooks';
import { useAccounts, useApi, useCall, useTeleport } from '@polkadot/react-hooks';

import { findMissingApis } from '../endpoint';
import { useTranslation } from '../translate';
Expand Down Expand Up @@ -42,7 +42,7 @@ function createExternals (t: TFunction): ItemRoute[] {
];
}

function checkVisible ({ api, isApiConnected, isApiReady }: ApiProps, hasAccounts: boolean, hasSudo: boolean, { isHidden, needsAccounts, needsApi, needsSudo }: Route['display']): boolean {
function checkVisible ({ api, isApiConnected, isApiReady }: ApiProps, allowTeleport: boolean, hasAccounts: boolean, hasSudo: boolean, { isHidden, needsAccounts, needsApi, needsSudo, needsTeleport }: Route['display']): boolean {
if (isHidden) {
return false;
} else if (needsAccounts && !hasAccounts) {
Expand All @@ -53,12 +53,14 @@ function checkVisible ({ api, isApiConnected, isApiReady }: ApiProps, hasAccount
return false;
} else if (needsSudo && !hasSudo) {
return false;
} else if (needsTeleport && !allowTeleport) {
return false;
}

return findMissingApis(api, needsApi).length === 0;
}

function extractGroups (routing: Routes, groupNames: Record<string, string>, apiProps: ApiProps, hasAccounts: boolean, hasSudo: boolean): Group[] {
function extractGroups (routing: Routes, groupNames: Record<string, string>, apiProps: ApiProps, allowTeleport: boolean, hasAccounts: boolean, hasSudo: boolean): Group[] {
return Object
.values(
routing.reduce((all: Groups, route): Groups => {
Expand All @@ -77,7 +79,7 @@ function extractGroups (routing: Routes, groupNames: Record<string, string>, api
.map(({ name, routes }): Group => ({
name,
routes: routes.filter(({ display }) =>
checkVisible(apiProps, hasAccounts, hasSudo, display)
checkVisible(apiProps, allowTeleport, hasAccounts, hasSudo, display)
)
}))
.filter(({ routes }) => routes.length);
Expand All @@ -87,6 +89,7 @@ function Menu ({ className = '' }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { allAccounts, hasAccounts } = useAccounts();
const apiProps = useApi();
const { allowTeleport } = useTeleport();
const sudoKey = useCall<AccountId>(apiProps.isApiReady && apiProps.api.query.sudo?.key);
const location = useLocation();

Expand All @@ -107,8 +110,8 @@ function Menu ({ className = '' }: Props): React.ReactElement<Props> {
);

const visibleGroups = useMemo(
() => extractGroups(routeRef.current, groupRef.current, apiProps, hasAccounts, hasSudo),
[apiProps, hasAccounts, hasSudo]
() => extractGroups(routeRef.current, groupRef.current, apiProps, allowTeleport, hasAccounts, hasSudo),
[allowTeleport, apiProps, hasAccounts, hasSudo]
);

const activeRoute = useMemo(
Expand Down
Loading

0 comments on commit 7148e7e

Please sign in to comment.