Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

feat(light-apps): Add in-page WasmProvider in provider list #896

Merged
merged 10 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
build/
dist/
lib/
packages/electron-app/static/substrate
coverage/
master-build.zip
packages/electron-app/static/substrate
packages/light-apps/public/*.wasm

# node_modules
node_modules/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scripts": {
"build:ui": "yarn clean && lerna exec yarn build --stream --scope=@substrate/ui-components --scope=@substrate/light --scope=@substrate/light-apps",
"build:electron": "yarn build:ui && lerna run build --scope=@substrate/electron-app",
"build:extension": "INLINE_RUNTIME_CHUNK=false yarn build:ui && lerna run build --scope=@substrate/extension-app",
"build:extension": "CI=false INLINE_RUNTIME_CHUNK=false yarn build:ui && lerna run build --scope=@substrate/extension-app",
"clean": "rimraf packages/**/build packages/**/lib packages/**/dist",
"lint": "tsc --noEmit && eslint --ext js,ts,tsx .",
"package:electron": "yarn build:electron && lerna run package --scope=@substrate/electron-app",
Expand Down
4 changes: 2 additions & 2 deletions packages/electron-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"private": true,
"repository": "https://github.com/paritytech/substrate-light-ui.git",
"scripts": {
"prebuild": "copyfiles -u 2 \"../light-apps/build/**/*\" static/",
"build": "electron-webpack",
"copy:static": "copyfiles -u 2 \"../light-apps/build/**/*\" static/",
"build": "yarn copy:static && electron-webpack",
"electron": "electron dist/main/main.js",
"package": "electron-builder",
"start": "electron-webpack dev",
Expand Down
9 changes: 6 additions & 3 deletions packages/extension-app/src/background/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

import { ProviderMeta } from '@polkadot/extension-inject/types';
import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { kusama_cc3, LightClient, westend } from '@substrate/light';

import { WasmProvider } from './WasmProvider';
import {
kusama_cc3,
LightClient,
WasmProvider,
westend,
} from '@substrate/light';

// We bundled the .wasm file inside the extension, at this relative url. See
// the structure of the output `build/` folder.
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-app/src/background/handlers/State.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @paritytech/substrate-light-ui authors & contributors
// Copyright 2018-2020 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

Expand Down
2 changes: 1 addition & 1 deletion packages/extension-app/src/background/handlers/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @paritytech/substrate-light-ui authors & contributors
// Copyright 2018-2020 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

Expand Down
2 changes: 1 addition & 1 deletion packages/extension-app/src/background/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @paritytech/substrate-light-ui authors & contributors
// Copyright 2018-2020 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

Expand Down
2 changes: 1 addition & 1 deletion packages/extension-app/src/extensionizer.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @paritytech/substrate-light-ui authors & contributors
// Copyright 2018-2020 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

Expand Down
1 change: 1 addition & 0 deletions packages/extension-app/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function createWebpack({ alias = {}, context }) {
},
{
test: /\.js$/,
// https://github.com/webpack/webpack/issues/6719#issuecomment-546840116
loader: require.resolve('@open-wc/webpack-import-meta-loader'),
},
{
Expand Down
24 changes: 24 additions & 0 deletions packages/light-apps/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018-2020 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

module.exports = {
babel: {
plugins: ['@babel/plugin-syntax-import-meta'],
},
webpack: {
alias: {},
plugins: [],
configure: {
module: {
rules: [
{
test: /\.js$/,
// https://github.com/webpack/webpack/issues/6719#issuecomment-546840116
loader: require.resolve('@open-wc/webpack-import-meta-loader'),
},
],
},
},
},
};
10 changes: 7 additions & 3 deletions packages/light-apps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
"not op_mini all"
],
"scripts": {
"start": "BROWSER=none SKIP_PREFLIGHT_CHECK=true react-scripts start",
"build": "SKIP_PREFLIGHT_CHECK=true react-scripts build",
"build": "yarn copy:wasm && SKIP_PREFLIGHT_CHECK=true craco build",
"copy:wasm": "copyfiles -Eu5 \"../light/src/generated/kusama_cc3/kusama_cc3.wasm\" public",
"start": "yarn copy:wasm && BROWSER=none SKIP_PREFLIGHT_CHECK=true craco start",
"test": "jest --coverage"
},
"dependencies": {
"@polkadot/extension-dapp": "^0.24.0-beta.17",
"@polkadot/ui-assets": "^0.51.1",
"@rehooks/local-storage": "^2.3.0",
"@substrate/context": "^0.3.24",
"@substrate/light": "^0.3.32",
"@substrate/ui-components": "^0.3.32",
"@types/chrome": "^0.0.104",
"@types/react-dom": "^16.9.6",
Expand All @@ -33,8 +35,10 @@
"react-router-dom": "^5.0.0",
"react-scripts": "^3.4.1",
"semantic-ui-css": "^2.4.1",
"store": "^2.0.12",
"tachyons": "^4.11.1",
"web-ext-types": "^3.2.1"
},
"devDependencies": {
"@craco/craco": "^5.6.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 @paritytech/substrate-light-ui authors & contributors
// Copyright 2018-2020 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

/* eslint-disable @typescript-eslint/camelcase */

import { WsProvider } from '@polkadot/api';
import { ProviderMeta } from '@polkadot/extension-inject/types';
import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { kusama_cc3, WasmProvider, westend } from '@substrate/light';

import { detectEnvironment } from '../../../util/env';
import { Injected } from '../../context';

/**
Expand All @@ -18,19 +22,43 @@ export interface LazyProvider extends ProviderMeta {
}

/**
* These fallback providers connect to a centralized remote RPC node.
* The URL to the WASM blob of the light client. In this project, we put it in
* the `public` folder so that it's statically available.
*/
export const FALLBACK_PROVIDERS: Record<string, LazyProvider> = {
'westend-WsProvider': {
description: 'Remote node hosted by W3F',
id: 'westend-WsProvider',
const WASM_BLOB_URL = '/kusama_cc3.wasm';

/**
* These are the WASM providers that the user can run directly inside the
* browser tab. They are only available in `TAB_ENV`. Note that we made sure to
* copy the WASM blob inside
*/
export const TAB_WASM_PROVIDERS: Record<string, LazyProvider> = {
'kusama-WasmProvider': {
description: 'In-tab WASM light client',
id: 'kusama-WasmProvider',
network: 'kusama',
node: 'light',
source: 'tab',
start: (): Promise<ProviderInterface> =>
Promise.resolve(new WasmProvider(kusama_cc3.fromUrl(WASM_BLOB_URL))),
transport: 'WasmProvider',
},
'westend-WasmProvider': {
description: 'In-tab WASM light client',
id: 'westend-WasmProvider',
network: 'westend',
node: 'light',
source: 'fallback',
source: 'tab',
start: (): Promise<ProviderInterface> =>
Promise.resolve(new WsProvider('wss://westend-rpc.polkadot.io')),
transport: 'WsProvider',
Promise.resolve(new WasmProvider(westend.fromUrl(WASM_BLOB_URL))),
transport: 'WasmProvider',
},
};

/**
* These fallback providers connect to a centralized remote RPC node.
*/
export const FALLBACK_PROVIDERS: Record<string, LazyProvider> = {
'kusama-WsProvider': {
description: 'Remote node hosted by W3F',
id: 'kusama-WsProvider',
Expand All @@ -41,6 +69,16 @@ export const FALLBACK_PROVIDERS: Record<string, LazyProvider> = {
Promise.resolve(new WsProvider('wss://kusama-rpc.polkadot.io')),
transport: 'WsProvider',
},
'westend-WsProvider': {
description: 'Remote node hosted by W3F',
id: 'westend-WsProvider',
network: 'westend',
node: 'light',
source: 'fallback',
start: (): Promise<ProviderInterface> =>
Promise.resolve(new WsProvider('wss://westend-rpc.polkadot.io')),
transport: 'WsProvider',
},
};

interface ProviderSources {
Expand Down Expand Up @@ -101,13 +139,16 @@ async function getProvidersFromInjected(
export async function getAllProviders(
additionalSources: ProviderSources = {}
): Promise<Record<string, LazyProvider>> {
const [extensionProviders, fallbackProviders] = await Promise.all([
getProvidersFromInjected(additionalSources.injected),
Promise.resolve(FALLBACK_PROVIDERS),
]);
const extensionProviders = await getProvidersFromInjected(
additionalSources.injected
);

const isTabEnv = detectEnvironment() === 'TAB_ENV';

return {
...fallbackProviders,
// Only add tab WASM providers if we're in a browser tab
...(isTabEnv ? TAB_WASM_PROVIDERS : {}),
...FALLBACK_PROVIDERS,
...extensionProviders,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ export function ProviderContextProvider(
const [provider, setProvider] = useState<ProviderInterface>();

useEffect(() => {
// Stop the currently running provider
if (provider) {
provider.disconnect();
}

if (lazy) {
lazy.start().then(setProvider).catch(l.error);
}
// We don't want `provider` in the deps
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [lazy]);

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/light/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
},
"repository": "https://github.com/paritytech/substrate-light-ui.git",
"scripts": {
"build": "rimraf lib && mkdir lib && cp -r src/generated lib/generated && tsc"
"build": "rimraf lib && copyfiles -Eu2 \"src/generated/**/*\" lib/generated && tsc"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright 2017-2019 @paritytech/substrate-light-ui authors & contributors
// Copyright 2018-2020 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

/* eslint-disable @typescript-eslint/camelcase */

import Coder from '@polkadot/rpc-provider/coder';
import {
ProviderInterface,
Expand All @@ -12,9 +10,10 @@ import {
ProviderInterfaceEmitted,
} from '@polkadot/rpc-provider/types';
import { assert, logger } from '@polkadot/util';
import { LightClient, WasmRpcClient } from '@substrate/light';
import EventEmitter from 'eventemitter3';

import { LightClient, WasmRpcClient } from './types';

// Same as https://github.com/polkadot-js/api/blob/57ca9a9c3204339e1e1f693fcacc33039868dc27/packages/rpc-provider/src/ws/Provider.ts#L17
interface SubscriptionHandler {
callback: ProviderInterfaceCallback;
Expand All @@ -36,14 +35,7 @@ export class WasmProvider implements ProviderInterface {
this.#coder = new Coder();
this.light = light;

light
.startClient()
.then((rpcClient) => {
this.#rpcClient = rpcClient;
this.#isConnected = true;
this.emit('connected');
})
.catch((error) => l.error(error));
this.connect();
}

/**
Expand All @@ -61,14 +53,24 @@ export class WasmProvider implements ProviderInterface {
}

public connect(): void {
console.error('connect is noop');
this.light
.startClient()
.then((rpcClient) => {
this.#rpcClient = rpcClient;
this.#isConnected = true;
this.emit('connected');
})
.catch(l.error);
}

/**
* @description Manually disconnect from the connection, clearing autoconnect logic
* @description Manually disconnect from the connection.
*/
public disconnect(): void {
console.error('disconnect is noop');
if (this.#rpcClient) {
l.log('Destroying WASM light client');
this.#rpcClient.free();
}
}

/**
Expand Down Expand Up @@ -168,9 +170,9 @@ export class WasmProvider implements ProviderInterface {
* const rpc = new Rpc(provider);
*
* rpc.state.subscribeStorage([[storage.balances.freeBalance, <Address>]], (_, values) => {
* console.log(values)
* l.log(values)
* }).then((subscriptionId) => {
* console.log('balance changes subscription id: ', subscriptionId)
* l.log('balance changes subscription id: ', subscriptionId)
* })
* ```
*/
Expand Down
Loading