From 5e0c0ba91a1ae2536ec02a779c5fc79a40a776aa Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 2 Feb 2020 20:16:41 +0100 Subject: [PATCH] Align jsonrpc augmentation with others (#1796) --- packages/api-contract/src/base/util.ts | 4 +- packages/api/src/base/Decorate.ts | 2 +- packages/api/src/base/index.ts | 2 +- packages/api/src/types/augment/index.ts | 1 + packages/api/src/types/augment/rpc.ts | 177 +++++++++++++++++++++ packages/api/src/types/index.ts | 5 +- packages/rpc-core/src/index.ts | 3 +- packages/rpc-core/src/jsonrpc.types.ts | 67 -------- packages/rpc-core/src/types.jsonrpc.ts | 6 + packages/rpc-core/src/types.ts | 2 + packages/types/src/scripts/generate/rpc.ts | 17 +- 11 files changed, 201 insertions(+), 85 deletions(-) create mode 100644 packages/api/src/types/augment/rpc.ts delete mode 100644 packages/rpc-core/src/jsonrpc.types.ts create mode 100644 packages/rpc-core/src/types.jsonrpc.ts diff --git a/packages/api-contract/src/base/util.ts b/packages/api-contract/src/base/util.ts index d167e34ddf..a70442e0e4 100644 --- a/packages/api-contract/src/base/util.ts +++ b/packages/api-contract/src/base/util.ts @@ -3,9 +3,9 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ApiTypes, DecorateMethod, DecoratedRpc, SubmittableModuleExtrinsics } from '@polkadot/api/types'; -import { ApiObject, ContractABIMessage, ContractABIPre, ContractBase, ContractMessage } from '../types'; -import { RpcInterface } from '@polkadot/rpc-core/jsonrpc.types'; +import { RpcInterface } from '@polkadot/rpc-core/types'; import { Registry } from '@polkadot/types/types'; +import { ApiObject, ContractABIMessage, ContractABIPre, ContractBase, ContractMessage } from '../types'; import { assert, isFunction, stringCamelCase } from '@polkadot/util'; import Abi from '../Abi'; diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index 4ef1fc3659..f4631043cb 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { Constants, Storage } from '@polkadot/metadata/Decorated/types'; -import { RpcInterface } from '@polkadot/rpc-core/jsonrpc.types'; +import { RpcInterface } from '@polkadot/rpc-core/types'; import { InterfaceRegistry } from '@polkadot/types/interfaceRegistry'; import { Call, Hash, RuntimeVersion } from '@polkadot/types/interfaces'; import { AnyFunction, CallFunction, Codec, CodecArg as Arg, ITuple, InterfaceTypes, ModulesWithCalls, Registry, RegistryTypes } from '@polkadot/types/types'; diff --git a/packages/api/src/base/index.ts b/packages/api/src/base/index.ts index f6ac2f8eea..e2fc398be4 100644 --- a/packages/api/src/base/index.ts +++ b/packages/api/src/base/index.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { Constants } from '@polkadot/metadata/Decorated/types'; -import { RpcInterface } from '@polkadot/rpc-core/jsonrpc.types'; +import { RpcInterface } from '@polkadot/rpc-core/types'; import { Hash, RuntimeVersion } from '@polkadot/types/interfaces'; import { InterfaceRegistry } from '@polkadot/types/interfaceRegistry'; import { CallFunction, InterfaceTypes, RegistryError, RegistryTypes, SignerPayloadRawBase } from '@polkadot/types/types'; diff --git a/packages/api/src/types/augment/index.ts b/packages/api/src/types/augment/index.ts index d739c29cc4..aa136b8f45 100644 --- a/packages/api/src/types/augment/index.ts +++ b/packages/api/src/types/augment/index.ts @@ -4,4 +4,5 @@ import './consts'; import './query'; +import './rpc'; import './tx'; diff --git a/packages/api/src/types/augment/rpc.ts b/packages/api/src/types/augment/rpc.ts new file mode 100644 index 0000000000..85582ab350 --- /dev/null +++ b/packages/api/src/types/augment/rpc.ts @@ -0,0 +1,177 @@ +// Auto-generated via `yarn polkadot-types-from-defs`, do not edit +/* eslint-disable @typescript-eslint/no-empty-interface */ + +import { Observable } from 'rxjs'; +import { Option, Vec } from '@polkadot/types/codec'; +import { Bytes, H256, Metadata, StorageData, StorageKey, Text, u32, u64 } from '@polkadot/types'; +import { AccountId, BlockNumber, Extrinsic, Hash, Header, Index, SignedBlock } from '@polkadot/types/interfaces/runtime'; +import { ContractCallRequest, ContractExecResult } from '@polkadot/types/interfaces/contracts'; +import { BlockHash, ChainProperties, ExtrinsicOrHash, ExtrinsicStatus, Health, NetworkState, PeerInfo, RpcMethods, RuntimeDispatchInfo, RuntimeVersion, StorageChangeSet } from '@polkadot/types/interfaces/rpc'; +import { AnyNumber, Codec, IExtrinsic } from '@polkadot/types/types'; + +declare module '@polkadot/rpc-core/types.jsonrpc' { + export interface RpcInterface { + account: { + /** + * Retrieves the next accountIndex as available on the node + **/ + nextIndex(accountId: AccountId | string | Uint8Array): Observable; + }; + author: { + /** + * Insert a key into the keystore. + **/ + insertKey(keyType: Text | string, suri: Text | string, publicKey: Bytes | string | Uint8Array): Observable; + /** + * Returns all pending extrinsics, potentially grouped by sender + **/ + pendingExtrinsics(): Observable>; + /** + * Remove given extrinsic from the pool and temporarily ban it to prevent reimporting + **/ + removeExtrinsic(bytesOrHash: Vec | (ExtrinsicOrHash | object | number | Uint8Array | string)[]): Observable>; + /** + * Generate new session keys and returns the corresponding public keys + **/ + rotateKeys(): Observable; + /** + * Submit and subscribe to watch an extrinsic until unsubscribed + **/ + submitAndWatchExtrinsic(extrinsic: IExtrinsic): Observable; + /** + * Submit a fully formatted extrinsic for block inclusion + **/ + submitExtrinsic(extrinsic: IExtrinsic): Observable; + }; + chain: { + /** + * Get header and body of a relay chain block + **/ + getBlock(hash?: BlockHash | string | Uint8Array): Observable; + /** + * Get the block hash for a specific block + **/ + getBlockHash(blockNumber?: BlockNumber | AnyNumber | Uint8Array): Observable; + /** + * Get hash of the last finalized block in the canon chain + **/ + getFinalizedHead(): Observable; + /** + * Retrieves the header for a specific block + **/ + getHeader(hash?: BlockHash | string | Uint8Array): Observable
; + /** + * Retrieves the best finalized header via subscription + **/ + subscribeFinalizedHeads(): Observable
; + /** + * Retrieves the best header via subscription + **/ + subscribeNewHeads(): Observable
; + }; + contracts: { + /** + * Executes a call to a contract + **/ + call(callRequest: ContractCallRequest | object | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable; + /** + * Returns the value under a specified storage key in a contract + **/ + getStorage(address: AccountId | string | Uint8Array, key: H256 | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable>; + }; + payment: { + /** + * Retrieves the fee information for an encoded extrinsic + **/ + queryInfo(extrinsic: Bytes | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable; + }; + rpc: { + /** + * Retrieves the list of RPC methods that are exposed by the node + **/ + methods(): Observable; + }; + state: { + /** + * Perform a call to a builtin on the chain + **/ + call(method: Text | string, data: Bytes | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable; + /** + * Retrieves the keys with prefix of a specific child storage + **/ + getChildKeys(childStorageKey: StorageKey | string | Uint8Array | any, childDefinition: StorageKey | string | Uint8Array | any, childType: u32 | AnyNumber | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable>; + /** + * Retrieves the child storage for a key + **/ + getChildStorage(childStorageKey: StorageKey | string | Uint8Array | any, childDefinition: StorageKey | string | Uint8Array | any, childType: u32 | AnyNumber | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; + /** + * Retrieves the child storage hash + **/ + getChildStorageHash(childStorageKey: StorageKey | string | Uint8Array | any, childDefinition: StorageKey | string | Uint8Array | any, childType: u32 | AnyNumber | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; + /** + * Retrieves the child storage size + **/ + getChildStorageSize(childStorageKey: StorageKey | string | Uint8Array | any, childDefinition: StorageKey | string | Uint8Array | any, childType: u32 | AnyNumber | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; + /** + * Retrieves the keys with a certain prefix + **/ + getKeys(key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable>; + /** + * Returns the runtime metadata + **/ + getMetadata(at?: BlockHash | string | Uint8Array): Observable; + /** + * Get the runtime version + **/ + getRuntimeVersion(at?: BlockHash | string | Uint8Array): Observable; + getStorage(key: any, block?: Hash | Uint8Array | string): Observable; + /** + * Retrieves the storage hash + **/ + getStorageHash(key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; + /** + * Retrieves the storage size + **/ + getStorageSize(key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; + /** + * Query historical storage entries (by key) starting from a start block + **/ + queryStorage(keys: Vec | (StorageKey | string | Uint8Array | any)[], startBlock: Hash | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable>; + /** + * Retrieves the runtime version via subscription + **/ + subscribeRuntimeVersion(): Observable; + subscribeStorage(keys: any[]): Observable; + }; + system: { + /** + * Retrieves the chain + **/ + chain(): Observable; + /** + * Return health status of the node + **/ + health(): Observable; + /** + * Retrieves the node name + **/ + name(): Observable; + /** + * Returns current state of the network + **/ + networkState(): Observable; + /** + * Returns the currently connected peers + **/ + peers(): Observable>; + /** + * Get a custom set of properties as a JSON object, defined in the chain spec + **/ + properties(): Observable; + /** + * Retrieves the version of the node + **/ + version(): Observable; + }; + } +} diff --git a/packages/api/src/types/index.ts b/packages/api/src/types/index.ts index 71b580a24c..7a6d07e2f2 100644 --- a/packages/api/src/types/index.ts +++ b/packages/api/src/types/index.ts @@ -2,14 +2,13 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -// Module augmentation so that `api.{consts,query,tx}.*.*` work +// Module augmentation so that `api.{consts,query,rpc,tx}.*.*` work import './augment'; import BN from 'bn.js'; import { DeriveCustom, ExactDerive } from '@polkadot/api-derive'; import { Constants } from '@polkadot/metadata/Decorated/types'; -import { RpcInterface } from '@polkadot/rpc-core/jsonrpc.types'; -import { UserRpc } from '@polkadot/rpc-core/types'; +import { RpcInterface, UserRpc } from '@polkadot/rpc-core/types'; import { Metadata } from '@polkadot/types'; import { Hash, RuntimeVersion } from '@polkadot/types/interfaces'; import { RegistryTypes, SignatureOptions, SignerPayloadJSON, SignerPayloadRaw, Registry } from '@polkadot/types/types'; diff --git a/packages/rpc-core/src/index.ts b/packages/rpc-core/src/index.ts index f40b5c3682..246f1eb42c 100644 --- a/packages/rpc-core/src/index.ts +++ b/packages/rpc-core/src/index.ts @@ -5,8 +5,7 @@ import { ProviderInterface } from '@polkadot/rpc-provider/types'; import { RpcMethod, RpcSection, RpcParam } from '@polkadot/jsonrpc/types'; import { AnyJson, Codec, Registry } from '@polkadot/types/types'; -import { RpcInterface } from './jsonrpc.types'; -import { RpcInterfaceMethod, UserRpc } from './types'; +import { RpcInterface, RpcInterfaceMethod, UserRpc } from './types'; import memoizee from 'memoizee'; import { combineLatest, from, Observable, Observer, of, throwError } from 'rxjs'; diff --git a/packages/rpc-core/src/jsonrpc.types.ts b/packages/rpc-core/src/jsonrpc.types.ts deleted file mode 100644 index 273ef72e85..0000000000 --- a/packages/rpc-core/src/jsonrpc.types.ts +++ /dev/null @@ -1,67 +0,0 @@ -// Auto-generated via `yarn polkadot-types-from-defs`, do not edit -/* eslint-disable @typescript-eslint/no-empty-interface */ - -import { Observable } from 'rxjs'; -import { Option, Vec } from '@polkadot/types/codec'; -import { Bytes, H256, Metadata, StorageData, StorageKey, Text, u32, u64 } from '@polkadot/types'; -import { AccountId, BlockNumber, Extrinsic, Hash, Header, Index, SignedBlock } from '@polkadot/types/interfaces/runtime'; -import { ContractCallRequest, ContractExecResult } from '@polkadot/types/interfaces/contracts'; -import { BlockHash, ChainProperties, ExtrinsicOrHash, ExtrinsicStatus, Health, NetworkState, PeerInfo, RpcMethods, RuntimeDispatchInfo, RuntimeVersion, StorageChangeSet } from '@polkadot/types/interfaces/rpc'; -import { AnyNumber, Codec, IExtrinsic } from '@polkadot/types/types'; - -export interface RpcInterface { - account: { - nextIndex(accountId: AccountId | string | Uint8Array): Observable; - }; - author: { - insertKey(keyType: Text | string, suri: Text | string, publicKey: Bytes | string | Uint8Array): Observable; - pendingExtrinsics(): Observable>; - removeExtrinsic(bytesOrHash: Vec | (ExtrinsicOrHash | object | number | Uint8Array | string)[]): Observable>; - rotateKeys(): Observable; - submitAndWatchExtrinsic(extrinsic: IExtrinsic): Observable; - submitExtrinsic(extrinsic: IExtrinsic): Observable; - }; - chain: { - getBlock(hash?: BlockHash | string | Uint8Array): Observable; - getBlockHash(blockNumber?: BlockNumber | AnyNumber | Uint8Array): Observable; - getFinalizedHead(): Observable; - getHeader(hash?: BlockHash | string | Uint8Array): Observable
; - subscribeFinalizedHeads(): Observable
; - subscribeNewHeads(): Observable
; - }; - contracts: { - call(callRequest: ContractCallRequest | object | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable; - getStorage(address: AccountId | string | Uint8Array, key: H256 | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable>; - }; - payment: { - queryInfo(extrinsic: Bytes | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable; - }; - rpc: { - methods(): Observable; - }; - state: { - call(method: Text | string, data: Bytes | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable; - getChildKeys(childStorageKey: StorageKey | string | Uint8Array | any, childDefinition: StorageKey | string | Uint8Array | any, childType: u32 | AnyNumber | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable>; - getChildStorage(childStorageKey: StorageKey | string | Uint8Array | any, childDefinition: StorageKey | string | Uint8Array | any, childType: u32 | AnyNumber | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; - getChildStorageHash(childStorageKey: StorageKey | string | Uint8Array | any, childDefinition: StorageKey | string | Uint8Array | any, childType: u32 | AnyNumber | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; - getChildStorageSize(childStorageKey: StorageKey | string | Uint8Array | any, childDefinition: StorageKey | string | Uint8Array | any, childType: u32 | AnyNumber | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; - getKeys(key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable>; - getMetadata(at?: BlockHash | string | Uint8Array): Observable; - getRuntimeVersion(at?: BlockHash | string | Uint8Array): Observable; - getStorage(key: any, block?: Hash | Uint8Array | string): Observable; - getStorageHash(key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; - getStorageSize(key: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array): Observable; - queryStorage(keys: Vec | (StorageKey | string | Uint8Array | any)[], startBlock: Hash | string | Uint8Array, at?: BlockHash | string | Uint8Array): Observable>; - subscribeRuntimeVersion(): Observable; - subscribeStorage(keys: any[]): Observable; - }; - system: { - chain(): Observable; - health(): Observable; - name(): Observable; - networkState(): Observable; - peers(): Observable>; - properties(): Observable; - version(): Observable; - }; -} diff --git a/packages/rpc-core/src/types.jsonrpc.ts b/packages/rpc-core/src/types.jsonrpc.ts new file mode 100644 index 0000000000..3f668ae08d --- /dev/null +++ b/packages/rpc-core/src/types.jsonrpc.ts @@ -0,0 +1,6 @@ +// Copyright 2017-2020 @polkadot/rpc-core authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface RpcInterface {} diff --git a/packages/rpc-core/src/types.ts b/packages/rpc-core/src/types.ts index 00926a720b..9a4b67064c 100644 --- a/packages/rpc-core/src/types.ts +++ b/packages/rpc-core/src/types.ts @@ -4,6 +4,8 @@ import { Observable } from 'rxjs'; +export * from './types.jsonrpc'; + export interface RpcInterfaceMethod { (...params: any[]): Observable; } diff --git a/packages/types/src/scripts/generate/rpc.ts b/packages/types/src/scripts/generate/rpc.ts index a0bf0125d8..44203fee76 100644 --- a/packages/types/src/scripts/generate/rpc.ts +++ b/packages/types/src/scripts/generate/rpc.ts @@ -10,12 +10,11 @@ import { TypeRegistry } from '../../codec/create'; import { createImportCode, createImports, FOOTER, getSimilarTypes, HEADER, setImports } from '../util'; /** @internal */ -export default function generateRpcTypes (dest = 'packages/rpc-core/src/jsonrpc.types.ts'): void { +export default function generateRpcTypes (dest = 'packages/api/src/types/augment/rpc.ts'): void { console.log(`${dest}\n\tGenerating`); const registry = new TypeRegistry(); const imports = createImports({ '@polkadot/types/interfaces': definitions }); - const body = Object.keys(interfaces).sort().reduce((allSections, section): string[] => { const allMethods = Object.keys(interfaces[section].methods).sort().map((key): string => { const method = interfaces[section].methods[key]; @@ -26,9 +25,9 @@ export default function generateRpcTypes (dest = 'packages/rpc-core/src/jsonrpc. if (section === 'state') { if (method.method === 'getStorage') { setImports(definitions, imports, ['Codec']); - return ' getStorage(key: any, block?: Hash | Uint8Array | string): Observable;'; + return ' getStorage(key: any, block?: Hash | Uint8Array | string): Observable;'; } else if (method.method === 'subscribeStorage') { - return ' subscribeStorage(keys: any[]): Observable;'; + return ' subscribeStorage(keys: any[]): Observable;'; } } @@ -39,14 +38,14 @@ export default function generateRpcTypes (dest = 'packages/rpc-core/src/jsonrpc. return `${param.name}${param.isOptional ? '?' : ''}: ${similarTypes.join(' | ')}`; }); - return ` ${method.method}(${args.join(', ')}): Observable<${method.type}>;`; + return ` /**\n * ${method.description}\n **/\n ${method.method}(${args.join(', ')}): Observable<${method.type}>;`; }); return allSections.concat( [ - ` ${section}: {`, + ` ${section}: {`, ...allMethods, - ' };' + ' };' ].join('\n') ); }, []).join('\n'); @@ -73,8 +72,8 @@ export default function generateRpcTypes (dest = 'packages/rpc-core/src/jsonrpc. types: Object.keys(imports.typesTypes) } ]); - const interfaceStart = 'export interface RpcInterface {\n'; - const interfaceEnd = '\n}'; + const interfaceStart = "declare module '@polkadot/rpc-core/types.jsonrpc' {\n export interface RpcInterface {\n"; + const interfaceEnd = '\n }\n}'; console.log('\tWriting');