Handle storage with optional modifiers (#717)
* Handle storage with optional modifiers * Storage docs indicate Option * Cleanup Option logic * Convert Optional derive to use Option * Bump version, update changelog * Update CHANGELOG description * Enum.spec.js -> Enum.spec.ts * Add isEmpty to primitive types
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/api-derive",
|
||||
"version": "0.44.1",
|
||||
"version": "0.45.0",
|
||||
"description": "Common functions used across Polkadot, derived from RPC calls and storage queries.",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -31,8 +31,8 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-derive#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.3.1",
|
||||
"@polkadot/api": "^0.44.1",
|
||||
"@polkadot/types": "^0.44.1",
|
||||
"@polkadot/api": "^0.45.0",
|
||||
"@polkadot/types": "^0.45.0",
|
||||
"@types/memoizee": "^0.4.2",
|
||||
"memoizee": "^0.4.14"
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { ReferendumInfo, ReferendumIndex } from '@polkadot/types/index';
|
||||
import { Option, ReferendumInfo, ReferendumIndex } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
@@ -16,27 +16,32 @@ import { drr } from '../util/drr';
|
||||
* A [[ReferendumInfo]] with an additional `index` field
|
||||
*/
|
||||
export class ReferendumInfoExtended extends ReferendumInfo {
|
||||
private _index: ReferendumIndex;
|
||||
|
||||
constructor (info: ReferendumInfo, index: BN | number) {
|
||||
constructor (info: any) {
|
||||
super(info);
|
||||
|
||||
this._index = new ReferendumIndex(index);
|
||||
this.set('index', new ReferendumIndex(info.index));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Convenience getter, returns the referendumIndex
|
||||
*/
|
||||
get index (): ReferendumIndex {
|
||||
return this._index;
|
||||
return this.get('index') as ReferendumIndex;
|
||||
}
|
||||
}
|
||||
|
||||
export function referendumInfo (api: ApiInterface$Rx) {
|
||||
return (id: BN | number): Observable<ReferendumInfoExtended> => {
|
||||
return (api.query.democracy.referendumInfoOf(id) as Observable<ReferendumInfo>)
|
||||
return (index: BN | number): Observable<Option<ReferendumInfoExtended>> => {
|
||||
return (api.query.democracy.referendumInfoOf(index) as Observable<Option<ReferendumInfo>>)
|
||||
.pipe(
|
||||
map((info) => new ReferendumInfoExtended(info, id)),
|
||||
map((info) =>
|
||||
new Option<ReferendumInfoExtended>(
|
||||
ReferendumInfoExtended,
|
||||
info.isSome
|
||||
? { ...info.toJSON(), index }
|
||||
: null
|
||||
)
|
||||
),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import BN from 'bn.js';
|
||||
import { combineLatest, Observable, of } from 'rxjs';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { Option } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
import { referendumInfo, ReferendumInfoExtended } from './referendumInfo';
|
||||
@@ -12,7 +13,7 @@ import { referendumInfo, ReferendumInfoExtended } from './referendumInfo';
|
||||
export function referendumInfos (api: ApiInterface$Rx) {
|
||||
const referendumInfoOf = referendumInfo(api);
|
||||
|
||||
return (ids: Array<BN | number> = []): Observable<Array<ReferendumInfoExtended>> => {
|
||||
return (ids: Array<BN | number> = []): Observable<Array<Option<ReferendumInfoExtended>>> => {
|
||||
return !ids || !ids.length
|
||||
? of([]).pipe(drr())
|
||||
: combineLatest(
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
import { combineLatest, Observable, of } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { ReferendumIndex } from '@polkadot/types/index';
|
||||
import { Option, ReferendumIndex } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
import { ReferendumInfoExtended } from './referendumInfo';
|
||||
import { referendumInfos } from './referendumInfos';
|
||||
|
||||
export function referendums (api: ApiInterface$Rx) {
|
||||
return (): Observable<Array<ReferendumInfoExtended>> =>
|
||||
return (): Observable<Array<Option<ReferendumInfoExtended>>> =>
|
||||
(combineLatest(
|
||||
api.query.democracy.nextTally(),
|
||||
api.query.democracy.referendumCount()
|
||||
|
||||
@@ -6,7 +6,7 @@ import BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { BlockNumber } from '@polkadot/types/index';
|
||||
import { BlockNumber, Option } from '@polkadot/types/index';
|
||||
|
||||
import { bestNumber } from '../chain';
|
||||
import { drr } from '../util/drr';
|
||||
@@ -21,7 +21,7 @@ export function sessionProgress (api: ApiInterface$Rx) {
|
||||
map(
|
||||
([bestNumber, sessionLength, lastLengthChange]) =>
|
||||
(bestNumber || new BN(0))
|
||||
.sub(lastLengthChange as BlockNumber || new BN(0))
|
||||
.sub((lastLengthChange as Option<BlockNumber>).unwrapOr(new BN(0)))
|
||||
.add(sessionLength as BlockNumber || new BN(1))
|
||||
.mod(sessionLength as BlockNumber || new BN(1))
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user