Make api.rpc.*.* typed (#1237)

* Generate correctly

* Fix rpc-core

* Flexible types

* Make types work!

* Lint

* Fix grumble

* Re generate

* Fix lint

* Fix small bugs

* Maybe really fix lint?

* Fix lint again

* Fix lint

* Small ordering & formatting  changes while playing...
This commit is contained in:
Amaury Martiny
2019-08-01 10:46:28 +02:00
committed by GitHub
parent 3290f14a75
commit 92044d471c
18 changed files with 330 additions and 157 deletions
+11 -10
View File
@@ -17,8 +17,8 @@ describe('Cached Observables', (): void => {
});
it('creates a single observable for subscriptions (multiple calls)', (): void => {
const observable1 = rpc.chain.subscribeNewHead(123);
const observable2 = rpc.chain.subscribeNewHead(123);
const observable1 = rpc.state.subscribeStorage([123]);
const observable2 = rpc.state.subscribeStorage([123]);
expect(observable2).toBe(observable1);
});
@@ -31,15 +31,15 @@ describe('Cached Observables', (): void => {
});
it('creates a single observable (multiple calls, different arguments that should be cached together)', (): void => {
const observable1 = rpc.chain.subscribeNewHead(keyring.alice.address);
const observable2 = rpc.chain.subscribeNewHead(createType('AccountId', keyring.alice.address));
const observable1 = rpc.state.subscribeStorage([keyring.alice.address]);
const observable2 = rpc.state.subscribeStorage([createType('AccountId', keyring.alice.address)]);
expect(observable2).toBe(observable1);
});
it('creates multiple observables for different values', (): void => {
const observable1 = rpc.chain.subscribeNewHead(123);
const observable2 = rpc.chain.subscribeNewHead(456);
const observable1 = rpc.chain.getBlockHash(123);
const observable2 = rpc.chain.getBlockHash(456);
expect(observable2).not.toBe(observable1);
});
@@ -63,15 +63,16 @@ describe('Cached Observables', (): void => {
});
it('creates different observables for different methods but same arguments', (): void => {
const observable1 = rpc.chain.subscribeNewHead(123);
const observable2 = rpc.state.subscribeStorage(123);
// @ts-ignore
const observable1 = rpc.chain.subscribeNewHead([123]);
const observable2 = rpc.state.subscribeStorage([123]);
expect(observable2).not.toBe(observable1);
});
it('creates multiple observables for one-shots', (): void => {
const observable1 = rpc.system.chain(123);
const observable2 = rpc.system.chain(123);
const observable1 = rpc.chain.getBlockHash(123);
const observable2 = rpc.chain.getBlockHash(123);
expect(observable2).not.toBe(observable1);
});
+4 -4
View File
@@ -79,7 +79,7 @@ function formattingTests (version: string, storage: Storage, encodedValues: [str
it('encodes key (with params), decoding response', (done): void => {
api.state
.getStorage([storage.balances.freeBalance, ADDR_ONE])
.getStorage<Balance>([storage.balances.freeBalance, ADDR_ONE])
.subscribe((value): void => {
expect(
provider.send
@@ -102,12 +102,12 @@ function formattingTests (version: string, storage: Storage, encodedValues: [str
});
it('encodes multiple keys, decoding multiple results', (done): void => {
api.state.subscribeStorage(
api.state.subscribeStorage<[Balance, Balance]>(
[
[storage.balances.freeBalance, ADDR_ONE],
[storage.balances.freeBalance, ADDR_TWO]
]
).subscribe((value: Balance[]): void => {
).subscribe((value): void => {
// console.error(value);
expect(
@@ -173,7 +173,7 @@ function formattingTests (version: string, storage: Storage, encodedValues: [str
api.state
.subscribeStorage([[storage.staking.validators, '0x00']])
.subscribe((value: Codec): void => {
.subscribe(([value]: Codec[]): void => {
expect(value).toBeDefined();
// console.error('linked falklback', value);
+23 -14
View File
@@ -3,10 +3,11 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { RpcSection, RpcMethod } from '@polkadot/jsonrpc/types';
import { RpcMethod } from '@polkadot/jsonrpc/types';
import { StorageChangeSet } from '@polkadot/types/interfaces';
import { AnyJson, Codec } from '@polkadot/types/types';
import { RpcInterface, RpcInterfaceMethod, RpcInterfaceSection } from './types';
import { RpcInterface } from './jsonrpc.types';
import { RpcInterfaceMethod } from './types';
import memoizee from 'memoizee';
import { combineLatest, from, Observable, Observer, of, throwError } from 'rxjs';
@@ -55,13 +56,13 @@ export default class Rpc implements RpcInterface {
public readonly provider: ProviderInterface;
public readonly author: RpcInterfaceSection;
public readonly author: RpcInterface['author'];
public readonly chain: RpcInterfaceSection;
public readonly chain: RpcInterface['chain'];
public readonly state: RpcInterfaceSection;
public readonly state: RpcInterface['state'];
public readonly system: RpcInterfaceSection;
public readonly system: RpcInterface['system'];
/**
* @constructor
@@ -73,10 +74,10 @@ export default class Rpc implements RpcInterface {
this.provider = provider;
this.author = this.createInterface(interfaces.author);
this.chain = this.createInterface(interfaces.chain);
this.state = this.createInterface(interfaces.state);
this.system = this.createInterface(interfaces.system);
this.author = this.createInterface('author');
this.chain = this.createInterface('chain');
this.state = this.createInterface('state');
this.system = this.createInterface('system');
}
/**
@@ -113,18 +114,26 @@ export default class Rpc implements RpcInterface {
return `${Rpc.signature(method)}:: ${error.message}`;
}
private createInterface ({ methods }: RpcSection): RpcInterfaceSection {
private createInterface<Section extends keyof RpcInterface> (section: Section): RpcInterface[Section] {
const { methods } = interfaces[section];
return Object
.keys(methods)
.reduce((exposed, methodName): RpcInterfaceSection => {
.reduce((exposed, methodName): RpcInterface[Section] => {
const def = methods[methodName];
exposed[methodName] = def.isSubscription
// FIXME Remove any here
// To do so, remove `RpcInterfaceMethod` from './types.ts', and refactor
// every method inside this class to take:
// `<S extends keyof RpcInterface, M extends keyof RpcInterface[S]>`
// Not doing so, because it makes this class a little bit less readable,
// and leaving it as-is doesn't harm much
(exposed as any)[methodName] = def.isSubscription
? this.createMethodSubscribe(def)
: this.createMethodSend(def);
return exposed;
}, {} as unknown as RpcInterfaceSection);
}, {} as unknown as RpcInterface[Section]);
}
private createMethodSend (method: RpcMethod): RpcInterfaceMethod {
+51
View File
@@ -0,0 +1,51 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Observable } from 'rxjs';
import { Vec } from '@polkadot/types/codec';
import { Bytes, Metadata, StorageData, StorageKey, Text, u64 } from '@polkadot/types';
import { BlockNumber, Extrinsic, Hash, Header, SignedBlock } from '@polkadot/types/interfaces/runtime';
import { ChainProperties, ExtrinsicOrHash, ExtrinsicStatus, Health, NetworkState, PeerInfo, RuntimeVersion, StorageChangeSet } from '@polkadot/types/interfaces/rpc';
import { Codec, IExtrinsic } from '@polkadot/types/types';
export interface RpcInterface {
author: {
removeExtrinsic(bytesOrHash: (ExtrinsicOrHash)[]): Observable<Vec<Hash>>;
pendingExtrinsics(): Observable<Vec<Extrinsic>>;
submitExtrinsic(extrinsic: IExtrinsic): Observable<Hash>;
submitAndWatchExtrinsic(extrinsic: IExtrinsic): Observable<ExtrinsicStatus>;
};
chain: {
getBlock(hash?: Hash | Uint8Array | string): Observable<SignedBlock>;
getBlockHash(blockNumber?: BlockNumber | Uint8Array | number | string): Observable<Hash>;
getFinalizedHead(): Observable<Hash>;
getHeader(hash?: Hash | Uint8Array | string): Observable<Header>;
getRuntimeVersion(hash?: Hash | Uint8Array | string): Observable<RuntimeVersion>;
subscribeFinalizedHeads(): Observable<Header>;
subscribeRuntimeVersion(): Observable<RuntimeVersion>;
subscribeNewHead(): Observable<Header>;
};
state: {
call(method: Text | string, data: Bytes | Uint8Array | string, block?: Hash | Uint8Array | string): Observable<Bytes>;
getChildKeys(childStorageKey: any, prefix: any, block?: Hash | Uint8Array | string): Observable<Vec<StorageKey>>;
getChildStorage(childStorageKey: any, key: any, block?: Hash | Uint8Array | string): Observable<StorageData>;
getChildStorageHash(childStorageKey: any, key: any, block?: Hash | Uint8Array | string): Observable<Hash>;
getChildStorageSize(childStorageKey: any, key: any, block?: Hash | Uint8Array | string): Observable<u64>;
getKeys(prefix: any, block?: Hash | Uint8Array | string): Observable<Vec<StorageKey>>;
getMetadata(block?: Hash | Uint8Array | string): Observable<Metadata>;
getRuntimeVersion(hash?: Hash | Uint8Array | string): Observable<RuntimeVersion>;
getStorage<T = Codec>(key: any, block?: Hash | Uint8Array | string): Observable<T>;
getStorageHash(key: any, block?: Hash | Uint8Array | string): Observable<Hash>;
getStorageSize(key: any, block?: Hash | Uint8Array | string): Observable<u64>;
queryStorage(keys: (any)[], startBlock: Hash | Uint8Array | string, block?: Hash | Uint8Array | string): Observable<Vec<StorageChangeSet>>;
subscribeStorage<T = Codec[]>(keys: any[]): Observable<T>;
};
system: {
chain(): Observable<Text>;
health(): Observable<Health>;
name(): Observable<Text>;
networkState(): Observable<NetworkState>;
peers(): Observable<Vec<PeerInfo>>;
properties(): Observable<ChainProperties>;
version(): Observable<Text>;
};
}
+2
View File
@@ -15,8 +15,10 @@ describe('replay', (): void => {
});
it('subscribes via the rpc section', (done): void => {
// @ts-ignore
rpc.chain.getBlockHash = jest.fn((): Observable<number> => of(1));
// @ts-ignore
rpc.chain.getBlockHash(123, false).subscribe((): void => {
expect(
rpc.chain.getBlockHash
-9
View File
@@ -7,12 +7,3 @@ import { Observable } from 'rxjs';
export interface RpcInterfaceMethod {
(...params: any[]): Observable<any>;
}
export type RpcInterfaceSection = Record<string, RpcInterfaceMethod>;
export interface RpcInterface {
readonly author: RpcInterfaceSection;
readonly chain: RpcInterfaceSection;
readonly state: RpcInterfaceSection;
readonly system: RpcInterfaceSection;
}