@parity/light.js
A high-level reactive JS library optimized for light clients.
Getting Started
yarn add @parity/light.js rxjs
Usage
Reactively observe JSONRPC methods:
import light, { defaultAccount$ } from '@parity/light.js';
light.setProvider();
defaultAccount$().subscribe(publicAddress => console.log(publicAddress));
All RxJS tools are available for manipulating Observables:
import { balanceOf$, blockNumber$, defaultAccount$ } from '@parity/light.js';
import { filter, map, switchMap } from 'rxjs/operators';
blockNumber$()
.pipe(filter(n => n % 2 === 0))
.subscribe(console.log);
defaultAccount$()
.pipe(
switchMap(balanceOf$),
map(value => +value)
)
.subscribe(console.log);
import { myBalance$ } from '@parity/light.js';
myBalance$().subscribe(console.log);
Contract support:
import { defaultAccount$, makeContract } from '@parity/light.js';
import { map, switchMap } from 'rxjs/operators';
defaultAccount$()
.pipe(
switchMap(defaultAccount =>
makeContract(, )
.myMethod$(defaultAccount)
) )
.subscribe(console.log);
All available methods are documented in the docs.
Usage with React
We provide another library, @parity/light.js-react
, with a higher-order component to use these Observables easily with React apps.
import light from 'parity/ligth.js-react';
import { syncStatus$ } from '@parity/light.js';
@light({
mySyncVariable: syncStatus$
})
class MyClass extends React.Component {
render() {
return <div>{JSON.stringify(this.props.mySyncVariable)}</div>;
}
}
The UI will automatically update when the syncing state changes.