Use jsonrpc definitions from @polkadot/jsonrpc (#91)

* Drop api-jsonrpc in favour of @polkadot/jsonrpc

* Change converters

* Address issues after conversion
This commit is contained in:
Jaco Greeff
2018-05-18 10:08:08 +02:00
committed by GitHub
parent 50e597c943
commit 861155f9ef
46 changed files with 221 additions and 628 deletions
-1
View File
@@ -18,5 +18,4 @@ The API is split up into a number of internal packages -
- [@polkadot/api](packages/api/) The low-level base API library
- [@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-jsonrpc](packages/api-jsonrpc/) Interface definitions for RPC
- [@polkadot/api-provider](packages/api-provider/) Providers for connecting
+1 -1
View File
@@ -10,5 +10,5 @@
"packages": [
"packages/*"
],
"version": "0.11.3"
"version": "0.12.0"
}
+1 -1
View File
@@ -11,6 +11,6 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.19.9"
"@polkadot/dev": "^0.19.10"
}
}
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-format",
"version": "0.11.3",
"version": "0.12.0",
"description": "Input/Output formatters for JsonRPC exchange",
"main": "index.js",
"keywords": [
@@ -31,8 +31,8 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0-beta.47",
"@polkadot/api-jsonrpc": "^0.11.3",
"@polkadot/primitives-json": "^0.14.3",
"@polkadot/util": "^0.20.5"
"@polkadot/jsonrpc": "^0.15.2",
"@polkadot/primitives-json": "^0.15.2",
"@polkadot/util": "^0.21.1"
}
}
+15 -17
View File
@@ -3,22 +3,21 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { FormatInputType, FormatOutputType } from '@polkadot/api-jsonrpc/types';
import type { Param$Type, Param$Type$Tuple, Param$Types } from '@polkadot/params/types';
import type { FormatterFunction } from './types';
type FormattersFunctionType = FormatInputType | FormatOutputType;
type FormattersFunctionMap = {
[FormattersFunctionType]: FormatterFunction
};
type FormattersFunctionMap = $Shape<{
[Param$Type]: FormatterFunction
}>;
const typeToString = require('@polkadot/params/typeToString');
const isUndefined = require('@polkadot/util/is/undefined');
const l = require('@polkadot/util/logger')('api-format');
const echo = require('./echo');
const arrayTypeRegex = /\[\]$/;
function formatSingleType (formatters: FormattersFunctionMap, type: FormattersFunctionType, value: mixed): mixed {
// flowlint-next-line unclear-type:off
function formatSingleType (formatters: FormattersFunctionMap, type: Param$Type, value: any): any {
const formatter: FormatterFunction = formatters[type];
if (isUndefined(formatter)) {
@@ -30,26 +29,25 @@ function formatSingleType (formatters: FormattersFunctionMap, type: FormattersFu
try {
return formatter(value);
} catch (error) {
// $FlowFixMe just let JS coerce all it wants to
throw new Error(`Error formatting '${value}' as '${type}': ${error.message}`);
throw new Error(`Error formatting '${value.toString()}' as '${typeToString(type)}': ${error.message}`);
}
}
function formatArrayType (formatters: FormattersFunctionMap, type: FormattersFunctionType, value: Array<mixed>): mixed {
// flowlint-next-line unclear-type:off
type = ((type.replace(arrayTypeRegex, ''): any): FormattersFunctionType);
// flowlint-next-line unclear-type:off
function formatArrayType (formatters: FormattersFunctionMap, [ type ]: Param$Type$Tuple, value: Array<any>): any {
return value.map((value) => {
return formatSingleType(formatters, type, value);
});
}
module.exports = function format (formatters: FormattersFunctionMap, types: Array<FormattersFunctionType>, values: Array<mixed>): Array<mixed> {
// flowlint-next-line unclear-type:off
module.exports = function format (formatters: FormattersFunctionMap, types: Array<Param$Types>, values: Array<any>): Array<any> {
return values.map((value, index): mixed => {
const type = types[index];
if (arrayTypeRegex.test(type)) {
// $FlowFixMe array type
if (Array.isArray(type)) {
// FIXME we are currently not catering for tuples
// $FlowFixMe assume that we are only passing a single type through
return formatArrayType(formatters, type, value);
}
+2 -3
View File
@@ -16,7 +16,6 @@ describe('format', () => {
formatters = {
'Address': echoSpy,
'Address[]': echoSpy,
'Exception': () => {
throw new Error('something went wrong');
}
@@ -57,7 +56,7 @@ describe('format', () => {
describe('primitive[]', () => {
it('formats using the primitive type', () => {
format(formatters, ['Address[]'], [['0x123', '0x234']]);
format(formatters, [['Address']], [['0x123', '0x234']]);
expect(echoSpy).toHaveBeenCalledWith('0x123');
expect(echoSpy).toHaveBeenCalledWith('0x234');
@@ -65,7 +64,7 @@ describe('format', () => {
it('returns formatted values as arrays', () => {
expect(
format(formatters, ['Address[]'], [['0x123', '0x234']])
format(formatters, [['Address']], [['0x123', '0x234']])
).toEqual([['0x123', '0x234']]);
});
});
+5 -5
View File
@@ -3,7 +3,7 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceInputType } from '@polkadot/api-jsonrpc/types';
import type { Params, Param$Types } from '@polkadot/params/types';
const bytesEncode = require('@polkadot/primitives-json/bytes/encode');
const hashEncode = require('@polkadot/primitives-json/hash/encode');
@@ -12,12 +12,12 @@ const format = require('./format');
const formatters = {
'Bytes': bytesEncode,
'H256': hashEncode,
'HeaderHash': hashEncode
'Hash': hashEncode
};
module.exports = function formatInputs (inputs: Array<InterfaceInputType>, values: Array<mixed>): Array<mixed> {
const types = inputs.map(({ type }) => type);
// flowlint-next-line unclear-type:off
module.exports = function formatInputs (params: Params, values: Array<any>): Array<any> {
const types: Array<Param$Types> = Object.keys(params).map((name) => params[name].type);
return format(formatters, types, values);
};
+7 -4
View File
@@ -8,11 +8,14 @@ describe('formatInputs', () => {
it('formats each value in an array', () => {
expect(
formatInputs(
{
foo: { type: 'Bytes' },
bar: { type: 'Hash' }
},
[
{ name: 'foo', type: 'Bytes' },
{ name: 'bar', type: 'H256' }
],
[new Uint8Array([0x12, 0x34]), new Uint8Array([0xab, 0xcd])]
new Uint8Array([0x12, 0x34]),
new Uint8Array([0xab, 0xcd])
]
)
).toEqual([
'0x1234',
+4 -4
View File
@@ -3,7 +3,7 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
import type { Param$Types } from '@polkadot/params/types';
const bnDecode = require('@polkadot/primitives-json/bn/decode');
const bytesDecode = require('@polkadot/primitives-json/bytes/decode');
@@ -17,13 +17,13 @@ const formatters = {
'BlockNumber': bnDecode,
'Bytes': bytesDecode,
'Header': headerDecode,
'U64': bnDecode
'u64': bnDecode
};
module.exports = function formatOutput (output: InterfaceOutputType, value?: mixed): ?mixed {
module.exports = function formatOutput (type: Param$Types, value?: mixed): ?mixed {
if (isUndefined(value) || isNull(value)) {
return value;
}
return format(formatters, [output.type], [value])[0];
return format(formatters, [type], [value])[0];
};
+12 -9
View File
@@ -10,15 +10,18 @@ describe('formatOutput', () => {
it('formats the value', () => {
expect(
JSON.stringify(
formatOutput({ type: 'Header' }, {
digest: {
logs: ['0x5678', '0x789a']
},
extrinsicsRoot: '0xabcd',
number: '0x1234',
parentHash: '0x1234',
stateRoot: '0x5678'
})
formatOutput(
'Header',
{
digest: {
logs: ['0x5678', '0x789a']
},
extrinsicsRoot: '0xabcd',
number: '0x1234',
parentHash: '0x1234',
stateRoot: '0x5678'
}
)
)
).toEqual(
JSON.stringify({
-15
View File
@@ -1,15 +0,0 @@
ISC License (ISC)
Copyright 2017-2018 Jaco Greeff <jacogr@gmail.com>
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.
-30
View File
@@ -1,30 +0,0 @@
[![polkadotjs](https://img.shields.io/badge/polkadot-js-orange.svg?style=flat-square)](https://polkadot.js.org)
![isc](https://img.shields.io/badge/license-ISC-lightgrey.svg?style=flat-square)
[![style](https://img.shields.io/badge/code%20style-semistandard-lightgrey.svg?style=flat-square)](https://github.com/Flet/semistandard)
[![npm](https://img.shields.io/npm/v/@polkadot/api-jsonrpc.svg?style=flat-square)](https://www.npmjs.com/package/@polkadot/api-jsonrpc)
[![travis](https://img.shields.io/travis/polkadot-js/api.svg?style=flat-square)](https://travis-ci.org/polkadot-js/api)
[![maintainability](https://img.shields.io/codeclimate/maintainability/polkadot-js/api.svg?style=flat-square)](https://codeclimate.com/github/polkadot-js/api/maintainability)
[![coverage](https://img.shields.io/coveralls/polkadot-js/api.svg?style=flat-square)](https://coveralls.io/github/polkadot-js/api?branch=master)
[![dependency](https://david-dm.org/polkadot-js/api.svg?style=flat-square&path=packages/api-jsonrpc)](https://david-dm.org/polkadot-js/api?path=packages/api-jsonrpc)
[![devDependency](https://david-dm.org/polkadot-js/api/dev-status.svg?style=flat-square&path=packages/api-jsonrpc)](https://david-dm.org/polkadot-js/api?path=packages/api-jsonrpc#info=devDependencies)
# @polkadot/api-jsonrpc
A defintion of all the methods exposed in a general Polkadot client application. These are used not only to provide a comprehensive code-generated document of the available methods, but are also used in the API to auto-generate endpoints with the required type-checking.
For a list of currently exposed methods, see the [method documentation](docs/README.md).
## Usage
Installation -
```
npm install --save @polkadot/api-jsonrpc
```
## Adding methods
As methods are added, simply adding the name, inputs & output will prepare it for use.
- Add the method to the correct file in [src/rpc/](src/rpc/) (Input/Output types as cross-referenced from the canonical implementation and match one-to-one)
- Should a new type be required, add it to the type list, [src/types.js](src/types.js) (Required for flow type checking)
-7
View File
@@ -1,7 +0,0 @@
# Available interfaces
Exposes the definition for the RPC endpoints for a Polkadot client node
- [chain](chain.md) Methods to retrieve chain data.
- [state](state.md) Query the state and state storage.
-17
View File
@@ -1,17 +0,0 @@
# chain
Methods to retrieve chain data.
- [getHeader](#getheader) Retrieves the header for a specific block.
## getHeader
Retrieves the header for a specific block.
```js
chain_getHeader (hash: HeaderHash): Header
```
Given a block header specified by `hash`, return the full block `Header` information.
-31
View File
@@ -1,31 +0,0 @@
# state
Query the state and state storage.
- [call](#call) Perform a call to a builtin on the chain.
- [getStorage](#getstorage) Retrieves the storage for an address.
## call
Perform a call to a builtin on the chain.
```js
chain_call (address: Address, method: String, data: CallData, block: HeaderHash): OutData
```
Calls a `method` at a specific `address`, passing the encoded `data`. The query is executed at the block specified by `block`.
## getStorage
Retrieves the storage for an address.
```js
chain_getStorage (address: Address, key: H256, block: HeaderHash): StorageData
```
Retrieves the storage `key` at a specific `address`, executing the query at a specific `block`.
-35
View File
@@ -1,35 +0,0 @@
{
"name": "@polkadot/api-jsonrpc",
"version": "0.11.3",
"description": "Method definitions for the Polkadot RPC layer",
"main": "index.js",
"engines": {
"node": ">=6.4"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"repository": {
"type": "git",
"url": "git+https://github.com/polkadot-js/api.git"
},
"keywords": [
"Polkadot",
"JsonRPC"
],
"author": "Jaco Greeff <jacogr@gmail.com>",
"license": "ISC",
"bugs": {
"url": "https://github.com/polkadot-js/api/issues"
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-jsonrpc#readme",
"scripts": {
"build": "polkadot-dev-build-babel && polkadot-dev-build-docs",
"check": "eslint src && flow check",
"test": "echo \"Tests only available from root wrapper\""
},
"dependencies": {
"@babel/runtime": "^7.0.0-beta.47"
}
}
-17
View File
@@ -1,17 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceDefinition } from '../types';
const submitExtrinsic = require('./submitExtrinsic');
/**
@summary Methods to work with authors.
*/
module.exports = ({
methods: {
submitExtrinsic
}
}: InterfaceDefinition);
@@ -1,23 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '../types';
/**
@name submitExtrinsic
@signature author_submitExtrinsic (extrinsic: Bytes): Header
@summary Submit a fully formatted extrinsic for block inclusion.
@description
Given a block header specified by `hash`, return the full block `Header` information.
*/
module.exports = ({
inputs: [
{
name: 'extrinsic',
type: 'Bytes'
}
],
output: { type: 'Header' }
}: InterfaceMethodDefinition);
-20
View File
@@ -1,20 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '../types';
/**
@name getHead
@signature chain_getHead (): HeaderHash
@summary Retrieves the best headerHash.
@description
Return the block hash for the lastest/best.
*/
module.exports = ({
inputs: [],
output: {
type: 'HeaderHash'
}
}: InterfaceMethodDefinition);
@@ -1,25 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '../types';
/**
@name getHeader
@signature chain_getHeader (hash: HeaderHash): Header
@summary Retrieves the header for a specific block.
@description
Given a block header specified by `hash`, return the full block `Header` information.
*/
module.exports = ({
inputs: [
{
name: 'hash',
type: 'HeaderHash'
}
],
output: {
type: 'Header'
}
}: InterfaceMethodDefinition);
-21
View File
@@ -1,21 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceDefinition } from '../types';
const getHead = require('./getHead');
const getHeader = require('./getHeader');
const newHead = require('./newHead');
/**
@summary Methods to retrieve chain data.
*/
module.exports = ({
methods: {
getHead,
getHeader,
newHead
}
}: InterfaceDefinition);
-21
View File
@@ -1,21 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '../types';
/**
@name getHead
@signature chain_newHead (): HeaderHash
@summary Retrieves the best headerHash via subscription.
@description
Return the block hash for the lastest/best.
*/
module.exports = ({
isSubscription: true,
inputs: [],
output: {
type: 'Header'
}
}: InterfaceMethodDefinition);
-19
View File
@@ -1,19 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceDefinitions } from './types';
const author = require('./author');
const chain = require('./chain');
const state = require('./state');
/**
@summary Exposes the definition for the RPC endpoints for a Polkadot client node
*/
module.exports = ({
author,
chain,
state
}: InterfaceDefinitions);
-12
View File
@@ -1,12 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const interfaces = require('./index');
describe('jsonrpc', () => {
it('exports the available interfaces', () => {
expect(interfaces).toBeDefined();
expect(Object.keys(interfaces).length).toBeGreaterThan(0);
});
});
-33
View File
@@ -1,33 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '../types';
/**
@name call
@signature chain_call (address: Address, method: String, data: Bytes, block: HeaderHash): OutData
@summary Perform a call to a builtin on the chain.
@description
Calls a `method` at a specific `address`, passing the encoded `data`. The query is executed at the block specified by `block`.
*/
module.exports = ({
inputs: [
{
name: 'method',
type: 'String'
},
{
name: 'data',
type: 'Bytes'
},
{
name: 'block',
type: 'HeaderHash'
}
],
output: {
type: 'Bytes'
}
}: InterfaceMethodDefinition);
@@ -1,30 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '../types';
/**
@name getStorage
@signature chain_getStorage (key: H256, blockHash: HeaderHash): StorageData
@summary Retrieves the storage for a key at a specific block.
@description
Retrieves the storage `key` at a specific `blockHash`.
*/
module.exports = ({
inputs: [
{
name: 'key',
type: 'H256'
},
{
isOptional: true,
name: 'blockHash',
type: 'HeaderHash'
}
],
output: {
type: 'Bytes'
}
}: InterfaceMethodDefinition);
-19
View File
@@ -1,19 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceDefinition } from '../types';
const call = require('./call');
const getStorage = require('./getStorage');
/**
@summary Query the state and state storage.
*/
module.exports = ({
methods: {
call,
getStorage
}
}: InterfaceDefinition);
-44
View File
@@ -1,44 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
export type InterfaceTypes = 'author' | 'chain' | 'state';
export type FormatInputType = 'Bytes' | 'H256' | 'HeaderHash' | 'String';
export type FormatOutputType = 'BlockNumber' | 'Bytes' | 'Header' | 'HeaderHash' | 'U64';
export type InterfaceInputType = {
isOptional?: boolean,
name: string,
type: FormatInputType
};
export type InterfaceOutputType = {
type: FormatOutputType
};
export type InterfaceMethodDefinition$Subscribe = {
subscribe: string,
unsubscribe: string
}
export type InterfaceMethodDefinition = {
isDeprecated?: boolean,
isSubscription?: boolean,
inputs: Array<InterfaceInputType>,
output: InterfaceOutputType
};
export type InterfaceDefinition$Methods = {
[string]: InterfaceMethodDefinition
};
export type InterfaceDefinition = {
methods: InterfaceDefinition$Methods
};
export type InterfaceDefinitions = {
[InterfaceTypes]: InterfaceDefinition
};
+5 -5
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-provider",
"version": "0.11.3",
"version": "0.12.0",
"description": "Transport providers for the API",
"main": "index.js",
"keywords": [
@@ -35,10 +35,10 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0-beta.47",
"@polkadot/storage-substrate": "^0.14.3",
"@polkadot/util": "^0.20.5",
"@polkadot/util-crypto": "^0.20.5",
"@polkadot/util-keyring": "^0.20.5",
"@polkadot/storage-substrate": "^0.15.2",
"@polkadot/util": "^0.21.1",
"@polkadot/util-crypto": "^0.21.1",
"@polkadot/util-keyring": "^0.21.1",
"eventemitter3": "^2.0.3",
"isomorphic-fetch": "^2.2.1",
"websocket": "^1.0.25"
+1 -1
View File
@@ -6,7 +6,7 @@
import type { MockState, MockState$Storage } from './types';
const EventEmitter = require('eventemitter3');
const interfaces = require('@polkadot/api-jsonrpc');
const interfaces = require('@polkadot/jsonrpc');
const u8aToHex = require('@polkadot/util/u8a/toHex');
const l = require('@polkadot/util/logger')('api-mock');
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-rx",
"version": "0.11.3",
"version": "0.12.0",
"description": "An RxJs wrapper around the Polkadot JS API",
"main": "index.js",
"keywords": [
@@ -31,8 +31,8 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0-beta.47",
"@polkadot/api": "^0.11.3",
"@polkadot/api-provider": "^0.11.3",
"@polkadot/api": "^0.12.0",
"@polkadot/api-provider": "^0.12.0",
"rxjs": "^5.5.10"
}
}
+2 -2
View File
@@ -4,11 +4,11 @@
// @flow
import type { ProviderInterface } from '@polkadot/api-provider/types';
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { InterfaceTypes } from '@polkadot/jsonrpc/types';
import type { RxApiInterface } from './types';
const createApi = require('@polkadot/api');
const interfaces = require('@polkadot/api-jsonrpc');
const interfaces = require('@polkadot/jsonrpc');
const createWs = require('@polkadot/api-provider/ws');
const createExposed = require('./api');
+1 -1
View File
@@ -4,7 +4,7 @@
// @flow
import type { ApiInterface, ApiInterface$Section } from '@polkadot/api/types';
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { InterfaceTypes } from '@polkadot/jsonrpc/types';
import type { RxApiInterface$Section } from './types';
const observable = require('./observable');
+1 -1
View File
@@ -3,7 +3,7 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { InterfaceTypes } from '@polkadot/jsonrpc/types';
// flowlint-next-line unclear-type:off
export type RxApiInterface$Method = (...params: Array<mixed>) => rxjs$Observable<any> | rxjs$BehaviorSubject<any>;
+1 -1
View File
@@ -12,7 +12,7 @@
Warning - currently this does not actually do all that much, it is an attempt to put into code some thoughts about how to maintain the endpoints. This library provides a clean wrapper around all the methods exposed by a Polkadot network client.
Methods are auto-generated for the [JsonRPC interface definitions](../api-jsonrpc#readme), containing a full list of all sections and the methods cotained within.
Methods are auto-generated for the [JsonRPC interface definitions](https://github.com/polkadot-js/types/packages/jsonrpc), containing a full list of all sections and the methods contained within.
## Usage
+6 -5
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api",
"version": "0.11.3",
"version": "0.12.0",
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
"main": "index.js",
"keywords": [
@@ -31,9 +31,10 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0-beta.47",
"@polkadot/api-format": "^0.11.3",
"@polkadot/api-jsonrpc": "^0.11.3",
"@polkadot/api-provider": "^0.11.3",
"@polkadot/util": "^0.20.5"
"@polkadot/api-format": "^0.12.0",
"@polkadot/api-provider": "^0.12.0",
"@polkadot/jsonrpc": "^0.15.2",
"@polkadot/params": "^0.15.2",
"@polkadot/util": "^0.21.1"
}
}
+2 -2
View File
@@ -3,11 +3,11 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { InterfaceTypes } from '@polkadot/jsonrpc/types';
import type { ProviderInterface } from '@polkadot/api-provider/types';
import type { ApiInterface$Section } from '../types';
const interfaces = require('@polkadot/api-jsonrpc');
const interfaces = require('@polkadot/jsonrpc');
const methodSend = require('./methodSend');
const methodSubscribe = require('./methodSubscribe');
+9 -9
View File
@@ -4,23 +4,23 @@
const isFunction = require('@polkadot/util/is/function');
jest.mock('./@polkadot/api-jsonrpc', () => ({
jest.mock('@polkadot/jsonrpc', () => ({
test: {
methods: {
blah: {
inputs: [
{ name: 'foo', type: 'Address' }
],
output: { type: 'Address' }
params: {
foo: { type: 'Address' }
},
type: 'Address'
},
bleh: {
inputs: [],
output: { type: 'Address' }
params: {},
type: 'Address'
},
pubsub: {
isSubscription: true,
inputs: [],
output: { type: 'Address' }
params: {},
type: 'Address'
}
}
}
+7 -7
View File
@@ -3,26 +3,26 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '@polkadot/api-jsonrpc/types';
import type { InterfaceMethodDefinition } from '@polkadot/jsonrpc/types';
import type { ProviderInterface } from '@polkadot/api-provider/types';
import type { ApiInterface$Section$Method } from '../types';
const formatOutput = require('@polkadot/api-format/output');
const ExtError = require('@polkadot/util/ext/error');
const jsonrpcSignature = require('@polkadot/util/jsonrpc/signature');
const jsonrpcSignature = require('@polkadot/params/signature');
const createParams = require('./params');
module.exports = function createMethodSend (provider: ProviderInterface, rpcName: string, name: string, { inputs, output }: InterfaceMethodDefinition): ApiInterface$Section$Method {
const call = async (..._params: Array<mixed>): Promise<mixed> => {
module.exports = function createMethodSend (provider: ProviderInterface, rpcName: string, name: string, method: InterfaceMethodDefinition): ApiInterface$Section$Method {
const call = async (...values: Array<mixed>): Promise<mixed> => {
// TODO: Deprecated warning
try {
const params = createParams(_params, inputs);
const params = createParams(method.params, values);
const result = await provider.send(rpcName, params);
return formatOutput(output, result);
return formatOutput(method.type, result);
} catch (error) {
throw new ExtError(`${jsonrpcSignature(rpcName, inputs, output)}:: ${error.message}`, (error: ExtError).code);
throw new ExtError(`${jsonrpcSignature(method)}:: ${error.message}`, (error: ExtError).code);
}
};
+9 -7
View File
@@ -11,14 +11,16 @@ describe('methodCall', () => {
beforeEach(() => {
methods = {
blah: {
inputs: [
{ name: 'foo', type: 'Bytes' }
],
output: { type: 'Bytes' }
name: 'blah',
params: {
foo: { type: 'Bytes' }
},
type: 'Bytes'
},
bleh: {
inputs: [],
output: { type: 'Bytes' }
name: 'bleh',
params: {},
type: 'Bytes'
}
};
@@ -33,7 +35,7 @@ describe('methodCall', () => {
const method = createMethod(provider, 'test_blah', 'blah', methods.blah);
return method().catch((error) => {
expect(error.message).toMatch(/test_blah \(foo: Bytes\): Bytes/);
expect(error.message).toMatch(/blah \(foo: Bytes\): Bytes/);
});
});
+8 -8
View File
@@ -3,36 +3,36 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '@polkadot/api-jsonrpc/types';
import type { InterfaceMethodDefinition } from '@polkadot/jsonrpc/types';
import type { ProviderInterface, ProviderInterface$Callback } from '@polkadot/api-provider/types';
import type { ApiInterface$Section$Method } from '../types';
const formatOutput = require('@polkadot/api-format/output');
const signature = require('@polkadot/params/signature');
const assert = require('@polkadot/util/assert');
const ExtError = require('@polkadot/util/ext/error');
const isFunction = require('@polkadot/util/is/function');
const jsonrpcSignature = require('@polkadot/util/jsonrpc/signature');
const createParams = require('./params');
module.exports = function methodSubscribe (provider: ProviderInterface, rpcName: string, name: string, { inputs, output }: InterfaceMethodDefinition): ApiInterface$Section$Method {
module.exports = function methodSubscribe (provider: ProviderInterface, rpcName: string, name: string, method: InterfaceMethodDefinition): ApiInterface$Section$Method {
const unsubscribe = (subscriptionId: mixed): Promise<mixed> =>
provider.send(`unsubscribe_${name}`, [subscriptionId]);
const call = async (..._params: Array<mixed>): Promise<mixed> => {
const call = async (...values: Array<mixed>): Promise<mixed> => {
try {
// flowlint-next-line unclear-type:off
const cb = ((_params.pop(): any): ProviderInterface$Callback);
const cb = ((values.pop(): any): ProviderInterface$Callback);
assert(isFunction(cb), `Expected callback in last position of params`);
const params = createParams(_params, inputs);
const params = createParams(method.params, values);
const update = (error: ?Error, result?: mixed) => {
cb(error, formatOutput(output, result));
cb(error, formatOutput(method.type, result));
};
return provider.subscribe(`subscribe_${name}`, params, update);
} catch (error) {
throw new ExtError(`${jsonrpcSignature(rpcName, inputs, output)}:: ${error.message}`, (error: ExtError).code);
throw new ExtError(`${signature(method)}:: ${error.message}`, (error: ExtError).code);
}
};
+8 -7
View File
@@ -3,18 +3,19 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceInputType } from '@polkadot/api-jsonrpc/types';
import type { Params } from '@polkadot/params/types';
const formatInputs = require('@polkadot/api-format/input');
const assert = require('@polkadot/util/assert');
module.exports = function createParams (params: Array<mixed>, inputs: Array<InterfaceInputType>): Array<mixed> {
const required = inputs.filter(({ isOptional }) => !isOptional);
const optionalText = inputs.length
? ` (${(inputs.length - required.length) || 'none'} optional)`
module.exports = function createParams (_params: Params, values: Array<mixed>): Array<mixed> {
const params = Object.keys(_params).map((key) => _params[key]);
const required = params.filter(({ isOptional }) => !isOptional);
const optionalText = params.length
? ` (${(params.length - required.length) || 'none'} optional)`
: '';
assert(params.length >= required.length && params.length <= inputs.length, `${inputs.length || 'no'} params expected${optionalText}, found ${params.length} instead`);
assert(values.length >= required.length && values.length <= params.length, `${params.length || 'no'} params expected${optionalText}, found ${values.length} instead`);
return formatInputs(inputs, params);
return formatInputs(_params, values);
};
+10 -10
View File
@@ -10,29 +10,29 @@ describe('params', () => {
beforeEach(() => {
methods = {
blah: {
inputs: [
{ name: 'foo', type: 'Bytes' }
],
output: { type: 'Bytes' }
params: {
foo: { type: 'Bytes' }
},
type: 'Bytes'
},
bleh: {
inputs: [
{ name: 'foo', type: 'Bytes', isOptional: true }
],
output: { type: 'Bytes' }
params: {
foo: { type: 'Bytes', isOptional: true }
},
type: 'Bytes'
}
};
});
it('check against params', () => {
expect(
() => params([], methods.blah.inputs)
() => params(methods.blah.params, [])
).toThrow(/params expected/);
});
it('check against params (required)', () => {
expect(
params([], methods.bleh.inputs)
params(methods.bleh.params, [])
).toBeDefined();
});
});
+2 -2
View File
@@ -3,11 +3,11 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { InterfaceTypes } from '@polkadot/jsonrpc/types';
import type { ProviderInterface } from '@polkadot/api-provider/types';
import type { ApiInterface } from './types';
const interfaces = require('@polkadot/api-jsonrpc');
const interfaces = require('@polkadot/jsonrpc');
const assert = require('@polkadot/util/assert');
const isFunction = require('@polkadot/util/is/function');
+1 -1
View File
@@ -3,7 +3,7 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { InterfaceTypes } from '@polkadot/jsonrpc/types';
export type ApiInterface$Section$Method = {
(...params: Array<mixed>): Promise<mixed>;
+94 -88
View File
@@ -664,9 +664,9 @@
lodash "^4.17.5"
to-fast-properties "^2.0.0"
"@polkadot/dev@^0.19.9":
version "0.19.9"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.19.9.tgz#cc37c7364e010d96f45132d0069eda32f52e8462"
"@polkadot/dev@^0.19.10":
version "0.19.10"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.19.10.tgz#ccfbfb8632371876456a8e3e89ff26f10b27ba26"
dependencies:
"@babel/cli" "^7.0.0-beta.47"
"@babel/core" "^7.0.0-beta.47"
@@ -700,137 +700,143 @@
mkdirp "^0.5.1"
rimraf "^2.6.2"
"@polkadot/extrinsics-substrate@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/extrinsics-substrate/-/extrinsics-substrate-0.14.3.tgz#0937450ca3f1ada77857e26291e24ff3b2383749"
"@polkadot/extrinsics-substrate@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/extrinsics-substrate/-/extrinsics-substrate-0.15.2.tgz#eceb2928c4682d21ec5288200f79c75a13b08772"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/extrinsics" "^0.14.3"
"@polkadot/extrinsics" "^0.15.2"
"@polkadot/extrinsics@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/extrinsics/-/extrinsics-0.14.3.tgz#350150658e78fef3dbf21639926eadf817d2bfc4"
"@polkadot/extrinsics@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/extrinsics/-/extrinsics-0.15.2.tgz#4ac10e6f40059e90e5ee01a0bab3c520e7e69c57"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/params" "^0.14.3"
"@polkadot/primitives" "^0.14.3"
"@polkadot/util" "^0.20.5"
"@polkadot/params" "^0.15.2"
"@polkadot/primitives" "^0.15.2"
"@polkadot/util" "^0.21.1"
"@polkadot/params@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/params/-/params-0.14.3.tgz#135719c1fbbf5bb100bf648969deb78628d5e26b"
"@polkadot/jsonrpc@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/jsonrpc/-/jsonrpc-0.15.2.tgz#8237038b7af25e3212008276c92620928001ea5f"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/extrinsics" "^0.14.3"
"@polkadot/extrinsics-substrate" "^0.14.3"
"@polkadot/primitives" "^0.14.3"
"@polkadot/util" "^0.20.5"
"@polkadot/params" "^0.15.2"
babel-runtime "^6.26.0"
"@polkadot/primitives-builder@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-builder/-/primitives-builder-0.14.3.tgz#0b8f90d4276cd82c0c58824cc2ca19f3af2c2de9"
"@polkadot/params@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/params/-/params-0.15.2.tgz#f196ec68454b4856a2602f9cd53c00e695ccd7ff"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/primitives" "^0.14.3"
"@polkadot/primitives-codec" "^0.14.3"
"@polkadot/util" "^0.20.5"
"@polkadot/util-crypto" "^0.20.5"
"@polkadot/util-triehash" "^0.20.5"
"@polkadot/extrinsics" "^0.15.2"
"@polkadot/extrinsics-substrate" "^0.15.2"
"@polkadot/primitives" "^0.15.2"
"@polkadot/util" "^0.21.1"
"@polkadot/primitives-codec@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-codec/-/primitives-codec-0.14.3.tgz#85d26834e58f02c3d038539d696b68f68326c86a"
"@polkadot/primitives-builder@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-builder/-/primitives-builder-0.15.2.tgz#b9d839a8fab631c598408af3a10c83be04f8b47f"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/primitives" "^0.14.3"
"@polkadot/primitives-builder" "^0.14.3"
"@polkadot/primitives-rlp" "^0.14.3"
"@polkadot/util" "^0.20.5"
"@polkadot/util-keyring" "^0.20.5"
"@polkadot/primitives" "^0.15.2"
"@polkadot/primitives-codec" "^0.15.2"
"@polkadot/util" "^0.21.1"
"@polkadot/util-crypto" "^0.21.1"
"@polkadot/util-triehash" "^0.21.1"
"@polkadot/primitives-json@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-json/-/primitives-json-0.14.3.tgz#30ca431c2ba483035a18d391f269555c4b314258"
"@polkadot/primitives-codec@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-codec/-/primitives-codec-0.15.2.tgz#41c7b5982be53995b56a5f11e56ab67e00f2ab99"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/primitives" "^0.14.3"
"@polkadot/primitives-builder" "^0.14.3"
"@polkadot/util" "^0.20.5"
"@polkadot/primitives" "^0.15.2"
"@polkadot/primitives-builder" "^0.15.2"
"@polkadot/primitives-rlp" "^0.15.2"
"@polkadot/util" "^0.21.1"
"@polkadot/util-keyring" "^0.21.1"
"@polkadot/primitives-rlp@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-rlp/-/primitives-rlp-0.14.3.tgz#7ab6550a1df71a2a9cc5c740d65eeba173d1bf89"
"@polkadot/primitives-json@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-json/-/primitives-json-0.15.2.tgz#242f417a0656d53edd8c3bb96769491bebb9f8dd"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/primitives" "^0.14.3"
"@polkadot/primitives-builder" "^0.14.3"
"@polkadot/primitives-json" "^0.14.3"
"@polkadot/util" "^0.20.5"
"@polkadot/util-crypto" "^0.20.5"
"@polkadot/primitives" "^0.15.2"
"@polkadot/primitives-builder" "^0.15.2"
"@polkadot/util" "^0.21.1"
"@polkadot/primitives@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/primitives/-/primitives-0.14.3.tgz#f0f6db6c6b1ddcdee826fedddd9ec675cb15736b"
"@polkadot/primitives-rlp@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-rlp/-/primitives-rlp-0.15.2.tgz#461dcbadad3c1ed5931f4fe9805f01c1ef58cdc0"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/util" "^0.20.5"
"@polkadot/primitives" "^0.15.2"
"@polkadot/primitives-builder" "^0.15.2"
"@polkadot/util" "^0.21.1"
"@polkadot/util-crypto" "^0.21.1"
"@polkadot/storage-substrate@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/storage-substrate/-/storage-substrate-0.14.3.tgz#6989885362d5446e03c4ca3af4cadc82e7e828f1"
"@polkadot/primitives@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/primitives/-/primitives-0.15.2.tgz#60c97223286de22fdbda089718fb0dd98d0d96cc"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/storage" "^0.14.3"
"@polkadot/util" "^0.20.5"
"@polkadot/util" "^0.21.1"
"@polkadot/storage@^0.14.3":
version "0.14.3"
resolved "https://registry.yarnpkg.com/@polkadot/storage/-/storage-0.14.3.tgz#3cf2d28eeb711b6add92cfe9778b0e7f140f3547"
"@polkadot/storage-substrate@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/storage-substrate/-/storage-substrate-0.15.2.tgz#a2966f51d1c939609832864a4bd73cd1f69db675"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/params" "^0.14.3"
"@polkadot/primitives" "^0.14.3"
"@polkadot/util" "^0.20.5"
"@polkadot/storage" "^0.15.2"
"@polkadot/util" "^0.21.1"
"@polkadot/util-crypto@^0.20.5":
version "0.20.5"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.20.5.tgz#6317f15d1bbd2314e2e08e28fd236e548b1ee731"
"@polkadot/storage@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/storage/-/storage-0.15.2.tgz#2ac60d398a3a61a515e9d62f92e5166c72c9666f"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/util" "^0.20.5"
"@polkadot/params" "^0.15.2"
"@polkadot/primitives" "^0.15.2"
"@polkadot/util" "^0.21.1"
"@polkadot/util-crypto@^0.21.1":
version "0.21.1"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.21.1.tgz#45b7cb7cba89a740d9fb179b5af0e5e071f2f9a5"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/util" "^0.21.1"
blakejs "^1.1.0"
js-sha3 "^0.7.0"
tweetnacl "^1.0.0"
xxhashjs "^0.2.2"
"@polkadot/util-keyring@^0.20.5":
version "0.20.5"
resolved "https://registry.yarnpkg.com/@polkadot/util-keyring/-/util-keyring-0.20.5.tgz#eec652706e3036d43d73a8d49155b2f868fab0d3"
"@polkadot/util-keyring@^0.21.1":
version "0.21.1"
resolved "https://registry.yarnpkg.com/@polkadot/util-keyring/-/util-keyring-0.21.1.tgz#1b4d9ad969af948d1b8a0fbd8fe0d4abcc8a6474"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/util" "^0.20.5"
"@polkadot/util-crypto" "^0.20.5"
"@polkadot/util" "^0.21.1"
"@polkadot/util-crypto" "^0.21.1"
bs58 "^4.0.1"
"@polkadot/util-rlp@^0.20.5":
version "0.20.5"
resolved "https://registry.yarnpkg.com/@polkadot/util-rlp/-/util-rlp-0.20.5.tgz#c82638652215d8f0fce91637f46ebbbdc561f5bf"
"@polkadot/util-rlp@^0.21.1":
version "0.21.1"
resolved "https://registry.yarnpkg.com/@polkadot/util-rlp/-/util-rlp-0.21.1.tgz#4c9134c8cbac477486cfd2f2fe5e217b4d6057c7"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/util" "^0.20.5"
"@polkadot/util" "^0.21.1"
"@polkadot/util-triehash@^0.20.5":
version "0.20.5"
resolved "https://registry.yarnpkg.com/@polkadot/util-triehash/-/util-triehash-0.20.5.tgz#99bddb80abe4d6fc159ec5cb8aac13841e845eee"
"@polkadot/util-triehash@^0.21.1":
version "0.21.1"
resolved "https://registry.yarnpkg.com/@polkadot/util-triehash/-/util-triehash-0.21.1.tgz#4ebbf12055ec355afca0effde56375d3abd210c8"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
"@polkadot/util" "^0.20.5"
"@polkadot/util-crypto" "^0.20.5"
"@polkadot/util-rlp" "^0.20.5"
"@polkadot/util" "^0.21.1"
"@polkadot/util-crypto" "^0.21.1"
"@polkadot/util-rlp" "^0.21.1"
"@polkadot/util@^0.20.5":
version "0.20.5"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.20.5.tgz#b5a74e79d24ee5421a337443c5a2e1f5a6e3a1e1"
"@polkadot/util@^0.21.1":
version "0.21.1"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.21.1.tgz#d116b2de0bd6d8ed5cccd51b8a05f910c66e6282"
dependencies:
"@babel/runtime" "^7.0.0-beta.47"
bn.js "^4.11.8"