` mockRpc$
8 |
9 | **● mockRpc$**: *`(Anonymous function)`* = createRpc({ frequency: [() => timer(0, 1000)] })()
10 |
11 | *Defined in [utils/testHelpers/mockRpc.ts:18](https://github.com/paritytech/js-libs/blob/66e2091/packages/light.js/src/utils/testHelpers/mockRpc.ts#L18)*
12 |
13 | ___
14 |
15 |
--------------------------------------------------------------------------------
/packages/light.js/docs/api/modules/_utils_testhelpers_sleep_.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/packages/light.js/docs/getting-started/does-it-work-with-a-full-node.md:
--------------------------------------------------------------------------------
1 | # Does this work with a full node?
2 |
3 | Yes. Despite what its name would suggest.
4 |
5 | ## Building a dapp with a remote full node as the backend (MetaMask, INFURA)
6 |
7 | For most of the JSONRPC calls, a dapp needs to make wait for a network call to fetch the result, which may cause some latency in the response:
8 |
9 | - in the case of a remote full node: the dapp makes a JSONRPC call to the remote full node.
10 | - in the case of a Light Client: the Light Client asks its peers for the result.
11 |
12 | As such, the best development patterns which apply to Light Clients also apply to remote full nodes. Read the chapter on [Light Client Development](/concepts/light-client-development.html) to learn more.
13 |
14 | `@parity/light.js` is merely a library which regroups all these best development patterns in a simple API.
15 |
--------------------------------------------------------------------------------
/packages/light.js/docs/guides/tutorial1-set-up-a-light-client.md:
--------------------------------------------------------------------------------
1 | # Tutorial Part 1: Set up a Light Client for Development
2 |
3 | At Parity Technologies, we believe that Light Clients will [play a big role](https://paritytech.io/what-is-a-light-client/) in the Ethereum network in the future. However, today, Light Clients are still mainly experimental.
4 |
5 | For development, there's no big risk in using a Light Client. If anything, it's a small step towards making the network more decentralized.
6 |
7 | ## Download the Light Client
8 |
9 | Please install Parity Ethereum first: https://parity.io. Then run Parity Ethereum with the following flags:
10 |
11 | ```bash
12 | /path/to/parity --chain kovan --light --ws-origins all
13 | ```
14 |
15 | We are using the Kovan testnet so that we are not testing our dapp with real Ether. To get some fake Kovan ETH to play with, head to the faucet: https://faucet.kovan.network/.
16 |
17 | The Light Client should take a couple of minutes to sync. Head to [our wiki](https://wiki.parity.io/Light-Client) to learn more about the Light Client.
18 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "example-react",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@parity/api": "../../api",
7 | "@parity/light.js": "../",
8 | "@parity/light.js-react": "../../light.js-react",
9 | "react": "^16.7.0",
10 | "react-dom": "^16.7.0",
11 | "react-router-dom": "^4.3.1",
12 | "react-scripts": "^3.3.0",
13 | "rxjs": "~6.2.2"
14 | },
15 | "scripts": {
16 | "start": "react-scripts start",
17 | "build": "react-scripts build",
18 | "test": "react-scripts test",
19 | "eject": "react-scripts eject"
20 | },
21 | "eslintConfig": {
22 | "extends": "react-app"
23 | },
24 | "browserslist": [
25 | ">0.2%",
26 | "not dead",
27 | "not ie <= 11",
28 | "not op_mini all"
29 | ]
30 | }
31 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/openethereum/js-libs/f985750827c3d795710b00708aa1badedd76c88d/packages/light.js/example-react/public/favicon.ico
--------------------------------------------------------------------------------
/packages/light.js/example-react/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | }
10 | ],
11 | "start_url": ".",
12 | "display": "standalone",
13 | "theme_color": "#000000",
14 | "background_color": "#ffffff"
15 | }
16 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/App.css:
--------------------------------------------------------------------------------
1 | /* Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | * This file is part of Parity.
3 | *
4 | * SPDX-License-Identifier: MIT */
5 |
6 | .App {
7 | text-align: center;
8 | }
9 |
10 | .App-logo {
11 | animation: App-logo-spin infinite 20s linear;
12 | height: 80px;
13 | }
14 |
15 | .App-header {
16 | background-color: #222;
17 | height: 150px;
18 | padding: 20px;
19 | color: white;
20 | text-align: center;
21 | }
22 |
23 | .App-link {
24 | color: #61dafb;
25 | }
26 |
27 | @keyframes App-logo-spin {
28 | from {
29 | transform: rotate(0deg);
30 | }
31 | to {
32 | transform: rotate(360deg);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/App.test.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import App from './App';
4 |
5 | it('renders without crashing', () => {
6 | const div = document.createElement('div');
7 | ReactDOM.render(, div);
8 | ReactDOM.unmountComponentAtNode(div);
9 | });
10 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/BalanceOf/BalanceOfAddress/BalanceOfAddress.js:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import React, { Component } from 'react';
7 | import { map } from 'rxjs/operators';
8 | import { balanceOf$ } from '@parity/light.js';
9 | import light from '@parity/light.js-react';
10 |
11 | // NOTE: with the right Babel configuration (or TypeScript),
12 | // you can use use `light` as a decorator:
13 | // @light({
14 | // balance: ownProps =>
15 | // balanceOf$(ownProps.address).pipe(
16 | // map(_ => +_)
17 | // )
18 | // })
19 | class BalanceOfAddress extends Component {
20 | render() {
21 | const { address, balance } = this.props;
22 | return (
23 |
24 | Balance of {address}
: {balance} wei.
25 |
26 | );
27 | }
28 | }
29 |
30 | BalanceOfAddress = light({
31 | balance: ownProps =>
32 | balanceOf$(ownProps.address).pipe(
33 | map(_ => +_)
34 | )
35 | })(BalanceOfAddress);
36 |
37 | export default BalanceOfAddress;
38 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/BalanceOf/BalanceOfAddress/index.js:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import BalanceOfAddress from './BalanceOfAddress';
7 |
8 | export default BalanceOfAddress;
9 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/BalanceOf/index.js:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import Balance from './Balance';
7 |
8 | export default Balance;
9 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/BlockNumber/BlockNumber.js:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import React, { Component } from 'react';
7 | import { blockNumber$ } from '@parity/light.js';
8 | import light from '@parity/light.js-react';
9 |
10 | // NOTE: with the right Babel configuration (or TypeScript),
11 | // you can use use `light` as a decorator:
12 | // @light({
13 | // blockNumber: blockNumber$
14 | // })
15 | class BlockNumber extends Component {
16 | render() {
17 | const { blockNumber } = this.props;
18 |
19 | return (
20 |
21 |
blockNumber$
22 |
23 | Current block number
24 | {+blockNumber}
25 |
26 | );
27 | }
28 | }
29 |
30 | BlockNumber = light({
31 | blockNumber: blockNumber$
32 | })(BlockNumber);
33 |
34 | export default BlockNumber;
35 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/BlockNumber/index.js:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import BlockNumber from './BlockNumber';
7 |
8 | export default BlockNumber;
9 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/PeerCount/PeerCount.js:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import React, { Component } from 'react';
7 | import { peerCount$ } from '@parity/light.js';
8 | import light from '@parity/light.js-react';
9 |
10 | // NOTE: with the right Babel configuration (or TypeScript),
11 | // you can use use `light` as a decorator:
12 | // @light({
13 | // peerCount: peerCount$
14 | // })
15 | class PeerCount extends Component {
16 | render() {
17 | const { peerCount } = this.props;
18 |
19 | return (
20 |
21 |
peerCount$
22 |
23 | Current peer count
24 | {+peerCount}
25 |
26 | );
27 | }
28 | }
29 |
30 | PeerCount = light({
31 | peerCount: peerCount$
32 | })(PeerCount);
33 |
34 | export default PeerCount;
35 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/PeerCount/index.js:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import PeerCount from './PeerCount';
7 |
8 | export default PeerCount;
9 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | padding: 0;
4 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
5 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
6 | sans-serif;
7 | -webkit-font-smoothing: antialiased;
8 | -moz-osx-font-smoothing: grayscale;
9 | }
10 |
11 | code {
12 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
13 | monospace;
14 | }
15 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/index.js:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import 'symbol-observable'; // TODO Remove this once https://github.com/acdlite/recompose/pull/660 is merged
7 |
8 | import React from 'react';
9 | import ReactDOM from 'react-dom';
10 | import light from '@parity/light.js';
11 |
12 | import './index.css';
13 | import App from './App';
14 | import provider from './provider';
15 |
16 | import * as serviceWorker from './serviceWorker';
17 |
18 | light.setProvider(provider);
19 |
20 |
21 | ReactDOM.render(, document.getElementById('root'));
22 |
23 | // If you want your app to work offline and load faster, you can change
24 | // unregister() to register() below. Note this comes with some pitfalls.
25 | // Learn more about service workers: http://bit.ly/CRA-PWA
26 | serviceWorker.unregister();
27 |
--------------------------------------------------------------------------------
/packages/light.js/example-react/src/provider.js:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import Api from '@parity/api';
7 |
8 | export const currentProvider = window.web3 && window.web3.currentProvider;
9 | export const localProvider = new Api.Provider.Ws('ws://127.0.0.1:8546');
10 | export const infuraProvider = new Api.Provider.Ws(
11 | 'wss://mainnet.infura.io/_ws'
12 | );
13 |
14 | const provider = currentProvider || localProvider;
15 |
16 | export default provider;
17 |
--------------------------------------------------------------------------------
/packages/light.js/jest.config.js:
--------------------------------------------------------------------------------
1 | const baseConfig = require('../../jest.config');
2 |
3 | module.exports = baseConfig;
4 |
--------------------------------------------------------------------------------
/packages/light.js/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "@parity/light.js",
3 | "description": "A high-level reactive JS library optimized for light clients",
4 | "version": "5.1.19",
5 | "author": "Parity Team ",
6 | "license": "MIT",
7 | "repository": "https://github.com/paritytech/js-libs/tree/master/packages/light.js",
8 | "main": "lib/index.js",
9 | "keywords": [
10 | "API",
11 | "Ethereum",
12 | "Light",
13 | "Light Client",
14 | "Observable",
15 | "Parity",
16 | "Reactive",
17 | "RxJS"
18 | ],
19 | "publishConfig": {
20 | "access": "public"
21 | },
22 | "scripts": {
23 | "docs": "typedoc",
24 | "prebuild": "rimraf lib",
25 | "typecheck": "tsc --noEmit",
26 | "build": "tsc",
27 | "test": "jest --runInBand"
28 | },
29 | "dependencies": {
30 | "@parity/abi": "^5.1.19",
31 | "@parity/api": "^5.1.19",
32 | "async-retry": "^1.2.3",
33 | "bignumber.js": "^8.0.1",
34 | "debug": "^4.1.0",
35 | "memoizee": "^0.4.12",
36 | "rxjs-etc": "^9.4.0"
37 | },
38 | "devDependencies": {
39 | "@types/debug": "^4.1.0",
40 | "@types/memoizee": "^0.4.2",
41 | "eventemitter3": "^3.1.0",
42 | "rxjs": "~6.2.2",
43 | "typescript": "^3.1.6"
44 | },
45 | "peerDependencies": {
46 | "rxjs": "~6.2.2"
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/packages/light.js/src/ambient.d.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | declare module '@parity/api';
7 | declare module '@parity/api/lib/util';
8 | declare module '@parity/api/lib/util/encode';
9 | declare module '@parity/api/lib/util/types';
10 |
--------------------------------------------------------------------------------
/packages/light.js/src/api.spec.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import * as Api from '@parity/api';
7 |
8 | import { getApi, setApi, setProvider } from './api';
9 | import { resolveApi } from './utils/testHelpers/mockApi';
10 |
11 | it('should return the Null provider', () => {
12 | expect(getApi).toThrow();
13 | });
14 |
15 | it('should correctly set a new api', () => {
16 | const api = resolveApi();
17 | setApi(api);
18 | expect(getApi()).toBe(api);
19 | });
20 |
21 | it('should correctly set a new provider', () => {
22 | const provider = new Api.Provider.Ws('ws://127.0.0.1:8546');
23 | setProvider(provider);
24 | expect(getApi().provider).toBe(provider);
25 | });
26 |
--------------------------------------------------------------------------------
/packages/light.js/src/api.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import * as Api from '@parity/api';
7 | import * as memoizee from 'memoizee';
8 |
9 | // This is our global api object, to be used if no provider is passed to RpcObservables.
10 | let api: any; // TODO @parity/api
11 |
12 | /**
13 | * Like `return new Api(provider);`, but memoized.
14 | *
15 | * @ignore
16 | */
17 | export const createApiFromProvider = memoizee(
18 | (provider?: any) => new Api(provider)
19 | );
20 |
21 | /**
22 | * Sets a new Api object.
23 | *
24 | * @param newApi - An Api object.
25 | */
26 | export const setApi = (newApi: any) => {
27 | api = newApi;
28 | };
29 |
30 | /**
31 | * Sets a new Ethereum provider object.
32 | *
33 | * @param provider - An Ethereum provider object.
34 | */
35 | export const setProvider = (provider?: any) => {
36 | setApi(createApiFromProvider(provider));
37 | };
38 |
39 | /**
40 | * We only ever use api() at call-time of functions; this allows the options
41 | * (particularly the transport option) to be changed dynamically and the
42 | * data structure to be reused.
43 | *
44 | * @return - The current Api object.
45 | */
46 | export const getApi = () => {
47 | if (!api) {
48 | throw new Error('Please define a provider before using any RpcObservable.');
49 | }
50 | return api;
51 | };
52 |
53 | export default getApi;
54 |
--------------------------------------------------------------------------------
/packages/light.js/src/frequency/accounts.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import { AccountsInfo, Address, FrequencyObservableOptions } from '../types';
7 | import createPubsubObservable from './utils/createPubsubObservable';
8 |
9 | /**
10 | * Observable that emits each time the default account changes
11 | *
12 | * @param options - Options to pass to {@link FrequencyObservable}.
13 | */
14 | export function onAccountsChanged$ (options?: FrequencyObservableOptions) {
15 | return createPubsubObservable(
16 | 'eth_accounts',
17 | 'eth_accounts',
18 | options
19 | );
20 | }
21 |
22 | /**
23 | * Observable that emits each time the default account changes
24 | *
25 | * @param options - Options to pass to {@link FrequencyObservable}.
26 | */
27 | export function onAccountsInfoChanged$ (options?: FrequencyObservableOptions) {
28 | return createPubsubObservable(
29 | 'parity_accountsInfo',
30 | 'parity_accountsInfo',
31 | options
32 | );
33 | }
34 |
--------------------------------------------------------------------------------
/packages/light.js/src/frequency/frequency.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import * as accounts from './accounts';
7 | import * as blocks from './blocks';
8 | import * as health from './health';
9 | import * as other from './other';
10 | import * as time from './time';
11 |
12 | const frequency = {
13 | ...accounts,
14 | ...blocks,
15 | ...health,
16 | ...other,
17 | ...time
18 | };
19 |
20 | export default frequency;
21 |
--------------------------------------------------------------------------------
/packages/light.js/src/frequency/health.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2015-2019 Parity Technologies (UK) Ltd.
2 | // This file is part of Parity.
3 | //
4 | // SPDX-License-Identifier: MIT
5 |
6 | import createPubsubObservable from './utils/createPubsubObservable';
7 | import { FrequencyObservableOptions } from '../types';
8 |
9 | /**
10 | * Observable that emits when syncing status changes.
11 | */
12 | // TODO Pubsub doesn't exist on `net_peerCount`
13 |
14 | /**
15 | * Observable that emits when syncing status changes.
16 | *
17 | * @param options - Options to pass to {@link FrequencyObservable}.
18 | */
19 | export function onSyncingChanged$ (options?: FrequencyObservableOptions) {
20 | return createPubsubObservable