Skip to content

Commit

Permalink
Merge 724d109 into 62ba9f9
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Aug 10, 2019
2 parents 62ba9f9 + 724d109 commit cff29ff
Show file tree
Hide file tree
Showing 49 changed files with 436 additions and 432 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

If you are upgrading form an older version, use the CHANGELOG hand-in-hand with the [migration guide](UPGRADING.md).

- **Breaking change** `Api.create(...)` and `new Api(...)` now only takes an options Object, so if you passed the provider directly previously, you need to swap the use to `Api.create({ provider: ... })`
- Support substrate v7 metadata
- The `Method.findFunction(callIndex)` (allowing decoding of raw data), is now available on `api.findCall(callIndex)`. To keep backwards compatibility, it is still available on `GenericCall.findMethod` but the `api.findCall` is recommended and suggested.
- Runtime types have been extended and moved to definitions instead of classes
Expand Down
18 changes: 9 additions & 9 deletions README.md
@@ -1,12 +1,12 @@
[![polkadotjs](https://img.shields.io/badge/polkadot-js-orange.svg?style=flat-square)](https://polkadot.js.org)
![license](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)
[![npm](https://img.shields.io/npm/v/@polkadot/api.svg?style=flat-square)](https://www.npmjs.com/package/@polkadot/api)
[![beta](https://img.shields.io/npm/v/@polkadot/api/beta?label=beta&style=flat-square)](https://www.npmjs.com/package/@polkadot/api)
[![travisci](https://img.shields.io/travis/com/polkadot-js/api?label=travisci&style=flat-square)](https://travis-ci.com/polkadot-js/api)
[![circleci](https://img.shields.io/circleci/build/github/polkadot-js/api/master?label=circleci&style=flat-square)](https://circleci.com/gh/polkadot-js/api)
[![maintainability](https://img.shields.io/codeclimate/maintainability/polkadot-js/api.svg?style=flat-square)](https://codeclimate.com/github/polkadot-js/api)
[![coverage](https://img.shields.io/codeclimate/coverage/polkadot-js/api.svg?style=flat-square)](https://codeclimate.com/github/polkadot-js/api)
[![greenkeeper](https://img.shields.io/badge/greenkeeper-enabled-brightgreen.svg?style=flat-square)](https://greenkeeper.io/)
[![polkadotjs](https://img.shields.io/badge/polkadot-js-orange?style=for-the-badge)](https://polkadot.js.org)
![license](https://img.shields.io/badge/License-Apache%202.0-blue?label=&logo=apache&style=for-the-badge)
[![npm](https://img.shields.io/npm/v/@polkadot/api?label=&logo=npm&style=for-the-badge)](https://www.npmjs.com/package/@polkadot/api)
[![beta](https://img.shields.io/npm/v/@polkadot/api/beta?label=&logo=npm&&style=for-the-badge)](https://www.npmjs.com/package/@polkadot/api)
[![travisci](https://img.shields.io/travis/com/polkadot-js/api?label=&logo=travis&style=for-the-badge)](https://travis-ci.com/polkadot-js/api)
[![circleci](https://img.shields.io/circleci/build/github/polkadot-js/api/master?label=&logo=circleci&style=for-the-badge)](https://circleci.com/gh/polkadot-js/api)
[![maintainability](https://img.shields.io/codeclimate/maintainability/polkadot-js/api?label=&logo=code-climate&style=for-the-badge)](https://codeclimate.com/github/polkadot-js/api)
[![coverage](https://img.shields.io/codeclimate/coverage/polkadot-js/api?label=&logo=code-climate&style=for-the-badge)](https://codeclimate.com/github/polkadot-js/api)
[![greenkeeper](https://img.shields.io/badge/greenkeeper-enabled-brightgreen?label=&logo=greenkeeper&style=for-the-badge)](https://greenkeeper.io/)

# @polkadot/api

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/promise/01_simple_connect/index.js
Expand Up @@ -7,7 +7,7 @@ async function main () {
const provider = new WsProvider('ws://127.0.0.1:9944');

// Create the API and wait until ready
const api = await ApiPromise.create(provider);
const api = await ApiPromise.create({ provider });

// Retrieve the chain & node information information via rpc calls
const [chain, nodeName, nodeVersion] = await Promise.all([
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/promise/10_upgrade_chain/index.js
Expand Up @@ -12,7 +12,7 @@ async function main () {
const provider = new WsProvider('ws://127.0.0.1:9944');

// Create the API and wait until ready (optional provider passed through)
const api = await ApiPromise.create(provider);
const api = await ApiPromise.create({ provider });

// retrieve the upgrade key from the chain state
const adminId = await api.query.sudo.key();
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/rx/01_simple_connect/index.js
Expand Up @@ -9,7 +9,7 @@ function main () {
const provider = new WsProvider('ws://127.0.0.1:9944');

// Create the API and wait until ready
const api = await ApiRx.create(provider).toPromise();
const api = await ApiRx.create({ provider }).toPromise();

// We're using RxJs 'zip()' combination operator to get the emitted values
// of multiple observables as an array
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/rx/10_upgrade_chain/index.js
Expand Up @@ -10,7 +10,7 @@ async function main () {
const provider = new WsProvider('ws://127.0.0.1:9944');

// Create the API and wait until ready (optional provider passed through)
const api = await ApiRx.create(provider).toPromise();
const api = await ApiRx.create({ provider }).toPromise();

// retrieve the upgrade key from the chain state
const adminId = await api.query.sudo.key().toPromise();
Expand Down
2 changes: 1 addition & 1 deletion packages/api-derive/src/index.spec.ts
Expand Up @@ -27,7 +27,7 @@ const testFunction = (api: ApiRx): any => {

describe('derive', (): void => {
describe('builtin', (): void => {
const api = new ApiRx(new MockProvider());
const api = new ApiRx({ provider: new MockProvider() });

beforeAll((done): void => {
api.isReady.subscribe((): void => done());
Expand Down
42 changes: 16 additions & 26 deletions packages/api/src/Base.ts
Expand Up @@ -3,7 +3,6 @@
// of the Apache-2.0 license. See the LICENSE file for details.

import { RpcInterface } from '@polkadot/rpc-core/jsonrpc.types';
import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { Hash, RuntimeVersion, SignedBlock } from '@polkadot/types/interfaces';
import { AnyFunction, CallFunction, Codec, CodecArg, ModulesWithCalls, RegistryTypes } from '@polkadot/types/types';
import { ApiInterfaceRx, ApiInterfaceEvents, ApiOptions, ApiTypes, DecorateMethodOptions, DecoratedRpc, DecoratedRpcSection, QueryableModuleStorage, QueryableStorage, QueryableStorageEntry, QueryableStorageMulti, QueryableStorageMultiArg, QueryableStorageMultiArgs, SignerPayloadRawBase, SubmittableExtrinsicFunction, SubmittableExtrinsics, SubmittableModuleExtrinsics, Signer } from './types';
Expand All @@ -22,9 +21,8 @@ import { WsProvider } from '@polkadot/rpc-provider';
import { getTypeRegistry, GenericCall, GenericEvent, Metadata, Null, u64 } from '@polkadot/types';
import Linkage, { LinkageResult } from '@polkadot/types/codec/Linkage';
import { DEFAULT_VERSION as EXTRINSIC_DEFAULT_VERSION } from '@polkadot/types/primitive/Extrinsic/constants';
import * as interfacesTypes from '@polkadot/types/interfaces/definitions';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { assert, compactStripLength, isFunction, isObject, isString, isUndefined, logger, u8aToHex, u8aToU8a } from '@polkadot/util';
import { assert, compactStripLength, isString, isUndefined, logger, u8aToHex, u8aToU8a } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';

import createSubmittable, { SubmittableExtrinsic } from './SubmittableExtrinsic';
Expand Down Expand Up @@ -117,14 +115,18 @@ export default abstract class ApiBase<ApiType> {
* });
* ```
*/
public constructor (provider: ApiOptions | ProviderInterface = {}, type: ApiTypes) {
const options = isObject(provider) && isFunction((provider as ProviderInterface).send)
? { provider } as unknown as ApiOptions
: provider as ApiOptions;
public constructor (options: ApiOptions = {}, type: ApiTypes) {
const thisProvider = options.source
? options.source._rpcCore.provider.clone()
: (options.provider || new WsProvider());

// We only register the types (global) if this is not a cloned instance.
// Do right up-front, so we get in the user types before we are actually
// doing anything on-chain, this ensures we have the overrides in-place
if (!options.source && options.types) {
this.registerTypes(options.types);
}

this._options = options;
this._type = type;
this._eventemitter = new EventEmitter();
Expand All @@ -139,17 +141,6 @@ export default abstract class ApiBase<ApiType> {
this._rx.queryMulti = this.decorateMulti(rxDecorateMethod);
this._rx.signer = options.signer;

// we only re-register the types (global) if this is not a cloned instance
if (!options.source) {
// first register the definitions we have, i.e. those where there are no type classes
Object.values(interfacesTypes).forEach(({ types }): void =>
this.registerTypes(types)
);

// next register all the user types
this.registerTypes(options.types);
}

this.init();
}

Expand Down Expand Up @@ -538,6 +529,13 @@ export default abstract class ApiBase<ApiType> {
this._rpcCore.chain.getRuntimeVersion().toPromise()
]);

// NOTE The SessionKeys definition for Polkadot and Substrate (OpaqueKeys
// implementation) are different. Detect Polkadot and inject the `Keys`
// definition as applicable. (3 keys in substrate vs 4 in Polkadot).
if (this._runtimeVersion.specName.eq('polkadot')) {
getTypeRegistry().register({ Keys: 'SessionKeysPolkadot' });
}

const metadataKey = `${this._genesisHash}-${(this._runtimeVersion as RuntimeVersion).specVersion}`;

if (metadataKey in metadata) {
Expand Down Expand Up @@ -574,14 +572,6 @@ export default abstract class ApiBase<ApiType> {
this._query = this.decorateStorage(storage, this.decorateMethod);
this._consts = constants;

// NOTE The SessionKeys definition for Polkadot and Substrate (OpaqueKeys
// implementation) are different. Detect Polkadot and inject the `Keys`
// definition as applicable. (3 keys in substrate vs 4 in Polkadot). If
// we have reflected metadata, this override becomes unneeded
if (this._query.parachains) {
getTypeRegistry().register({ Keys: 'SessionKeysPolkadot' });
}

this._rx.extrinsicType = this._extrinsicType;
this._rx.genesisHash = this._genesisHash;
this._rx.runtimeVersion = this._runtimeVersion;
Expand Down
7 changes: 3 additions & 4 deletions packages/api/src/promise/Api.ts
Expand Up @@ -2,7 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { AnyFunction, Callback, Codec } from '@polkadot/types/types';
import { ApiOptions, DecorateMethodOptions, ObsInnerType, StorageEntryPromiseOverloads, UnsubscribePromise } from '../types';

Expand Down Expand Up @@ -55,7 +54,7 @@ import Combinator, { CombinatorCallback, CombinatorFunction } from './Combinator
* const provider = new WsProvider('wss://example.com:9944')
*
* // initialise via isReady & new with specific provider
* const api = await new ApiPromise(provider).isReady;
* const api = await new ApiPromise({ provider }).isReady;
*
* // retrieve the block target time
* const blockPeriod = await api.query.timestamp.blockPeriod().toNumber();
Expand Down Expand Up @@ -120,7 +119,7 @@ export default class ApiPromise extends ApiBase<'promise'> {
* });
* ```
*/
public static create (options: ApiOptions | ProviderInterface = {}): Promise<ApiPromise> {
public static create (options?: ApiOptions): Promise<ApiPromise> {
return new ApiPromise(options).isReady;
}

Expand All @@ -143,7 +142,7 @@ export default class ApiPromise extends ApiBase<'promise'> {
* });
* ```
*/
public constructor (options?: ApiOptions | ProviderInterface) {
public constructor (options?: ApiOptions) {
super(options, 'promise');

this._isReadyPromise = new Promise((resolve): void => {
Expand Down
9 changes: 4 additions & 5 deletions packages/api/src/rx/Api.ts
Expand Up @@ -2,7 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { AnyFunction } from '@polkadot/types/types';
import { ApiOptions } from '../types';

Expand Down Expand Up @@ -55,7 +54,7 @@ import ApiBase from '../Base';
* const provider = new WsProvider('wss://example.com:9944')
*
* // initialise via isReady & new with specific provider
* new ApiRx(provider)
* new ApiRx({ provider })
* .isReady
* .pipe(
* switchMap((api) =>
Expand Down Expand Up @@ -135,7 +134,7 @@ export default class ApiRx extends ApiBase<'rxjs'> {
* });
* ```
*/
public static create (options?: ApiOptions | ProviderInterface): Observable<ApiRx> {
public static create (options?: ApiOptions): Observable<ApiRx> {
return new ApiRx(options).isReady;
}

Expand All @@ -161,8 +160,8 @@ export default class ApiRx extends ApiBase<'rxjs'> {
* });
* ```
*/
public constructor (provider?: ApiOptions | ProviderInterface) {
super(provider, 'rxjs');
public constructor (options?: ApiOptions) {
super(options, 'rxjs');

this._isReadyRx = from(
// convinced you can observable from an event, however my mind groks this form better
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api-derive/promise-dev.spec.ts
Expand Up @@ -21,7 +21,7 @@ describeE2E({
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });
done();
});

Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api-derive/promise.spec.ts
Expand Up @@ -13,7 +13,7 @@ describeE2E()('Derive Promise e2e', (wsUrl: string): void => {
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });
done();
});

Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api-derive/rx.spec.ts
Expand Up @@ -31,7 +31,7 @@ describeE2E({
});

beforeEach(async (done): Promise<void> => {
api = await ApiRx.create(new WsProvider(wsUrl)).toPromise();
api = await ApiRx.create({ provider: new WsProvider(wsUrl) }).toPromise();

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-alex-archive.spec.ts
Expand Up @@ -15,7 +15,7 @@ describeE2E({
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-alex.spec.ts
Expand Up @@ -16,7 +16,7 @@ describeE2E({
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-consts.spec.ts
Expand Up @@ -19,7 +19,7 @@ describeE2E({
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-contract.spec.ts
Expand Up @@ -37,7 +37,7 @@ describeE2E({
let CREATION_FEE: number;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });
CREATION_FEE = 123456789123456;

done();
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-queries-dev.spec.ts
Expand Up @@ -26,7 +26,7 @@ describeE2E({
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
Expand Up @@ -25,7 +25,7 @@ describeE2E({
const keyring = testingPairs({ type: 'ed25519' });

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-queries.spec.ts
Expand Up @@ -16,7 +16,7 @@ describeE2E()('Promise e2e queries', (wsUrl: string): void => {
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-tx-chained.spec.ts
Expand Up @@ -37,7 +37,7 @@ describeE2E({
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-tx-eras.spec.ts
Expand Up @@ -43,7 +43,7 @@ describeE2E({
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-tx-signer.spec.ts
Expand Up @@ -37,7 +37,7 @@ describeE2E({
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/promise-tx.spec.ts
Expand Up @@ -40,7 +40,7 @@ describeE2E({
let api: ApiPromise;

beforeEach(async (done): Promise<void> => {
api = await ApiPromise.create(new WsProvider(wsUrl));
api = await ApiPromise.create({ provider: new WsProvider(wsUrl) });

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/rx-queries-dev.spec.ts
Expand Up @@ -21,7 +21,7 @@ describeE2E({
let api: ApiRx;

beforeEach(async (done): Promise<void> => {
api = await ApiRx.create(new WsProvider(wsUrl)).toPromise();
api = await ApiRx.create({ provider: new WsProvider(wsUrl) }).toPromise();

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/rx-queries.spec.ts
Expand Up @@ -15,7 +15,7 @@ describeE2E()('Rx e2e queries', (wsUrl: string): void => {
let api: ApiRx;

beforeEach(async (done): Promise<void> => {
api = await ApiRx.create(new WsProvider(wsUrl)).toPromise();
api = await ApiRx.create({ provider: new WsProvider(wsUrl) }).toPromise();

done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/api/rx-tx.spec.ts
Expand Up @@ -21,7 +21,7 @@ describeE2E()('Rx e2e transactions', (wsUrl: string): void => {
let api: ApiRx;

beforeEach(async (done): Promise<void> => {
api = await ApiRx.create(new WsProvider(wsUrl)).toPromise();
api = await ApiRx.create({ provider: new WsProvider(wsUrl) }).toPromise();

done();
});
Expand Down

0 comments on commit cff29ff

Please sign in to comment.