Skip to content

Commit

Permalink
Merge 468cb32 into 8e9243f
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Oct 10, 2018
2 parents 8e9243f + 468cb32 commit e92193b
Show file tree
Hide file tree
Showing 18 changed files with 623 additions and 43 deletions.
2 changes: 2 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Packages

- [api](api/README.md)
- [ApiRx](api/classes/_rx_index_.apirx.md)
- [rpc-core](rpc-core/README.md)
- [Rpc](rpc-core/classes/_index_.rpc.md)
- [rpc-provider](rpc-provider/README.md)
Expand Down
5 changes: 2 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ const config = require('@polkadot/dev/config/jest');

module.exports = Object.assign({}, config, {
moduleNameMapper: {
'@polkadot/api-observable(.*)$': '<rootDir>/packages/api-observable/src/$1',
'@polkadot/api-(observable)(.*)$': '<rootDir>/packages/api-$1/src/$2',
'@polkadot/api$': '<rootDir>/packages/api/src/$1',
'@polkadot/rpc-(core|provider|rx)(.*)$': '<rootDir>/packages/rpc-$1/src/$2',
'@polkadot/extrinsics(.*)$': '<rootDir>/packages/type-extrinsics/src/$1',
'@polkadot/jsonrpc(.*)$': '<rootDir>/packages/type-jsonrpc/src/$1',
'@polkadot/params(.*)$': '<rootDir>/packages/type-params/src/$1',
'@polkadot/primitives(.*)$': '<rootDir>/packages/type-primitives/src/$1',
'@polkadot/storage(.*)$': '<rootDir>/packages/type-storage/src/$1',
'@polkadot/types(.*)$': '<rootDir>/packages/types/src/$1'
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api-observable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

# @polkadot/api-observable

An RxJs wrapper around the [@polkadot/api](../api).
An Observable wrapper around [@polkadot/rpc-core](../rpc-core).
9 changes: 8 additions & 1 deletion packages/api-observable/src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import extrinsicsStatic from '@polkadot/extrinsics/static';
import storageFromMeta from '@polkadot/storage/fromMetadata';
import storageStatic from '@polkadot/storage/static';
import { Vector } from '@polkadot/types/codec';
import { Hash } from '@polkadot/types/index';
import { Hash, Method } from '@polkadot/types/index';
import { StorageFunction } from '@polkadot/types/StorageKey';
import assert from '@polkadot/util/assert';
import isUndefined from '@polkadot/util/is/undefined';
Expand Down Expand Up @@ -40,6 +40,8 @@ export default class ApiBase {
static extrinsics = extrinsicsStatic;
static storage = storageStatic;

// FIXME This logic is duplicated in api-rx, since that should derive
// from that base, it should be removed when the actual extend is done
private init (): Promise<boolean> {
let isReady: boolean = false;

Expand All @@ -56,9 +58,12 @@ export default class ApiBase {
const meta = await this._api.state.getMetadata().toPromise();

this._genesisHash = await this._api.chain.getBlockHash(0).toPromise();

ApiBase.extrinsics = extrinsicsFromMeta(meta);
ApiBase.storage = storageFromMeta(meta);

Method.injectExtrinsics(ApiBase.extrinsics);

if (!isReady) {
isReady = true;
resolveReady(true);
Expand Down Expand Up @@ -97,6 +102,7 @@ export default class ApiBase {
return fn.apply(null, params);
}

// FIXME Remove when extending from api-rx
rawStorage = <T> (key: StorageFunction, ...params: Array<any>): Observable<T | undefined> => {
return this
.rawStorageMulti([key, ...params] as [StorageFunction, any])
Expand All @@ -109,6 +115,7 @@ export default class ApiBase {
);
}

// FIXME Remove when extending from api-rx
rawStorageMulti = <T extends []> (...keys: Array<[StorageFunction] | [StorageFunction, any]>): Observable<T> => {
let observable;

Expand Down
15 changes: 15 additions & 0 deletions packages/api/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License (ISC)

Copyright 2017-2018 @polkadot/api authors & contributors

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.
51 changes: 51 additions & 0 deletions packages/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[![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-observable.svg?style=flat-square)](https://www.npmjs.com/package/@polkadot/api)
[![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)](https://david-dm.org/polkadot-js/api?path=packages/api)
[![devDependency](https://david-dm.org/polkadot-js/api/dev-status.svg?style=flat-square&path=packages/api)](https://david-dm.org/polkadot-js/api?path=packages/api#info=devDependencies)

# @polkadot/api

An RxJs wrapper around [@polkadot/rpc-core](../rpc-core).

# Usage

Installation -

```
npm install --save @polkadot/api/rx
```

Making rpc calls -

```javascript
import ApiRx from '@polkadot/api/rx';
// alternatively
// import { ApiRx } from '@polkadot/api';

// initialise via Promise & static create
const api = await ApiRx.create().toPromise();

// make a call to retrieve the current network head
api.rpc.chain.newHead().subscribe((header) => {
console.log(`Chain is at #${header.blockNumber}`);
});
```

Subscribing to chain state -

```javascript
import Api from '@polkadot/api';

// initialise via isReady & new
new Api().isReady.subscribe((api) => {
// make a call to retrieve the current network head
api.st.timestamp.now().subscribe((timestamp) => {
console.log(`Current block timestamp ${timestamp}`);
});
});
```
40 changes: 40 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@polkadot/api",
"version": "0.31.0",
"description": "An RxJS wrapper around the Polkadot JS RPC",
"main": "index.js",
"keywords": [
"Polkadot",
"RxJs"
],
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [
"Jaco Greeff <jacogr@gmail.com>"
],
"contributors": [],
"license": "ISC",
"engines": {
"node": ">=8.0"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"repository": {
"type": "git",
"url": "git+https://github.com/polkadot-js/api.git"
},
"bugs": {
"url": "https://github.com/polkadot-js/api/issues"
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/rpc-rx": "^0.31.0",
"@polkadot/rpc-provider": "^0.31.0",
"@polkadot/extrinsics": "^0.31.0",
"@polkadot/storage": "^0.31.0",
"@types/rx": "^4.1.1",
"rxjs": "^6.3.2"
}
}
5 changes: 5 additions & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2017-2018 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

export { default as ApiRx } from './rx';
34 changes: 34 additions & 0 deletions packages/api/src/rx/SubmittableExtrinsic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2017-2018 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import { KeyringPair } from '@polkadot/util-keyring/types';
import { AnyNumber, AnyU8a } from '@polkadot/types/types';
import { RxApiInterface } from './types';

import { Observable } from 'rxjs';
import { Extrinsic } from '@polkadot/types/index';

export default class SubmittableExtrinsic extends Extrinsic {
private _api: RxApiInterface;

constructor (api: RxApiInterface, extrinsic: Extrinsic) {
super({
method: extrinsic.method,
signature: extrinsic.signature
});

this._api = api;
}

// TODO needs to be submitAndWatch
send (): Observable<any> {
return this._api.rpc.author.submitExtrinsic(this);
}

sign (signerPair: KeyringPair, nonce: AnyNumber, blockHash?: AnyU8a): SubmittableExtrinsic {
super.sign(signerPair, nonce, blockHash || this._api.genesisHash);

return this;
}
}

0 comments on commit e92193b

Please sign in to comment.