Refactor api-promise/rx (#566)

* Use a generic abstract OnCall

* Add OnCall for Promise

* First run at fixing extrinsics

* Fix types

* Make submittable extrinsic work

* Move file

* Make derive work

* Fix types

* Only call metadata once

* Make tests pass

* Put event emitter in apirx

* Fix small bugs

* Fix tests

* Fix bug decorateStorage

* Make tests pass for tx

* Move rpc base to apirx

* Fix lint

* Fix small bugs

* Fix bug with isSubscription

* Fix codeclimate

* Revert

* Add tslint disable line

* Fix lint

* Skip e2e

* Typo

* Lint

* Re-add combinator back

* Add ._rx in Base

* Make extrinsics work

* Update derivex

* Fix bug lint

* Fix circular dep

* Fix bug e2e

* Remove useless check

* Add description comment

* Clean stuff

* Move assert to Base

* Bump version, update CHANGELOG

* Make _rx private

* Private emit
This commit is contained in:
Amaury Martiny
2019-01-16 15:42:59 +01:00
committed by Jaco Greeff
parent 38d2d06772
commit d07ed8ac38
49 changed files with 523 additions and 617 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-derive",
"version": "0.39.12",
"version": "0.40.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.2.0",
"@polkadot/api": "^0.39.12",
"@polkadot/types": "^0.39.12",
"@polkadot/api": "^0.40.0",
"@polkadot/types": "^0.40.0",
"@types/memoizee": "^0.4.2",
"@types/rx": "^4.1.1",
"memoizee": "^0.4.14",
@@ -4,7 +4,7 @@
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { decodeAddress } from '@polkadot/keyring';
import { AccountId, AccountIndex } from '@polkadot/types/index';
@@ -14,7 +14,7 @@ import { drr } from '../util/drr';
export type IdAndIndex = [AccountId | undefined, AccountIndex | undefined];
export function accountIdAndIndex (api: ApiRx) {
export function accountIdAndIndex (api: ApiInterface$Rx) {
return (address: AccountId | AccountIndex | string | null | undefined)
: Observable<(AccountIndex | AccountId | undefined)[]> => {
try {
@@ -4,13 +4,13 @@
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, AccountIndex } from '@polkadot/types/index';
import { accountIndexes, AccountIndexes } from './accountIndexes';
import { drr } from '../util/drr';
export function accountIdToIndex (api: ApiRx) {
export function accountIdToIndex (api: ApiInterface$Rx) {
return (accountId: AccountId | string): Observable<AccountIndex> =>
accountIndexes(api)()
.pipe(
@@ -4,14 +4,14 @@
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { ENUMSET_SIZE } from '@polkadot/types/AccountIndex';
import { Vector } from '@polkadot/types/codec';
import { AccountId, AccountIndex } from '@polkadot/types/index';
import { drr } from '../util/drr';
export function accountIndexToId (api: ApiRx) {
export function accountIndexToId (api: ApiInterface$Rx) {
return (accountIndex: AccountIndex | string): Observable<AccountId> => {
const _accountIndex = accountIndex instanceof AccountIndex
? accountIndex
@@ -5,7 +5,7 @@
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { ENUMSET_SIZE } from '@polkadot/types/AccountIndex';
import { AccountId, AccountIndex } from '@polkadot/types/index';
@@ -13,7 +13,7 @@ import { drr } from '../util/drr';
export type AccountIndexes = { [index: string]: AccountIndex };
export function accountIndexes (api: ApiRx) {
export function accountIndexes (api: ApiInterface$Rx) {
return (): Observable<AccountIndexes> =>
(api.query.balances.nextEnumSet() as Observable<AccountIndex>)
.pipe(
+2 -2
View File
@@ -5,11 +5,11 @@
import BN from 'bn.js';
import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { drr } from '../util/drr';
export function fees (api: ApiRx) {
export function fees (api: ApiInterface$Rx) {
return () =>
combineLatest(
api.query.balances.creationFee(),
@@ -5,7 +5,7 @@
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, Balance } from '@polkadot/types/index';
import { DerivedBalances } from '../types';
@@ -13,7 +13,7 @@ import { drr } from '../util/drr';
import { votingBalance } from './votingBalance';
import { votingBalancesNominatorsFor } from './votingBalancesNominatorsFor';
export function validatingBalance (api: ApiRx) {
export function validatingBalance (api: ApiInterface$Rx) {
return (address: AccountId | string): Observable<DerivedBalances> =>
combineLatest(
votingBalance(api)(address),
@@ -4,14 +4,14 @@
import { combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId } from '@polkadot/types/index';
import { DerivedBalancesMap } from '../types';
import { drr } from '../util/drr';
import { validatingBalance } from './validatingBalance';
export function validatingBalances (api: ApiRx) {
export function validatingBalances (api: ApiInterface$Rx) {
return (...params: Array<any>): Observable<DerivedBalancesMap> => {
const accountIds: Array<AccountId | string> = params.slice(0, params.length - 1);
@@ -4,7 +4,7 @@
import { combineLatest, of, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, AccountIndex, Balance } from '@polkadot/types/index';
import { accountIdAndIndex } from './accountIdAndIndex';
@@ -13,7 +13,7 @@ import { drr } from '../util/drr';
const EMPTY_ACCOUNT = new AccountId(new Uint8Array(32));
export function votingBalance (api: ApiRx) {
export function votingBalance (api: ApiInterface$Rx) {
return (address: AccountIndex | AccountId | string): Observable<DerivedBalances> =>
accountIdAndIndex(api)(address).pipe(
switchMap(([accountId]) =>
@@ -3,14 +3,14 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { combineLatest, Observable } from 'rxjs';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, AccountIndex } from '@polkadot/types/index';
import { DerivedBalances } from '../types';
import { drr } from '../util/drr';
import { votingBalance } from './votingBalance';
export function votingBalances (api: ApiRx) {
export function votingBalances (api: ApiInterface$Rx) {
return (...params: Array<any>): Observable<Array<DerivedBalances>> => {
const addresses: Array<AccountIndex | AccountId | string> = params.slice(0, params.length - 1);
@@ -4,14 +4,14 @@
import { Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId } from '@polkadot/types/index';
import { DerivedBalances } from '../types';
import { drr } from '../util/drr';
import { votingBalances } from './votingBalances';
export function votingBalancesNominatorsFor (api: ApiRx) {
export function votingBalancesNominatorsFor (api: ApiInterface$Rx) {
return (accountId: AccountId | string): Observable<Array<DerivedBalances>> =>
(api.query.staking.nominatorsFor(accountId) as Observable<Array<AccountId | undefined | null> | undefined>).pipe(
map((nominators) => nominators || []),
+3 -3
View File
@@ -4,7 +4,7 @@
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { BlockNumber, Header } from '@polkadot/types/index';
import { drr } from '../util/drr';
@@ -12,9 +12,9 @@ import { drr } from '../util/drr';
/**
* Get the latest block number.
*/
export function bestNumber (api: ApiRx) {
export function bestNumber (api: ApiInterface$Rx) {
return (): Observable<BlockNumber> =>
api.rpc.chain.subscribeNewHead()
(api.rpc.chain.subscribeNewHead() as Observable<Header>)
.pipe(
filter((header: Header) => header && !!header.blockNumber),
map(({ blockNumber }: Header) => blockNumber),
@@ -4,7 +4,7 @@
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { BlockNumber, Header } from '@polkadot/types/index';
import { drr } from '../util/drr';
@@ -12,9 +12,9 @@ import { drr } from '../util/drr';
/**
* Get the latest finalised block number.
*/
export function bestNumberFinalised (api: ApiRx) {
export function bestNumberFinalised (api: ApiInterface$Rx) {
return (): Observable<BlockNumber> =>
api.rpc.chain.subscribeFinalisedHeads()
(api.rpc.chain.subscribeFinalisedHeads() as Observable<Header>)
.pipe(
filter((header: Header) => header && !!header.blockNumber),
map(({ blockNumber }: Header) => blockNumber),
@@ -5,12 +5,12 @@
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { ReferendumInfo } from '@polkadot/types/index';
import { drr } from '../util/drr';
export function referendumInfos (api: ApiRx) {
export function referendumInfos (api: ApiInterface$Rx) {
return (...params: Array<any>): Observable<Array<ReferendumInfo>> => {
const ids: Array<BN | number> = params.slice(0, params.length - 1);
@@ -5,7 +5,7 @@
import BN from 'bn.js';
import { combineLatest, Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { Vector } from '@polkadot/types/codec';
import { AccountId, Balance, Vote } from '@polkadot/types/index';
@@ -14,7 +14,7 @@ import { drr } from '../util/drr';
import { votes } from './votes';
import { votingBalances } from '../balances/votingBalances';
export function referendumVotesFor (api: ApiRx) {
export function referendumVotesFor (api: ApiInterface$Rx) {
return (referendumId: BN | number): Observable<Array<DerivedReferendumVote>> =>
(api.query.democracy.votersFor(referendumId) as Observable<Vector<AccountId>>).pipe(
switchMap((votersFor) =>
@@ -4,13 +4,13 @@
import { combineLatest, Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { ReferendumIndex, ReferendumInfo } from '@polkadot/types/index';
import { referendumInfos } from './referendumInfos';
import { drr } from '../util/drr';
export function referendums (api: ApiRx) {
export function referendums (api: ApiInterface$Rx) {
return (): Observable<Array<ReferendumInfo>> =>
(combineLatest(
api.query.democracy.nextTally(),
+2 -2
View File
@@ -4,12 +4,12 @@
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, Vote } from '@polkadot/types/index';
import { drr } from '../util/drr';
export function votes (api: ApiRx) {
export function votes (api: ApiInterface$Rx) {
return (referendumId: BN, ...accountIds: Array<AccountId>): Observable<Array<Vote>> =>
combineLatest(
accountIds.map(
+2 -1
View File
@@ -4,11 +4,12 @@
import { Observable } from 'rxjs';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import MockProvider from '@polkadot/rpc-provider/mock';
import { Derive } from './index';
const testFunction = (api: ApiRx) => {
const testFunction = (api: ApiInterface$Rx) => {
return <
Section extends keyof Derive,
Method extends keyof (typeof api.derive[Section])
+2 -2
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import * as balances from './balances';
import * as chain from './chain';
@@ -28,7 +28,7 @@ export interface Derive {
session: ReturnTypes<typeof session>;
}
export default function decorateDerive (api: ApiRx): Derive {
export default function decorateDerive (api: ApiInterface$Rx): Derive {
const derive = {} as Derive;
Object.keys(functions).forEach((sectionName: string) => {
+2 -2
View File
@@ -5,12 +5,12 @@
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { BlockNumber } from '@polkadot/types/index';
import { drr } from '../util/drr';
export function eraLength (api: ApiRx) {
export function eraLength (api: ApiInterface$Rx) {
return (): Observable<BN> =>
(combineLatest(
api.query.session.sessionLength(),
@@ -5,13 +5,13 @@
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { BlockNumber } from '@polkadot/types/index';
import { sessionProgress } from './sessionProgress';
import { drr } from '../util/drr';
export function eraProgress (api: ApiRx) {
export function eraProgress (api: ApiInterface$Rx) {
return (): Observable<BN> =>
(combineLatest(
sessionProgress(api)(),
@@ -5,13 +5,13 @@
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { BlockNumber } from '@polkadot/types/index';
import { bestNumber } from '../chain';
import { drr } from '../util/drr';
export function sessionProgress (api: ApiRx) {
export function sessionProgress (api: ApiInterface$Rx) {
return (): Observable<BN> =>
combineLatest(
bestNumber(api)(),
+4 -4
View File
@@ -2,13 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { cache } from './cache';
describe('cache', () => {
const f = cache((api: ApiRx) => (a: number, b: number) => [api, a, b]);
const api = {} as ApiRx;
const f = cache((api: ApiInterface$Rx) => (a: number, b: number) => [api, a, b]);
const api = {} as ApiInterface$Rx;
it('should hit cache when all arguments are the same', () => {
const first = f(api)(2, 3);
@@ -21,7 +21,7 @@ describe('cache', () => {
const first = f(api)(2, 3);
const second1 = f(api)(5, 3);
const second2 = f(api)(2, 5);
const second3 = f({} as ApiRx)(2, 3);
const second3 = f({} as ApiInterface$Rx)(2, 3);
expect(first).not.toBe(second1);
expect(first).not.toBe(second2);
+3 -3
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import memoizee from 'memoizee';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
/**
* Create memoization for a 2-currified function.
@@ -12,7 +12,7 @@ import ApiRx from '@polkadot/api/rx';
* @param map - The Map used to cache function returns.
* @example
* ```javascript
* const f = (api: ApiRx) => (b: number) => a * b;
* const f = (api: ApiInterface$Rx) => (b: number) => a * b;
* const cached = cache(f);
* f(api)(1, 2);
* f(api)(1); // Cache not hit
@@ -20,7 +20,7 @@ import ApiRx from '@polkadot/api/rx';
* f(api)(1, 2); // Cache hit
* ```
*/
export const cache = (fn: Function, map = new WeakMap()) => (api: ApiRx) => {
export const cache = (fn: Function, map = new WeakMap()) => (api: ApiInterface$Rx) => {
if (!map.has(api)) {
const innerFn = fn(api);
map.set(api, memoizee(innerFn));
+7 -5
View File
@@ -4,29 +4,31 @@
import BN from 'bn.js';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { BlockNumber } from '@polkadot/types/index';
describe.skip('derive e2e', () => {
let api: ApiRx;
let api: ApiInterface$Rx;
beforeAll(() => {
jest.setTimeout(30000);
});
beforeEach(async () => {
beforeEach(async (done) => {
api = await ApiRx.create().toPromise();
done();
});
it('derive.chain.bestNumber', async (done) => {
api.derive.chain.bestNumber().subscribe((blockNumber: BlockNumber) => {
api.derive.chain.bestNumber().subscribe((blockNumber) => {
expect(blockNumber instanceof BlockNumber).toBe(true);
expect(blockNumber.gten(0)).toBe(true);
expect((blockNumber as BlockNumber).gten(0)).toBe(true);
done();
});
});
it('derive.session.sessionProgress', async (done) => {
api.derive.session.sessionProgress().subscribe((progress: BN) => {
api.derive.session.sessionProgress().subscribe((progress) => {
expect(progress instanceof BN).toBe(true);
done();
});