Pull in observable API from UI (#175)
* Pull in observable api... * Progress changing stuff... * votingBalance * Update resul types * Expose toBn on Moment * Rework... WIP * ... More type cleanups * Linting passes * BN helpers for UInt * Update api-observable * Add api-observable package.json * Allow Struct/Tuple to create from hex string * Future-proof Length with bitLength (u64/u128 compant encoding PR out) * Make Compact number encoding shareable * Split Api into functional sections * Cleanup types * Additional per-use comments * Remove params/primitives from README * api-format -> api-codec * Whitespace.
This commit is contained in:
@@ -16,14 +16,12 @@ This library provides a clean wrapper around all the methods exposed by a Polkad
|
|||||||
The API is split up into a number of internal packages -
|
The API is split up into a number of internal packages -
|
||||||
|
|
||||||
- [@polkadot/api](packages/api/) The low-level base API library
|
- [@polkadot/api](packages/api/) The low-level base API library
|
||||||
|
- [@polkadot/api-codec](packages/api-codec/) Codecs for all Polkadot primitives
|
||||||
- [@polkadot/api-rx](packages/api-rx/) A RxJs Observable wrapper around the API
|
- [@polkadot/api-rx](packages/api-rx/) A RxJs Observable wrapper around the API
|
||||||
- [@polkadot/api-format](packages/api-format/) Input and output formatters
|
|
||||||
- [@polkadot/api-provider](packages/api-provider/) Providers for connecting
|
- [@polkadot/api-provider](packages/api-provider/) Providers for connecting
|
||||||
|
|
||||||
Type definitions for interfaces as exposed by Polkadot & Substrate clients -
|
Type definitions for interfaces as exposed by Polkadot & Substrate clients -
|
||||||
|
|
||||||
- [@polkadot/extrinsics](packages/type-extrinsics/) Base extrinsic definitions & codecs
|
- [@polkadot/extrinsics](packages/type-extrinsics/) Base extrinsic definitions & codecs
|
||||||
- [@polkadot/jsonrpc](packages/type-jsonrpc/) Definitions for JSONRPC endpoints
|
- [@polkadot/jsonrpc](packages/type-jsonrpc/) Definitions for JSONRPC endpoints
|
||||||
- [@polkadot/params](packages/type-params/) Input/output parameter formatting
|
|
||||||
- [@polkadot/primitives](packages/type-primitives/) Primitive type definitions, builder & codecs
|
|
||||||
- [@polkadot/storage](packages/type-storage/) Definitions for storage entries
|
- [@polkadot/storage](packages/type-storage/) Definitions for storage entries
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ const config = require('@polkadot/dev/config/jest');
|
|||||||
|
|
||||||
module.exports = Object.assign({}, config, {
|
module.exports = Object.assign({}, config, {
|
||||||
moduleNameMapper: {
|
moduleNameMapper: {
|
||||||
'@polkadot/api-(codec|jsonrpc|provider|rx)(.*)$': '<rootDir>/packages/api-$1/src/$2',
|
'@polkadot/api-(codec|jsonrpc|observable|provider|rx)(.*)$': '<rootDir>/packages/api-$1/src/$2',
|
||||||
'@polkadot/api(.*)$': '<rootDir>/packages/api/src/$1',
|
'@polkadot/api(.*)$': '<rootDir>/packages/api/src/$1',
|
||||||
'@polkadot/extrinsics(.*)$': '<rootDir>/packages/type-extrinsics/src/$1',
|
'@polkadot/extrinsics(.*)$': '<rootDir>/packages/type-extrinsics/src/$1',
|
||||||
'@polkadot/jsonrpc(.*)$': '<rootDir>/packages/type-jsonrpc/src/$1',
|
'@polkadot/jsonrpc(.*)$': '<rootDir>/packages/type-jsonrpc/src/$1',
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
import { AnyNumber } from './types';
|
import { AnyNumber } from './types';
|
||||||
|
|
||||||
|
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||||
|
|
||||||
import Vector from './codec/Vector';
|
import Vector from './codec/Vector';
|
||||||
import Base from './codec/Base';
|
import Base from './codec/Base';
|
||||||
import Enum from './codec/Enum';
|
import Enum from './codec/Enum';
|
||||||
@@ -285,9 +287,9 @@ export default class RuntimeMetadata extends Struct {
|
|||||||
|
|
||||||
// We receive this as an Array<number> in the JSON output from the Node. Convert
|
// We receive this as an Array<number> in the JSON output from the Node. Convert
|
||||||
// to u8a and use the fromU8a to do the actual parsing
|
// to u8a and use the fromU8a to do the actual parsing
|
||||||
fromJSON (input: Array<number>): RuntimeMetadata {
|
fromJSON (input: Uint8Array | string | Array<number>): RuntimeMetadata {
|
||||||
return this.fromU8a(
|
return this.fromU8a(
|
||||||
Uint8Array.from(input)
|
toU8a(input)
|
||||||
) as RuntimeMetadata;
|
) as RuntimeMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const BITLENGTH = 64;
|
|||||||
// per-second. For any encoding/decoding the 1000 multiplier would be applied to
|
// per-second. For any encoding/decoding the 1000 multiplier would be applied to
|
||||||
// get it in line with JavaScript formats
|
// get it in line with JavaScript formats
|
||||||
export default class Moment extends Base<Date> {
|
export default class Moment extends Base<Date> {
|
||||||
constructor (value: Uint8Array | Moment | Date | number = 0) {
|
constructor (value: Uint8Array | Moment | Date | BN | number = 0) {
|
||||||
super(
|
super(
|
||||||
value instanceof Date
|
value instanceof Date
|
||||||
? new Date(Math.ceil(value.getTime() / 1000) * 1000)
|
? new Date(Math.ceil(value.getTime() / 1000) * 1000)
|
||||||
@@ -55,6 +55,10 @@ export default class Moment extends Base<Date> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getTime (): number {
|
||||||
|
return this.raw.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
toJSON (): any {
|
toJSON (): any {
|
||||||
return this.toNumber();
|
return this.toNumber();
|
||||||
}
|
}
|
||||||
@@ -67,6 +71,10 @@ export default class Moment extends Base<Date> {
|
|||||||
return this.raw.toISOString();
|
return this.raw.toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toBn (): BN {
|
||||||
|
return new BN(this.toNumber());
|
||||||
|
}
|
||||||
|
|
||||||
toNumber (): number {
|
toNumber (): number {
|
||||||
return Math.ceil(this.raw.getTime() / 1000);
|
return Math.ceil(this.raw.getTime() / 1000);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// Copyright 2017-2018 @polkadot/api-codec authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import Extrinsic from './Extrinsic';
|
||||||
|
|
||||||
|
// The Nonce or number of transactiosn sent by a specific account. Generally used
|
||||||
|
// with extrinsics to determine the order of execution.
|
||||||
|
export default class Proposal extends Extrinsic {
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
// Copyright 2017-2018 @polkadot/api-codec authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import BN from 'bn.js';
|
||||||
|
import bnToU8a from '@polkadot/util/bn/toU8a';
|
||||||
|
import u8aConcat from '@polkadot/util/u8a/concat';
|
||||||
|
import u8aToBn from '@polkadot/util/u8a/toBn';
|
||||||
|
|
||||||
|
const MAX_U8 = new BN(2).pow(new BN(8 - 2)).subn(1);
|
||||||
|
const MAX_U16 = new BN(2).pow(new BN(16 - 2)).subn(1);
|
||||||
|
const MAX_U32 = new BN(2).pow(new BN(32 - 2)).subn(1);
|
||||||
|
|
||||||
|
type BitLength = 32;
|
||||||
|
|
||||||
|
// A new compact length-encoding algorithm. It performs the same function as Length, however
|
||||||
|
// differs in that it uses a variable number of bytes to do the actual encoding. From the Rust
|
||||||
|
// implementation for compact encoding
|
||||||
|
//
|
||||||
|
// 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
|
||||||
|
// (0 ... 2**6 - 1) (u8)
|
||||||
|
// xx xx xx 00
|
||||||
|
// (2**6 ... 2**14 - 1) (u8, u16) low LH high
|
||||||
|
// yL yL yL 01 / yH yH yH yL
|
||||||
|
// (2**14 ... 2**30 - 1) (u16, u32) low LMMH high
|
||||||
|
// zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM
|
||||||
|
// (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
|
||||||
|
// nn nn nn 11 [ / zz zz zz zz ]{4 + n}
|
||||||
|
//
|
||||||
|
// Note: we use *LOW BITS* of the LSB in LE encoding to encode the 2 bit key.
|
||||||
|
export default class Compact {
|
||||||
|
static decode (input: Uint8Array, bitLength: BitLength): BN {
|
||||||
|
const flag = input[0] & 0b11;
|
||||||
|
|
||||||
|
if (flag === 0b00) {
|
||||||
|
return new BN(input[0]).shrn(2);
|
||||||
|
} else if (flag === 0b01) {
|
||||||
|
return u8aToBn(input.slice(0, 2), true).shrn(2);
|
||||||
|
} else if (flag === 0b10) {
|
||||||
|
return u8aToBn(input.slice(0, 4), true).shrn(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
const byteLength = bitLength / 8;
|
||||||
|
|
||||||
|
return u8aToBn(input.subarray(1, 1 + byteLength), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
static encode (length: BN, bitLength: BitLength): Uint8Array {
|
||||||
|
if (length.lte(MAX_U8)) {
|
||||||
|
return new Uint8Array([length.toNumber() << 2]);
|
||||||
|
} else if (length.lte(MAX_U16)) {
|
||||||
|
return bnToU8a(length.shln(2).addn(0b01), 16, true);
|
||||||
|
} else if (length.lte(MAX_U32)) {
|
||||||
|
return bnToU8a(length.shln(2).addn(0b10), 32, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return u8aConcat(
|
||||||
|
new Uint8Array([
|
||||||
|
0b11
|
||||||
|
]),
|
||||||
|
bnToU8a(length, bitLength, true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,33 +6,16 @@ import { AnyNumber } from '../types';
|
|||||||
|
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
import bnToBn from '@polkadot/util/bn/toBn';
|
import bnToBn from '@polkadot/util/bn/toBn';
|
||||||
import bnToU8a from '@polkadot/util/bn/toU8a';
|
|
||||||
import u8aConcat from '@polkadot/util/u8a/concat';
|
|
||||||
import u8aToBn from '@polkadot/util/u8a/toBn';
|
|
||||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||||
|
|
||||||
import Base from './Base';
|
import Base from './Base';
|
||||||
|
import Compact from './Compact';
|
||||||
import UInt from './UInt';
|
import UInt from './UInt';
|
||||||
|
|
||||||
const MAX_U8 = new BN(2).pow(new BN(8 - 2)).subn(1);
|
type BitLength = 32;
|
||||||
const MAX_U16 = new BN(2).pow(new BN(16 - 2)).subn(1);
|
|
||||||
const MAX_U32 = new BN(2).pow(new BN(32 - 2)).subn(1);
|
|
||||||
|
|
||||||
// A new compact length-encoding algorithm. It performs the same function as Length, however
|
// A Length-prefix that can be added to any variable length stream of bytes, e.g. String,
|
||||||
// differs in that it uses a variable number of bytes to do the actual encoding. From the Rust
|
// Vectors and Bytes
|
||||||
// implementation for compact encoding
|
|
||||||
//
|
|
||||||
// 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
|
|
||||||
// (0 ... 2**6 - 1) (u8)
|
|
||||||
// xx xx xx 00
|
|
||||||
// (2**6 ... 2**14 - 1) (u8, u16) low LH high
|
|
||||||
// yL yL yL 01 / yH yH yH yL
|
|
||||||
// (2**14 ... 2**30 - 1) (u16, u32) low LMMH high
|
|
||||||
// zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM
|
|
||||||
// (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
|
|
||||||
// nn nn nn 11 [ / zz zz zz zz ]{4 + n}
|
|
||||||
//
|
|
||||||
// Note: we use *LOW BITS* of the LSB in LE encoding to encode the 2 bit key.
|
|
||||||
//
|
//
|
||||||
// FIXME Not crazy about the use of Length. If we look at Vector, Bytes & String,
|
// FIXME Not crazy about the use of Length. If we look at Vector, Bytes & String,
|
||||||
// the implementations are basically the same. Vector is slightly different since
|
// the implementations are basically the same. Vector is slightly different since
|
||||||
@@ -40,41 +23,14 @@ const MAX_U32 = new BN(2).pow(new BN(32 - 2)).subn(1);
|
|||||||
// code and a more generic implementation around the use of Length. Looking at
|
// code and a more generic implementation around the use of Length. Looking at
|
||||||
// Array or Struct, the same type of wrapper would be useful here.
|
// Array or Struct, the same type of wrapper would be useful here.
|
||||||
export default class Length extends Base<BN> {
|
export default class Length extends Base<BN> {
|
||||||
constructor (value: AnyNumber = new BN(0)) {
|
private _bitLength: BitLength;
|
||||||
|
|
||||||
|
constructor (value: AnyNumber = new BN(0), bitLength: BitLength = 32) {
|
||||||
super(
|
super(
|
||||||
UInt.decode(value)
|
UInt.decode(value)
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
static decode (input: Uint8Array): BN {
|
this._bitLength = bitLength;
|
||||||
const flag = input[0] & 0b11;
|
|
||||||
|
|
||||||
if (flag === 0b00) {
|
|
||||||
return new BN(input[0]).shrn(2);
|
|
||||||
} else if (flag === 0b01) {
|
|
||||||
return u8aToBn(input.slice(0, 2), true).shrn(2);
|
|
||||||
} else if (flag === 0b10) {
|
|
||||||
return u8aToBn(input.slice(0, 4), true).shrn(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return u8aToBn(input.subarray(1, 5), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
static encode (length: BN): Uint8Array {
|
|
||||||
if (length.lte(MAX_U8)) {
|
|
||||||
return new Uint8Array([length.toNumber() << 2]);
|
|
||||||
} else if (length.lte(MAX_U16)) {
|
|
||||||
return bnToU8a(length.shln(2).addn(0b01), 16, true);
|
|
||||||
} else if (length.lte(MAX_U32)) {
|
|
||||||
return bnToU8a(length.shln(2).addn(0b10), 32, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return u8aConcat(
|
|
||||||
new Uint8Array([
|
|
||||||
0b11
|
|
||||||
]),
|
|
||||||
bnToU8a(length, 32, true)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
byteLength (): number {
|
byteLength (): number {
|
||||||
@@ -82,7 +38,7 @@ export default class Length extends Base<BN> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fromU8a (input: Uint8Array): Length {
|
fromU8a (input: Uint8Array): Length {
|
||||||
this.raw = Length.decode(input);
|
this.raw = Compact.decode(input, this._bitLength);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -98,7 +54,7 @@ export default class Length extends Base<BN> {
|
|||||||
toU8a (isBare?: boolean): Uint8Array {
|
toU8a (isBare?: boolean): Uint8Array {
|
||||||
return isBare
|
return isBare
|
||||||
? new Uint8Array()
|
? new Uint8Array()
|
||||||
: Length.encode(this.raw);
|
: Compact.encode(this.raw, this._bitLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
setValue (value: BN | number): void {
|
setValue (value: BN | number): void {
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
// This software may be modified and distributed under the terms
|
// This software may be modified and distributed under the terms
|
||||||
// of the ISC license. See the LICENSE file for details.
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import hexToU8a from '@polkadot/util/hex/toU8a';
|
||||||
|
import isHex from '@polkadot/util/is/hex';
|
||||||
import u8aConcat from '@polkadot/util/u8a/concat';
|
import u8aConcat from '@polkadot/util/u8a/concat';
|
||||||
|
|
||||||
import Base, { l } from './Base';
|
import Base, { l } from './Base';
|
||||||
@@ -82,6 +84,10 @@ export default class Struct <
|
|||||||
}
|
}
|
||||||
|
|
||||||
fromJSON (input: any): Struct<S, T, V, E> {
|
fromJSON (input: any): Struct<S, T, V, E> {
|
||||||
|
if (isHex(input)) {
|
||||||
|
return this.fromU8a(hexToU8a(input));
|
||||||
|
}
|
||||||
|
|
||||||
Object.keys(this.raw).forEach((key) => {
|
Object.keys(this.raw).forEach((key) => {
|
||||||
const jsonKey = this._jsonMap.get(key as any) || key;
|
const jsonKey = this._jsonMap.get(key as any) || key;
|
||||||
|
|
||||||
@@ -102,6 +108,10 @@ export default class Struct <
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get (index: number): Base {
|
||||||
|
return this.values()[index];
|
||||||
|
}
|
||||||
|
|
||||||
toJSON (): any {
|
toJSON (): any {
|
||||||
return Object.keys(this.raw).reduce((json, key) => {
|
return Object.keys(this.raw).reduce((json, key) => {
|
||||||
const jsonKey = this._jsonMap.get(key as any) || key;
|
const jsonKey = this._jsonMap.get(key as any) || key;
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
// This software may be modified and distributed under the terms
|
// This software may be modified and distributed under the terms
|
||||||
// of the ISC license. See the LICENSE file for details.
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import hexToU8a from '@polkadot/util/hex/toU8a';
|
||||||
|
import isHex from '@polkadot/util/is/hex';
|
||||||
|
|
||||||
import Base from './Base';
|
import Base from './Base';
|
||||||
import Struct from './Struct';
|
import Struct from './Struct';
|
||||||
|
|
||||||
@@ -29,6 +32,10 @@ export default class Tuple <
|
|||||||
}
|
}
|
||||||
|
|
||||||
fromJSON (input: any): Tuple<S, T, V> {
|
fromJSON (input: any): Tuple<S, T, V> {
|
||||||
|
if (isHex(input)) {
|
||||||
|
return this.fromU8a(hexToU8a(input));
|
||||||
|
}
|
||||||
|
|
||||||
Object.values(this.raw).forEach((entry, index) => {
|
Object.values(this.raw).forEach((entry, index) => {
|
||||||
entry.fromJSON(input[index]);
|
entry.fromJSON(input[index]);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -89,4 +89,54 @@ export default class UInt extends Base<BN> {
|
|||||||
toNumber (): number {
|
toNumber (): number {
|
||||||
return this.raw.toNumber();
|
return this.raw.toNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helpers from BN, this would be great as a "don't do this", i.e. extending properly
|
||||||
|
// from BN. Underlying these will always return BN (unless it is compare checks)
|
||||||
|
add (other: UInt | BN | number): BN {
|
||||||
|
return other instanceof UInt
|
||||||
|
? this.raw.add(other.raw)
|
||||||
|
: this.raw.add(bnToBn(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
div (other: UInt | BN | number): BN {
|
||||||
|
return other instanceof UInt
|
||||||
|
? this.raw.div(other.raw)
|
||||||
|
: this.raw.div(bnToBn(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
lt (test: UInt | BN | number): boolean {
|
||||||
|
return test instanceof UInt
|
||||||
|
? this.raw.lt(test.raw)
|
||||||
|
: this.raw.lt(bnToBn(test));
|
||||||
|
}
|
||||||
|
|
||||||
|
lte (test: UInt | BN | number): boolean {
|
||||||
|
return test instanceof UInt
|
||||||
|
? this.raw.lte(test.raw)
|
||||||
|
: this.raw.lte(bnToBn(test));
|
||||||
|
}
|
||||||
|
|
||||||
|
gt (test: UInt | BN | number): boolean {
|
||||||
|
return test instanceof UInt
|
||||||
|
? this.raw.gt(test.raw)
|
||||||
|
: this.raw.gt(bnToBn(test));
|
||||||
|
}
|
||||||
|
|
||||||
|
gte (test: UInt | BN | number): boolean {
|
||||||
|
return test instanceof UInt
|
||||||
|
? this.raw.gte(test.raw)
|
||||||
|
: this.raw.gte(bnToBn(test));
|
||||||
|
}
|
||||||
|
|
||||||
|
mul (other: UInt | BN | number): BN {
|
||||||
|
return other instanceof UInt
|
||||||
|
? this.raw.mul(other.raw)
|
||||||
|
: this.raw.mul(bnToBn(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
sub (other: UInt | BN | number): BN {
|
||||||
|
return other instanceof UInt
|
||||||
|
? this.raw.sub(other.raw)
|
||||||
|
: this.raw.sub(bnToBn(other));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ export { default as PendingExtrinsics } from './PendingExtrinsics';
|
|||||||
export { default as Permill } from './Permill';
|
export { default as Permill } from './Permill';
|
||||||
export { default as Perbill } from './Perbill';
|
export { default as Perbill } from './Perbill';
|
||||||
export { default as PropIndex } from './PropIndex';
|
export { default as PropIndex } from './PropIndex';
|
||||||
|
export { default as Proposal } from './Proposal';
|
||||||
export { default as ProposalIndex } from './ProposalIndex';
|
export { default as ProposalIndex } from './ProposalIndex';
|
||||||
export { default as RawAddress } from './RawAddress';
|
export { default as RawAddress } from './RawAddress';
|
||||||
export { default as ReferendumIndex } from './ReferendumIndex';
|
export { default as ReferendumIndex } from './ReferendumIndex';
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
ISC License (ISC)
|
||||||
|
|
||||||
|
Copyright 2017-2018 @polkadot/api-observable authors & contributors
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||||
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||||
|
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||||
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||||
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||||
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||||
|
PERFORMANCE OF THIS SOFTWARE.
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
[](https://polkadot.js.org)
|
||||||
|

|
||||||
|
[](https://github.com/Flet/semistandard)
|
||||||
|
[](https://www.npmjs.com/package/@polkadot/api-observable)
|
||||||
|
[](https://travis-ci.org/polkadot-js/api)
|
||||||
|
[](https://codeclimate.com/github/polkadot-js/api/maintainability)
|
||||||
|
[](https://coveralls.io/github/polkadot-js/api?branch=master)
|
||||||
|
[](https://david-dm.org/polkadot-js/api?path=packages/api-observable)
|
||||||
|
[](https://david-dm.org/polkadot-js/api?path=packages/api-observable#info=devDependencies)
|
||||||
|
|
||||||
|
# @polkadot/api-observable
|
||||||
|
|
||||||
|
An RxJs wrapper around the [@polkadot/api](../api).
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
{
|
||||||
|
"name": "@polkadot/api-observable",
|
||||||
|
"version": "0.30.3",
|
||||||
|
"description": "An Observable wrapper around the Polkadot JS API",
|
||||||
|
"main": "index.js",
|
||||||
|
"keywords": [
|
||||||
|
"Polkadot",
|
||||||
|
"RxJs"
|
||||||
|
],
|
||||||
|
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||||
|
"maintainers": [
|
||||||
|
"Jaco Greeff <jacogr@gmail.com>"
|
||||||
|
],
|
||||||
|
"contributors": [],
|
||||||
|
"license": "ISC",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=8.0"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public",
|
||||||
|
"registry": "https://registry.npmjs.org"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/polkadot-js/api.git"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/polkadot-js/api/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-rx#readme",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/runtime": "^7.0.0",
|
||||||
|
"@polkadot/api": "^0.30.3",
|
||||||
|
"@polkadot/api-provider": "^0.30.3",
|
||||||
|
"@types/rx": "^4.1.1",
|
||||||
|
"rxjs": "^6.3.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import { RxApiInterface, RxApiInterface$Method, RxApiInterface$Section } from '@polkadot/api-rx/types';
|
||||||
|
import { Method } from '@polkadot/jsonrpc/types';
|
||||||
|
|
||||||
|
import { Observable, combineLatest } from 'rxjs';
|
||||||
|
import { defaultIfEmpty, map } from 'rxjs/operators';
|
||||||
|
import { StorageFunction } from '@polkadot/api-codec/StorageKey';
|
||||||
|
import assert from '@polkadot/util/assert';
|
||||||
|
import isUndefined from '@polkadot/util/is/undefined';
|
||||||
|
|
||||||
|
type MapFn<R, T> = (combined: R) => T;
|
||||||
|
|
||||||
|
const defaultMapFn = (result: any): any =>
|
||||||
|
result;
|
||||||
|
|
||||||
|
// Raw base implementation for the observable API. It simply provides access to raw calls, allowing
|
||||||
|
// decendants to make direct queries to either API methods or actual storage
|
||||||
|
export default class ApiBase {
|
||||||
|
protected api: RxApiInterface;
|
||||||
|
|
||||||
|
constructor (api: RxApiInterface) {
|
||||||
|
this.api = api;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected combine = <T, R> (observables: Array<Observable<any>>, mapfn: MapFn<R, T> = defaultMapFn): Observable<T> => {
|
||||||
|
return combineLatest(...observables).pipe(
|
||||||
|
// FIXME There are a couple of places now where this casting happens after rxjs 6.3.2
|
||||||
|
defaultIfEmpty([] as any),
|
||||||
|
map(mapfn)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
isConnected = (): Observable<boolean> => {
|
||||||
|
return this.api.isConnected();
|
||||||
|
}
|
||||||
|
|
||||||
|
rawCall = <T> ({ name, section }: Method, ...params: Array<any>): Observable<T> => {
|
||||||
|
const apiSection = this.api[section as keyof RxApiInterface] as RxApiInterface$Section;
|
||||||
|
|
||||||
|
assert(apiSection, `Unable to find 'api.${section}'`);
|
||||||
|
|
||||||
|
const fn: RxApiInterface$Method = apiSection[name];
|
||||||
|
|
||||||
|
assert(fn, `Unable to find 'api.${section}.${name}'`);
|
||||||
|
|
||||||
|
return fn.apply(null, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
rawStorage = <T> (key: StorageFunction, ...params: Array<any>): Observable<T> => {
|
||||||
|
return this
|
||||||
|
.rawStorageMulti([key, ...params] as [StorageFunction, any])
|
||||||
|
.pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
map(([result]: Array<T>): T =>
|
||||||
|
result
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
rawStorageMulti = <T> (...keys: Array<[StorageFunction] | [StorageFunction, any]>): Observable<T> => {
|
||||||
|
return this.api.state
|
||||||
|
.storage(keys)
|
||||||
|
.pipe(
|
||||||
|
map((result?: any) =>
|
||||||
|
isUndefined(result)
|
||||||
|
? []
|
||||||
|
: result
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
import { defaultIfEmpty, map } from 'rxjs/operators';
|
||||||
|
import { BlockNumber, Header, SignedBlock } from '@polkadot/api-codec/index';
|
||||||
|
|
||||||
|
import ApiQueries from './Queries';
|
||||||
|
|
||||||
|
// Implementation of calls to API endpoints.
|
||||||
|
export default class ApiCalls extends ApiQueries {
|
||||||
|
bestNumber = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this
|
||||||
|
.chainNewHead()
|
||||||
|
.pipe(
|
||||||
|
map((header?: Header): BlockNumber | undefined =>
|
||||||
|
header && header.blockNumber
|
||||||
|
? header.blockNumber
|
||||||
|
: undefined
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
chainGetBlock = (hash: Uint8Array): Observable<SignedBlock | undefined> => {
|
||||||
|
return this.api.chain.getBlock(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
chainNewHead = (): Observable<Header | undefined> => {
|
||||||
|
return this.api.chain.newHead().pipe(
|
||||||
|
defaultIfEmpty()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,329 @@
|
|||||||
|
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import { RxBalance, RxBalanceMap, RxReferendumVote } from './types';
|
||||||
|
|
||||||
|
import BN from 'bn.js';
|
||||||
|
import { EMPTY, Observable } from 'rxjs';
|
||||||
|
import { switchMap, defaultIfEmpty, map } from 'rxjs/operators';
|
||||||
|
import { AccountId, Balance, Bool, BlockNumber, Moment, ReferendumIndex } from '@polkadot/api-codec/index';
|
||||||
|
import isString from '@polkadot/util/is/string';
|
||||||
|
|
||||||
|
import ApiCalls from './Calls';
|
||||||
|
import { RxProposal, RxReferendum } from './classes';
|
||||||
|
|
||||||
|
// Combines API calls and queries into single results. This allos for the exposed API to have
|
||||||
|
// useful extensions, i.e. queries can be made that returns the results from multiple observables,
|
||||||
|
// make the noise for the API users significantly less
|
||||||
|
export default class ApiCombined extends ApiCalls {
|
||||||
|
democracyProposalCount = (): Observable<number> => {
|
||||||
|
return this
|
||||||
|
.democracyPublicProposals()
|
||||||
|
.pipe(
|
||||||
|
map((proposals: Array<RxProposal>) =>
|
||||||
|
proposals.length
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyReferendumInfos = (referendumIds: Array<ReferendumIndex | BN | number>): Observable<Array<RxReferendum>> => {
|
||||||
|
return this.combine(
|
||||||
|
referendumIds.map((referendumId) =>
|
||||||
|
this.democracyReferendumInfoOf(referendumId)
|
||||||
|
),
|
||||||
|
(referendums: Array<RxReferendum> = []): Array<RxReferendum> =>
|
||||||
|
referendums.filter((referendum) =>
|
||||||
|
referendum
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyReferendumVoters = (referendumId: ReferendumIndex | BN | number): Observable<Array<RxReferendumVote>> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.democacyVotersFor(referendumId),
|
||||||
|
this.democracyVotersBalancesOf(referendumId),
|
||||||
|
this.democracyVotersVotesOf(referendumId)
|
||||||
|
],
|
||||||
|
([voters, balances, votes]: [Array<AccountId>, Array<Balance>, Array<Bool>]): Array<RxReferendumVote> =>
|
||||||
|
voters.map((address, index): RxReferendumVote => ({
|
||||||
|
address,
|
||||||
|
balance: balances[index] || new Balance(0),
|
||||||
|
vote: votes[index] || new Bool(false)
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyReferendums = (): Observable<Array<RxReferendum>> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.democracyReferendumCount(),
|
||||||
|
this.democracyNextTally()
|
||||||
|
]
|
||||||
|
).pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
switchMap(([referendumCount, nextTally]: [ReferendumIndex | undefined, ReferendumIndex | undefined]): Observable<Array<RxReferendum>> =>
|
||||||
|
referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gt(0)
|
||||||
|
? this.democracyReferendumInfos(
|
||||||
|
[...Array(referendumCount.toBn().sub(nextTally.toBn()).toNumber())].map((_, i) =>
|
||||||
|
nextTally.add(i).toNumber()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
: EMPTY
|
||||||
|
),
|
||||||
|
defaultIfEmpty([])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyVotesOf = (index: ReferendumIndex | BN | number, addresses: Array<AccountId | string>): Observable<boolean> => {
|
||||||
|
return this.combine(
|
||||||
|
addresses.map((address) =>
|
||||||
|
this.democacyVoteOf(index, address)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyVotersBalancesOf = (referendumId: ReferendumIndex | BN | number): Observable<Array<Balance>> => {
|
||||||
|
return this
|
||||||
|
.democacyVotersFor(referendumId)
|
||||||
|
.pipe(
|
||||||
|
switchMap((voters: Array<AccountId> = []) =>
|
||||||
|
this.votingBalances(...voters)
|
||||||
|
),
|
||||||
|
defaultIfEmpty([] as any),
|
||||||
|
map((balances: Array<RxBalance>) =>
|
||||||
|
balances.map(({ votingBalance }) =>
|
||||||
|
votingBalance
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyVotersVotesOf = (referendumId: ReferendumIndex | BN | number): Observable<Array<Bool>> => {
|
||||||
|
return this
|
||||||
|
.democacyVotersFor(referendumId)
|
||||||
|
.pipe(
|
||||||
|
switchMap((voters: Array<AccountId> = []) =>
|
||||||
|
this.democracyVotesOf(referendumId, voters)
|
||||||
|
),
|
||||||
|
defaultIfEmpty([] as any)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
eraBlockLength = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.sessionLength(),
|
||||||
|
this.sessionsPerEra()
|
||||||
|
],
|
||||||
|
([sessionLength, sessionsPerEra]: [BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
|
||||||
|
sessionLength && sessionsPerEra
|
||||||
|
? new BlockNumber(
|
||||||
|
sessionLength.mul(sessionsPerEra)
|
||||||
|
)
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
eraBlockProgress = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.sessionBlockProgress(),
|
||||||
|
this.sessionLength(),
|
||||||
|
this.sessionCurrentIndex(),
|
||||||
|
this.sessionsPerEra(),
|
||||||
|
this.eraLastLengthChange()
|
||||||
|
],
|
||||||
|
([sessionBlockProgress, sessionLength, sessionCurrentIndex, sessionsPerEra, eraLastLengthChange = new BlockNumber(0)]: [BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
|
||||||
|
sessionsPerEra && sessionCurrentIndex && sessionLength && sessionBlockProgress && eraLastLengthChange
|
||||||
|
? new BlockNumber(
|
||||||
|
sessionCurrentIndex
|
||||||
|
.sub(eraLastLengthChange)
|
||||||
|
.mod(sessionsPerEra.toBn())
|
||||||
|
.mul(sessionLength.toBn())
|
||||||
|
.add(sessionBlockProgress.toBn())
|
||||||
|
)
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
eraBlockRemaining = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.eraBlockLength(),
|
||||||
|
this.eraBlockProgress()
|
||||||
|
],
|
||||||
|
([eraBlockLength, eraBlockProgress]: [BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
|
||||||
|
eraBlockLength && eraBlockProgress
|
||||||
|
? new BlockNumber(
|
||||||
|
eraBlockLength.sub(eraBlockProgress)
|
||||||
|
)
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionBlockProgress = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.bestNumber(),
|
||||||
|
this.sessionLength(),
|
||||||
|
this.sessionLastLengthChange()
|
||||||
|
],
|
||||||
|
([bestNumber, sessionLength, lastSessionLengthChange]: [BlockNumber | undefined, BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
|
||||||
|
bestNumber && sessionLength && lastSessionLengthChange
|
||||||
|
? new BlockNumber(
|
||||||
|
bestNumber
|
||||||
|
.sub(lastSessionLengthChange)
|
||||||
|
.add(sessionLength.toBn())
|
||||||
|
.mod(sessionLength.toBn())
|
||||||
|
)
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionBlockRemaining = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.sessionBlockProgress(),
|
||||||
|
this.sessionLength()
|
||||||
|
],
|
||||||
|
([sessionBlockProgress, sessionLength]: [BlockNumber | undefined, BlockNumber | undefined]): BlockNumber | undefined =>
|
||||||
|
sessionBlockProgress && sessionLength
|
||||||
|
? new BlockNumber(
|
||||||
|
sessionLength.sub(sessionBlockProgress)
|
||||||
|
)
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionBrokenValue = (): Observable<Moment | undefined> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.timestampNow(),
|
||||||
|
this.sessionTimeExpected(),
|
||||||
|
this.sessionTimeRemaining(),
|
||||||
|
this.sessionCurrentStart()
|
||||||
|
],
|
||||||
|
([now, sessionTimeExpected, sessionTimeRemaining, sessionCurrentStart]: [Moment | undefined, Moment | undefined, Moment | undefined, Moment | undefined]): Moment | undefined =>
|
||||||
|
sessionTimeExpected && sessionTimeRemaining && sessionCurrentStart && now
|
||||||
|
? new Moment(
|
||||||
|
Math.round(
|
||||||
|
100 * (now.getTime() + sessionTimeRemaining.toNumber() - sessionCurrentStart.getTime()) / sessionTimeExpected.toNumber() - 100
|
||||||
|
)
|
||||||
|
)
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionTimeExpected = (): Observable<Moment | undefined> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.sessionLength(),
|
||||||
|
this.timestampBlockPeriod()
|
||||||
|
],
|
||||||
|
([sessionLength, blockPeriod]: [BlockNumber | undefined, Moment | undefined]): Moment | undefined =>
|
||||||
|
sessionLength && blockPeriod
|
||||||
|
? new Moment(
|
||||||
|
sessionLength.mul(blockPeriod.toBn()).muln(1000)
|
||||||
|
)
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionTimeRemaining = (): Observable<Moment | undefined> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.sessionBlockRemaining(),
|
||||||
|
this.timestampBlockPeriod()
|
||||||
|
],
|
||||||
|
([sessionBlockRemaining, blockPeriod]: [BlockNumber | undefined, Moment | undefined]): Moment | undefined =>
|
||||||
|
blockPeriod && sessionBlockRemaining
|
||||||
|
? new Moment(
|
||||||
|
sessionBlockRemaining.mul(blockPeriod.toBn()).muln(1000)
|
||||||
|
)
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
validatingBalance = (address: AccountId | string): Observable<RxBalance> => {
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.votingBalance(address),
|
||||||
|
this.votingBalancesNominatorsFor(address)
|
||||||
|
],
|
||||||
|
([balance, nominators = []]: [RxBalance, Array<RxBalance>]): RxBalance => {
|
||||||
|
const nominatedBalance = nominators.reduce((total, nominator: RxBalance) => {
|
||||||
|
return total.add(nominator.votingBalance.toBn());
|
||||||
|
}, new BN(0));
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
...balance,
|
||||||
|
nominators,
|
||||||
|
nominatedBalance: new Balance(nominatedBalance),
|
||||||
|
stakingBalance: new Balance(
|
||||||
|
nominatedBalance.add(balance.votingBalance.toBn())
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
validatingBalances = (...addresses: Array<AccountId | string>): Observable<RxBalanceMap> => {
|
||||||
|
return this.combine(
|
||||||
|
addresses.map((address) =>
|
||||||
|
this.validatingBalance(address)
|
||||||
|
),
|
||||||
|
(result: Array<RxBalance>): RxBalanceMap =>
|
||||||
|
result.reduce((balances, balance) => {
|
||||||
|
balances[balance.address.toString()] = balance;
|
||||||
|
|
||||||
|
return balances;
|
||||||
|
}, {} as RxBalanceMap)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
votingBalance = (_address: AccountId | string): Observable<RxBalance> => {
|
||||||
|
const address = isString(_address)
|
||||||
|
? new AccountId(_address)
|
||||||
|
: _address;
|
||||||
|
|
||||||
|
return this.combine(
|
||||||
|
[
|
||||||
|
this.stakingFreeBalanceOf(address),
|
||||||
|
this.stakingReservedBalanceOf(address)
|
||||||
|
],
|
||||||
|
([freeBalance = new Balance(0), reservedBalance = new Balance(0)]: [Balance | undefined, Balance | undefined]): RxBalance => ({
|
||||||
|
address,
|
||||||
|
freeBalance,
|
||||||
|
nominatedBalance: new Balance(0),
|
||||||
|
reservedBalance,
|
||||||
|
stakingBalance: new Balance(0),
|
||||||
|
votingBalance: new Balance(
|
||||||
|
freeBalance.add(reservedBalance)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
votingBalancesNominatorsFor = (address: AccountId | string) => {
|
||||||
|
return this
|
||||||
|
.stakingNominatorsFor(address)
|
||||||
|
.pipe(
|
||||||
|
switchMap((nominators: Array<AccountId>) =>
|
||||||
|
this.votingBalances(...nominators)
|
||||||
|
),
|
||||||
|
defaultIfEmpty([] as any)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
votingBalances = (...addresses: Array<AccountId | string>): Observable<RxBalance[]> => {
|
||||||
|
return this.combine(
|
||||||
|
addresses.map((address) =>
|
||||||
|
this.votingBalance(address)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,210 @@
|
|||||||
|
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import { RxFees } from './types';
|
||||||
|
|
||||||
|
import BN from 'bn.js';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
import { map } from 'rxjs/operators';
|
||||||
|
import { AccountId, Balance, Bool, BlockNumber, Index, Moment, Perbill, PropIndex, ReferendumIndex, u32 } from '@polkadot/api-codec/index';
|
||||||
|
import { Tuple } from '@polkadot/api-codec/codec';
|
||||||
|
import storage from '@polkadot/storage/testing';
|
||||||
|
|
||||||
|
import ApiBase from './Base';
|
||||||
|
import { RxProposal, RxProposalDeposits, RxReferendum } from './classes';
|
||||||
|
|
||||||
|
// Perform storage queries to the API endpoints.
|
||||||
|
export default class ApiQueries extends ApiBase {
|
||||||
|
democracyLaunchPeriod = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.rawStorage(storage.democracy.launchPeriod);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyNextTally = (): Observable<ReferendumIndex | undefined> => {
|
||||||
|
return this.rawStorage(storage.democracy.nextTally);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyProposalDeposits = (proposalId: PropIndex | BN | number): Observable<RxProposalDeposits | undefined> => {
|
||||||
|
return this
|
||||||
|
.rawStorage(storage.democracy.depositOf, proposalId)
|
||||||
|
.pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
map((result: Tuple): RxProposalDeposits | undefined =>
|
||||||
|
result
|
||||||
|
? new RxProposalDeposits(result)
|
||||||
|
: undefined
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyPublicProposals = (): Observable<Array<RxProposal>> => {
|
||||||
|
return this
|
||||||
|
.rawStorage(storage.democracy.publicProps)
|
||||||
|
.pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
map((proposals: Array<Tuple> = []) =>
|
||||||
|
proposals
|
||||||
|
.map((result: Tuple): RxProposal | undefined =>
|
||||||
|
result
|
||||||
|
? new RxProposal(result)
|
||||||
|
: undefined
|
||||||
|
)
|
||||||
|
.filter((proposal) =>
|
||||||
|
proposal
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyReferendumCount = (): Observable<ReferendumIndex | undefined> => {
|
||||||
|
return this.rawStorage(storage.democracy.referendumCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyReferendumInfoOf = (referendumId: ReferendumIndex | BN | number): Observable<RxReferendum | undefined> => {
|
||||||
|
return this
|
||||||
|
.rawStorage(storage.democracy.referendumInfoOf, referendumId)
|
||||||
|
.pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
map((result: Tuple): RxReferendum | undefined =>
|
||||||
|
result
|
||||||
|
? new RxReferendum(result, referendumId)
|
||||||
|
: undefined
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democacyVoteOf = (index: ReferendumIndex | BN | number, address: AccountId | string): Observable<Bool> => {
|
||||||
|
return this.rawStorage(storage.democracy.voteOf, index, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
democacyVotersFor = (index: ReferendumIndex | BN | number): Observable<Array<AccountId>> => {
|
||||||
|
return this
|
||||||
|
.rawStorage(storage.democracy.votersFor, index)
|
||||||
|
.pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
map((voters: Array<AccountId> = []) =>
|
||||||
|
voters
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
democracyVotingPeriod = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.rawStorage(storage.democracy.votingPeriod);
|
||||||
|
}
|
||||||
|
|
||||||
|
fees = (): Observable<RxFees> => {
|
||||||
|
return this
|
||||||
|
.rawStorageMulti(
|
||||||
|
[storage.balances.transactionBaseFee],
|
||||||
|
[storage.balances.transactionByteFee],
|
||||||
|
[storage.balances.creationFee],
|
||||||
|
[storage.balances.existentialDeposit],
|
||||||
|
[storage.balances.transferFee]
|
||||||
|
)
|
||||||
|
.pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
map(([baseFee = new Balance(0), byteFee = new Balance(0), creationFee = new Balance(0), existentialDeposit = new Balance(0), transferFee = new Balance(0)]: [Balance | undefined, Balance | undefined, Balance | undefined, Balance | undefined, Balance | undefined]) => ({
|
||||||
|
baseFee,
|
||||||
|
byteFee,
|
||||||
|
creationFee,
|
||||||
|
existentialDeposit,
|
||||||
|
transferFee
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
eraLastLengthChange = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.rawStorage(storage.staking.lastEraLengthChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionReward = (): Observable<Perbill | undefined> => {
|
||||||
|
return this.rawStorage(storage.staking.sessionReward);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionRewardCurrent = (): Observable<Balance | undefined> => {
|
||||||
|
return this.rawStorage(storage.staking.currentSessionReward);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionCurrentIndex = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.rawStorage(storage.session.currentIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionCurrentStart = (): Observable<Moment | undefined> => {
|
||||||
|
return this.rawStorage(storage.session.currentStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionLastLengthChange = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.rawStorage(storage.session.lastLengthChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionLength = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.rawStorage(storage.session.sessionLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionsPerEra = (): Observable<BlockNumber | undefined> => {
|
||||||
|
return this.rawStorage(storage.staking.sessionsPerEra);
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionValidators = (): Observable<Array<AccountId>> => {
|
||||||
|
return this
|
||||||
|
.rawStorage(storage.session.validators)
|
||||||
|
.pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
map((validators: Array<AccountId> = []) =>
|
||||||
|
validators
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
stakingIntentions = (): Observable<Array<AccountId>> => {
|
||||||
|
return this
|
||||||
|
.rawStorage(storage.staking.intentions)
|
||||||
|
.pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
map((intentions: Array<AccountId> = []) =>
|
||||||
|
intentions
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME -> freeBalance
|
||||||
|
stakingFreeBalanceOf = (address: AccountId | string): Observable<Balance | undefined> => {
|
||||||
|
return this.rawStorage(storage.balances.freeBalance, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
stakingNominatorsFor = (address: AccountId | string): Observable<Array<AccountId>> => {
|
||||||
|
return this
|
||||||
|
.rawStorage(storage.staking.nominatorsFor, address)
|
||||||
|
.pipe(
|
||||||
|
// @ts-ignore After upgrade to 6.3.2
|
||||||
|
map((nominators: Array<AccountId> = []) =>
|
||||||
|
nominators
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
stakingNominating = (address: AccountId | string): Observable<AccountId | undefined> => {
|
||||||
|
return this.rawStorage(storage.staking.nominating, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME -> reservedBalance
|
||||||
|
stakingReservedBalanceOf = (address: AccountId | string): Observable<Balance | undefined> => {
|
||||||
|
return this.rawStorage(storage.balances.reservedBalance, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
timestampBlockPeriod = (): Observable<Moment | undefined> => {
|
||||||
|
return this.rawStorage(storage.timestamp.blockPeriod);
|
||||||
|
}
|
||||||
|
|
||||||
|
timestampNow = (): Observable<Moment | undefined> => {
|
||||||
|
return this.rawStorage(storage.timestamp.now);
|
||||||
|
}
|
||||||
|
|
||||||
|
systemAccountIndexOf = (address: AccountId | string): Observable<Index | undefined> => {
|
||||||
|
return this.rawStorage(storage.system.accountNonce, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
validatorCount = (): Observable<u32> => {
|
||||||
|
return this.rawStorage(storage.staking.validatorCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import BN from 'bn.js';
|
||||||
|
import { AccountId, Balance, BlockNumber, PropIndex, Proposal, ReferendumIndex, VoteThreshold } from '@polkadot/api-codec/index';
|
||||||
|
import { Struct, Tuple, Vector } from '@polkadot/api-codec/codec';
|
||||||
|
|
||||||
|
export class RxProposal extends Struct.with({ id: PropIndex, proposal: Proposal, address: AccountId }) {
|
||||||
|
constructor (value: Tuple) {
|
||||||
|
super({
|
||||||
|
id: value.get(0),
|
||||||
|
proposal: value.get(1),
|
||||||
|
address: value.get(2)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
get address (): AccountId {
|
||||||
|
return this.raw.address as AccountId;
|
||||||
|
}
|
||||||
|
|
||||||
|
get id (): PropIndex {
|
||||||
|
return this.raw.id as PropIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
get proposal (): Proposal {
|
||||||
|
return this.raw.proposal as Proposal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RxProposalDeposits extends Struct.with({ balance: Balance, addresses: Vector.with(AccountId) }) {
|
||||||
|
constructor (value: Tuple) {
|
||||||
|
super({
|
||||||
|
balance: value.get(0),
|
||||||
|
addresses: value.get(1)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
get addresses (): Vector<AccountId> {
|
||||||
|
return this.raw.addresses as Vector<AccountId>;
|
||||||
|
}
|
||||||
|
|
||||||
|
get balance (): Balance {
|
||||||
|
return this.raw.balance as Balance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RxReferendum extends Struct.with({ blockNumber: BlockNumber, proposal: Proposal, voteThreshold: VoteThreshold, id: ReferendumIndex }) {
|
||||||
|
constructor (value: Tuple, id: ReferendumIndex | BN | number) {
|
||||||
|
super({
|
||||||
|
blockNumber: value.get(0),
|
||||||
|
proposal: value.get(1),
|
||||||
|
voteThreshold: value.get(2),
|
||||||
|
id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
get blockNumber (): BlockNumber {
|
||||||
|
return this.raw.blockNumber as BlockNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
get id (): ReferendumIndex {
|
||||||
|
return this.raw.id as ReferendumIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
get proposal (): Proposal {
|
||||||
|
return this.raw.proposal as Proposal;
|
||||||
|
}
|
||||||
|
|
||||||
|
get voteThreshold (): VoteThreshold {
|
||||||
|
return this.raw.voteThreshold as VoteThreshold;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import ApiCombined from './Combined';
|
||||||
|
|
||||||
|
export default class Api extends ApiCombined {
|
||||||
|
}
|
||||||
+42
@@ -0,0 +1,42 @@
|
|||||||
|
// Copyright 2017-2018 @polkadot/ui-observable authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the ISC license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
import { AccountId, Balance, BlockNumber, Bool, Header, PropIndex, Proposal, ReferendumIndex, u32, VoteThreshold } from '@polkadot/api-codec/index';
|
||||||
|
import { Tuple } from '@polkadot/api-codec/codec';
|
||||||
|
|
||||||
|
import ApiBase from './Base';
|
||||||
|
import ApiCalls from './Calls';
|
||||||
|
import ApiCombined from './Combined';
|
||||||
|
import ApiQueries from './Queries';
|
||||||
|
|
||||||
|
export type ApiFunctions = keyof ApiBase | keyof ApiCalls | keyof ApiCombined | keyof ApiQueries;
|
||||||
|
|
||||||
|
export type RxBalance = {
|
||||||
|
address: AccountId,
|
||||||
|
freeBalance: Balance,
|
||||||
|
nominatedBalance: Balance,
|
||||||
|
reservedBalance: Balance,
|
||||||
|
votingBalance: Balance,
|
||||||
|
stakingBalance: Balance,
|
||||||
|
nominators?: Array<RxBalance>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RxFees = {
|
||||||
|
baseFee: Balance,
|
||||||
|
byteFee: Balance,
|
||||||
|
creationFee: Balance,
|
||||||
|
existentialDeposit: Balance,
|
||||||
|
transferFee: Balance,
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RxReferendumVote = {
|
||||||
|
address: AccountId,
|
||||||
|
balance: Balance,
|
||||||
|
vote: Bool
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RxBalanceMap = {
|
||||||
|
[index: string]: RxBalance
|
||||||
|
}
|
||||||
Vendored
+5
-4
@@ -12,10 +12,11 @@ export type RxApiInterface$Section = {
|
|||||||
[index: string]: RxApiInterface$Method
|
[index: string]: RxApiInterface$Method
|
||||||
};
|
};
|
||||||
|
|
||||||
type RxApiInterface$Keys = {
|
export type RxApiInterface = {
|
||||||
readonly [key in keyof typeof interfaces]: RxApiInterface$Section;
|
readonly author: RxApiInterface$Section;
|
||||||
};
|
readonly chain: RxApiInterface$Section;
|
||||||
|
readonly state: RxApiInterface$Section;
|
||||||
|
readonly system: RxApiInterface$Section;
|
||||||
|
|
||||||
export type RxApiInterface = RxApiInterface$Keys & {
|
|
||||||
isConnected: () => BehaviorSubject<boolean>
|
isConnected: () => BehaviorSubject<boolean>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
"paths":
|
"paths":
|
||||||
{
|
{
|
||||||
"@polkadot/api-codec/*": ["packages/api-codec/src/*"],
|
"@polkadot/api-codec/*": ["packages/api-codec/src/*"],
|
||||||
|
"@polkadot/api-observable/*": ["packages/api-observable/src/*"],
|
||||||
"@polkadot/api-provider/*": ["packages/api-provider/src/*"],
|
"@polkadot/api-provider/*": ["packages/api-provider/src/*"],
|
||||||
"@polkadot/api-rx/*": ["packages/api-rx/src/*"],
|
"@polkadot/api-rx/*": ["packages/api-rx/src/*"],
|
||||||
"@polkadot/api/*": ["packages/api/src/*"],
|
"@polkadot/api/*": ["packages/api/src/*"],
|
||||||
|
|||||||
Reference in New Issue
Block a user